Maestro 0.3.1
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
DistributedGpuLibStateVectorSim.h
Go to the documentation of this file.
1// Owns one distributed state and keeps its originating plugin alive.
2#pragma once
3#ifdef __linux__
5#include <cmath>
6#include <utility>
7namespace Simulators {
8class DistributedGpuLibStateVectorSim {
9 public:
10 DistributedGpuLibStateVectorSim(std::shared_ptr<DistributedGpuLibrary> lib,
11 void *obj)
12 : lib(std::move(lib)), obj(obj) {
13 if (!obj) throw std::runtime_error("Null distributed GPU state");
14 }
15 virtual ~DistributedGpuLibStateVectorSim() { lib->DestroyNative(obj); }
16 DistributedGpuLibStateVectorSim(const DistributedGpuLibStateVectorSim &) =
17 delete;
18 DistributedGpuLibStateVectorSim &operator=(
19 const DistributedGpuLibStateVectorSim &) = delete;
20 DistributedGpuLibStateVectorSim(
21 DistributedGpuLibStateVectorSim &&other) noexcept
22 : lib(other.lib), obj(std::exchange(other.obj, nullptr)) {}
23 DistributedGpuLibStateVectorSim &operator=(
24 DistributedGpuLibStateVectorSim &&) = delete;
25 std::unique_ptr<DistributedGpuLibStateVectorSim> Clone() const {
26 return std::make_unique<DistributedGpuLibStateVectorSim>(
27 lib, lib->CloneNative(obj));
28 }
29 int CheckLicense() const {
30 auto result = lib->CheckLicense(obj);
31 lib->Check(result, "CheckLicense");
32 return result;
33 }
34 int GetBackend() const {
35 auto result = lib->GetBackend(obj);
36 if (result < 0) lib->Fail("GetBackend");
37 const char *error = lib->GetLastError();
38 if (error && *error) throw std::runtime_error(error);
39 return result;
40 }
41 int SetExExecutionConfig(
42 const DistributedGpuApi::MgdExExecutionConfig *config) const {
43 auto result = lib->SetExExecutionConfig(obj, config);
44 lib->Check(result, "SetExExecutionConfig");
45 return result;
46 }
47 int SetDataType(int useDoublePrecision) const {
48 auto result = lib->SetDataType(obj, useDoublePrecision);
49 lib->Check(result, "SetDataType");
50 return result;
51 }
52 int SetSeed(unsigned long long seed) const {
53 auto result = lib->SetSeed(obj, seed);
54 lib->Check(result, "SetSeed");
55 return result;
56 }
57 int IsDoublePrecision() const {
58 auto result = lib->IsDoublePrecision(obj);
59 const char *error = lib->GetLastError();
60 if (error && *error) throw std::runtime_error(error);
61 return result;
62 }
63 int GetNrQubits() const {
64 auto result = lib->GetNrQubits(obj);
65 if (result < 0) lib->Fail("GetNrQubits");
66 const char *error = lib->GetLastError();
67 if (error && *error) throw std::runtime_error(error);
68 return result;
69 }
70 int GetStateVectorGpuId() const {
71 auto result = lib->GetStateVectorGpuId(obj);
72 const char *error = lib->GetLastError();
73 if (error && *error) throw std::runtime_error(error);
74 return result;
75 }
76 int Create(unsigned int nrQubits) const {
77 auto result = lib->Create(obj, nrQubits);
78 lib->Check(result, "Create");
79 return result;
80 }
81 int CreateWithState(unsigned int nrQubits, const double *state) const {
82 auto result = lib->CreateWithState(obj, nrQubits, state);
83 lib->Check(result, "CreateWithState");
84 return result;
85 }
86 int Reset() const {
87 auto result = lib->Reset(obj);
88 lib->Check(result, "Reset");
89 return result;
90 }
91 int MeasureQubitCollapse(int qubitIndex) const {
92 auto result = lib->MeasureQubitCollapse(obj, qubitIndex);
93 if (result != 0 && result != 1) lib->Fail("MeasureQubitCollapse");
94 return result;
95 }
96 int MeasureQubitNoCollapse(int qubitIndex) const {
97 auto result = lib->MeasureQubitNoCollapse(obj, qubitIndex);
98 if (result != 0 && result != 1) lib->Fail("MeasureQubitNoCollapse");
99 return result;
100 }
101 int MeasureQubitsCollapse(int *qubits, int *bitstring,
102 int bitstringLen) const {
103 auto result =
104 lib->MeasureQubitsCollapse(obj, qubits, bitstring, bitstringLen);
105 lib->Check(result, "MeasureQubitsCollapse");
106 return result;
107 }
108 int MeasureQubitsNoCollapse(int *qubits, int *bitstring,
109 int bitstringLen) const {
110 auto result =
111 lib->MeasureQubitsNoCollapse(obj, qubits, bitstring, bitstringLen);
112 lib->Check(result, "MeasureQubitsNoCollapse");
113 return result;
114 }
115 unsigned long long MeasureAllQubitsCollapse() const {
116 auto result = lib->MeasureAllQubitsCollapse(obj);
117 if (result == UINT64_MAX) lib->Fail("MeasureAllQubitsCollapse");
118 return result;
119 }
120 unsigned long long MeasureAllQubitsNoCollapse() const {
121 auto result = lib->MeasureAllQubitsNoCollapse(obj);
122 if (result == UINT64_MAX) lib->Fail("MeasureAllQubitsNoCollapse");
123 return result;
124 }
125 int SaveState() const {
126 auto result = lib->SaveState(obj);
127 lib->Check(result, "SaveState");
128 return result;
129 }
130 int SaveStateToHost() const {
131 auto result = lib->SaveStateToHost(obj);
132 lib->Check(result, "SaveStateToHost");
133 return result;
134 }
135 int SaveStateDestructive() const {
136 auto result = lib->SaveStateDestructive(obj);
137 lib->Check(result, "SaveStateDestructive");
138 return result;
139 }
140 int RestoreStateFreeSaved() const {
141 auto result = lib->RestoreStateFreeSaved(obj);
142 lib->Check(result, "RestoreStateFreeSaved");
143 return result;
144 }
145 int RestoreStateNoFreeSaved() const {
146 auto result = lib->RestoreStateNoFreeSaved(obj);
147 lib->Check(result, "RestoreStateNoFreeSaved");
148 return result;
149 }
150 void FreeSavedState() const {
151 lib->FreeSavedState(obj);
152 const char *error = lib->GetLastError();
153 if (error && *error) throw std::runtime_error(error);
154 }
155 int Sample(unsigned int nSamples, long int *samples, unsigned int nBits,
156 int *bitOrdering) const {
157 auto result = lib->Sample(obj, nSamples, samples, nBits, bitOrdering);
158 lib->Check(result, "Sample");
159 return result;
160 }
161 int SampleAll(unsigned int nSamples, long int *samples) const {
162 auto result = lib->SampleAll(obj, nSamples, samples);
163 lib->Check(result, "SampleAll");
164 return result;
165 }
166 int Amplitude(long long int state, double *real, double *imaginary) const {
167 auto result = lib->Amplitude(obj, state, real, imaginary);
168 lib->Check(result, "Amplitude");
169 return result;
170 }
171 double Probability(int *qubits, int *mask, int len) const {
172 auto result = lib->Probability(obj, qubits, mask, len);
173 if (!std::isfinite(result)) lib->Fail("Probability");
174 return result;
175 }
176 double BasisStateProbability(long long int state) const {
177 auto result = lib->BasisStateProbability(obj, state);
178 if (!std::isfinite(result)) lib->Fail("BasisStateProbability");
179 return result;
180 }
181 int AllProbabilities(double *probabilities) const {
182 auto result = lib->AllProbabilities(obj, probabilities);
183 lib->Check(result, "AllProbabilities");
184 return result;
185 }
186 double ExpectationValue(const char *pauliString, int len) const {
187 auto result = lib->ExpectationValue(obj, pauliString, len);
188 if (!std::isfinite(result)) lib->Fail("ExpectationValue");
189 return result;
190 }
191 int ApplyX(int qubit) const {
192 auto result = lib->ApplyX(obj, qubit);
193 lib->Check(result, "ApplyX");
194 return result;
195 }
196 int ApplyY(int qubit) const {
197 auto result = lib->ApplyY(obj, qubit);
198 lib->Check(result, "ApplyY");
199 return result;
200 }
201 int ApplyZ(int qubit) const {
202 auto result = lib->ApplyZ(obj, qubit);
203 lib->Check(result, "ApplyZ");
204 return result;
205 }
206 int ApplyH(int qubit) const {
207 auto result = lib->ApplyH(obj, qubit);
208 lib->Check(result, "ApplyH");
209 return result;
210 }
211 int ApplyS(int qubit) const {
212 auto result = lib->ApplyS(obj, qubit);
213 lib->Check(result, "ApplyS");
214 return result;
215 }
216 int ApplySDG(int qubit) const {
217 auto result = lib->ApplySDG(obj, qubit);
218 lib->Check(result, "ApplySDG");
219 return result;
220 }
221 int ApplyT(int qubit) const {
222 auto result = lib->ApplyT(obj, qubit);
223 lib->Check(result, "ApplyT");
224 return result;
225 }
226 int ApplyTDG(int qubit) const {
227 auto result = lib->ApplyTDG(obj, qubit);
228 lib->Check(result, "ApplyTDG");
229 return result;
230 }
231 int ApplySX(int qubit) const {
232 auto result = lib->ApplySX(obj, qubit);
233 lib->Check(result, "ApplySX");
234 return result;
235 }
236 int ApplySXDG(int qubit) const {
237 auto result = lib->ApplySXDG(obj, qubit);
238 lib->Check(result, "ApplySXDG");
239 return result;
240 }
241 int ApplyK(int qubit) const {
242 auto result = lib->ApplyK(obj, qubit);
243 lib->Check(result, "ApplyK");
244 return result;
245 }
246 int ApplyP(int qubit, double theta) const {
247 auto result = lib->ApplyP(obj, qubit, theta);
248 lib->Check(result, "ApplyP");
249 return result;
250 }
251 int ApplyRx(int qubit, double theta) const {
252 auto result = lib->ApplyRx(obj, qubit, theta);
253 lib->Check(result, "ApplyRx");
254 return result;
255 }
256 int ApplyRy(int qubit, double theta) const {
257 auto result = lib->ApplyRy(obj, qubit, theta);
258 lib->Check(result, "ApplyRy");
259 return result;
260 }
261 int ApplyRz(int qubit, double theta) const {
262 auto result = lib->ApplyRz(obj, qubit, theta);
263 lib->Check(result, "ApplyRz");
264 return result;
265 }
266 int ApplyU(int qubit, double theta, double phi, double lambda,
267 double gamma) const {
268 auto result = lib->ApplyU(obj, qubit, theta, phi, lambda, gamma);
269 lib->Check(result, "ApplyU");
270 return result;
271 }
272 int ApplyCX(int controlQubit, int targetQubit) const {
273 auto result = lib->ApplyCX(obj, controlQubit, targetQubit);
274 lib->Check(result, "ApplyCX");
275 return result;
276 }
277 int ApplyCY(int controlQubit, int targetQubit) const {
278 auto result = lib->ApplyCY(obj, controlQubit, targetQubit);
279 lib->Check(result, "ApplyCY");
280 return result;
281 }
282 int ApplyCZ(int controlQubit, int targetQubit) const {
283 auto result = lib->ApplyCZ(obj, controlQubit, targetQubit);
284 lib->Check(result, "ApplyCZ");
285 return result;
286 }
287 int ApplyCH(int controlQubit, int targetQubit) const {
288 auto result = lib->ApplyCH(obj, controlQubit, targetQubit);
289 lib->Check(result, "ApplyCH");
290 return result;
291 }
292 int ApplyCSX(int controlQubit, int targetQubit) const {
293 auto result = lib->ApplyCSX(obj, controlQubit, targetQubit);
294 lib->Check(result, "ApplyCSX");
295 return result;
296 }
297 int ApplyCSXDG(int controlQubit, int targetQubit) const {
298 auto result = lib->ApplyCSXDG(obj, controlQubit, targetQubit);
299 lib->Check(result, "ApplyCSXDG");
300 return result;
301 }
302 int ApplyCP(int controlQubit, int targetQubit, double theta) const {
303 auto result = lib->ApplyCP(obj, controlQubit, targetQubit, theta);
304 lib->Check(result, "ApplyCP");
305 return result;
306 }
307 int ApplyCRx(int controlQubit, int targetQubit, double theta) const {
308 auto result = lib->ApplyCRx(obj, controlQubit, targetQubit, theta);
309 lib->Check(result, "ApplyCRx");
310 return result;
311 }
312 int ApplyCRy(int controlQubit, int targetQubit, double theta) const {
313 auto result = lib->ApplyCRy(obj, controlQubit, targetQubit, theta);
314 lib->Check(result, "ApplyCRy");
315 return result;
316 }
317 int ApplyCRz(int controlQubit, int targetQubit, double theta) const {
318 auto result = lib->ApplyCRz(obj, controlQubit, targetQubit, theta);
319 lib->Check(result, "ApplyCRz");
320 return result;
321 }
322 int ApplyCCX(int controlQubit1, int controlQubit2, int targetQubit) const {
323 auto result = lib->ApplyCCX(obj, controlQubit1, controlQubit2, targetQubit);
324 lib->Check(result, "ApplyCCX");
325 return result;
326 }
327 int ApplySwap(int qubit1, int qubit2) const {
328 auto result = lib->ApplySwap(obj, qubit1, qubit2);
329 lib->Check(result, "ApplySwap");
330 return result;
331 }
332 int ApplyCSwap(int controlQubit, int qubit1, int qubit2) const {
333 auto result = lib->ApplyCSwap(obj, controlQubit, qubit1, qubit2);
334 lib->Check(result, "ApplyCSwap");
335 return result;
336 }
337 int ApplyCU(int controlQubit, int targetQubit, double theta, double phi,
338 double lambda, double gamma) const {
339 auto result =
340 lib->ApplyCU(obj, controlQubit, targetQubit, theta, phi, lambda, gamma);
341 lib->Check(result, "ApplyCU");
342 return result;
343 }
344 int ConfigureDistribution(
345 const DistributedGpuApi::MgdDistributionConfig *config) const {
346 auto result = lib->ConfigureDistribution(obj, config);
347 lib->Check(result, "ConfigureDistribution");
348 return result;
349 }
350 int GetShardDevices(int32_t *devices, uint32_t capacity) const {
351 auto result = lib->GetShardDevices(obj, devices, capacity);
352 if (result < 0) lib->Fail("GetShardDevices");
353 const char *error = lib->GetLastError();
354 if (error && *error) throw std::runtime_error(error);
355 return result;
356 }
357 int GetGlobalQubits(int32_t *qubits, uint32_t capacity) const {
358 auto result = lib->GetGlobalQubits(obj, qubits, capacity);
359 if (result < 0) lib->Fail("GetGlobalQubits");
360 const char *error = lib->GetLastError();
361 if (error && *error) throw std::runtime_error(error);
362 return result;
363 }
364 int GetQubitLayout(int32_t *wires, uint32_t capacity) const {
365 auto result = lib->GetQubitLayout(obj, wires, capacity);
366 if (result < 0) lib->Fail("GetQubitLayout");
367 const char *error = lib->GetLastError();
368 if (error && *error) throw std::runtime_error(error);
369 return result;
370 }
371 int Redistribute(const int32_t *global_qubits, uint32_t count) const {
372 auto result = lib->Redistribute(obj, global_qubits, count);
373 lib->Check(result, "Redistribute");
374 return result;
375 }
376 int SwapGlobalLocalQubits(const int32_t *global_qubits,
377 const int32_t *local_qubits, uint32_t count) const {
378 auto result =
379 lib->SwapGlobalLocalQubits(obj, global_qubits, local_qubits, count);
380 lib->Check(result, "SwapGlobalLocalQubits");
381 return result;
382 }
383 int Synchronize() const {
384 auto result = lib->Synchronize(obj);
385 lib->Check(result, "Synchronize");
386 return result;
387 }
388 int GetStateRange(uint64_t begin, uint64_t end, double *output) const {
389 auto result = lib->GetStateRange(obj, begin, end, output);
390 lib->Check(result, "GetStateRange");
391 return result;
392 }
393 int SetStateRange(uint64_t begin, uint64_t end, const double *input) const {
394 auto result = lib->SetStateRange(obj, begin, end, input);
395 lib->Check(result, "SetStateRange");
396 return result;
397 }
398 int GetLocalStateBounds(uint64_t *begin, uint64_t *end) const {
399 auto result = lib->GetLocalStateBounds(obj, begin, end);
400 lib->Check(result, "GetLocalStateBounds");
401 return result;
402 }
403 int GetLocalStateRange(uint64_t begin, uint64_t end, double *output) const {
404 auto result = lib->GetLocalStateRange(obj, begin, end, output);
405 lib->Check(result, "GetLocalStateRange");
406 return result;
407 }
408 int SetLocalStateRange(uint64_t begin, uint64_t end,
409 const double *input) const {
410 auto result = lib->SetLocalStateRange(obj, begin, end, input);
411 lib->Check(result, "SetLocalStateRange");
412 return result;
413 }
414 int CreateWithBasisState(uint32_t nrQubits, uint64_t basis) const {
415 auto result = lib->CreateWithBasisState(obj, nrQubits, basis);
416 lib->Check(result, "CreateWithBasisState");
417 return result;
418 }
419 int ApplyOneQubitMatrix(int qubit, const double *matrix) const {
420 auto result = lib->ApplyOneQubitMatrix(obj, qubit, matrix);
421 lib->Check(result, "ApplyOneQubitMatrix");
422 return result;
423 }
424 int ApplyOneQubitMatrixWithLayout(int qubit, const double *matrix,
425 int layout) const {
426 auto result =
427 lib->ApplyOneQubitMatrixWithLayout(obj, qubit, matrix, layout);
428 lib->Check(result, "ApplyOneQubitMatrixWithLayout");
429 return result;
430 }
431 int ApplyTwoQubitMatrix(int qubit0, int qubit1, const double *matrix) const {
432 auto result = lib->ApplyTwoQubitMatrix(obj, qubit0, qubit1, matrix);
433 lib->Check(result, "ApplyTwoQubitMatrix");
434 return result;
435 }
436 int ApplyTwoQubitMatrixWithLayout(int qubit0, int qubit1,
437 const double *matrix, int layout) const {
438 auto result =
439 lib->ApplyTwoQubitMatrixWithLayout(obj, qubit0, qubit1, matrix, layout);
440 lib->Check(result, "ApplyTwoQubitMatrixWithLayout");
441 return result;
442 }
443
444 protected:
445 std::shared_ptr<DistributedGpuLibrary> lib;
446 void *obj;
447};
448} // namespace Simulators
449#endif
int ApplyK(void *sim, int qubit)
double Probability(void *sim, unsigned long long int outcome)
int ApplyRx(void *sim, int qubit, double theta)
int ApplyX(void *sim, int qubit)
int ApplyU(void *sim, int qubit, double theta, double phi, double lambda, double gamma)
int ApplyCRy(void *sim, int controlQubit, int targetQubit, double theta)
int ApplyTDG(void *sim, int qubit)
int ApplyCSXDG(void *sim, int controlQubit, int targetQubit)
int ApplyS(void *sim, int qubit)
int ApplyCX(void *sim, int controlQubit, int targetQubit)
int ApplyCRz(void *sim, int controlQubit, int targetQubit, double theta)
double * AllProbabilities(void *sim)
int ApplyCP(void *sim, int controlQubit, int targetQubit, double theta)
int ApplySXDG(void *sim, int qubit)
int ApplySDG(void *sim, int qubit)
int ApplyCSwap(void *sim, int controlQubit, int qubit1, int qubit2)
int ApplyCCX(void *sim, int controlQubit1, int controlQubit2, int targetQubit)
int ApplyY(void *sim, int qubit)
double * Amplitude(void *sim, unsigned long long int outcome)
int ApplyZ(void *sim, int qubit)
int ApplyH(void *sim, int qubit)
int ApplyCY(void *sim, int controlQubit, int targetQubit)
int ApplyCU(void *sim, int controlQubit, int targetQubit, double theta, double phi, double lambda, double gamma)
int ApplySwap(void *sim, int qubit1, int qubit2)
int ApplyRy(void *sim, int qubit, double theta)
int ApplyP(void *sim, int qubit, double theta)
int ApplyCH(void *sim, int controlQubit, int targetQubit)
int ApplySX(void *sim, int qubit)
int ApplyCZ(void *sim, int controlQubit, int targetQubit)
int ApplyRz(void *sim, int qubit, double theta)
int ApplyT(void *sim, int qubit)
int ApplyCRx(void *sim, int controlQubit, int targetQubit, double theta)
int ApplyCSX(void *sim, int controlQubit, int targetQubit)
int SaveState(void *sim)