Maestro 0.1.0
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
GpuLibrary.h
Go to the documentation of this file.
1
11
12#pragma once
13
14#ifndef _GPU_LIBRARY_H
15#define _GPU_LIBRARY_H
16
17#ifdef __linux__
18
19#include "../Utils/Library.h"
20
21#include <stdint.h>
22#include <unordered_map>
23#include <vector>
24
25namespace Simulators {
26
27// use it as a singleton
28class GpuLibrary : public Utils::Library {
29public:
30 GpuLibrary(const GpuLibrary &) = delete;
31 GpuLibrary &operator=(const GpuLibrary &) = delete;
32
33 GpuLibrary(GpuLibrary &&) = default;
34 GpuLibrary &operator=(GpuLibrary &&) = default;
35
36 GpuLibrary() noexcept {}
37
38 virtual ~GpuLibrary() {
39 if (LibraryHandle)
40 FreeLib();
41 }
42
43 bool Init(const char *libName) noexcept override {
44 if (Utils::Library::Init(libName)) {
45 InitLib = (void *(*)())GetFunction("InitLib");
46 CheckFunction((void *)InitLib, __LINE__);
47 if (InitLib) {
48 LibraryHandle = InitLib();
49 if (LibraryHandle) {
50 FreeLib = (void (*)())GetFunction("FreeLib");
51 CheckFunction((void *)FreeLib, __LINE__);
52
53 // state vector api functions
54
55 fCreateStateVector =
56 (void *(*)(void *))GetFunction("CreateStateVector");
57 CheckFunction((void *)fCreateStateVector, __LINE__);
58 fDestroyStateVector =
59 (void (*)(void *))GetFunction("DestroyStateVector");
60 CheckFunction((void *)fDestroyStateVector, __LINE__);
61
62 fCreate = (int (*)(void *, unsigned int))GetFunction("Create");
63 CheckFunction((void *)fCreate, __LINE__);
64 fCreateWithState =
65 (int (*)(void *, unsigned int, const double *))GetFunction(
66 "CreateWithState");
67 CheckFunction((void *)fCreateWithState, __LINE__);
68 fReset = (int (*)(void *))GetFunction("Reset");
69 CheckFunction((void *)fReset, __LINE__);
70
71 fSetDataType = (int (*)(void *, int))GetFunction("SetDataType");
72 CheckFunction((void *)fSetDataType, __LINE__);
73 fIsDoublePrecision =
74 (int (*)(void *))GetFunction("IsDoublePrecision");
75 CheckFunction((void *)fIsDoublePrecision, __LINE__);
76
77 fMeasureQubitCollapse =
78 (int (*)(void *, int))GetFunction("MeasureQubitCollapse");
79 CheckFunction((void *)fMeasureQubitCollapse, __LINE__);
80 fMeasureQubitNoCollapse =
81 (int (*)(void *, int))GetFunction("MeasureQubitNoCollapse");
82 CheckFunction((void *)fMeasureQubitNoCollapse, __LINE__);
83 fMeasureQubitsCollapse = (int (*)(
84 void *, int *, int *, int))GetFunction("MeasureQubitsCollapse");
85 CheckFunction((void *)fMeasureQubitsCollapse, __LINE__);
86 fMeasureQubitsNoCollapse = (int (*)(
87 void *, int *, int *, int))GetFunction("MeasureQubitsNoCollapse");
88 CheckFunction((void *)fMeasureQubitsNoCollapse, __LINE__);
89 fMeasureAllQubitsCollapse = (unsigned long long (*)(
90 void *))GetFunction("MeasureAllQubitsCollapse");
91 CheckFunction((void *)fMeasureAllQubitsCollapse, __LINE__);
92 fMeasureAllQubitsNoCollapse = (unsigned long long (*)(
93 void *))GetFunction("MeasureAllQubitsNoCollapse");
94 CheckFunction((void *)fMeasureAllQubitsNoCollapse, __LINE__);
95
96 fSaveState = (int (*)(void *))GetFunction("SaveState");
97 CheckFunction((void *)fSaveState, __LINE__);
98 fSaveStateToHost = (int (*)(void *))GetFunction("SaveStateToHost");
99 CheckFunction((void *)fSaveStateToHost, __LINE__);
100 fSaveStateDestructive =
101 (int (*)(void *))GetFunction("SaveStateDestructive");
102 CheckFunction((void *)fSaveStateDestructive, __LINE__);
103 fRestoreStateFreeSaved =
104 (int (*)(void *))GetFunction("RestoreStateFreeSaved");
105 CheckFunction((void *)fRestoreStateFreeSaved, __LINE__);
106 fRestoreStateNoFreeSaved =
107 (int (*)(void *))GetFunction("RestoreStateNoFreeSaved");
108 CheckFunction((void *)fRestoreStateNoFreeSaved, __LINE__);
109 fFreeSavedState = (void (*)(void *))GetFunction("FreeSavedState");
110 CheckFunction((void *)fFreeSavedState, __LINE__);
111 fClone = (void *(*)(void *))GetFunction("Clone");
112 CheckFunction((void *)fClone, __LINE__);
113
114 fSample = (int (*)(void *, unsigned int, long int *, unsigned int,
115 int *))GetFunction("Sample");
116 CheckFunction((void *)fSample, __LINE__);
117 fSampleAll = (int (*)(void *, unsigned int, long int *))GetFunction(
118 "SampleAll");
119 CheckFunction((void *)fSampleAll, __LINE__);
120 fAmplitude = (int (*)(void *, long long int, double *,
121 double *))GetFunction("Amplitude");
122 CheckFunction((void *)fAmplitude, __LINE__);
123 fProbability =
124 (double (*)(void *, int *, int *, int))GetFunction("Probability");
125 CheckFunction((void *)fProbability, __LINE__);
126 fBasisStateProbability = (double (*)(
127 void *, long long int))GetFunction("BasisStateProbability");
128 CheckFunction((void *)fBasisStateProbability, __LINE__);
129 fAllProbabilities = (int (*)(
130 void *obj, double *probabilities))GetFunction("AllProbabilities");
131 CheckFunction((void *)fAllProbabilities, __LINE__);
132 fExpectationValue = (double (*)(void *, const char *,
133 int))GetFunction("ExpectationValue");
134 CheckFunction((void *)fExpectationValue, __LINE__);
135
136 fApplyX = (int (*)(void *, int))GetFunction("ApplyX");
137 CheckFunction((void *)fApplyX, __LINE__);
138 fApplyY = (int (*)(void *, int))GetFunction("ApplyY");
139 CheckFunction((void *)fApplyY, __LINE__);
140 fApplyZ = (int (*)(void *, int))GetFunction("ApplyZ");
141 CheckFunction((void *)fApplyZ, __LINE__);
142 fApplyH = (int (*)(void *, int))GetFunction("ApplyH");
143 CheckFunction((void *)fApplyH, __LINE__);
144 fApplyS = (int (*)(void *, int))GetFunction("ApplyS");
145 CheckFunction((void *)fApplyS, __LINE__);
146 fApplySDG = (int (*)(void *, int))GetFunction("ApplySDG");
147 CheckFunction((void *)fApplySDG, __LINE__);
148 fApplyT = (int (*)(void *, int))GetFunction("ApplyT");
149 CheckFunction((void *)fApplyT, __LINE__);
150 fApplyTDG = (int (*)(void *, int))GetFunction("ApplyTDG");
151 CheckFunction((void *)fApplyTDG, __LINE__);
152 fApplySX = (int (*)(void *, int))GetFunction("ApplySX");
153 CheckFunction((void *)fApplySX, __LINE__);
154 fApplySXDG = (int (*)(void *, int))GetFunction("ApplySXDG");
155 CheckFunction((void *)fApplySXDG, __LINE__);
156 fApplyK = (int (*)(void *, int))GetFunction("ApplyK");
157 CheckFunction((void *)fApplyK, __LINE__);
158 fApplyP = (int (*)(void *, int, double))GetFunction("ApplyP");
159 CheckFunction((void *)fApplyP, __LINE__);
160 fApplyRx = (int (*)(void *, int, double))GetFunction("ApplyRx");
161 CheckFunction((void *)fApplyRx, __LINE__);
162 fApplyRy = (int (*)(void *, int, double))GetFunction("ApplyRy");
163 CheckFunction((void *)fApplyRy, __LINE__);
164 fApplyRz = (int (*)(void *, int, double))GetFunction("ApplyRz");
165 CheckFunction((void *)fApplyRz, __LINE__);
166 fApplyU = (int (*)(void *, int, double, double, double,
167 double))GetFunction("ApplyU");
168 CheckFunction((void *)fApplyU, __LINE__);
169 fApplyCX = (int (*)(void *, int, int))GetFunction("ApplyCX");
170 CheckFunction((void *)fApplyCX, __LINE__);
171 fApplyCY = (int (*)(void *, int, int))GetFunction("ApplyCY");
172 CheckFunction((void *)fApplyCY, __LINE__);
173 fApplyCZ = (int (*)(void *, int, int))GetFunction("ApplyCZ");
174 CheckFunction((void *)fApplyCZ, __LINE__);
175 fApplyCH = (int (*)(void *, int, int))GetFunction("ApplyCH");
176 CheckFunction((void *)fApplyCH, __LINE__);
177 fApplyCSX = (int (*)(void *, int, int))GetFunction("ApplyCSX");
178 CheckFunction((void *)fApplyCSX, __LINE__);
179 fApplyCSXDG = (int (*)(void *, int, int))GetFunction("ApplyCSXDG");
180 CheckFunction((void *)fApplyCSXDG, __LINE__);
181 fApplyCP = (int (*)(void *, int, int, double))GetFunction("ApplyCP");
182 CheckFunction((void *)fApplyCP, __LINE__);
183 fApplyCRx =
184 (int (*)(void *, int, int, double))GetFunction("ApplyCRx");
185 CheckFunction((void *)fApplyCRx, __LINE__);
186 fApplyCRy =
187 (int (*)(void *, int, int, double))GetFunction("ApplyCRy");
188 CheckFunction((void *)fApplyCRy, __LINE__);
189 fApplyCRz =
190 (int (*)(void *, int, int, double))GetFunction("ApplyCRz");
191 CheckFunction((void *)fApplyCRz, __LINE__);
192 fApplyCCX = (int (*)(void *, int, int, int))GetFunction("ApplyCCX");
193 CheckFunction((void *)fApplyCCX, __LINE__);
194 fApplySwap = (int (*)(void *, int, int))GetFunction("ApplySwap");
195 CheckFunction((void *)fApplySwap, __LINE__);
196 fApplyCSwap =
197 (int (*)(void *, int, int, int))GetFunction("ApplyCSwap");
198 CheckFunction((void *)fApplyCSwap, __LINE__);
199 fApplyCU = (int (*)(void *, int, int, double, double, double,
200 double))GetFunction("ApplyCU");
201 CheckFunction((void *)fApplyCU, __LINE__);
202
203 // mps api functions
204
205 fCreateMPS = (void *(*)(void *))GetFunction("CreateMPS");
206 CheckFunction((void *)fCreateMPS, __LINE__);
207 fDestroyMPS = (void (*)(void *))GetFunction("DestroyMPS");
208 CheckFunction((void *)fDestroyMPS, __LINE__);
209
210 fMPSCreate = (int (*)(void *, unsigned int))GetFunction("MPSCreate");
211 CheckFunction((void *)fMPSCreate, __LINE__);
212 fMPSReset = (int (*)(void *))GetFunction("MPSReset");
213 CheckFunction((void *)fMPSReset, __LINE__);
214
215 fMPSIsValid = (int (*)(void *))GetFunction("MPSIsValid");
216 CheckFunction((void *)fMPSIsValid, __LINE__);
217 fMPSIsCreated = (int (*)(void *))GetFunction("MPSIsCreated");
218 CheckFunction((void *)fMPSIsCreated, __LINE__);
219
220 fMPSSetDataType = (int (*)(void *, int))GetFunction("MPSSetDataType");
221 CheckFunction((void *)fMPSSetDataType, __LINE__);
222 fMPSIsDoublePrecision =
223 (int (*)(void *))GetFunction("MPSIsDoublePrecision");
224 CheckFunction((void *)fMPSIsDoublePrecision, __LINE__);
225 fMPSSetCutoff = (int (*)(void *, double))GetFunction("MPSSetCutoff");
226 CheckFunction((void *)fMPSSetCutoff, __LINE__);
227 fMPSGetCutoff = (double (*)(void *))GetFunction("MPSGetCutoff");
228 CheckFunction((void *)fMPSGetCutoff, __LINE__);
229 fMPSSetGesvdJ = (int (*)(void *, int))GetFunction("MPSSetGesvdJ");
230 CheckFunction((void *)fMPSSetGesvdJ, __LINE__);
231 fMPSGetGesvdJ = (int (*)(void *))GetFunction("MPSGetGesvdJ");
232 CheckFunction((void *)fMPSGetGesvdJ, __LINE__);
233 fMPSSetMaxExtent =
234 (int (*)(void *, long int))GetFunction("MPSSetMaxExtent");
235 CheckFunction((void *)fMPSSetMaxExtent, __LINE__);
236 fMPSGetMaxExtent =
237 (long int (*)(void *))GetFunction("MPSGetMaxExtent");
238 CheckFunction((void *)fMPSGetMaxExtent, __LINE__);
239 fMPSGetNrQubits = (int (*)(void *))GetFunction("MPSGetNrQubits");
240 CheckFunction((void *)fMPSGetNrQubits, __LINE__);
241 fMPSAmplitude = (int (*)(void *, long int, long int *, double *,
242 double *))GetFunction("MPSAmplitude");
243 CheckFunction((void *)fMPSAmplitude, __LINE__);
244 fMPSProbability0 =
245 (double (*)(void *, unsigned int))GetFunction("MPSProbability0");
246 CheckFunction((void *)fMPSProbability0, __LINE__);
247 fMPSMeasure =
248 (int (*)(void *, unsigned int))GetFunction("MPSMeasure");
249 CheckFunction((void *)fMPSMeasure, __LINE__);
250 fMPSMeasureQubits = (int (*)(void *, long int, unsigned int *,
251 int *))GetFunction("MPSMeasureQubits");
252 CheckFunction((void *)fMPSMeasureQubits, __LINE__);
253
254 fMPSGetMapForSample = (void *(*)())GetFunction("MPSGetMapForSample");
255 CheckFunction((void *)fMPSGetMapForSample, __LINE__);
256 fMPSFreeMapForSample =
257 (int (*)(void *))GetFunction("MPSFreeMapForSample");
258 CheckFunction((void *)fMPSFreeMapForSample, __LINE__);
259 fMPSSample = (int (*)(void *, long int, long int, unsigned int *,
260 void *))GetFunction("MPSSample");
261 CheckFunction((void *)fMPSSample, __LINE__);
262
263 fMPSSaveState = (int (*)(void *))GetFunction("MPSSaveState");
264 CheckFunction((void *)fMPSSaveState, __LINE__);
265 fMPSRestoreState = (int (*)(void *))GetFunction("MPSRestoreState");
266 CheckFunction((void *)fMPSRestoreState, __LINE__);
267 fMPSCleanSavedState =
268 (int (*)(void *))GetFunction("MPSCleanSavedState");
269 CheckFunction((void *)fMPSCleanSavedState, __LINE__);
270 fMPSClone = (void *(*)(void *))GetFunction("MPSClone");
271 CheckFunction((void *)fMPSClone, __LINE__);
272
273 fMPSExpectationValue = (double (*)(
274 void *, const char *, int))GetFunction("MPSExpectationValue");
275 CheckFunction((void *)fMPSExpectationValue, __LINE__);
276
277 fMPSApplyX = (int (*)(void *, unsigned int))GetFunction("MPSApplyX");
278 CheckFunction((void *)fMPSApplyX, __LINE__);
279 fMPSApplyY = (int (*)(void *, unsigned int))GetFunction("MPSApplyY");
280 CheckFunction((void *)fMPSApplyY, __LINE__);
281 fMPSApplyZ = (int (*)(void *, unsigned int))GetFunction("MPSApplyZ");
282 CheckFunction((void *)fMPSApplyZ, __LINE__);
283 fMPSApplyH = (int (*)(void *, unsigned int))GetFunction("MPSApplyH");
284 CheckFunction((void *)fMPSApplyH, __LINE__);
285 fMPSApplyS = (int (*)(void *, unsigned int))GetFunction("MPSApplyS");
286 CheckFunction((void *)fMPSApplyS, __LINE__);
287 fMPSApplySDG =
288 (int (*)(void *, unsigned int))GetFunction("MPSApplySDG");
289 CheckFunction((void *)fMPSApplySDG, __LINE__);
290 fMPSApplyT = (int (*)(void *, unsigned int))GetFunction("MPSApplyT");
291 CheckFunction((void *)fMPSApplyT, __LINE__);
292 fMPSApplyTDG =
293 (int (*)(void *, unsigned int))GetFunction("MPSApplyTDG");
294 CheckFunction((void *)fMPSApplyTDG, __LINE__);
295 fMPSApplySX =
296 (int (*)(void *, unsigned int))GetFunction("MPSApplySX");
297 CheckFunction((void *)fMPSApplySX, __LINE__);
298 fMPSApplySXDG =
299 (int (*)(void *, unsigned int))GetFunction("MPSApplySXDG");
300 CheckFunction((void *)fMPSApplySXDG, __LINE__);
301 fMPSApplyK = (int (*)(void *, unsigned int))GetFunction("MPSApplyK");
302 CheckFunction((void *)fMPSApplyK, __LINE__);
303 fMPSApplyP =
304 (int (*)(void *, unsigned int, double))GetFunction("MPSApplyP");
305 CheckFunction((void *)fMPSApplyP, __LINE__);
306 fMPSApplyRx =
307 (int (*)(void *, unsigned int, double))GetFunction("MPSApplyRx");
308 CheckFunction((void *)fMPSApplyRx, __LINE__);
309 fMPSApplyRy =
310 (int (*)(void *, unsigned int, double))GetFunction("MPSApplyRy");
311 CheckFunction((void *)fMPSApplyRy, __LINE__);
312 fMPSApplyRz =
313 (int (*)(void *, unsigned int, double))GetFunction("MPSApplyRz");
314 CheckFunction((void *)fMPSApplyRz, __LINE__);
315 fMPSApplyU = (int (*)(void *, unsigned int, double, double, double,
316 double))GetFunction("MPSApplyU");
317 CheckFunction((void *)fMPSApplyU, __LINE__);
318 fMPSApplySwap = (int (*)(void *, unsigned int,
319 unsigned int))GetFunction("MPSApplySwap");
320 CheckFunction((void *)fMPSApplySwap, __LINE__);
321 fMPSApplyCX = (int (*)(void *, unsigned int,
322 unsigned int))GetFunction("MPSApplyCX");
323 CheckFunction((void *)fMPSApplyCX, __LINE__);
324 fMPSApplyCY = (int (*)(void *, unsigned int,
325 unsigned int))GetFunction("MPSApplyCY");
326 CheckFunction((void *)fMPSApplyCY, __LINE__);
327 fMPSApplyCZ = (int (*)(void *, unsigned int,
328 unsigned int))GetFunction("MPSApplyCZ");
329 CheckFunction((void *)fMPSApplyCZ, __LINE__);
330 fMPSApplyCH = (int (*)(void *, unsigned int,
331 unsigned int))GetFunction("MPSApplyCH");
332 CheckFunction((void *)fMPSApplyCH, __LINE__);
333 fMPSApplyCSX = (int (*)(void *, unsigned int,
334 unsigned int))GetFunction("MPSApplyCSX");
335 CheckFunction((void *)fMPSApplyCSX, __LINE__);
336 fMPSApplyCSXDG = (int (*)(void *, unsigned int,
337 unsigned int))GetFunction("MPSApplyCSXDG");
338 CheckFunction((void *)fMPSApplyCSXDG, __LINE__);
339 fMPSApplyCP = (int (*)(void *, unsigned int, unsigned int,
340 double))GetFunction("MPSApplyCP");
341 CheckFunction((void *)fMPSApplyCP, __LINE__);
342 fMPSApplyCRx = (int (*)(void *, unsigned int, unsigned int,
343 double))GetFunction("MPSApplyCRx");
344 CheckFunction((void *)fMPSApplyCRx, __LINE__);
345 fMPSApplyCRy = (int (*)(void *, unsigned int, unsigned int,
346 double))GetFunction("MPSApplyCRy");
347 CheckFunction((void *)fMPSApplyCRy, __LINE__);
348 fMPSApplyCRz = (int (*)(void *, unsigned int, unsigned int,
349 double))GetFunction("MPSApplyCRz");
350 CheckFunction((void *)fMPSApplyCRz, __LINE__);
351 fMPSApplyCU =
352 (int (*)(void *, unsigned int, unsigned int, double, double,
353 double, double))GetFunction("MPSApplyCU");
354 CheckFunction((void *)fMPSApplyCU, __LINE__);
355
356 return true;
357 } else
358 std::cout << "GpuLibrary: Unable to initialize gpu library"
359 << std::endl;
360 } else
361 std::cout << "GpuLibrary: Unable to get initialization function for "
362 "gpu library"
363 << std::endl;
364 } else if (!Utils::Library::IsMuted())
365 std::cout << "GpuLibrary: Unable to load gpu library" << std::endl;
366
367 return false;
368 }
369
370 static void CheckFunction(void *func, int line) {
371 if (!func) {
372 std::cout << "GpuLibrary: Unable to load function, line #: "
373 << std::to_string(line);
374 const char *dlsym_error = dlerror();
375 if (dlsym_error)
376 std::cout << ", error: " << dlsym_error;
377
378 std::cout << std::endl;
379 }
380 }
381
382 bool IsValid() const { return LibraryHandle != nullptr; }
383
384 // statevector functions
385
386 void *CreateStateVector() {
387 if (LibraryHandle)
388 return fCreateStateVector(LibraryHandle);
389 else
390 throw std::runtime_error("GpuLibrary: Unable to create state vector");
391 }
392
393 void DestroyStateVector(void *obj) {
394 if (LibraryHandle)
395 fDestroyStateVector(obj);
396 else
397 throw std::runtime_error("GpuLibrary: Unable to destroy state vector");
398 }
399
400 bool Create(void *obj, unsigned int nrQubits) {
401 if (LibraryHandle)
402 return fCreate(obj, nrQubits) == 1;
403 else
404 throw std::runtime_error(
405 "GpuLibrary: Unable to create state vector state");
406
407 return false;
408 }
409
410 bool CreateWithState(void *obj, unsigned int nrQubits, const double *state) {
411 if (LibraryHandle)
412 return fCreateWithState(obj, nrQubits, state) == 1;
413 else
414 throw std::runtime_error(
415 "GpuLibrary: Unable to create state vector state with a state");
416
417 return false;
418 }
419
420 bool Reset(void *obj) {
421 if (LibraryHandle)
422 return fReset(obj) == 1;
423 else
424 throw std::runtime_error("GpuLibrary: Unable to reset state vector");
425
426 return false;
427 }
428
429 bool SetDataType(void *obj, int dataType) {
430 if (LibraryHandle)
431 return fSetDataType(obj, dataType) == 1;
432 else
433 throw std::runtime_error("GpuLibrary: Unable to set data type");
434
435 return false;
436 }
437
438 bool IsDoublePrecision(void *obj) const {
439 if (LibraryHandle)
440 return fIsDoublePrecision(obj) == 1;
441 else
442 throw std::runtime_error(
443 "GpuLibrary: Unable to check if double precision");
444
445 return false;
446 }
447
448 bool MeasureQubitCollapse(void *obj, int qubitIndex) {
449 if (LibraryHandle)
450 return fMeasureQubitCollapse(obj, qubitIndex) == 1;
451 else
452 throw std::runtime_error(
453 "GpuLibrary: Unable to measure qubit with collapse");
454
455 return false;
456 }
457
458 bool MeasureQubitNoCollapse(void *obj, int qubitIndex) {
459 if (LibraryHandle)
460 return fMeasureQubitNoCollapse(obj, qubitIndex) == 1;
461 else
462 throw std::runtime_error(
463 "GpuLibrary: Unable to measure qubit no collapse");
464
465 return false;
466 }
467
468 bool MeasureQubitsCollapse(void *obj, int *qubits, int *bitstring,
469 int bitstringLen) {
470 if (LibraryHandle)
471 return fMeasureQubitsCollapse(obj, qubits, bitstring, bitstringLen) == 1;
472 else
473 throw std::runtime_error(
474 "GpuLibrary: Unable to measure qubits with collapse");
475
476 return false;
477 }
478
479 bool MeasureQubitsNoCollapse(void *obj, int *qubits, int *bitstring,
480 int bitstringLen) {
481 if (LibraryHandle)
482 return fMeasureQubitsNoCollapse(obj, qubits, bitstring, bitstringLen) ==
483 1;
484 else
485 throw std::runtime_error(
486 "GpuLibrary: Unable to measure qubits with no collapse");
487
488 return false;
489 }
490
491 unsigned long long MeasureAllQubitsCollapse(void *obj) {
492 if (LibraryHandle)
493 return fMeasureAllQubitsCollapse(obj);
494 else
495 throw std::runtime_error(
496 "GpuLibrary: Unable to measure all qubits with collapse");
497
498 return 0;
499 }
500
501 unsigned long long MeasureAllQubitsNoCollapse(void *obj) {
502 if (LibraryHandle)
503 return fMeasureAllQubitsNoCollapse(obj);
504 else
505 throw std::runtime_error(
506 "GpuLibrary: Unable to measure all qubits with no collapse");
507
508 return 0;
509 }
510
511 bool SaveState(void *obj) {
512 if (LibraryHandle)
513 return fSaveState(obj) == 1;
514 else
515 throw std::runtime_error("GpuLibrary: Unable to save state");
516
517 return false;
518 }
519
520 bool SaveStateToHost(void *obj) {
521 if (LibraryHandle)
522 return fSaveStateToHost(obj) == 1;
523 else
524 throw std::runtime_error("GpuLibrary: Unable to save state to host");
525
526 return false;
527 }
528
529 bool SaveStateDestructive(void *obj) {
530 if (LibraryHandle)
531 return fSaveStateDestructive(obj) == 1;
532 else
533 throw std::runtime_error(
534 "GpuLibrary: Unable to save state destructively");
535
536 return false;
537 }
538
539 bool RestoreStateFreeSaved(void *obj) {
540 if (LibraryHandle)
541 return fRestoreStateFreeSaved(obj) == 1;
542 else
543 throw std::runtime_error(
544 "GpuLibrary: Unable to restore state free saved");
545
546 return false;
547 }
548
549 bool RestoreStateNoFreeSaved(void *obj) {
550 if (LibraryHandle)
551 return fRestoreStateNoFreeSaved(obj) == 1;
552 else
553 throw std::runtime_error(
554 "GpuLibrary: Unable to restore state no free saved");
555
556 return false;
557 }
558
559 void FreeSavedState(void *obj) {
560 if (LibraryHandle)
561 fFreeSavedState(obj);
562 else
563 throw std::runtime_error("GpuLibrary: Unable to free saved state");
564 }
565
566 void *Clone(void *obj) const {
567 if (LibraryHandle)
568 return fClone(obj);
569 else
570 throw std::runtime_error("GpuLibrary: Unable to clone state vector");
571
572 return nullptr;
573 }
574
575 bool Sample(void *obj, unsigned int nSamples, long int *samples,
576 unsigned int nBits, int *bits) {
577 if (LibraryHandle)
578 return fSample(obj, nSamples, samples, nBits, bits) == 1;
579 else
580 throw std::runtime_error("GpuLibrary: Unable to sample state vector");
581
582 return false;
583 }
584
585 bool SampleAll(void *obj, unsigned int nSamples, long int *samples) {
586 if (LibraryHandle)
587 return fSampleAll(obj, nSamples, samples) == 1;
588 else
589 throw std::runtime_error("GpuLibrary: Unable to sample state vector");
590
591 return false;
592 }
593
594 bool Amplitude(void *obj, long long int state, double *real,
595 double *imaginary) const {
596 if (LibraryHandle)
597 return fAmplitude(obj, state, real, imaginary) == 1;
598 else
599 throw std::runtime_error("GpuLibrary: Unable to get amplitude");
600
601 return false;
602 }
603
604 double Probability(void *obj, int *qubits, int *mask, int len) const {
605 if (LibraryHandle)
606 return fProbability(obj, qubits, mask, len);
607 else
608 throw std::runtime_error("GpuLibrary: Unable to get probability");
609
610 return 0;
611 }
612
613 double BasisStateProbability(void *obj, long long int state) const {
614 if (LibraryHandle)
615 return fBasisStateProbability(obj, state);
616 else
617 throw std::runtime_error(
618 "GpuLibrary: Unable to get basis state probability");
619
620 return 0;
621 }
622
623 bool AllProbabilities(void *obj, double *probabilities) const {
624 if (LibraryHandle)
625 return fAllProbabilities(obj, probabilities) == 1;
626 else
627 throw std::runtime_error("GpuLibrary: Unable to get all probabilities");
628
629 return false;
630 }
631
632 double ExpectationValue(void *obj, const char *pauliString, int len) const {
633 if (LibraryHandle)
634 return fExpectationValue(obj, pauliString, len);
635 else
636 throw std::runtime_error("GpuLibrary: Unable to get expectation value");
637
638 return 0;
639 }
640
641 bool ApplyX(void *obj, int qubit) {
642 if (LibraryHandle)
643 return fApplyX(obj, qubit) == 1;
644 else
645 throw std::runtime_error("GpuLibrary: Unable to apply X gate");
646
647 return false;
648 }
649
650 bool ApplyY(void *obj, int qubit) {
651 if (LibraryHandle)
652 return fApplyY(obj, qubit) == 1;
653 else
654 throw std::runtime_error("GpuLibrary: Unable to apply Y gate");
655
656 return false;
657 }
658
659 bool ApplyZ(void *obj, int qubit) {
660 if (LibraryHandle)
661 return fApplyZ(obj, qubit) == 1;
662 else
663 throw std::runtime_error("GpuLibrary: Unable to apply Z gate");
664
665 return false;
666 }
667
668 bool ApplyH(void *obj, int qubit) {
669 if (LibraryHandle)
670 return fApplyH(obj, qubit) == 1;
671 else
672 throw std::runtime_error("GpuLibrary: Unable to apply H gate");
673
674 return false;
675 }
676
677 bool ApplyS(void *obj, int qubit) {
678 if (LibraryHandle)
679 return fApplyS(obj, qubit) == 1;
680 else
681 throw std::runtime_error("GpuLibrary: Unable to apply S gate");
682
683 return false;
684 }
685
686 bool ApplySDG(void *obj, int qubit) {
687 if (LibraryHandle)
688 return fApplySDG(obj, qubit) == 1;
689 else
690 throw std::runtime_error("GpuLibrary: Unable to apply SDG gate");
691
692 return false;
693 }
694
695 bool ApplyT(void *obj, int qubit) {
696 if (LibraryHandle)
697 return fApplyT(obj, qubit) == 1;
698 else
699 throw std::runtime_error("GpuLibrary: Unable to apply T gate");
700
701 return false;
702 }
703
704 bool ApplyTDG(void *obj, int qubit) {
705 if (LibraryHandle)
706 return fApplyTDG(obj, qubit) == 1;
707 else
708 throw std::runtime_error("GpuLibrary: Unable to apply TDG gate");
709
710 return false;
711 }
712
713 bool ApplySX(void *obj, int qubit) {
714 if (LibraryHandle)
715 return fApplySX(obj, qubit) == 1;
716 else
717 throw std::runtime_error("GpuLibrary: Unable to apply SX gate");
718
719 return false;
720 }
721
722 bool ApplySXDG(void *obj, int qubit) {
723 if (LibraryHandle)
724 return fApplySXDG(obj, qubit) == 1;
725 else
726 throw std::runtime_error("GpuLibrary: Unable to apply SXDG gate");
727
728 return false;
729 }
730
731 bool ApplyK(void *obj, int qubit) {
732 if (LibraryHandle)
733 return fApplyK(obj, qubit) == 1;
734 else
735 throw std::runtime_error("GpuLibrary: Unable to apply K gate");
736
737 return false;
738 }
739
740 bool ApplyP(void *obj, int qubit, double theta) {
741 if (LibraryHandle)
742 return fApplyP(obj, qubit, theta) == 1;
743 else
744 throw std::runtime_error("GpuLibrary: Unable to apply P gate");
745
746 return false;
747 }
748
749 bool ApplyRx(void *obj, int qubit, double theta) {
750 if (LibraryHandle)
751 return fApplyRx(obj, qubit, theta) == 1;
752 else
753 throw std::runtime_error("GpuLibrary: Unable to apply Rx gate");
754
755 return false;
756 }
757
758 bool ApplyRy(void *obj, int qubit, double theta) {
759 if (LibraryHandle)
760 return fApplyRy(obj, qubit, theta) == 1;
761 else
762 throw std::runtime_error("GpuLibrary: Unable to apply Ry gate");
763
764 return false;
765 }
766
767 bool ApplyRz(void *obj, int qubit, double theta) {
768 if (LibraryHandle)
769 return fApplyRz(obj, qubit, theta) == 1;
770 else
771 throw std::runtime_error("GpuLibrary: Unable to apply Rz gate");
772
773 return false;
774 }
775
776 bool ApplyU(void *obj, int qubit, double theta, double phi, double lambda,
777 double gamma) {
778 if (LibraryHandle)
779 return fApplyU(obj, qubit, theta, phi, lambda, gamma) == 1;
780 else
781 throw std::runtime_error("GpuLibrary: Unable to apply U gate");
782
783 return false;
784 }
785
786 bool ApplyCX(void *obj, int controlQubit, int targetQubit) {
787 if (LibraryHandle)
788 return fApplyCX(obj, controlQubit, targetQubit) == 1;
789 else
790 throw std::runtime_error("GpuLibrary: Unable to apply CX gate");
791
792 return false;
793 }
794
795 bool ApplyCY(void *obj, int controlQubit, int targetQubit) {
796 if (LibraryHandle)
797 return fApplyCY(obj, controlQubit, targetQubit) == 1;
798 else
799 throw std::runtime_error("GpuLibrary: Unable to apply CY gate");
800
801 return false;
802 }
803
804 bool ApplyCZ(void *obj, int controlQubit, int targetQubit) {
805 if (LibraryHandle)
806 return fApplyCZ(obj, controlQubit, targetQubit) == 1;
807 else
808 throw std::runtime_error("GpuLibrary: Unable to apply CZ gate");
809
810 return false;
811 }
812
813 bool ApplyCH(void *obj, int controlQubit, int targetQubit) {
814 if (LibraryHandle)
815 return fApplyCH(obj, controlQubit, targetQubit) == 1;
816 else
817 throw std::runtime_error("GpuLibrary: Unable to apply CH gate");
818
819 return false;
820 }
821
822 bool ApplyCSX(void *obj, int controlQubit, int targetQubit) {
823 if (LibraryHandle)
824 return fApplyCSX(obj, controlQubit, targetQubit) == 1;
825 else
826 throw std::runtime_error("GpuLibrary: Unable to apply CSX gate");
827
828 return false;
829 }
830
831 bool ApplyCSXDG(void *obj, int controlQubit, int targetQubit) {
832 if (LibraryHandle)
833 return fApplyCSXDG(obj, controlQubit, targetQubit) == 1;
834 else
835 throw std::runtime_error("GpuLibrary: Unable to apply CSXDG gate");
836
837 return false;
838 }
839
840 bool ApplyCP(void *obj, int controlQubit, int targetQubit, double theta) {
841 if (LibraryHandle)
842 return fApplyCP(obj, controlQubit, targetQubit, theta) == 1;
843 else
844 throw std::runtime_error("GpuLibrary: Unable to apply CP gate");
845
846 return false;
847 }
848
849 bool ApplyCRx(void *obj, int controlQubit, int targetQubit, double theta) {
850 if (LibraryHandle)
851 return fApplyCRx(obj, controlQubit, targetQubit, theta) == 1;
852 else
853 throw std::runtime_error("GpuLibrary: Unable to apply CRx gate");
854
855 return false;
856 }
857
858 bool ApplyCRy(void *obj, int controlQubit, int targetQubit, double theta) {
859 if (LibraryHandle)
860 return fApplyCRy(obj, controlQubit, targetQubit, theta) == 1;
861 else
862 throw std::runtime_error("GpuLibrary: Unable to apply CRy gate");
863
864 return false;
865 }
866
867 bool ApplyCRz(void *obj, int controlQubit, int targetQubit, double theta) {
868 if (LibraryHandle)
869 return fApplyCRz(obj, controlQubit, targetQubit, theta) == 1;
870 else
871 throw std::runtime_error("GpuLibrary: Unable to apply CRz gate");
872
873 return false;
874 }
875
876 bool ApplyCCX(void *obj, int controlQubit1, int controlQubit2,
877 int targetQubit) {
878 if (LibraryHandle)
879 return fApplyCCX(obj, controlQubit1, controlQubit2, targetQubit) == 1;
880 else
881 throw std::runtime_error("GpuLibrary: Unable to apply CCX gate");
882
883 return false;
884 }
885
886 bool ApplySwap(void *obj, int qubit1, int qubit2) {
887 if (LibraryHandle)
888 return fApplySwap(obj, qubit1, qubit2) == 1;
889 else
890 throw std::runtime_error("GpuLibrary: Unable to apply Swap gate");
891
892 return false;
893 }
894
895 bool ApplyCSwap(void *obj, int controlQubit, int qubit1, int qubit2) {
896 if (LibraryHandle)
897 return fApplyCSwap(obj, controlQubit, qubit1, qubit2) == 1;
898 else
899 throw std::runtime_error("GpuLibrary: Unable to apply CSwap gate");
900
901 return false;
902 }
903
904 bool ApplyCU(void *obj, int controlQubit, int targetQubit, double theta,
905 double phi, double lambda, double gamma) {
906 if (LibraryHandle)
907 return fApplyCU(obj, controlQubit, targetQubit, theta, phi, lambda,
908 gamma) == 1;
909 else
910 throw std::runtime_error("GpuLibrary: Unable to apply CU gate");
911
912 return false;
913 }
914
915 // mps functions
916
917 void *CreateMPS() {
918 if (LibraryHandle)
919 return fCreateMPS(LibraryHandle);
920 else
921 throw std::runtime_error("GpuLibrary: Unable to create mps");
922 }
923
924 void DestroyMPS(void *obj) {
925 if (LibraryHandle)
926 fDestroyMPS(obj);
927 else
928 throw std::runtime_error("GpuLibrary: Unable to destroy mps");
929 }
930
931 bool MPSCreate(void *obj, unsigned int nrQubits) {
932 if (LibraryHandle)
933 return fMPSCreate(obj, nrQubits) == 1;
934 else
935 throw std::runtime_error("GpuLibrary: Unable to create mps with the "
936 "specified number of qubits");
937
938 return false;
939 }
940
941 bool MPSReset(void *obj) {
942 if (LibraryHandle)
943 return fMPSReset(obj) == 1;
944 else
945 throw std::runtime_error("GpuLibrary: Unable to reset mps");
946
947 return false;
948 }
949
950 bool MPSIsValid(void *obj) const {
951 if (LibraryHandle)
952 return fMPSIsValid(obj) == 1;
953 else
954 throw std::runtime_error("GpuLibrary: Unable to check if mps is valid");
955
956 return false;
957 }
958
959 bool MPSIsCreated(void *obj) const {
960 if (LibraryHandle)
961 return fMPSIsCreated(obj) == 1;
962 else
963 throw std::runtime_error("GpuLibrary: Unable to check if mps is created");
964
965 return false;
966 }
967
968 bool MPSSetDataType(void *obj, int useDoublePrecision) {
969 if (LibraryHandle)
970 return fMPSSetDataType(obj, useDoublePrecision) == 1;
971 else
972 throw std::runtime_error("GpuLibrary: Unable to set precision for mps");
973
974 return false;
975 }
976
977 bool MPSIsDoublePrecision(void *obj) const {
978 if (LibraryHandle)
979 return fMPSIsDoublePrecision(obj) == 1;
980 else
981 throw std::runtime_error("GpuLibrary: Unable to get precision for mps");
982
983 return false;
984 }
985
986 bool MPSSetCutoff(void *obj, double val) {
987 if (LibraryHandle)
988 return fMPSSetCutoff(obj, val) == 1;
989 else
990 throw std::runtime_error("GpuLibrary: Unable to set cutoff for mps");
991
992 return false;
993 }
994
995 double MPSGetCutoff(void *obj) const {
996 if (LibraryHandle)
997 return fMPSGetCutoff(obj);
998 else
999 throw std::runtime_error("GpuLibrary: Unable to get cutoff for mps");
1000 }
1001
1002 bool MPSSetGesvdJ(void *obj, int val) {
1003 if (LibraryHandle)
1004 return fMPSSetGesvdJ(obj, val) == 1;
1005 else
1006 throw std::runtime_error("GpuLibrary: Unable to set GesvdJ for mps");
1007
1008 return false;
1009 }
1010
1011 bool MPSGetGesvdJ(void *obj) const {
1012 if (LibraryHandle)
1013 return fMPSGetGesvdJ(obj) == 1;
1014 else
1015 throw std::runtime_error("GpuLibrary: Unable to get GesvdJ for mps");
1016
1017 return false;
1018 }
1019
1020 bool MPSSetMaxExtent(void *obj, long int val) {
1021 if (LibraryHandle)
1022 return fMPSSetMaxExtent(obj, val) == 1;
1023 else
1024 throw std::runtime_error("GpuLibrary: Unable to set max extent for mps");
1025
1026 return false;
1027 }
1028
1029 long int MPSGetMaxExtent(void *obj) {
1030 if (LibraryHandle)
1031 return fMPSGetMaxExtent(obj);
1032 else
1033 throw std::runtime_error("GpuLibrary: Unable to get max extent for mps");
1034
1035 return 0;
1036 }
1037
1038 int MPSGetNrQubits(void *obj) {
1039 if (LibraryHandle)
1040 return fMPSGetNrQubits(obj);
1041 else
1042 throw std::runtime_error("GpuLibrary: Unable to get nr qubits for mps");
1043
1044 return 0;
1045 }
1046
1047 bool MPSAmplitude(void *obj, long int numFixedValues, long int *fixedValues,
1048 double *real, double *imaginary) {
1049 if (LibraryHandle)
1050 return fMPSAmplitude(obj, numFixedValues, fixedValues, real, imaginary) ==
1051 1;
1052 else
1053 throw std::runtime_error("GpuLibrary: Unable to get mps amplitude");
1054
1055 return false;
1056 }
1057
1058 double MPSProbability0(void *obj, unsigned int qubit) {
1059 if (LibraryHandle)
1060 return fMPSProbability0(obj, qubit);
1061 else
1062 throw std::runtime_error(
1063 "GpuLibrary: Unable to get probability for 0 for mps");
1064
1065 return 0.0;
1066 }
1067
1068 bool MPSMeasure(void *obj, unsigned int qubit) {
1069 if (LibraryHandle)
1070 return fMPSMeasure(obj, qubit) == 1;
1071 else
1072 throw std::runtime_error("GpuLibrary: Unable to measure qubit on mps");
1073
1074 return false;
1075 }
1076
1077 bool MPSMeasureQubits(void *obj, long int numQubits, unsigned int *qubits,
1078 int *result) {
1079 if (LibraryHandle)
1080 return fMPSMeasureQubits(obj, numQubits, qubits, result) == 1;
1081 else
1082 throw std::runtime_error("GpuLibrary: Unable to measure qubits on mps");
1083
1084 return false;
1085 }
1086
1087 std::unordered_map<std::vector<bool>, int64_t> *MPSGetMapForSample() {
1088 if (LibraryHandle)
1089 return (std::unordered_map<std::vector<bool>, int64_t> *)
1090 fMPSGetMapForSample();
1091 else
1092 throw std::runtime_error(
1093 "GpuLibrary: Unable to get map for sample for mps");
1094
1095 return nullptr;
1096 }
1097
1098 bool
1099 MPSFreeMapForSample(std::unordered_map<std::vector<bool>, int64_t> *map) {
1100 if (LibraryHandle)
1101 return fMPSFreeMapForSample((void *)map) == 1;
1102 else
1103 throw std::runtime_error(
1104 "GpuLibrary: Unable to free map for sample for mps");
1105
1106 return false;
1107 }
1108
1109 bool MPSSample(void *obj, long int numShots, long int numQubits,
1110 unsigned int *qubits, void *resultMap) {
1111 if (LibraryHandle)
1112 return fMPSSample(obj, numShots, numQubits, qubits, resultMap) == 1;
1113 else
1114 throw std::runtime_error("GpuLibrary: Unable to sample mps");
1115
1116 return false;
1117 }
1118
1119 bool MPSSaveState(void *obj) {
1120 if (LibraryHandle)
1121 return fMPSSaveState(obj) == 1;
1122 else
1123 throw std::runtime_error("GpuLibrary: Unable to save mps state");
1124
1125 return false;
1126 }
1127
1128 bool MPSRestoreState(void *obj) {
1129 if (LibraryHandle)
1130 return fMPSRestoreState(obj) == 1;
1131 else
1132 throw std::runtime_error("GpuLibrary: Unable to restore mps state");
1133
1134 return false;
1135 }
1136
1137 bool MPSCleanSavedState(void *obj) {
1138 if (LibraryHandle)
1139 return fMPSCleanSavedState(obj) == 1;
1140 else
1141 throw std::runtime_error("GpuLibrary: Unable to clean mps saved state");
1142
1143 return false;
1144 }
1145
1146 void *MPSClone(void *obj) {
1147 if (LibraryHandle)
1148 return fMPSClone(obj);
1149 else
1150 throw std::runtime_error("GpuLibrary: Unable to clone mps");
1151
1152 return nullptr;
1153 }
1154
1155 double MPSExpectationValue(void *obj, const char *pauliString,
1156 int len) const {
1157 if (LibraryHandle)
1158 return fMPSExpectationValue(obj, pauliString, len);
1159 else
1160 throw std::runtime_error(
1161 "GpuLibrary: Unable to get mps expectation value");
1162
1163 return 0;
1164 }
1165
1166 bool MPSApplyX(void *obj, unsigned int siteA) {
1167 if (LibraryHandle)
1168 return fMPSApplyX(obj, siteA) == 1;
1169 else
1170 throw std::runtime_error("GpuLibrary: Unable to apply X gate on mps");
1171
1172 return false;
1173 }
1174
1175 bool MPSApplyY(void *obj, unsigned int siteA) {
1176 if (LibraryHandle)
1177 return fMPSApplyY(obj, siteA) == 1;
1178 else
1179 throw std::runtime_error("GpuLibrary: Unable to apply Y gate on mps");
1180
1181 return false;
1182 }
1183
1184 bool MPSApplyZ(void *obj, unsigned int siteA) {
1185 if (LibraryHandle)
1186 return fMPSApplyZ(obj, siteA) == 1;
1187 else
1188 throw std::runtime_error("GpuLibrary: Unable to apply Z gate on mps");
1189
1190 return false;
1191 }
1192
1193 bool MPSApplyH(void *obj, unsigned int siteA) {
1194 if (LibraryHandle)
1195 return fMPSApplyH(obj, siteA) == 1;
1196 else
1197 throw std::runtime_error("GpuLibrary: Unable to apply H gate on mps");
1198
1199 return false;
1200 }
1201
1202 bool MPSApplyS(void *obj, unsigned int siteA) {
1203 if (LibraryHandle)
1204 return fMPSApplyS(obj, siteA) == 1;
1205 else
1206 throw std::runtime_error("GpuLibrary: Unable to apply S gate on mps");
1207
1208 return false;
1209 }
1210
1211 bool MPSApplySDG(void *obj, unsigned int siteA) {
1212 if (LibraryHandle)
1213 return fMPSApplySDG(obj, siteA) == 1;
1214 else
1215 throw std::runtime_error("GpuLibrary: Unable to apply sdg gate on mps");
1216
1217 return false;
1218 }
1219
1220 bool MPSApplyT(void *obj, unsigned int siteA) {
1221 if (LibraryHandle)
1222 return fMPSApplyT(obj, siteA) == 1;
1223 else
1224 throw std::runtime_error("GpuLibrary: Unable to apply t gate on mps");
1225
1226 return false;
1227 }
1228
1229 bool MPSApplyTDG(void *obj, unsigned int siteA) {
1230 if (LibraryHandle)
1231 return fMPSApplyTDG(obj, siteA) == 1;
1232 else
1233 throw std::runtime_error("GpuLibrary: Unable to appl tdg gate on mps");
1234
1235 return false;
1236 }
1237
1238 bool MPSApplySX(void *obj, unsigned int siteA) {
1239 if (LibraryHandle)
1240 return fMPSApplySX(obj, siteA) == 1;
1241 else
1242 throw std::runtime_error("GpuLibrary: Unable to apply sx gate on mps");
1243
1244 return false;
1245 }
1246
1247 bool MPSApplySXDG(void *obj, unsigned int siteA) {
1248 if (LibraryHandle)
1249 return fMPSApplySXDG(obj, siteA) == 1;
1250 else
1251 throw std::runtime_error("GpuLibrary: Unable to apply sxdg gate on mps");
1252
1253 return false;
1254 }
1255
1256 bool MPSApplyK(void *obj, unsigned int siteA) {
1257 if (LibraryHandle)
1258 return fMPSApplyK(obj, siteA) == 1;
1259 else
1260 throw std::runtime_error("GpuLibrary: Unable to apply k gate on mps");
1261
1262 return false;
1263 }
1264
1265 bool MPSApplyP(void *obj, unsigned int siteA, double theta) {
1266 if (LibraryHandle)
1267 return fMPSApplyP(obj, siteA, theta) == 1;
1268 else
1269 throw std::runtime_error("GpuLibrary: Unable to apply p gate on mps");
1270 return false;
1271 }
1272
1273 bool MPSApplyRx(void *obj, unsigned int siteA, double theta) {
1274 if (LibraryHandle)
1275 return fMPSApplyRx(obj, siteA, theta) == 1;
1276 else
1277 throw std::runtime_error("GpuLibrary: Unable to apply rx gate on mps");
1278
1279 return false;
1280 }
1281
1282 bool MPSApplyRy(void *obj, unsigned int siteA, double theta) {
1283 if (LibraryHandle)
1284 return fMPSApplyRy(obj, siteA, theta) == 1;
1285 else
1286 throw std::runtime_error("GpuLibrary: Unable to apply ry gate on mps");
1287
1288 return false;
1289 }
1290
1291 bool MPSApplyRz(void *obj, unsigned int siteA, double theta) {
1292 if (LibraryHandle)
1293 return fMPSApplyRz(obj, siteA, theta) == 1;
1294 else
1295 throw std::runtime_error("GpuLibrary: Unable to apply rz gate on mps");
1296
1297 return false;
1298 }
1299
1300 bool MPSApplyU(void *obj, unsigned int siteA, double theta, double phi,
1301 double lambda, double gamma) {
1302 if (LibraryHandle)
1303 return fMPSApplyU(obj, siteA, theta, phi, lambda, gamma) == 1;
1304 else
1305 throw std::runtime_error("GpuLibrary: Unable to apply u gate on mps");
1306
1307 return false;
1308 }
1309
1310 bool MPSApplySwap(void *obj, unsigned int controlQubit,
1311 unsigned int targetQubit) {
1312 if (LibraryHandle)
1313 return fMPSApplySwap(obj, controlQubit, targetQubit) == 1;
1314 else
1315 throw std::runtime_error("GpuLibrary: Unable to apply swap gate on mps");
1316
1317 return false;
1318 }
1319
1320 bool MPSApplyCX(void *obj, unsigned int controlQubit,
1321 unsigned int targetQubit) {
1322 if (LibraryHandle)
1323 return fMPSApplyCX(obj, controlQubit, targetQubit) == 1;
1324 else
1325 throw std::runtime_error("GpuLibrary: Unable to apply cx gate on mps");
1326
1327 return false;
1328 }
1329
1330 bool MPSApplyCY(void *obj, unsigned int controlQubit,
1331 unsigned int targetQubit) {
1332 if (LibraryHandle)
1333 return fMPSApplyCY(obj, controlQubit, targetQubit) == 1;
1334 else
1335 throw std::runtime_error("GpuLibrary: Unable to apply cy gate on mps");
1336
1337 return false;
1338 }
1339
1340 bool MPSApplyCZ(void *obj, unsigned int controlQubit,
1341 unsigned int targetQubit) {
1342 if (LibraryHandle)
1343 return fMPSApplyCZ(obj, controlQubit, targetQubit) == 1;
1344 else
1345 throw std::runtime_error("GpuLibrary: Unable to apply cz gate on mps");
1346
1347 return false;
1348 }
1349
1350 bool MPSApplyCH(void *obj, unsigned int controlQubit,
1351 unsigned int targetQubit) {
1352 if (LibraryHandle)
1353 return fMPSApplyCH(obj, controlQubit, targetQubit) == 1;
1354 else
1355 throw std::runtime_error("GpuLibrary: Unable to apply ch gate on mps");
1356
1357 return false;
1358 }
1359
1360 bool MPSApplyCSX(void *obj, unsigned int controlQubit,
1361 unsigned int targetQubit) {
1362 if (LibraryHandle)
1363 return fMPSApplyCSX(obj, controlQubit, targetQubit) == 1;
1364 else
1365 throw std::runtime_error("GpuLibrary: Unable to apply csx gate on mps");
1366 }
1367
1368 bool MPSApplyCSXDG(void *obj, unsigned int controlQubit,
1369 unsigned int targetQubit) {
1370 if (LibraryHandle)
1371 return fMPSApplyCSXDG(obj, controlQubit, targetQubit) == 1;
1372 else
1373 throw std::runtime_error("GpuLibrary: Unable to apply csxdg gate on mps");
1374
1375 return false;
1376 }
1377
1378 bool MPSApplyCP(void *obj, unsigned int controlQubit,
1379 unsigned int targetQubit, double theta) {
1380 if (LibraryHandle)
1381 return fMPSApplyCP(obj, controlQubit, targetQubit, theta) == 1;
1382 else
1383 throw std::runtime_error("GpuLibrary: Unable to apply cp gate on mps");
1384
1385 return false;
1386 }
1387
1388 bool MPSApplyCRx(void *obj, unsigned int controlQubit,
1389 unsigned int targetQubit, double theta) {
1390 if (LibraryHandle)
1391 return fMPSApplyCRx(obj, controlQubit, targetQubit, theta) == 1;
1392 else
1393 throw std::runtime_error("GpuLibrary: Unable to apply crx gate on mps");
1394
1395 return false;
1396 }
1397
1398 bool MPSApplyCRy(void *obj, unsigned int controlQubit,
1399 unsigned int targetQubit, double theta) {
1400 if (LibraryHandle)
1401 return fMPSApplyCRy(obj, controlQubit, targetQubit, theta) == 1;
1402 else
1403 throw std::runtime_error("GpuLibrary: Unable to apply cry gate on mps");
1404
1405 return false;
1406 }
1407
1408 bool MPSApplyCRz(void *obj, unsigned int controlQubit,
1409 unsigned int targetQubit, double theta) {
1410 if (LibraryHandle)
1411 return fMPSApplyCRz(obj, controlQubit, targetQubit, theta) == 1;
1412 else
1413 throw std::runtime_error("GpuLibrary: Unable to apply crz gate on mps");
1414
1415 return false;
1416 }
1417
1418 bool MPSApplyCU(void *obj, unsigned int controlQubit,
1419 unsigned int targetQubit, double theta, double phi,
1420 double lambda, double gamma) {
1421 if (LibraryHandle)
1422 return fMPSApplyCU(obj, controlQubit, targetQubit, theta, phi, lambda,
1423 gamma) == 1;
1424 else
1425 throw std::runtime_error("GpuLibrary: Unable to apply cu gate on mps");
1426
1427 return false;
1428 }
1429
1430private:
1431 void *LibraryHandle = nullptr;
1432
1433 void *(*InitLib)();
1434 void (*FreeLib)();
1435
1436 void *(*fCreateStateVector)(void *);
1437 void (*fDestroyStateVector)(void *);
1438
1439 // statevector functions
1440 int (*fSetDataType)(void *, int);
1441 int (*fIsDoublePrecision)(void *);
1442 int (*fCreate)(void *, unsigned int);
1443 int (*fReset)(void *);
1444 int (*fCreateWithState)(void *, unsigned int, const double *);
1445 int (*fMeasureQubitCollapse)(void *, int);
1446 int (*fMeasureQubitNoCollapse)(void *, int);
1447 int (*fMeasureQubitsCollapse)(void *, int *, int *, int);
1448 int (*fMeasureQubitsNoCollapse)(void *, int *, int *, int);
1449 unsigned long long (*fMeasureAllQubitsCollapse)(void *);
1450 unsigned long long (*fMeasureAllQubitsNoCollapse)(void *);
1451
1452 int (*fSaveState)(void *);
1453 int (*fSaveStateToHost)(void *);
1454 int (*fSaveStateDestructive)(void *);
1455 int (*fRestoreStateFreeSaved)(void *);
1456 int (*fRestoreStateNoFreeSaved)(void *);
1457 void (*fFreeSavedState)(void *obj);
1458 void *(*fClone)(void *);
1459 int (*fSample)(void *, unsigned int, long int *, unsigned int, int *);
1460 int (*fSampleAll)(void *, unsigned int, long int *);
1461 int (*fAmplitude)(void *, long long int, double *, double *);
1462 double (*fProbability)(void *, int *, int *, int);
1463 double (*fBasisStateProbability)(void *, long long int);
1464 int (*fAllProbabilities)(void *, double *);
1465 double (*fExpectationValue)(void *, const char *, int);
1466
1467 int (*fApplyX)(void *, int);
1468 int (*fApplyY)(void *, int);
1469 int (*fApplyZ)(void *, int);
1470 int (*fApplyH)(void *, int);
1471 int (*fApplyS)(void *, int);
1472 int (*fApplySDG)(void *, int);
1473 int (*fApplyT)(void *, int);
1474 int (*fApplyTDG)(void *, int);
1475 int (*fApplySX)(void *, int);
1476 int (*fApplySXDG)(void *, int);
1477 int (*fApplyK)(void *, int);
1478 int (*fApplyP)(void *, int, double);
1479 int (*fApplyRx)(void *, int, double);
1480 int (*fApplyRy)(void *, int, double);
1481 int (*fApplyRz)(void *, int, double);
1482 int (*fApplyU)(void *, int, double, double, double, double);
1483 int (*fApplyCX)(void *, int, int);
1484 int (*fApplyCY)(void *, int, int);
1485 int (*fApplyCZ)(void *, int, int);
1486 int (*fApplyCH)(void *, int, int);
1487 int (*fApplyCSX)(void *, int, int);
1488 int (*fApplyCSXDG)(void *, int, int);
1489 int (*fApplyCP)(void *, int, int, double);
1490 int (*fApplyCRx)(void *, int, int, double);
1491 int (*fApplyCRy)(void *, int, int, double);
1492 int (*fApplyCRz)(void *, int, int, double);
1493 int (*fApplyCCX)(void *, int, int, int);
1494 int (*fApplySwap)(void *, int, int);
1495 int (*fApplyCSwap)(void *, int, int, int);
1496 int (*fApplyCU)(void *, int, int, double, double, double, double);
1497
1498 // mps functions
1499 void *(*fCreateMPS)(void *);
1500 void (*fDestroyMPS)(void *);
1501
1502 int (*fMPSCreate)(void *, unsigned int);
1503 int (*fMPSReset)(void *);
1504
1505 int (*fMPSIsValid)(void *);
1506 int (*fMPSIsCreated)(void *);
1507
1508 int (*fMPSSetDataType)(void *, int);
1509 int (*fMPSIsDoublePrecision)(void *);
1510 int (*fMPSSetCutoff)(void *, double);
1511 double (*fMPSGetCutoff)(void *);
1512 int (*fMPSSetGesvdJ)(void *, int);
1513 int (*fMPSGetGesvdJ)(void *);
1514 int (*fMPSSetMaxExtent)(void *, long int);
1515 long int (*fMPSGetMaxExtent)(void *);
1516 int (*fMPSGetNrQubits)(void *);
1517 int (*fMPSAmplitude)(void *, long int, long int *, double *, double *);
1518 double (*fMPSProbability0)(void *, unsigned int);
1519 int (*fMPSMeasure)(void *, unsigned int);
1520 int (*fMPSMeasureQubits)(void *, long int, unsigned int *, int *);
1521
1522 void *(*fMPSGetMapForSample)();
1523 int (*fMPSFreeMapForSample)(void *);
1524 int (*fMPSSample)(void *, long int, long int, unsigned int *, void *);
1525
1526 int (*fMPSSaveState)(void *);
1527 int (*fMPSRestoreState)(void *);
1528 int (*fMPSCleanSavedState)(void *);
1529 void *(*fMPSClone)(void *);
1530
1531 double (*fMPSExpectationValue)(void *, const char *, int);
1532
1533 int (*fMPSApplyX)(void *, unsigned int);
1534 int (*fMPSApplyY)(void *, unsigned int);
1535 int (*fMPSApplyZ)(void *, unsigned int);
1536 int (*fMPSApplyH)(void *, unsigned int);
1537 int (*fMPSApplyS)(void *, unsigned int);
1538 int (*fMPSApplySDG)(void *, unsigned int);
1539 int (*fMPSApplyT)(void *, unsigned int);
1540 int (*fMPSApplyTDG)(void *, unsigned int);
1541 int (*fMPSApplySX)(void *, unsigned int);
1542 int (*fMPSApplySXDG)(void *, unsigned int);
1543 int (*fMPSApplyK)(void *, unsigned int);
1544 int (*fMPSApplyP)(void *, unsigned int, double);
1545 int (*fMPSApplyRx)(void *, unsigned int, double);
1546 int (*fMPSApplyRy)(void *, unsigned int, double);
1547 int (*fMPSApplyRz)(void *, unsigned int, double);
1548 int (*fMPSApplyU)(void *, unsigned int, double, double, double, double);
1549 int (*fMPSApplySwap)(void *, unsigned int, unsigned int);
1550 int (*fMPSApplyCX)(void *, unsigned int, unsigned int);
1551 int (*fMPSApplyCY)(void *, unsigned int, unsigned int);
1552 int (*fMPSApplyCZ)(void *, unsigned int, unsigned int);
1553 int (*fMPSApplyCH)(void *, unsigned int, unsigned int);
1554 int (*fMPSApplyCSX)(void *, unsigned int, unsigned int);
1555 int (*fMPSApplyCSXDG)(void *, unsigned int, unsigned int);
1556 int (*fMPSApplyCP)(void *, unsigned int, unsigned int, double);
1557 int (*fMPSApplyCRx)(void *, unsigned int, unsigned int, double);
1558 int (*fMPSApplyCRy)(void *, unsigned int, unsigned int, double);
1559 int (*fMPSApplyCRz)(void *, unsigned int, unsigned int, double);
1560 int (*fMPSApplyCU)(void *, unsigned int, unsigned int, double, double, double,
1561 double);
1562};
1563} // namespace Simulators
1564
1565#endif
1566#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)
virtual bool Init(const char *libName) noexcept
Definition Library.h:49
bool IsMuted() const noexcept
Definition Library.h:85