29 bool Init(
const char *libName)
noexcept override
33 fGetMaestroObject = (
void *(*)())
GetFunction(
"GetMaestroObjectWithMute");
35 if (fGetMaestroObject)
37 maestro = fGetMaestroObject();
40 fCreateSimpleSimulator = (
unsigned long int (*)(int))
GetFunction(
"CreateSimpleSimulator");
42 fDestroySimpleSimulator = (void (*)(
unsigned long int))
GetFunction(
"DestroySimpleSimulator");
45 fRemoveAllOptimizationSimulatorsAndAdd =
46 (int (*)(
unsigned long int simHandle,
int simType,
int simExecType))
GetFunction(
"RemoveAllOptimizationSimulatorsAndAdd");
47 CheckFunction((
void *)fRemoveAllOptimizationSimulatorsAndAdd, __LINE__);
48 fAddOptimizationSimulator = (int (*)(
unsigned long int simHandle,
int simType,
int simExecType))
GetFunction(
"AddOptimizationSimulator");
51 fSimpleExecute = (
char *(*)(
unsigned long int,
const char *,
const char *))
GetFunction(
"SimpleExecute");
54 fSimpleEstimate = (
char *(*)(
unsigned long int,
const char *,
const char *,
const char *))
GetFunction(
"SimpleEstimate");
57 fFreeResult = (void (*)(
char *))
GetFunction(
"FreeResult");
60 fCreateSimulator = (
unsigned long int (*)(int, int))
GetFunction(
"CreateSimulator");
62 fGetSimulator = (
void *(*)(
unsigned long int))
GetFunction(
"GetSimulator");
65 fExpectationValues =
reinterpret_cast<decltype(fExpectationValues)
>(
GetFunction(
"MaestroExpectationValues"));
66 fExpectationValuesComplex =
reinterpret_cast<decltype(fExpectationValuesComplex)
>(
GetFunction(
"MaestroExpectationValuesComplex"));
67 fGetStateVector =
reinterpret_cast<decltype(fGetStateVector)
>(
GetFunction(
"MaestroGetStateVector"));
68 fExpectationValueOperators =
reinterpret_cast<decltype(fExpectationValueOperators)
>(
GetFunction(
"MaestroExpectationValueOperators"));
69 fMoveAtBeginningOfChain =
reinterpret_cast<decltype(fMoveAtBeginningOfChain)
>(
GetFunction(
"MaestroMoveAtBeginningOfChain"));
70 fRunRequestJson =
reinterpret_cast<decltype(fRunRequestJson)
>(
GetFunction(
"MaestroRunRequestJson"));
71 fValidateRequestJson =
reinterpret_cast<decltype(fValidateRequestJson)
>(
GetFunction(
"MaestroValidateRequestJson"));
72 fDestroySimulator = (void (*)(
unsigned long int))
GetFunction(
"DestroySimulator");
75 fInitializeSimulator = (int (*)(
void *))
GetFunction(
"InitializeSimulator");
77 fResetSimulator = (int (*)(
void *))
GetFunction(
"ResetSimulator");
79 fConfigureSimulator = (int (*)(
void *,
const char *,
const char *))
GetFunction(
"ConfigureSimulator");
81 fGetConfiguration = (
char *(*)(
void *,
const char *))
GetFunction(
"GetConfiguration");
83 fAllocateQubits = (
unsigned long int (*)(
void *,
unsigned long int))
GetFunction(
"AllocateQubits");
85 fGetNumberOfQubits = (
unsigned long int (*)(
void *))
GetFunction(
"GetNumberOfQubits");
87 fClearSimulator = (int (*)(
void *))
GetFunction(
"ClearSimulator");
89 fMeasure = (
unsigned long long int (*)(
void *,
const unsigned long int *,
unsigned long int))
GetFunction(
"Measure");
91 fApplyReset = (int (*)(
void *,
const unsigned long int *,
unsigned long int))
GetFunction(
"ApplyReset");
93 fProbability = (double (*)(
void *,
unsigned long long int))
GetFunction(
"Probability");
95 fFreeDoubleVector = (void (*)(
double *))
GetFunction(
"FreeDoubleVector");
97 fFreeULLIVector = (void (*)(
unsigned long long int *))
GetFunction(
"FreeULLIVector");
99 fAmplitude = (
double *(*)(
void *,
unsigned long long int))
GetFunction(
"Amplitude");
101 fAllProbabilities = (
double *(*)(
void *))
GetFunction(
"AllProbabilities");
103 fProbabilities = (
double *(*)(
void *,
const unsigned long long int *,
unsigned long int))
GetFunction(
"Probabilities");
106 (
unsigned long long int *(*)(
void *,
const unsigned long long int *,
unsigned long int,
unsigned long int))
GetFunction(
"SampleCounts");
108 fGetSimulatorType = (int (*)(
void *))
GetFunction(
"GetSimulatorType");
110 fGetSimulationType = (int (*)(
void *))
GetFunction(
"GetSimulationType");
114 fGetGateFusionMaxQubits =
reinterpret_cast<decltype(fGetGateFusionMaxQubits)
>(
GetFunction(
"GetGateFusionMaxQubits"));
115 fIsGateFusionEnabled =
reinterpret_cast<decltype(fIsGateFusionEnabled)
>(
GetFunction(
"IsGateFusionEnabled"));
116 fGetGateFusionStatistics =
reinterpret_cast<decltype(fGetGateFusionStatistics)
>(
GetFunction(
"GetGateFusionStatistics"));
117 fApplyGenericOneQubitGate =
reinterpret_cast<decltype(fApplyGenericOneQubitGate)
>(
GetFunction(
"ApplyGenericOneQubitGate"));
118 fApplyGenericTwoQubitGate =
reinterpret_cast<decltype(fApplyGenericTwoQubitGate)
>(
GetFunction(
"ApplyGenericTwoQubitGate"));
119 fApplyGenericThreeQubitGate =
reinterpret_cast<decltype(fApplyGenericThreeQubitGate)
>(
GetFunction(
"ApplyGenericThreeQubitGate"));
120 fFlushSimulator = (int (*)(
void *))
GetFunction(
"FlushSimulator");
122 fSaveStateToInternalDestructive = (int (*)(
void *))
GetFunction(
"SaveStateToInternalDestructive");
123 CheckFunction((
void *)fSaveStateToInternalDestructive, __LINE__);
124 fRestoreInternalDestructiveSavedState = (int (*)(
void *))
GetFunction(
"RestoreInternalDestructiveSavedState");
125 CheckFunction((
void *)fRestoreInternalDestructiveSavedState, __LINE__);
126 fSaveState = (int (*)(
void *))
GetFunction(
"SaveState");
128 fRestoreState = (int (*)(
void *))
GetFunction(
"RestoreState");
130 fSetMultithreading = (int (*)(
void *, int))
GetFunction(
"SetMultithreading");
132 fGetMultithreading = (int (*)(
void *))
GetFunction(
"GetMultithreading");
134 fIsQcsim = (int (*)(
void *))
GetFunction(
"IsQcsim");
136 fMeasureNoCollapse = (
unsigned long long int (*)(
void *))
GetFunction(
"MeasureNoCollapse");
139 fApplyX = (int (*)(
void *, int))
GetFunction(
"ApplyX");
141 fApplyY = (int (*)(
void *, int))
GetFunction(
"ApplyY");
143 fApplyZ = (int (*)(
void *, int))
GetFunction(
"ApplyZ");
145 fApplyH = (int (*)(
void *, int))
GetFunction(
"ApplyH");
147 fApplyS = (int (*)(
void *, int))
GetFunction(
"ApplyS");
149 fApplySDG = (int (*)(
void *, int))
GetFunction(
"ApplySDG");
151 fApplyT = (int (*)(
void *, int))
GetFunction(
"ApplyT");
153 fApplyTDG = (int (*)(
void *, int))
GetFunction(
"ApplyTDG");
155 fApplySX = (int (*)(
void *, int))
GetFunction(
"ApplySX");
157 fApplySXDG = (int (*)(
void *, int))
GetFunction(
"ApplySXDG");
159 fApplyK = (int (*)(
void *, int))
GetFunction(
"ApplyK");
161 fApplyP = (int (*)(
void *, int, double))
GetFunction(
"ApplyP");
163 fApplyRx = (int (*)(
void *, int, double))
GetFunction(
"ApplyRx");
165 fApplyRy = (int (*)(
void *, int, double))
GetFunction(
"ApplyRy");
167 fApplyRz = (int (*)(
void *, int, double))
GetFunction(
"ApplyRz");
169 fApplyU = (int (*)(
void *, int, double, double, double, double))
GetFunction(
"ApplyU");
171 fApplyCX = (int (*)(
void *, int, int))
GetFunction(
"ApplyCX");
173 fApplyCY = (int (*)(
void *, int, int))
GetFunction(
"ApplyCY");
175 fApplyCZ = (int (*)(
void *, int, int))
GetFunction(
"ApplyCZ");
177 fApplyCH = (int (*)(
void *, int, int))
GetFunction(
"ApplyCH");
179 fApplyCSX = (int (*)(
void *, int, int))
GetFunction(
"ApplyCSX");
181 fApplyCSXDG = (int (*)(
void *, int, int))
GetFunction(
"ApplyCSXDG");
183 fApplyCP = (int (*)(
void *, int, int, double))
GetFunction(
"ApplyCP");
185 fApplyCRx = (int (*)(
void *, int, int, double))
GetFunction(
"ApplyCRx");
187 fApplyCRy = (int (*)(
void *, int, int, double))
GetFunction(
"ApplyCRy");
189 fApplyCRz = (int (*)(
void *, int, int, double))
GetFunction(
"ApplyCRz");
191 fApplyCCX = (int (*)(
void *, int, int, int))
GetFunction(
"ApplyCCX");
193 fApplySwap = (int (*)(
void *, int, int))
GetFunction(
"ApplySwap");
195 fApplyCSwap = (int (*)(
void *, int, int, int))
GetFunction(
"ApplyCSwap");
197 fApplyCU = (int (*)(
void *, int, int, double, double, double, double))
GetFunction(
"ApplyCU");
204 std::cerr <<
"MaestroLibrary: Unable to get initialization function "
209 std::cerr <<
"MaestroLibrary: Unable to load the library" << std::endl;
218 std::cerr <<
"MaestroLibrary: Unable to load function, line #: " << line;
221 const char *dlsym_error = dlerror();
223 std::cerr <<
", error: " << dlsym_error;
225 const DWORD error = GetLastError();
226 std::cerr <<
", error code: " << error;
229 std::cerr << std::endl;
235 return maestro !=
nullptr;
240 if (maestro && fCreateSimpleSimulator)
241 return fCreateSimpleSimulator(nrQubits);
243 throw std::runtime_error(
"MaestroLibrary: Unable to create the simple simulator.");
250 if (maestro && fDestroySimpleSimulator)
251 fDestroySimpleSimulator(simHandle);
253 throw std::runtime_error(
"MaestroLibrary: Unable to destroy the simple simulator.");
258 if (maestro && fRemoveAllOptimizationSimulatorsAndAdd)
259 return fRemoveAllOptimizationSimulatorsAndAdd(simHandle, simType, simExecType);
261 throw std::runtime_error(
"MaestroLibrary: Unable to remove all optimization simulators and "
269 if (maestro && fAddOptimizationSimulator)
270 return fAddOptimizationSimulator(simHandle, simType, simExecType);
272 throw std::runtime_error(
"MaestroLibrary: Unable to add an optimization simulator.");
277 char *
SimpleExecute(
unsigned long int simpleSim,
const char *jsonCircuit,
const char *jsonConfig)
279 if (maestro && fSimpleExecute)
280 return fSimpleExecute(simpleSim, jsonCircuit, jsonConfig);
282 throw std::runtime_error(
"MaestroLibrary: Unable to execute the simple simulator.");
287 char *
SimpleEstimate(
unsigned long int simpleSim,
const char *jsonCircuit,
const char *observableStr,
const char *jsonConfig)
289 if (maestro && fSimpleEstimate)
290 return fSimpleEstimate(simpleSim, jsonCircuit, observableStr, jsonConfig);
292 throw std::runtime_error(
"MaestroLibrary: Unable to execute the simple simulator for "
293 "expectation values.");
300 if (maestro && fFreeResult)
303 throw std::runtime_error(
"MaestroLibrary: Unable to free the result.");
308 if (maestro && fCreateSimulator)
309 return fCreateSimulator(simType, simExecType);
311 throw std::runtime_error(
"MaestroLibrary: Unable to create the simulator.");
318 if (maestro && fGetSimulator)
319 return fGetSimulator(simHandle);
321 throw std::runtime_error(
"MaestroLibrary: Unable to get the simulator.");
326 if (maestro && fDestroySimulator)
327 fDestroySimulator(simHandle);
329 throw std::runtime_error(
"MaestroLibrary: Unable to destroy the simulator.");
334 if (maestro && sim && fInitializeSimulator)
335 return fInitializeSimulator(sim);
337 throw std::runtime_error(
"MaestroLibrary: Unable to initialize the simulator.");
344 if (maestro && sim && fResetSimulator)
345 return fResetSimulator(sim);
347 throw std::runtime_error(
"MaestroLibrary: Unable to reset the simulator.");
354 if (maestro && sim && fConfigureSimulator)
355 return fConfigureSimulator(sim, key, value);
357 throw std::runtime_error(
"MaestroLibrary: Unable to configure the simulator.");
364 if (maestro && sim && fGetConfiguration)
365 return fGetConfiguration(sim, key);
367 throw std::runtime_error(
"MaestroLibrary: Unable to get the configuration of the simulator.");
373 if (maestro && sim && fAllocateQubits)
374 return fAllocateQubits(sim, nrQubits);
376 throw std::runtime_error(
"MaestroLibrary: Unable to allocate qubits in the simulator.");
382 if (maestro && sim && fGetNumberOfQubits)
383 return fGetNumberOfQubits(sim);
385 throw std::runtime_error(
"MaestroLibrary: Unable to get the number of qubits in the "
392 if (maestro && sim && fClearSimulator)
393 return fClearSimulator(sim);
395 throw std::runtime_error(
"MaestroLibrary: Unable to clear the simulator.");
399 unsigned long long int Measure(
void *sim,
const unsigned long int *qubits,
unsigned long int nrQubits)
401 if (maestro && sim && fMeasure)
402 return fMeasure(sim, qubits, nrQubits);
404 throw std::runtime_error(
"MaestroLibrary: Unable to measure the simulator.");
408 int ApplyReset(
void *sim,
const unsigned long int *qubits,
unsigned long int nrQubits)
410 if (maestro && sim && fApplyReset)
411 return fApplyReset(sim, qubits, nrQubits);
413 throw std::runtime_error(
"MaestroLibrary: Unable to apply reset to the simulator.");
419 if (maestro && sim && fProbability)
420 return fProbability(sim, outcome);
422 throw std::runtime_error(
"MaestroLibrary: Unable to get the probability of an outcome.");
428 if (maestro && fFreeDoubleVector)
429 fFreeDoubleVector(vec);
431 throw std::runtime_error(
"MaestroLibrary: Unable to free the double vector.");
436 if (maestro && fFreeULLIVector)
437 fFreeULLIVector(vec);
439 throw std::runtime_error(
"MaestroLibrary: Unable to free the unsigned long long int vector.");
442 double *
Amplitude(
void *sim,
unsigned long long int outcome)
444 if (maestro && sim && fAmplitude)
445 return fAmplitude(sim, outcome);
447 throw std::runtime_error(
"MaestroLibrary: Unable to get the amplitude of an outcome.");
453 if (maestro && sim && fAllProbabilities)
454 return fAllProbabilities(sim);
456 throw std::runtime_error(
"MaestroLibrary: Unable to get all probabilities.");
460 double *
Probabilities(
void *sim,
const unsigned long long int *qubits,
unsigned long int nrQubits)
462 if (maestro && sim && fProbabilities)
463 return fProbabilities(sim, qubits, nrQubits);
465 throw std::runtime_error(
"MaestroLibrary: Unable to get probabilities for specified qubits.");
469 unsigned long long int *
SampleCounts(
void *sim,
const unsigned long long int *qubits,
unsigned long int nrQubits,
unsigned long int shots)
471 if (maestro && sim && fSampleCounts)
472 return fSampleCounts(sim, qubits, nrQubits, shots);
474 throw std::runtime_error(
"MaestroLibrary: Unable to get sample counts for specified qubits.");
480 if (maestro && sim && fGetSimulatorType)
481 return fGetSimulatorType(sim);
483 throw std::runtime_error(
"MaestroLibrary: Unable to get the simulator type.");
489 if (maestro && sim && fGetSimulationType)
490 return fGetSimulationType(sim);
492 throw std::runtime_error(
"MaestroLibrary: Unable to get the simulation type.");
498 return maestro && sim && fGetGateFusionMaxQubits ? fGetGateFusionMaxQubits(sim) : 0;
503 return maestro && sim && fIsGateFusionEnabled ? fIsGateFusionEnabled(sim) : 0;
508 return maestro && sim && fGetGateFusionStatistics ? fGetGateFusionStatistics(sim, statistics) : 0;
513 return maestro && sim && fApplyGenericOneQubitGate ? fApplyGenericOneQubitGate(sim, q0, matrix) : 0;
518 return maestro && sim && fApplyGenericTwoQubitGate ? fApplyGenericTwoQubitGate(sim, q0, q1, matrix) : 0;
523 return maestro && sim && fApplyGenericThreeQubitGate ? fApplyGenericThreeQubitGate(sim, q0, q1, q2, matrix) : 0;
528 if (maestro && sim && fFlushSimulator)
529 return fFlushSimulator(sim);
531 throw std::runtime_error(
"MaestroLibrary: Unable to flush the simulator.");
537 if (maestro && sim && fSaveStateToInternalDestructive)
538 return fSaveStateToInternalDestructive(sim);
540 throw std::runtime_error(
"MaestroLibrary: Unable to save the state to internal destructive "
547 if (maestro && sim && fRestoreInternalDestructiveSavedState)
548 return fRestoreInternalDestructiveSavedState(sim);
550 throw std::runtime_error(
"MaestroLibrary: Unable to restore the state from internal "
551 "destructive storage.");
557 if (maestro && sim && fSaveState)
558 return fSaveState(sim);
560 throw std::runtime_error(
"MaestroLibrary: Unable to save the state.");
566 if (maestro && sim && fRestoreState)
567 return fRestoreState(sim);
569 throw std::runtime_error(
"MaestroLibrary: Unable to restore the state.");
575 if (maestro && sim && fSetMultithreading)
576 return fSetMultithreading(sim, multithreading);
578 throw std::runtime_error(
"MaestroLibrary: Unable to set multithreading.");
584 if (maestro && sim && fGetMultithreading)
585 return fGetMultithreading(sim);
587 throw std::runtime_error(
"MaestroLibrary: Unable to get multithreading status.");
593 if (maestro && sim && fIsQcsim)
594 return fIsQcsim(sim);
596 throw std::runtime_error(
"MaestroLibrary: Unable to check if the simulator is a QCSIM.");
602 if (maestro && sim && fMeasureNoCollapse)
603 return fMeasureNoCollapse(sim);
605 throw std::runtime_error(
"MaestroLibrary: Unable to measure without collapse.");
611 if (maestro && sim && fApplyX)
612 return fApplyX(sim, qubit);
614 throw std::runtime_error(
"MaestroLibrary: Unable to apply X gate.");
620 if (maestro && sim && fApplyY)
621 return fApplyY(sim, qubit);
623 throw std::runtime_error(
"MaestroLibrary: Unable to apply Y gate.");
629 if (maestro && sim && fApplyZ)
630 return fApplyZ(sim, qubit);
632 throw std::runtime_error(
"MaestroLibrary: Unable to apply Z gate.");
638 if (maestro && sim && fApplyH)
639 return fApplyH(sim, qubit);
641 throw std::runtime_error(
"MaestroLibrary: Unable to apply H gate.");
647 if (maestro && sim && fApplyS)
648 return fApplyS(sim, qubit);
650 throw std::runtime_error(
"MaestroLibrary: Unable to apply S gate.");
656 if (maestro && sim && fApplySDG)
657 return fApplySDG(sim, qubit);
659 throw std::runtime_error(
"MaestroLibrary: Unable to apply SDG gate.");
665 if (maestro && sim && fApplyT)
666 return fApplyT(sim, qubit);
668 throw std::runtime_error(
"MaestroLibrary: Unable to apply T gate.");
674 if (maestro && sim && fApplyTDG)
675 return fApplyTDG(sim, qubit);
677 throw std::runtime_error(
"MaestroLibrary: Unable to apply TDG gate.");
683 if (maestro && sim && fApplySX)
684 return fApplySX(sim, qubit);
686 throw std::runtime_error(
"MaestroLibrary: Unable to apply SX gate.");
692 if (maestro && sim && fApplySXDG)
693 return fApplySXDG(sim, qubit);
695 throw std::runtime_error(
"MaestroLibrary: Unable to apply SXDG gate.");
701 if (maestro && sim && fApplyK)
702 return fApplyK(sim, qubit);
704 throw std::runtime_error(
"MaestroLibrary: Unable to apply K gate.");
708 int ApplyP(
void *sim,
int qubit,
double theta)
710 if (maestro && sim && fApplyP)
711 return fApplyP(sim, qubit, theta);
713 throw std::runtime_error(
"MaestroLibrary: Unable to apply P gate.");
717 int ApplyRx(
void *sim,
int qubit,
double theta)
719 if (maestro && sim && fApplyRx)
720 return fApplyRx(sim, qubit, theta);
722 throw std::runtime_error(
"MaestroLibrary: Unable to apply Rx gate.");
726 int ApplyRy(
void *sim,
int qubit,
double theta)
728 if (maestro && sim && fApplyRy)
729 return fApplyRy(sim, qubit, theta);
731 throw std::runtime_error(
"MaestroLibrary: Unable to apply Ry gate.");
735 int ApplyRz(
void *sim,
int qubit,
double theta)
737 if (maestro && sim && fApplyRz)
738 return fApplyRz(sim, qubit, theta);
740 throw std::runtime_error(
"MaestroLibrary: Unable to apply Rz gate.");
744 int ApplyU(
void *sim,
int qubit,
double theta,
double phi,
double lambda,
double gamma)
746 if (maestro && sim && fApplyU)
747 return fApplyU(sim, qubit, theta, phi, lambda, gamma);
749 throw std::runtime_error(
"MaestroLibrary: Unable to apply U gate.");
753 int ApplyCX(
void *sim,
int controlQubit,
int targetQubit)
755 if (maestro && sim && fApplyCX)
756 return fApplyCX(sim, controlQubit, targetQubit);
758 throw std::runtime_error(
"MaestroLibrary: Unable to apply CX gate.");
762 int ApplyCY(
void *sim,
int controlQubit,
int targetQubit)
764 if (maestro && sim && fApplyCY)
765 return fApplyCY(sim, controlQubit, targetQubit);
767 throw std::runtime_error(
"MaestroLibrary: Unable to apply CY gate.");
771 int ApplyCZ(
void *sim,
int controlQubit,
int targetQubit)
773 if (maestro && sim && fApplyCZ)
774 return fApplyCZ(sim, controlQubit, targetQubit);
776 throw std::runtime_error(
"MaestroLibrary: Unable to apply CZ gate.");
780 int ApplyCH(
void *sim,
int controlQubit,
int targetQubit)
782 if (maestro && sim && fApplyCH)
783 return fApplyCH(sim, controlQubit, targetQubit);
785 throw std::runtime_error(
"MaestroLibrary: Unable to apply CH gate.");
789 int ApplyCSX(
void *sim,
int controlQubit,
int targetQubit)
791 if (maestro && sim && fApplyCSX)
792 return fApplyCSX(sim, controlQubit, targetQubit);
794 throw std::runtime_error(
"MaestroLibrary: Unable to apply CSX gate.");
800 if (maestro && sim && fApplyCSXDG)
801 return fApplyCSXDG(sim, controlQubit, targetQubit);
803 throw std::runtime_error(
"MaestroLibrary: Unable to apply CSXDG gate.");
807 int ApplyCP(
void *sim,
int controlQubit,
int targetQubit,
double theta)
809 if (maestro && sim && fApplyCP)
810 return fApplyCP(sim, controlQubit, targetQubit, theta);
812 throw std::runtime_error(
"MaestroLibrary: Unable to apply CP gate.");
816 int ApplyCRx(
void *sim,
int controlQubit,
int targetQubit,
double theta)
818 if (maestro && sim && fApplyCRx)
819 return fApplyCRx(sim, controlQubit, targetQubit, theta);
821 throw std::runtime_error(
"MaestroLibrary: Unable to apply CRx gate.");
825 int ApplyCRy(
void *sim,
int controlQubit,
int targetQubit,
double theta)
827 if (maestro && sim && fApplyCRy)
828 return fApplyCRy(sim, controlQubit, targetQubit, theta);
830 throw std::runtime_error(
"MaestroLibrary: Unable to apply CRy gate.");
834 int ApplyCRz(
void *sim,
int controlQubit,
int targetQubit,
double theta)
836 if (maestro && sim && fApplyCRz)
837 return fApplyCRz(sim, controlQubit, targetQubit, theta);
839 throw std::runtime_error(
"MaestroLibrary: Unable to apply CRz gate.");
843 int ApplyCCX(
void *sim,
int controlQubit1,
int controlQubit2,
int targetQubit)
845 if (maestro && sim && fApplyCCX)
846 return fApplyCCX(sim, controlQubit1, controlQubit2, targetQubit);
848 throw std::runtime_error(
"MaestroLibrary: Unable to apply CCX gate.");
854 if (maestro && sim && fApplySwap)
855 return fApplySwap(sim, qubit1, qubit2);
857 throw std::runtime_error(
"MaestroLibrary: Unable to apply Swap gate.");
861 int ApplyCSwap(
void *sim,
int controlQubit,
int qubit1,
int qubit2)
863 if (maestro && sim && fApplyCSwap)
864 return fApplyCSwap(sim, controlQubit, qubit1, qubit2);
866 throw std::runtime_error(
"MaestroLibrary: Unable to apply CSwap gate.");
870 int ApplyCU(
void *sim,
int controlQubit,
int targetQubit,
double theta,
double phi,
double lambda,
double gamma)
872 if (maestro && sim && fApplyCU)
873 return fApplyCU(sim, controlQubit, targetQubit, theta, phi, lambda, gamma);
875 throw std::runtime_error(
"MaestroLibrary: Unable to apply CU gate.");
881 if (!maestro || !sim || !fExpectationValues)
882 throw std::runtime_error(
"MaestroLibrary: Batch expectations unavailable.");
883 std::vector<const char *> strings;
884 strings.reserve(paulis.size());
885 for (
const auto &pauli : paulis)
886 strings.push_back(pauli.c_str());
887 std::vector<double> values(paulis.size());
888 if (!fExpectationValues(sim, strings.data(), strings.size(), values.data(), values.size()))
889 throw std::runtime_error(
"MaestroLibrary: Batch expectations failed.");
893 std::vector<std::complex<double>>
ExpectationValuesComplex(
void *sim,
const std::vector<std::string> &paulis,
bool normalized =
true)
895 if (!maestro || !sim || !fExpectationValuesComplex)
896 throw std::runtime_error(
"MaestroLibrary: Complex batch expectations unavailable.");
897 std::vector<const char *> strings;
898 strings.reserve(paulis.size());
899 for (
const auto &pauli : paulis)
900 strings.push_back(pauli.c_str());
901 std::vector<double> real(paulis.size()), imag(paulis.size());
902 if (!fExpectationValuesComplex(sim, strings.data(), strings.size(), normalized ? 1 : 0, real.data(), imag.data(), real.size()))
903 throw std::runtime_error(
"MaestroLibrary: Complex batch expectations failed.");
904 std::vector<std::complex<double>> values;
905 values.reserve(paulis.size());
906 for (
size_t i = 0; i < paulis.size(); ++i)
907 values.emplace_back(real[i], imag[i]);
914 return RequestJson(request, fRunRequestJson);
919 return RequestJson(request, fValidateRequestJson);
924 if (!maestro || !sim || !fGetStateVector)
925 throw std::runtime_error(
"MaestroLibrary: Statevector query unavailable.");
927 if (n >= std::numeric_limits<size_t>::digits)
928 throw std::length_error(
"Statevector exceeds the basis-index range");
929 const size_t count = n ?
size_t{1} << n : 0;
930 if (count > std::vector<double>().max_size() / 2)
931 throw std::length_error(
"Statevector output is too large");
932 std::vector<std::complex<double>> result(count);
933 if (!fGetStateVector(sim,
reinterpret_cast<double *
>(result.data()), count))
934 throw std::runtime_error(
"MaestroLibrary: Statevector query failed.");
941 const std::vector<std::array<std::complex<double>, 4>> &matrices)
943 if (!maestro || !sim || !fExpectationValueOperators)
944 throw std::runtime_error(
"MaestroLibrary: Operator expectation unavailable.");
945 if (qubits.size() != matrices.size())
946 throw std::invalid_argument(
"Each operator must have one target qubit");
947 if (matrices.size() > std::vector<double>().max_size() / 8)
948 throw std::length_error(
"Too many operators");
949 std::vector<double> raw;
950 raw.reserve(8 * matrices.size());
951 for (
const auto &matrix : matrices)
952 for (
const auto &entry : matrix)
954 raw.push_back(entry.real());
955 raw.push_back(entry.imag());
957 double re = 0., im = 0.;
958 if (!fExpectationValueOperators(sim, qubits.data(), qubits.size(), raw.data(), &re, &im))
959 throw std::runtime_error(
"MaestroLibrary: Operator expectation failed.");
965 if (!maestro || !sim || !fMoveAtBeginningOfChain)
966 throw std::runtime_error(
"MaestroLibrary: Chain movement unavailable.");
967 if (!fMoveAtBeginningOfChain(sim, qubits.data(), qubits.size()))
968 throw std::runtime_error(
"MaestroLibrary: Chain movement failed.");
972 std::string RequestJson(
const std::string &request,
char *(*call)(
const char *))
974 if (!maestro || !call || !fFreeResult)
975 throw std::runtime_error(
"MaestroLibrary: JSON request API unavailable.");
976 auto release = [
this](
char *p) {
FreeResult(p); };
977 std::unique_ptr<char,
decltype(release)> result(call(request.c_str()), release);
979 throw std::runtime_error(
"MaestroLibrary: JSON request returned no response.");
983 char *(*fRunRequestJson)(
const char *) =
nullptr;
984 char *(*fValidateRequestJson)(
const char *) =
nullptr;
985 int (*fGetStateVector)(
void *,
double *, size_t) =
nullptr;
986 int (*fExpectationValueOperators)(
void *,
const unsigned long *, size_t,
const double *,
double *,
double *) =
nullptr;
987 int (*fMoveAtBeginningOfChain)(
void *,
const unsigned long *, size_t) =
nullptr;
988 int (*fExpectationValues)(
void *,
const char *
const *, size_t,
double *, size_t) =
nullptr;
989 int (*fExpectationValuesComplex)(
void *,
const char *
const *, size_t, int,
double *,
double *, size_t) =
nullptr;
990 void *maestro =
nullptr;
992 void *(*fGetMaestroObject)();
994 unsigned long int (*fCreateSimpleSimulator)(int);
995 void (*fDestroySimpleSimulator)(
unsigned long int);
997 int (*fRemoveAllOptimizationSimulatorsAndAdd)(
unsigned long int, int, int);
998 int (*fAddOptimizationSimulator)(
unsigned long int, int, int);
1000 char *(*fSimpleExecute)(
unsigned long int,
const char *,
const char *);
1001 char *(*fSimpleEstimate)(
unsigned long int,
const char *,
const char *,
const char *);
1002 void (*fFreeResult)(
char *);
1004 unsigned long int (*fCreateSimulator)(int, int);
1005 void *(*fGetSimulator)(
unsigned long int);
1006 void (*fDestroySimulator)(
unsigned long int);
1008 int (*fInitializeSimulator)(
void *);
1009 int (*fResetSimulator)(
void *);
1010 int (*fConfigureSimulator)(
void *,
const char *,
const char *);
1011 char *(*fGetConfiguration)(
void *,
const char *);
1012 unsigned long int (*fAllocateQubits)(
void *,
unsigned long int);
1013 unsigned long int (*fGetNumberOfQubits)(
void *);
1014 int (*fClearSimulator)(
void *);
1015 unsigned long long int (*fMeasure)(
void *,
const unsigned long int *,
unsigned long int);
1016 int (*fApplyReset)(
void *,
const unsigned long int *,
unsigned long int);
1017 double (*fProbability)(
void *,
unsigned long long int);
1018 void (*fFreeDoubleVector)(
double *);
1019 void (*fFreeULLIVector)(
unsigned long long int *);
1020 double *(*fAmplitude)(
void *,
unsigned long long int);
1021 double *(*fAllProbabilities)(
void *);
1022 double *(*fProbabilities)(
void *,
const unsigned long long int *,
unsigned long int);
1023 unsigned long long int *(*fSampleCounts)(
void *,
const unsigned long long int *,
unsigned long int,
unsigned long int);
1024 int (*fGetSimulatorType)(
void *);
1025 int (*fGetSimulationType)(
void *);
1026 unsigned (*fGetGateFusionMaxQubits)(
void *) =
nullptr;
1027 int (*fIsGateFusionEnabled)(
void *) =
nullptr;
1029 int (*fApplyGenericOneQubitGate)(
void *,
unsigned long,
const double *) =
nullptr;
1030 int (*fApplyGenericTwoQubitGate)(
void *,
unsigned long,
unsigned long,
const double *) =
nullptr;
1031 int (*fApplyGenericThreeQubitGate)(
void *,
unsigned long,
unsigned long,
unsigned long,
const double *) =
nullptr;
1032 int (*fFlushSimulator)(
void *);
1033 int (*fSaveStateToInternalDestructive)(
void *);
1034 int (*fRestoreInternalDestructiveSavedState)(
void *);
1035 int (*fSaveState)(
void *);
1036 int (*fRestoreState)(
void *);
1037 int (*fSetMultithreading)(
void *, int);
1038 int (*fGetMultithreading)(
void *);
1039 int (*fIsQcsim)(
void *);
1040 unsigned long long int (*fMeasureNoCollapse)(
void *);
1042 int (*fApplyX)(
void *, int);
1043 int (*fApplyY)(
void *, int);
1044 int (*fApplyZ)(
void *, int);
1045 int (*fApplyH)(
void *, int);
1046 int (*fApplyS)(
void *, int);
1047 int (*fApplySDG)(
void *, int);
1048 int (*fApplyT)(
void *, int);
1049 int (*fApplyTDG)(
void *, int);
1050 int (*fApplySX)(
void *, int);
1051 int (*fApplySXDG)(
void *, int);
1052 int (*fApplyK)(
void *, int);
1053 int (*fApplyP)(
void *, int, double);
1054 int (*fApplyRx)(
void *, int, double);
1055 int (*fApplyRy)(
void *, int, double);
1056 int (*fApplyRz)(
void *, int, double);
1057 int (*fApplyU)(
void *, int, double, double, double, double);
1058 int (*fApplyCX)(
void *, int, int);
1059 int (*fApplyCY)(
void *, int, int);
1060 int (*fApplyCZ)(
void *, int, int);
1061 int (*fApplyCH)(
void *, int, int);
1062 int (*fApplyCSX)(
void *, int, int);
1063 int (*fApplyCSXDG)(
void *, int, int);
1064 int (*fApplyCP)(
void *, int, int, double);
1065 int (*fApplyCRx)(
void *, int, int, double);
1066 int (*fApplyCRy)(
void *, int, int, double);
1067 int (*fApplyCRz)(
void *, int, int, double);
1068 int (*fApplyCCX)(
void *, int, int, int);
1069 int (*fApplySwap)(
void *, int, int);
1070 int (*fApplyCSwap)(
void *, int, int, int);
1071 int (*fApplyCU)(
void *, int, int, double, double, double, double);
struct MaestroGateFusionStatistics MaestroGateFusionStatistics
int ApplyCCX(void *sim, int controlQubit1, int controlQubit2, int targetQubit)
int ResetSimulator(void *sim)
void * GetSimulator(unsigned long int simHandle)
int ApplyGenericThreeQubitGate(void *sim, unsigned long q0, unsigned long q1, unsigned long q2, const double *matrix)
int IsGateFusionEnabled(void *sim)
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
int ApplyGenericTwoQubitGate(void *sim, unsigned long q0, unsigned long q1, const double *matrix)
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)
std::string ValidateRequestJson(const std::string &request)
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 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 ApplyGenericOneQubitGate(void *sim, unsigned long q0, const double *matrix)
int ClearSimulator(void *sim)
int ApplyCSX(void *sim, int controlQubit, int targetQubit)
int ApplyK(void *sim, int qubit)
char * SimpleExecute(unsigned long int simpleSim, const char *jsonCircuit, const char *jsonConfig)
int RestoreState(void *sim)
std::complex< double > ExpectationValueOperators(void *sim, const std::vector< unsigned long > &qubits, const std::vector< std::array< std::complex< double >, 4 > > &matrices)
int ApplyCRz(void *sim, int controlQubit, int targetQubit, double theta)
unsigned GetGateFusionMaxQubits(void *sim)
double * Amplitude(void *sim, unsigned long long int outcome)
std::vector< std::complex< double > > ExpectationValuesComplex(void *sim, const std::vector< std::string > &paulis, bool normalized=true)
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 GetGateFusionStatistics(void *sim, MaestroGateFusionStatistics *statistics)
int ApplyCSwap(void *sim, int controlQubit, int qubit1, int qubit2)
virtual void FreeULLIVector(unsigned long long int *vec)
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)
std::string RunRequestJson(const std::string &request)
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)
void MoveAtBeginningOfChain(void *sim, const std::vector< unsigned long > &qubits)
virtual void FreeResult(char *result)
std::vector< std::complex< double > > GetStateVector(void *sim)
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)
std::vector< double > ExpectationValues(void *sim, const std::vector< std::string > &paulis)
unsigned long long int MeasureNoCollapse(void *sim)
int SetMultithreading(void *sim, int multithreading)
void * GetFunction(const char *funcName) noexcept
virtual bool Init(const char *libName) noexcept