Maestro 0.2.5
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
MaestroLib.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "../Utils/Library.h"
4
6 public:
7 MaestroLibrary(const MaestroLibrary&) = delete;
9
12
13 MaestroLibrary() noexcept {}
14
15 virtual ~MaestroLibrary() {}
16
17 bool Init(const char* libName) noexcept override {
18 if (Utils::Library::Init(libName)) {
19 fGetMaestroObject = (void* (*)())GetFunction("GetMaestroObjectWithMute");
20 CheckFunction((void*)fGetMaestroObject, __LINE__);
21 if (fGetMaestroObject) {
22 maestro = fGetMaestroObject();
23 if (maestro) {
24 fCreateSimpleSimulator =
25 (unsigned long int (*)(int))GetFunction("CreateSimpleSimulator");
26 CheckFunction((void*)fCreateSimpleSimulator, __LINE__);
27 fDestroySimpleSimulator = (void (*)(unsigned long int))GetFunction(
28 "DestroySimpleSimulator");
29 CheckFunction((void*)fDestroySimpleSimulator, __LINE__);
30
31 fRemoveAllOptimizationSimulatorsAndAdd = (int (*)(
32 unsigned long int simHandle, int simType, int simExecType))
33 GetFunction("RemoveAllOptimizationSimulatorsAndAdd");
34 CheckFunction((void*)fRemoveAllOptimizationSimulatorsAndAdd,
35 __LINE__);
36 fAddOptimizationSimulator =
37 (int (*)(unsigned long int simHandle, int simType,
38 int simExecType))GetFunction("AddOptimizationSimulator");
39 CheckFunction((void*)fAddOptimizationSimulator, __LINE__);
40
41 fSimpleExecute = (char* (*)(unsigned long int, const char*,
42 const char*))GetFunction("SimpleExecute");
43 CheckFunction((void*)fSimpleExecute, __LINE__);
44
45 fSimpleEstimate =
46 (char* (*)(unsigned long int, const char*, const char*,
47 const char*))GetFunction("SimpleEstimate");
48 CheckFunction((void*)fSimpleEstimate, __LINE__);
49
50 fFreeResult = (void (*)(char*))GetFunction("FreeResult");
51 CheckFunction((void*)fFreeResult, __LINE__);
52
53 fCreateSimulator =
54 (unsigned long int (*)(int, int))GetFunction("CreateSimulator");
55 CheckFunction((void*)fCreateSimulator, __LINE__);
56 fGetSimulator =
57 (void* (*)(unsigned long int))GetFunction("GetSimulator");
58 CheckFunction((void*)fGetSimulator, __LINE__);
59 fDestroySimulator =
60 (void (*)(unsigned long int))GetFunction("DestroySimulator");
61 CheckFunction((void*)fDestroySimulator, __LINE__);
62
63 fInitializeSimulator =
64 (int (*)(void*))GetFunction("InitializeSimulator");
65 CheckFunction((void*)fInitializeSimulator, __LINE__);
66 fResetSimulator = (int (*)(void*))GetFunction("ResetSimulator");
67 CheckFunction((void*)fResetSimulator, __LINE__);
68 fConfigureSimulator =
69 (int (*)(void*, const char*, const char*))GetFunction(
70 "ConfigureSimulator");
71 CheckFunction((void*)fConfigureSimulator, __LINE__);
72 fGetConfiguration =
73 (char* (*)(void*, const char*))GetFunction("GetConfiguration");
74 CheckFunction((void*)fGetConfiguration, __LINE__);
75 fAllocateQubits = (unsigned long int (*)(
76 void*, unsigned long int))GetFunction("AllocateQubits");
77 CheckFunction((void*)fAllocateQubits, __LINE__);
78 fGetNumberOfQubits =
79 (unsigned long int (*)(void*))GetFunction("GetNumberOfQubits");
80 CheckFunction((void*)fGetNumberOfQubits, __LINE__);
81 fClearSimulator = (int (*)(void*))GetFunction("ClearSimulator");
82 CheckFunction((void*)fClearSimulator, __LINE__);
83 fMeasure = (unsigned long long int (*)(
84 void*, const unsigned long int*,
85 unsigned long int))GetFunction("Measure");
86 CheckFunction((void*)fMeasure, __LINE__);
87 fApplyReset = (int (*)(void*, const unsigned long int*,
88 unsigned long int))GetFunction("ApplyReset");
89 CheckFunction((void*)fApplyReset, __LINE__);
90 fProbability = (double (*)(void*, unsigned long long int))GetFunction(
91 "Probability");
92 CheckFunction((void*)fProbability, __LINE__);
93 fFreeDoubleVector =
94 (void (*)(double*))GetFunction("FreeDoubleVector");
95 CheckFunction((void*)fFreeDoubleVector, __LINE__);
96 fFreeULLIVector =
97 (void (*)(unsigned long long int*))GetFunction("FreeULLIVector");
98 CheckFunction((void*)fFreeULLIVector, __LINE__);
99 fAmplitude = (double* (*)(void*, unsigned long long int))GetFunction(
100 "Amplitude");
101 CheckFunction((void*)fAmplitude, __LINE__);
102 fAllProbabilities =
103 (double* (*)(void*))GetFunction("AllProbabilities");
104 CheckFunction((void*)fAllProbabilities, __LINE__);
105 fProbabilities =
106 (double* (*)(void*, const unsigned long long int*,
107 unsigned long int))GetFunction("Probabilities");
108 CheckFunction((void*)fProbabilities, __LINE__);
109 fSampleCounts =
110 (unsigned long long int* (*)(void*, const unsigned long long int*,
111 unsigned long int,
112 unsigned long int))
113 GetFunction("SampleCounts");
114 CheckFunction((void*)fSampleCounts, __LINE__);
115 fGetSimulatorType = (int (*)(void*))GetFunction("GetSimulatorType");
116 CheckFunction((void*)fGetSimulatorType, __LINE__);
117 fGetSimulationType = (int (*)(void*))GetFunction("GetSimulationType");
118 CheckFunction((void*)fGetSimulationType, __LINE__);
119 fFlushSimulator = (int (*)(void*))GetFunction("FlushSimulator");
120 CheckFunction((void*)fFlushSimulator, __LINE__);
121 fSaveStateToInternalDestructive =
122 (int (*)(void*))GetFunction("SaveStateToInternalDestructive");
123 CheckFunction((void*)fSaveStateToInternalDestructive, __LINE__);
124 fRestoreInternalDestructiveSavedState = (int (*)(void*))GetFunction(
125 "RestoreInternalDestructiveSavedState");
126 CheckFunction((void*)fRestoreInternalDestructiveSavedState, __LINE__);
127 fSaveState = (int (*)(void*))GetFunction("SaveState");
128 CheckFunction((void*)fSaveState, __LINE__);
129 fRestoreState = (int (*)(void*))GetFunction("RestoreState");
130 CheckFunction((void*)fRestoreState, __LINE__);
131 fSetMultithreading =
132 (int (*)(void*, int))GetFunction("SetMultithreading");
133 CheckFunction((void*)fSetMultithreading, __LINE__);
134 fGetMultithreading = (int (*)(void*))GetFunction("GetMultithreading");
135 CheckFunction((void*)fGetMultithreading, __LINE__);
136 fIsQcsim = (int (*)(void*))GetFunction("IsQcsim");
137 CheckFunction((void*)fIsQcsim, __LINE__);
138 fMeasureNoCollapse = (unsigned long long int (*)(void*))GetFunction(
139 "MeasureNoCollapse");
140 CheckFunction((void*)fMeasureNoCollapse, __LINE__);
141
142 fApplyX = (int (*)(void*, int))GetFunction("ApplyX");
143 CheckFunction((void*)fApplyX, __LINE__);
144 fApplyY = (int (*)(void*, int))GetFunction("ApplyY");
145 CheckFunction((void*)fApplyY, __LINE__);
146 fApplyZ = (int (*)(void*, int))GetFunction("ApplyZ");
147 CheckFunction((void*)fApplyZ, __LINE__);
148 fApplyH = (int (*)(void*, int))GetFunction("ApplyH");
149 CheckFunction((void*)fApplyH, __LINE__);
150 fApplyS = (int (*)(void*, int))GetFunction("ApplyS");
151 CheckFunction((void*)fApplyS, __LINE__);
152 fApplySDG = (int (*)(void*, int))GetFunction("ApplySDG");
153 CheckFunction((void*)fApplySDG, __LINE__);
154 fApplyT = (int (*)(void*, int))GetFunction("ApplyT");
155 CheckFunction((void*)fApplyT, __LINE__);
156 fApplyTDG = (int (*)(void*, int))GetFunction("ApplyTDG");
157 CheckFunction((void*)fApplyTDG, __LINE__);
158 fApplySX = (int (*)(void*, int))GetFunction("ApplySX");
159 CheckFunction((void*)fApplySX, __LINE__);
160 fApplySXDG = (int (*)(void*, int))GetFunction("ApplySXDG");
161 CheckFunction((void*)fApplySXDG, __LINE__);
162 fApplyK = (int (*)(void*, int))GetFunction("ApplyK");
163 CheckFunction((void*)fApplyK, __LINE__);
164 fApplyP = (int (*)(void*, int, double))GetFunction("ApplyP");
165 CheckFunction((void*)fApplyP, __LINE__);
166 fApplyRx = (int (*)(void*, int, double))GetFunction("ApplyRx");
167 CheckFunction((void*)fApplyRx, __LINE__);
168 fApplyRy = (int (*)(void*, int, double))GetFunction("ApplyRy");
169 CheckFunction((void*)fApplyRy, __LINE__);
170 fApplyRz = (int (*)(void*, int, double))GetFunction("ApplyRz");
171 CheckFunction((void*)fApplyRz, __LINE__);
172 fApplyU = (int (*)(void*, int, double, double, double,
173 double))GetFunction("ApplyU");
174 CheckFunction((void*)fApplyU, __LINE__);
175 fApplyCX = (int (*)(void*, int, int))GetFunction("ApplyCX");
176 CheckFunction((void*)fApplyCX, __LINE__);
177 fApplyCY = (int (*)(void*, int, int))GetFunction("ApplyCY");
178 CheckFunction((void*)fApplyCY, __LINE__);
179 fApplyCZ = (int (*)(void*, int, int))GetFunction("ApplyCZ");
180 CheckFunction((void*)fApplyCZ, __LINE__);
181 fApplyCH = (int (*)(void*, int, int))GetFunction("ApplyCH");
182 CheckFunction((void*)fApplyCH, __LINE__);
183 fApplyCSX = (int (*)(void*, int, int))GetFunction("ApplyCSX");
184 CheckFunction((void*)fApplyCSX, __LINE__);
185 fApplyCSXDG = (int (*)(void*, int, int))GetFunction("ApplyCSXDG");
186 CheckFunction((void*)fApplyCSXDG, __LINE__);
187 fApplyCP = (int (*)(void*, int, int, double))GetFunction("ApplyCP");
188 CheckFunction((void*)fApplyCP, __LINE__);
189 fApplyCRx = (int (*)(void*, int, int, double))GetFunction("ApplyCRx");
190 CheckFunction((void*)fApplyCRx, __LINE__);
191 fApplyCRy = (int (*)(void*, int, int, double))GetFunction("ApplyCRy");
192 CheckFunction((void*)fApplyCRy, __LINE__);
193 fApplyCRz = (int (*)(void*, int, int, double))GetFunction("ApplyCRz");
194 CheckFunction((void*)fApplyCRz, __LINE__);
195 fApplyCCX = (int (*)(void*, int, int, int))GetFunction("ApplyCCX");
196 CheckFunction((void*)fApplyCCX, __LINE__);
197 fApplySwap = (int (*)(void*, int, int))GetFunction("ApplySwap");
198 CheckFunction((void*)fApplySwap, __LINE__);
199 fApplyCSwap =
200 (int (*)(void*, int, int, int))GetFunction("ApplyCSwap");
201 CheckFunction((void*)fApplyCSwap, __LINE__);
202 fApplyCU = (int (*)(void*, int, int, double, double, double,
203 double))GetFunction("ApplyCU");
204 CheckFunction((void*)fApplyCU, __LINE__);
205
206 return true;
207 }
208 } else
209 std::cerr << "MaestroLibrary: Unable to get initialization function "
210 "for library"
211 << std::endl;
212 } else
213 std::cerr << "MaestroLibrary: Unable to load the library" << std::endl;
214
215 return false;
216 }
217
218 static void CheckFunction(void* func, int line) noexcept {
219 if (!func) {
220 std::cerr << "MaestroLibrary: Unable to load function, line #: " << line;
221
222#ifdef __linux__
223 const char* dlsym_error = dlerror();
224 if (dlsym_error) std::cerr << ", error: " << dlsym_error;
225#elif defined(_WIN32)
226 const DWORD error = GetLastError();
227 std::cerr << ", error code: " << error;
228#endif
229
230 std::cerr << std::endl;
231 }
232 }
233
234 bool IsValid() const { return maestro != nullptr; }
235
236 virtual unsigned long int CreateSimpleSimulator(int nrQubits) {
237 if (maestro && fCreateSimpleSimulator)
238 return fCreateSimpleSimulator(nrQubits);
239 else
240 throw std::runtime_error(
241 "MaestroLibrary: Unable to create the simple simulator.");
242
243 return 0;
244 }
245
246 void DestroySimpleSimulator(unsigned long int simHandle) {
247 if (maestro && fDestroySimpleSimulator)
248 fDestroySimpleSimulator(simHandle);
249 else
250 throw std::runtime_error(
251 "MaestroLibrary: Unable to destroy the simple simulator.");
252 }
253
254 int RemoveAllOptimizationSimulatorsAndAdd(unsigned long int simHandle,
255 int simType, int simExecType) {
256 if (maestro && fRemoveAllOptimizationSimulatorsAndAdd)
257 return fRemoveAllOptimizationSimulatorsAndAdd(simHandle, simType,
258 simExecType);
259 else
260 throw std::runtime_error(
261 "MaestroLibrary: Unable to remove all optimization simulators and "
262 "add a new one.");
263
264 return 0;
265 }
266
267 int AddOptimizationSimulator(unsigned long int simHandle, int simType,
268 int simExecType) {
269 if (maestro && fAddOptimizationSimulator)
270 return fAddOptimizationSimulator(simHandle, simType, simExecType);
271 else
272 throw std::runtime_error(
273 "MaestroLibrary: Unable to add an optimization simulator.");
274
275 return 0;
276 }
277
278 char* SimpleExecute(unsigned long int simpleSim, const char* jsonCircuit,
279 const char* jsonConfig) {
280 if (maestro && fSimpleExecute)
281 return fSimpleExecute(simpleSim, jsonCircuit, jsonConfig);
282 else
283 throw std::runtime_error(
284 "MaestroLibrary: Unable to execute the simple simulator.");
285
286 return nullptr;
287 }
288
289 char* SimpleEstimate(unsigned long int simpleSim, const char* jsonCircuit,
290 const char* observableStr, const char* jsonConfig) {
291 if (maestro && fSimpleEstimate)
292 return fSimpleEstimate(simpleSim, jsonCircuit, observableStr, jsonConfig);
293 else
294 throw std::runtime_error(
295 "MaestroLibrary: Unable to execute the simple simulator for "
296 "expectation values.");
297
298 return nullptr;
299 }
300
301 virtual void FreeResult(char* result) {
302 if (maestro && fFreeResult)
303 fFreeResult(result);
304 else
305 throw std::runtime_error("MaestroLibrary: Unable to free the result.");
306 }
307
308 virtual unsigned long int CreateSimulator(int simType, int simExecType) {
309 if (maestro && fCreateSimulator)
310 return fCreateSimulator(simType, simExecType);
311 else
312 throw std::runtime_error(
313 "MaestroLibrary: Unable to create the simulator.");
314
315 return 0;
316 }
317
318 void* GetSimulator(unsigned long int simHandle) {
319 if (maestro && fGetSimulator)
320 return fGetSimulator(simHandle);
321 else
322 throw std::runtime_error("MaestroLibrary: Unable to get the simulator.");
323 }
324
325 void DestroySimulator(unsigned long int simHandle) {
326 if (maestro && fDestroySimulator)
327 fDestroySimulator(simHandle);
328 else
329 throw std::runtime_error(
330 "MaestroLibrary: Unable to destroy the simulator.");
331 }
332
333 int InitializeSimulator(void* sim) {
334 if (maestro && sim && fInitializeSimulator)
335 return fInitializeSimulator(sim);
336 else
337 throw std::runtime_error(
338 "MaestroLibrary: Unable to initialize the simulator.");
339
340 return 0;
341 }
342
343 int ResetSimulator(void* sim) {
344 if (maestro && sim && fResetSimulator)
345 return fResetSimulator(sim);
346 else
347 throw std::runtime_error(
348 "MaestroLibrary: Unable to reset the simulator.");
349
350 return 0;
351 }
352
353 int ConfigureSimulator(void* sim, const char* key, const char* value) {
354 if (maestro && sim && fConfigureSimulator)
355 return fConfigureSimulator(sim, key, value);
356 else
357 throw std::runtime_error(
358 "MaestroLibrary: Unable to configure the simulator.");
359
360 return 0;
361 }
362
363 char* GetConfiguration(void* sim, const char* key) {
364 if (maestro && sim && fGetConfiguration)
365 return fGetConfiguration(sim, key);
366 else
367 throw std::runtime_error(
368 "MaestroLibrary: Unable to get the configuration of the simulator.");
369 return nullptr;
370 }
371
372 unsigned long int AllocateQubits(void* sim, unsigned long int nrQubits) {
373 if (maestro && sim && fAllocateQubits)
374 return fAllocateQubits(sim, nrQubits);
375 else
376 throw std::runtime_error(
377 "MaestroLibrary: Unable to allocate qubits in the simulator.");
378 return 0;
379 }
380
381 unsigned long int GetNumberOfQubits(void* sim) {
382 if (maestro && sim && fGetNumberOfQubits)
383 return fGetNumberOfQubits(sim);
384 else
385 throw std::runtime_error(
386 "MaestroLibrary: Unable to get the number of qubits in the "
387 "simulator.");
388 return 0;
389 }
390
391 int ClearSimulator(void* sim) {
392 if (maestro && sim && fClearSimulator)
393 return fClearSimulator(sim);
394 else
395 throw std::runtime_error(
396 "MaestroLibrary: Unable to clear the simulator.");
397 return 0;
398 }
399
400 unsigned long long int Measure(void* sim, const unsigned long int* qubits,
401 unsigned long int nrQubits) {
402 if (maestro && sim && fMeasure)
403 return fMeasure(sim, qubits, nrQubits);
404 else
405 throw std::runtime_error(
406 "MaestroLibrary: Unable to measure the simulator.");
407 return 0;
408 }
409
410 int ApplyReset(void* sim, const unsigned long int* qubits,
411 unsigned long int nrQubits) {
412 if (maestro && sim && fApplyReset)
413 return fApplyReset(sim, qubits, nrQubits);
414 else
415 throw std::runtime_error(
416 "MaestroLibrary: Unable to apply reset to the simulator.");
417 return 0;
418 }
419
420 double Probability(void* sim, unsigned long long int outcome) {
421 if (maestro && sim && fProbability)
422 return fProbability(sim, outcome);
423 else
424 throw std::runtime_error(
425 "MaestroLibrary: Unable to get the probability of an outcome.");
426 return 0.0;
427 }
428
429 virtual void FreeDoubleVector(double* vec) {
430 if (maestro && fFreeDoubleVector)
431 fFreeDoubleVector(vec);
432 else
433 throw std::runtime_error(
434 "MaestroLibrary: Unable to free the double vector.");
435 }
436
437 virtual void FreeULLIVector(unsigned long long int* vec) {
438 if (maestro && fFreeULLIVector)
439 fFreeULLIVector(vec);
440 else
441 throw std::runtime_error(
442 "MaestroLibrary: Unable to free the unsigned long long int vector.");
443 }
444
445 double* Amplitude(void* sim, unsigned long long int outcome) {
446 if (maestro && sim && fAmplitude)
447 return fAmplitude(sim, outcome);
448 else
449 throw std::runtime_error(
450 "MaestroLibrary: Unable to get the amplitude of an outcome.");
451 return nullptr;
452 }
453
454 double* AllProbabilities(void* sim) {
455 if (maestro && sim && fAllProbabilities)
456 return fAllProbabilities(sim);
457 else
458 throw std::runtime_error(
459 "MaestroLibrary: Unable to get all probabilities.");
460 return nullptr;
461 }
462
463 double* Probabilities(void* sim, const unsigned long long int* qubits,
464 unsigned long int nrQubits) {
465 if (maestro && sim && fProbabilities)
466 return fProbabilities(sim, qubits, nrQubits);
467 else
468 throw std::runtime_error(
469 "MaestroLibrary: Unable to get probabilities for specified qubits.");
470 return nullptr;
471 }
472
473 unsigned long long int* SampleCounts(void* sim,
474 const unsigned long long int* qubits,
475 unsigned long int nrQubits,
476 unsigned long int shots) {
477 if (maestro && sim && fSampleCounts)
478 return fSampleCounts(sim, qubits, nrQubits, shots);
479 else
480 throw std::runtime_error(
481 "MaestroLibrary: Unable to get sample counts for specified qubits.");
482 return nullptr;
483 }
484
485 int GetSimulatorType(void* sim) {
486 if (maestro && sim && fGetSimulatorType)
487 return fGetSimulatorType(sim);
488 else
489 throw std::runtime_error(
490 "MaestroLibrary: Unable to get the simulator type.");
491 return -1;
492 }
493
494 int GetSimulationType(void* sim) {
495 if (maestro && sim && fGetSimulationType)
496 return fGetSimulationType(sim);
497 else
498 throw std::runtime_error(
499 "MaestroLibrary: Unable to get the simulation type.");
500 return -1;
501 }
502
503 int FlushSimulator(void* sim) {
504 if (maestro && sim && fFlushSimulator)
505 return fFlushSimulator(sim);
506 else
507 throw std::runtime_error(
508 "MaestroLibrary: Unable to flush the simulator.");
509 return 0;
510 }
511
513 if (maestro && sim && fSaveStateToInternalDestructive)
514 return fSaveStateToInternalDestructive(sim);
515 else
516 throw std::runtime_error(
517 "MaestroLibrary: Unable to save the state to internal destructive "
518 "storage.");
519 return 0;
520 }
521
523 if (maestro && sim && fRestoreInternalDestructiveSavedState)
524 return fRestoreInternalDestructiveSavedState(sim);
525 else
526 throw std::runtime_error(
527 "MaestroLibrary: Unable to restore the state from internal "
528 "destructive storage.");
529 return 0;
530 }
531
532 int SaveState(void* sim) {
533 if (maestro && sim && fSaveState)
534 return fSaveState(sim);
535 else
536 throw std::runtime_error("MaestroLibrary: Unable to save the state.");
537 return 0;
538 }
539
540 int RestoreState(void* sim) {
541 if (maestro && sim && fRestoreState)
542 return fRestoreState(sim);
543 else
544 throw std::runtime_error("MaestroLibrary: Unable to restore the state.");
545 return 0;
546 }
547
548 int SetMultithreading(void* sim, int multithreading) {
549 if (maestro && sim && fSetMultithreading)
550 return fSetMultithreading(sim, multithreading);
551 else
552 throw std::runtime_error("MaestroLibrary: Unable to set multithreading.");
553 return 0;
554 }
555
556 int GetMultithreading(void* sim) {
557 if (maestro && sim && fGetMultithreading)
558 return fGetMultithreading(sim);
559 else
560 throw std::runtime_error(
561 "MaestroLibrary: Unable to get multithreading status.");
562 return 0;
563 }
564
565 int IsQcsim(void* sim) {
566 if (maestro && sim && fIsQcsim)
567 return fIsQcsim(sim);
568 else
569 throw std::runtime_error(
570 "MaestroLibrary: Unable to check if the simulator is a QCSIM.");
571 return 0;
572 }
573
574 unsigned long long int MeasureNoCollapse(void* sim) {
575 if (maestro && sim && fMeasureNoCollapse)
576 return fMeasureNoCollapse(sim);
577 else
578 throw std::runtime_error(
579 "MaestroLibrary: Unable to measure without collapse.");
580 return 0;
581 }
582
583 int ApplyX(void* sim, int qubit) {
584 if (maestro && sim && fApplyX)
585 return fApplyX(sim, qubit);
586 else
587 throw std::runtime_error("MaestroLibrary: Unable to apply X gate.");
588 return 0;
589 }
590
591 int ApplyY(void* sim, int qubit) {
592 if (maestro && sim && fApplyY)
593 return fApplyY(sim, qubit);
594 else
595 throw std::runtime_error("MaestroLibrary: Unable to apply Y gate.");
596 return 0;
597 }
598
599 int ApplyZ(void* sim, int qubit) {
600 if (maestro && sim && fApplyZ)
601 return fApplyZ(sim, qubit);
602 else
603 throw std::runtime_error("MaestroLibrary: Unable to apply Z gate.");
604 return 0;
605 }
606
607 int ApplyH(void* sim, int qubit) {
608 if (maestro && sim && fApplyH)
609 return fApplyH(sim, qubit);
610 else
611 throw std::runtime_error("MaestroLibrary: Unable to apply H gate.");
612 return 0;
613 }
614
615 int ApplyS(void* sim, int qubit) {
616 if (maestro && sim && fApplyS)
617 return fApplyS(sim, qubit);
618 else
619 throw std::runtime_error("MaestroLibrary: Unable to apply S gate.");
620 return 0;
621 }
622
623 int ApplySDG(void* sim, int qubit) {
624 if (maestro && sim && fApplySDG)
625 return fApplySDG(sim, qubit);
626 else
627 throw std::runtime_error("MaestroLibrary: Unable to apply SDG gate.");
628 return 0;
629 }
630
631 int ApplyT(void* sim, int qubit) {
632 if (maestro && sim && fApplyT)
633 return fApplyT(sim, qubit);
634 else
635 throw std::runtime_error("MaestroLibrary: Unable to apply T gate.");
636 return 0;
637 }
638
639 int ApplyTDG(void* sim, int qubit) {
640 if (maestro && sim && fApplyTDG)
641 return fApplyTDG(sim, qubit);
642 else
643 throw std::runtime_error("MaestroLibrary: Unable to apply TDG gate.");
644 return 0;
645 }
646
647 int ApplySX(void* sim, int qubit) {
648 if (maestro && sim && fApplySX)
649 return fApplySX(sim, qubit);
650 else
651 throw std::runtime_error("MaestroLibrary: Unable to apply SX gate.");
652 return 0;
653 }
654
655 int ApplySXDG(void* sim, int qubit) {
656 if (maestro && sim && fApplySXDG)
657 return fApplySXDG(sim, qubit);
658 else
659 throw std::runtime_error("MaestroLibrary: Unable to apply SXDG gate.");
660 return 0;
661 }
662
663 int ApplyK(void* sim, int qubit) {
664 if (maestro && sim && fApplyK)
665 return fApplyK(sim, qubit);
666 else
667 throw std::runtime_error("MaestroLibrary: Unable to apply K gate.");
668 return 0;
669 }
670
671 int ApplyP(void* sim, int qubit, double theta) {
672 if (maestro && sim && fApplyP)
673 return fApplyP(sim, qubit, theta);
674 else
675 throw std::runtime_error("MaestroLibrary: Unable to apply P gate.");
676 return 0;
677 }
678
679 int ApplyRx(void* sim, int qubit, double theta) {
680 if (maestro && sim && fApplyRx)
681 return fApplyRx(sim, qubit, theta);
682 else
683 throw std::runtime_error("MaestroLibrary: Unable to apply Rx gate.");
684 return 0;
685 }
686
687 int ApplyRy(void* sim, int qubit, double theta) {
688 if (maestro && sim && fApplyRy)
689 return fApplyRy(sim, qubit, theta);
690 else
691 throw std::runtime_error("MaestroLibrary: Unable to apply Ry gate.");
692 return 0;
693 }
694
695 int ApplyRz(void* sim, int qubit, double theta) {
696 if (maestro && sim && fApplyRz)
697 return fApplyRz(sim, qubit, theta);
698 else
699 throw std::runtime_error("MaestroLibrary: Unable to apply Rz gate.");
700 return 0;
701 }
702
703 int ApplyU(void* sim, int qubit, double theta, double phi, double lambda,
704 double gamma) {
705 if (maestro && sim && fApplyU)
706 return fApplyU(sim, qubit, theta, phi, lambda, gamma);
707 else
708 throw std::runtime_error("MaestroLibrary: Unable to apply U gate.");
709 return 0;
710 }
711
712 int ApplyCX(void* sim, int controlQubit, int targetQubit) {
713 if (maestro && sim && fApplyCX)
714 return fApplyCX(sim, controlQubit, targetQubit);
715 else
716 throw std::runtime_error("MaestroLibrary: Unable to apply CX gate.");
717 return 0;
718 }
719
720 int ApplyCY(void* sim, int controlQubit, int targetQubit) {
721 if (maestro && sim && fApplyCY)
722 return fApplyCY(sim, controlQubit, targetQubit);
723 else
724 throw std::runtime_error("MaestroLibrary: Unable to apply CY gate.");
725 return 0;
726 }
727
728 int ApplyCZ(void* sim, int controlQubit, int targetQubit) {
729 if (maestro && sim && fApplyCZ)
730 return fApplyCZ(sim, controlQubit, targetQubit);
731 else
732 throw std::runtime_error("MaestroLibrary: Unable to apply CZ gate.");
733 return 0;
734 }
735
736 int ApplyCH(void* sim, int controlQubit, int targetQubit) {
737 if (maestro && sim && fApplyCH)
738 return fApplyCH(sim, controlQubit, targetQubit);
739 else
740 throw std::runtime_error("MaestroLibrary: Unable to apply CH gate.");
741 return 0;
742 }
743
744 int ApplyCSX(void* sim, int controlQubit, int targetQubit) {
745 if (maestro && sim && fApplyCSX)
746 return fApplyCSX(sim, controlQubit, targetQubit);
747 else
748 throw std::runtime_error("MaestroLibrary: Unable to apply CSX gate.");
749 return 0;
750 }
751
752 int ApplyCSXDG(void* sim, int controlQubit, int targetQubit) {
753 if (maestro && sim && fApplyCSXDG)
754 return fApplyCSXDG(sim, controlQubit, targetQubit);
755 else
756 throw std::runtime_error("MaestroLibrary: Unable to apply CSXDG gate.");
757 return 0;
758 }
759
760 int ApplyCP(void* sim, int controlQubit, int targetQubit, double theta) {
761 if (maestro && sim && fApplyCP)
762 return fApplyCP(sim, controlQubit, targetQubit, theta);
763 else
764 throw std::runtime_error("MaestroLibrary: Unable to apply CP gate.");
765 return 0;
766 }
767
768 int ApplyCRx(void* sim, int controlQubit, int targetQubit, double theta) {
769 if (maestro && sim && fApplyCRx)
770 return fApplyCRx(sim, controlQubit, targetQubit, theta);
771 else
772 throw std::runtime_error("MaestroLibrary: Unable to apply CRx gate.");
773 return 0;
774 }
775
776 int ApplyCRy(void* sim, int controlQubit, int targetQubit, double theta) {
777 if (maestro && sim && fApplyCRy)
778 return fApplyCRy(sim, controlQubit, targetQubit, theta);
779 else
780 throw std::runtime_error("MaestroLibrary: Unable to apply CRy gate.");
781 return 0;
782 }
783
784 int ApplyCRz(void* sim, int controlQubit, int targetQubit, double theta) {
785 if (maestro && sim && fApplyCRz)
786 return fApplyCRz(sim, controlQubit, targetQubit, theta);
787 else
788 throw std::runtime_error("MaestroLibrary: Unable to apply CRz gate.");
789 return 0;
790 }
791
792 int ApplyCCX(void* sim, int controlQubit1, int controlQubit2,
793 int targetQubit) {
794 if (maestro && sim && fApplyCCX)
795 return fApplyCCX(sim, controlQubit1, controlQubit2, targetQubit);
796 else
797 throw std::runtime_error("MaestroLibrary: Unable to apply CCX gate.");
798 return 0;
799 }
800
801 int ApplySwap(void* sim, int qubit1, int qubit2) {
802 if (maestro && sim && fApplySwap)
803 return fApplySwap(sim, qubit1, qubit2);
804 else
805 throw std::runtime_error("MaestroLibrary: Unable to apply Swap gate.");
806 return 0;
807 }
808
809 int ApplyCSwap(void* sim, int controlQubit, int qubit1, int qubit2) {
810 if (maestro && sim && fApplyCSwap)
811 return fApplyCSwap(sim, controlQubit, qubit1, qubit2);
812 else
813 throw std::runtime_error("MaestroLibrary: Unable to apply CSwap gate.");
814 return 0;
815 }
816
817 int ApplyCU(void* sim, int controlQubit, int targetQubit, double theta,
818 double phi, double lambda, double gamma) {
819 if (maestro && sim && fApplyCU)
820 return fApplyCU(sim, controlQubit, targetQubit, theta, phi, lambda,
821 gamma);
822 else
823 throw std::runtime_error("MaestroLibrary: Unable to apply CU gate.");
824 return 0;
825 }
826
827 private:
828 void* maestro = nullptr;
829
830 void* (*fGetMaestroObject)();
831
832 unsigned long int (*fCreateSimpleSimulator)(int);
833 void (*fDestroySimpleSimulator)(unsigned long int);
834
835 int (*fRemoveAllOptimizationSimulatorsAndAdd)(unsigned long int, int, int);
836 int (*fAddOptimizationSimulator)(unsigned long int, int, int);
837
838 char* (*fSimpleExecute)(unsigned long int, const char*, const char*);
839 char* (*fSimpleEstimate)(unsigned long int, const char*, const char*,
840 const char*);
841 void (*fFreeResult)(char*);
842
843 unsigned long int (*fCreateSimulator)(int, int);
844 void* (*fGetSimulator)(unsigned long int);
845 void (*fDestroySimulator)(unsigned long int);
846
847 int (*fInitializeSimulator)(void*);
848 int (*fResetSimulator)(void*);
849 int (*fConfigureSimulator)(void*, const char*, const char*);
850 char* (*fGetConfiguration)(void*, const char*);
851 unsigned long int (*fAllocateQubits)(void*, unsigned long int);
852 unsigned long int (*fGetNumberOfQubits)(void*);
853 int (*fClearSimulator)(void*);
854 unsigned long long int (*fMeasure)(void*, const unsigned long int*,
855 unsigned long int);
856 int (*fApplyReset)(void*, const unsigned long int*, unsigned long int);
857 double (*fProbability)(void*, unsigned long long int);
858 void (*fFreeDoubleVector)(double*);
859 void (*fFreeULLIVector)(unsigned long long int*);
860 double* (*fAmplitude)(void*, unsigned long long int);
861 double* (*fAllProbabilities)(void*);
862 double* (*fProbabilities)(void*, const unsigned long long int*,
863 unsigned long int);
864 unsigned long long int* (*fSampleCounts)(void*, const unsigned long long int*,
865 unsigned long int,
866 unsigned long int);
867 int (*fGetSimulatorType)(void*);
868 int (*fGetSimulationType)(void*);
869 int (*fFlushSimulator)(void*);
870 int (*fSaveStateToInternalDestructive)(void*);
871 int (*fRestoreInternalDestructiveSavedState)(void*);
872 int (*fSaveState)(void*);
873 int (*fRestoreState)(void*);
874 int (*fSetMultithreading)(void*, int);
875 int (*fGetMultithreading)(void*);
876 int (*fIsQcsim)(void*);
877 unsigned long long int (*fMeasureNoCollapse)(void*);
878
879 int (*fApplyX)(void*, int);
880 int (*fApplyY)(void*, int);
881 int (*fApplyZ)(void*, int);
882 int (*fApplyH)(void*, int);
883 int (*fApplyS)(void*, int);
884 int (*fApplySDG)(void*, int);
885 int (*fApplyT)(void*, int);
886 int (*fApplyTDG)(void*, int);
887 int (*fApplySX)(void*, int);
888 int (*fApplySXDG)(void*, int);
889 int (*fApplyK)(void*, int);
890 int (*fApplyP)(void*, int, double);
891 int (*fApplyRx)(void*, int, double);
892 int (*fApplyRy)(void*, int, double);
893 int (*fApplyRz)(void*, int, double);
894 int (*fApplyU)(void*, int, double, double, double, double);
895 int (*fApplyCX)(void*, int, int);
896 int (*fApplyCY)(void*, int, int);
897 int (*fApplyCZ)(void*, int, int);
898 int (*fApplyCH)(void*, int, int);
899 int (*fApplyCSX)(void*, int, int);
900 int (*fApplyCSXDG)(void*, int, int);
901 int (*fApplyCP)(void*, int, int, double);
902 int (*fApplyCRx)(void*, int, int, double);
903 int (*fApplyCRy)(void*, int, int, double);
904 int (*fApplyCRz)(void*, int, int, double);
905 int (*fApplyCCX)(void*, int, int, int);
906 int (*fApplySwap)(void*, int, int);
907 int (*fApplyCSwap)(void*, int, int, int);
908 int (*fApplyCU)(void*, int, int, double, double, double, double);
909};
int ApplyCCX(void *sim, int controlQubit1, int controlQubit2, int targetQubit)
int ResetSimulator(void *sim)
void * GetSimulator(unsigned long int simHandle)
int ApplyCZ(void *sim, int controlQubit, int targetQubit)
MaestroLibrary() noexcept
unsigned long int AllocateQubits(void *sim, unsigned long int nrQubits)
int ConfigureSimulator(void *sim, const char *key, const char *value)
MaestroLibrary & operator=(const MaestroLibrary &)=delete
unsigned long long int * SampleCounts(void *sim, const unsigned long long int *qubits, unsigned long int nrQubits, unsigned long int shots)
int ApplySXDG(void *sim, int qubit)
int ApplyS(void *sim, int qubit)
int GetSimulationType(void *sim)
int GetSimulatorType(void *sim)
int ApplyH(void *sim, int qubit)
double * AllProbabilities(void *sim)
unsigned long long int Measure(void *sim, const unsigned long int *qubits, unsigned long int nrQubits)
int ApplySDG(void *sim, int qubit)
int RemoveAllOptimizationSimulatorsAndAdd(unsigned long int simHandle, int simType, int simExecType)
int ApplyT(void *sim, int qubit)
int ApplyY(void *sim, int qubit)
virtual ~MaestroLibrary()
static void CheckFunction(void *func, int line) noexcept
int ApplySX(void *sim, int qubit)
int SaveState(void *sim)
int ApplyX(void *sim, int qubit)
int ApplyCY(void *sim, int controlQubit, int targetQubit)
int ApplyCX(void *sim, int controlQubit, int targetQubit)
void DestroySimpleSimulator(unsigned long int simHandle)
int ApplyCRx(void *sim, int controlQubit, int targetQubit, double theta)
int ClearSimulator(void *sim)
int ApplyCSX(void *sim, int controlQubit, int targetQubit)
bool IsValid() const
int ApplyK(void *sim, int qubit)
char * SimpleExecute(unsigned long int simpleSim, const char *jsonCircuit, const char *jsonConfig)
int RestoreState(void *sim)
int ApplyCRz(void *sim, int controlQubit, int targetQubit, double theta)
double * Amplitude(void *sim, unsigned long long int outcome)
int AddOptimizationSimulator(unsigned long int simHandle, int simType, int simExecType)
virtual unsigned long int CreateSimpleSimulator(int nrQubits)
char * SimpleEstimate(unsigned long int simpleSim, const char *jsonCircuit, const char *observableStr, const char *jsonConfig)
double Probability(void *sim, unsigned long long int outcome)
int ApplyCH(void *sim, int controlQubit, int targetQubit)
int ApplyCSwap(void *sim, int controlQubit, int qubit1, int qubit2)
virtual void FreeULLIVector(unsigned long long int *vec)
int IsQcsim(void *sim)
virtual unsigned long int CreateSimulator(int simType, int simExecType)
int ApplyCSXDG(void *sim, int controlQubit, int targetQubit)
int SaveStateToInternalDestructive(void *sim)
virtual void FreeDoubleVector(double *vec)
MaestroLibrary & operator=(MaestroLibrary &&)=default
int ApplyCRy(void *sim, int controlQubit, int targetQubit, double theta)
double * Probabilities(void *sim, const unsigned long long int *qubits, unsigned long int nrQubits)
int ApplyRz(void *sim, int qubit, double theta)
MaestroLibrary(const MaestroLibrary &)=delete
int ApplySwap(void *sim, int qubit1, int qubit2)
void DestroySimulator(unsigned long int simHandle)
int ApplyU(void *sim, int qubit, double theta, double phi, double lambda, double gamma)
int ApplyZ(void *sim, int qubit)
int RestoreInternalDestructiveSavedState(void *sim)
int ApplyCU(void *sim, int controlQubit, int targetQubit, double theta, double phi, double lambda, double gamma)
int ApplyReset(void *sim, const unsigned long int *qubits, unsigned long int nrQubits)
int InitializeSimulator(void *sim)
int ApplyP(void *sim, int qubit, double theta)
int ApplyRy(void *sim, int qubit, double theta)
bool Init(const char *libName) noexcept override
int ApplyCP(void *sim, int controlQubit, int targetQubit, double theta)
int FlushSimulator(void *sim)
MaestroLibrary(MaestroLibrary &&)=default
int GetMultithreading(void *sim)
virtual void FreeResult(char *result)
int ApplyRx(void *sim, int qubit, double theta)
unsigned long int GetNumberOfQubits(void *sim)
char * GetConfiguration(void *sim, const char *key)
int ApplyTDG(void *sim, int qubit)
unsigned long long int MeasureNoCollapse(void *sim)
int SetMultithreading(void *sim, int multithreading)
void * GetFunction(const char *funcName) noexcept
Definition Library.h:104
virtual bool Init(const char *libName) noexcept
Definition Library.h:78