Maestro 0.1.0
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
GpuStabilizer.h
Go to the documentation of this file.
1
12#pragma once
13
14#ifndef _GPU_STABILIZER_H
15#define _GPU_STABILIZER_H 1
16
17#ifdef __linux__
18
19#include "GpuLibrary.h"
20
21namespace Simulators {
22
23class GpuStabilizer {
24 public:
25 explicit GpuStabilizer(const std::shared_ptr<GpuLibrary> &lib)
26 : lib(lib), obj(nullptr) {}
27
28 GpuStabilizer() = delete;
29 GpuStabilizer(const GpuStabilizer &) = delete;
30 GpuStabilizer &operator=(const GpuStabilizer &) = delete;
31 GpuStabilizer(GpuStabilizer &&) = default;
32 GpuStabilizer &operator=(GpuStabilizer &&) = default;
33
34 ~GpuStabilizer() {
35 if (lib && obj) lib->DestroyStabilizerSimulator(obj);
36 }
37
38 bool CreateSimulator(long long int numQubits, long long int numShots,
39 long long int numMeasurements,
40 long long int numDetectors) {
41 if (lib) {
42 obj = lib->CreateStabilizerSimulator(numQubits, numShots, numMeasurements,
43 numDetectors);
44 return obj != nullptr;
45 }
46
47 return false;
48 }
49
50 bool ExecuteCircuit(const std::string &circuitStr, int randomizeMeasurements,
51 unsigned long long int seed) {
52 if (obj)
53 return lib->ExecuteStabilizerCircuit(obj, circuitStr.c_str(),
54 randomizeMeasurements, seed);
55 return false;
56 }
57
58 bool Clear() {
59 if (obj) {
60 lib->DestroyStabilizerSimulator(obj);
61 obj = nullptr;
62 return true;
63 }
64
65 return false;
66 }
67
68 long long GetNumQubits() {
69 if (obj) return lib->GetStabilizerNumQubits(obj);
70 return 0;
71 }
72
73 long long GetNumShots() {
74 if (obj) return lib->GetStabilizerNumShots(obj);
75 return 0;
76 }
77
78 long long GetNumMeasurements() {
79 if (obj) return lib->GetStabilizerNumMeasurements(obj);
80 return 0;
81 }
82
83 long long GetNumDetectors() {
84 if (obj) return lib->GetStabilizerNumDetectors(obj);
85 return 0;
86 }
87
88 bool IsCreated() const { return obj != nullptr; }
89
90 std::vector<std::vector<bool>> GetXTable() {
91 if (!obj) return std::vector<std::vector<bool>>();
92
93 std::vector<unsigned int> xTableRaw(GetStabilizerXZTableSize());
94 lib->CopyStabilizerXTable(obj, xTableRaw.data());
95
96 return ConvertToBoolVectorVector(xTableRaw, GetNumQubits(), GetNumShots());
97 }
98
99 std::vector<std::vector<bool>> GetZTable() {
100 if (!obj) return std::vector<std::vector<bool>>();
101
102 std::vector<unsigned int> zTableRaw(GetStabilizerXZTableSize());
103 lib->CopyStabilizerZTable(obj, zTableRaw.data());
104
105 return ConvertToBoolVectorVector(zTableRaw, GetNumQubits(), GetNumShots());
106 }
107
108 std::vector<std::vector<bool>> GetMTable() {
109 if (!obj) return std::vector<std::vector<bool>>();
110
111 std::vector<unsigned int> mTableRaw(GetStabilizerMTableSize());
112 lib->CopyStabilizerMTable(obj, mTableRaw.data());
113
114 return ConvertToBoolVectorVector(mTableRaw, GetNumMeasurements(),
115 GetNumShots());
116 }
117
118 bool InitXTable(const std::vector<std::vector<bool>> &xTable) {
119 if (!obj) return false;
120 const long long shots = GetNumShots();
121 const long long words_per_qubitormeas = (shots + 31) / 32;
122 const long long numQubits = GetNumQubits();
123 std::vector<unsigned int> xTableRaw(numQubits * words_per_qubitormeas, 0);
124 for (long long qubit = 0; qubit < numQubits; ++qubit) {
125 for (long long shot = 0; shot < shots; ++shot) {
126 if (xTable[qubit][shot]) {
127 xTableRaw[qubit * words_per_qubitormeas + (shot / 32)] |=
128 (1U << (shot % 32));
129 }
130 }
131 }
132 return lib->InitStabilizerXTable(obj, xTableRaw.data()) == 1;
133 }
134
135 bool InitXTableRepeat(const std::vector<bool> &xTable) {
136 // repeat xTable for all shots
137 if (!obj) return false;
138 const long long shots = GetNumShots();
139 const long long words_per_qubitormeas = (shots + 31) / 32;
140 const long long numQubits = GetNumQubits();
141 std::vector<unsigned int> xTableRaw(numQubits * words_per_qubitormeas, 0);
142 for (long long qubit = 0; qubit < numQubits; ++qubit) {
143 for (long long shot = 0; shot < shots; ++shot) {
144 if (xTable[qubit]) {
145 xTableRaw[qubit * words_per_qubitormeas + (shot / 32)] |=
146 (1U << (shot % 32));
147 }
148 }
149 }
150
151 return lib->InitStabilizerXTable(obj, xTableRaw.data()) == 1;
152 }
153
154 bool InitZTable(const std::vector<std::vector<bool>> &zTable) {
155 if (!obj) return false;
156 const long long shots = GetNumShots();
157 const long long words_per_qubitormeas = (shots + 31) / 32;
158 const long long numQubits = GetNumQubits();
159 std::vector<unsigned int> zTableRaw(numQubits * words_per_qubitormeas, 0);
160 for (long long qubit = 0; qubit < numQubits; ++qubit) {
161 for (long long shot = 0; shot < shots; ++shot) {
162 if (zTable[qubit][shot]) {
163 zTableRaw[qubit * words_per_qubitormeas + (shot / 32)] |=
164 (1U << (shot % 32));
165 }
166 }
167 }
168 return lib->InitStabilizerZTable(obj, zTableRaw.data()) == 1;
169 }
170
171 bool InitZTableRepeat(const std::vector<bool> &zTable) {
172 // repeat zTable for all shots
173 if (!obj) return false;
174 const long long shots = GetNumShots();
175 const long long words_per_qubitormeas = (shots + 31) / 32;
176 const long long numQubits = GetNumQubits();
177 std::vector<unsigned int> zTableRaw(numQubits * words_per_qubitormeas, 0);
178 for (long long qubit = 0; qubit < numQubits; ++qubit) {
179 for (long long shot = 0; shot < shots; ++shot) {
180 if (zTable[qubit]) {
181 zTableRaw[qubit * words_per_qubitormeas + (shot / 32)] |=
182 (1U << (shot % 32));
183 }
184 }
185 }
186 return lib->InitStabilizerZTable(obj, zTableRaw.data()) == 1;
187 }
188
189 protected:
190 std::vector<std::vector<bool>> ConvertToBoolVectorVector(
191 const std::vector<unsigned int> &tableRaw, long long int num,
192 long long int shots) {
193 const long long words_per_qubitormeas = (shots + 31) / 32;
194
195 std::vector<std::vector<bool>> result(num);
196
197 for (long long qubitormeas = 0; qubitormeas < num; ++qubitormeas) {
198 for (long long shot = 0; shot < shots; ++shot) {
199 const unsigned int word =
200 tableRaw[qubitormeas * words_per_qubitormeas + (shot / 32)];
201 const bool bit = ((word >> (shot % 32)) & 1) == 1;
202 result[qubitormeas].push_back(bit);
203 }
204 }
205
206 return result;
207 }
208
209 long long GetStabilizerXZTableSize() {
210 if (obj) return lib->GetStabilizerXZTableSize(obj);
211 return 0;
212 }
213
214 long long GetStabilizerMTableSize() {
215 if (obj) return lib->GetStabilizerMTableSize(obj);
216 return 0;
217 }
218
219 long long GetStabilizerTableStrideMajor() {
220 if (obj) return lib->GetStabilizerTableStrideMajor(obj);
221 return 0;
222 }
223
224 private:
225 std::shared_ptr<GpuLibrary> lib;
226 void *obj;
227};
228
229} // namespace Simulators
230
231#endif
232#endif
unsigned long int CreateSimulator(int simType, int simExecType)