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