14#ifndef _QUEST_LIB_SIM_H_
15#define _QUEST_LIB_SIM_H_
40 if (initialized && fFinalize)
47 bool Init(
const char *libName)
noexcept override
51 fInitialize = (void (*)())
GetFunction(
"Initialize");
56 fCreateSimulator = (
unsigned long int (*)(int))
GetFunction(
"CreateSimulator");
58 fDestroySimulator = (void (*)(
unsigned long int))
GetFunction(
"DestroySimulator");
60 fCloneSimulator = (
unsigned long int (*)(
void *))
GetFunction(
"CloneSimulator");
62 fGetSimulator = (
void *(*)(
unsigned long int))
GetFunction(
"GetSimulator");
65 fGetNumQubits = (int (*)(
void *))
GetFunction(
"GetNumQubits");
67 fGetQubitProbability0 = (double (*)(
void *, int))
GetFunction(
"GetQubitProbability0");
69 fGetQubitProbability1 = (double (*)(
void *, int))
GetFunction(
"GetQubitProbability1");
71 fGetOutcomeProbability = (double (*)(
void *,
long long int))
GetFunction(
"GetOutcomeProbability");
73 fGetExpectationValue = (double (*)(
void *,
const char *))
GetFunction(
"GetExpectationValue");
76 fMeasure = (int (*)(
void *, int))
GetFunction(
"Measure");
78 fMeasureQubits = (
long long int (*)(
void *,
int *, int))
GetFunction(
"MeasureQubits");
81 fApplyP = (void (*)(
void *, int, double))
GetFunction(
"ApplyP");
83 fApplyX = (void (*)(
void *, int))
GetFunction(
"ApplyX");
85 fApplyY = (void (*)(
void *, int))
GetFunction(
"ApplyY");
87 fApplyZ = (void (*)(
void *, int))
GetFunction(
"ApplyZ");
89 fApplyH = (void (*)(
void *, int))
GetFunction(
"ApplyH");
91 fApplyS = (void (*)(
void *, int))
GetFunction(
"ApplyS");
93 fApplyT = (void (*)(
void *, int))
GetFunction(
"ApplyT");
96 fApplyRx = (void (*)(
void *, int, double))
GetFunction(
"ApplyRx");
98 fApplyRy = (void (*)(
void *, int, double))
GetFunction(
"ApplyRy");
100 fApplyRz = (void (*)(
void *, int, double))
GetFunction(
"ApplyRz");
103 fApplyCS = (void (*)(
void *, int, int))
GetFunction(
"ApplyCS");
105 fApplyCT = (void (*)(
void *, int, int))
GetFunction(
"ApplyCT");
107 fApplyCH = (void (*)(
void *, int, int))
GetFunction(
"ApplyCH");
109 fApplySwap = (void (*)(
void *, int, int))
GetFunction(
"ApplySwap");
111 fApplyCX = (void (*)(
void *, int, int))
GetFunction(
"ApplyCX");
113 fApplyCY = (void (*)(
void *, int, int))
GetFunction(
"ApplyCY");
115 fApplyCZ = (void (*)(
void *, int, int))
GetFunction(
"ApplyCZ");
118 fApplyCRx = (void (*)(
void *, int, int, double))
GetFunction(
"ApplyCRx");
120 fApplyCRy = (void (*)(
void *, int, int, double))
GetFunction(
"ApplyCRy");
122 fApplyCRz = (void (*)(
void *, int, int, double))
GetFunction(
"ApplyCRz");
125 fApplyCSwap = (void (*)(
void *, int, int, int))
GetFunction(
"ApplyCSwap");
127 fApplyCCX = (void (*)(
void *, int, int, int))
GetFunction(
"ApplyCCX");
130 fApplySdg = (void (*)(
void *, int))
GetFunction(
"ApplySdg");
132 fApplyTdg = (void (*)(
void *, int))
GetFunction(
"ApplyTdg");
134 fApplySx = (void (*)(
void *, int))
GetFunction(
"ApplySx");
136 fApplySxDg = (void (*)(
void *, int))
GetFunction(
"ApplySxDg");
138 fApplyK = (void (*)(
void *, int))
GetFunction(
"ApplyK");
141 fApplyU = (void (*)(
void *, int, double, double, double, double))
GetFunction(
"ApplyU");
143 fApplyCU = (void (*)(
void *, int, int, double, double, double, double))
GetFunction(
"ApplyCU");
145 fApplyCP = (void (*)(
void *, int, int, double))
GetFunction(
"ApplyCP");
147 fApplyCSx = (void (*)(
void *, int, int))
GetFunction(
"ApplyCSx");
149 fApplyCSxDg = (void (*)(
void *, int, int))
GetFunction(
"ApplyCSxDg");
152 fGetAmplitudes = (int (*)(
void *,
void *,
unsigned long long int))
GetFunction(
"GetAmplitudes");
154 fGetAmplitude = (int (*)(
void *,
long long int,
void *,
unsigned long long int))
GetFunction(
"GetAmplitude");
156 fIsDoublePrecision = (int (*)())
GetFunction(
"IsDoublePrecision");
174 std::cerr <<
"QuestLibSim: Unable to load function, line #: " << line;
177 const char *dlsym_error = dlerror();
179 std::cerr <<
", error: " << dlsym_error;
181 const DWORD error = GetLastError();
182 std::cerr <<
", error code: " << error;
185 std::cerr << std::endl;
199 return fCreateSimulator(nrQubits);
201 throw std::runtime_error(
"QuestLibSim: Unable to create simulator");
209 fDestroySimulator(simHandle);
211 throw std::runtime_error(
"QuestLibSim: Unable to destroy simulator");
217 return fCloneSimulator(sim);
219 throw std::runtime_error(
"QuestLibSim: Unable to clone simulator");
227 return fGetSimulator(simHandle);
229 throw std::runtime_error(
"QuestLibSim: Unable to get simulator");
239 return fGetNumQubits(sim);
241 throw std::runtime_error(
"QuestLibSim: Unable to get number of qubits");
249 return fGetQubitProbability0(sim, qubit);
251 throw std::runtime_error(
"QuestLibSim: Unable to get qubit probability 0");
259 return fGetQubitProbability1(sim, qubit);
261 throw std::runtime_error(
"QuestLibSim: Unable to get qubit probability 1");
269 return fGetOutcomeProbability(sim, outcome);
271 throw std::runtime_error(
"QuestLibSim: Unable to get outcome probability");
279 return fGetExpectationValue(sim, pauliStr);
281 throw std::runtime_error(
"QuestLibSim: Unable to get expectation value");
291 return fMeasure(sim, qubit);
293 throw std::runtime_error(
"QuestLibSim: Unable to measure qubit");
301 return fMeasureQubits(sim, qubits, numQubits);
303 throw std::runtime_error(
"QuestLibSim: Unable to measure qubits");
310 void ApplyP(
void *sim,
int qubit,
double angle)
313 fApplyP(sim, qubit, angle);
315 throw std::runtime_error(
"QuestLibSim: Unable to apply P gate");
323 throw std::runtime_error(
"QuestLibSim: Unable to apply X gate");
331 throw std::runtime_error(
"QuestLibSim: Unable to apply Y gate");
339 throw std::runtime_error(
"QuestLibSim: Unable to apply Z gate");
347 throw std::runtime_error(
"QuestLibSim: Unable to apply H gate");
355 throw std::runtime_error(
"QuestLibSim: Unable to apply S gate");
363 throw std::runtime_error(
"QuestLibSim: Unable to apply T gate");
369 fApplySdg(sim, qubit);
371 throw std::runtime_error(
"QuestLibSim: Unable to apply Sdg gate");
377 fApplyTdg(sim, qubit);
379 throw std::runtime_error(
"QuestLibSim: Unable to apply Tdg gate");
385 fApplySx(sim, qubit);
387 throw std::runtime_error(
"QuestLibSim: Unable to apply Sx gate");
393 fApplySxDg(sim, qubit);
395 throw std::runtime_error(
"QuestLibSim: Unable to apply SxDg gate");
403 throw std::runtime_error(
"QuestLibSim: Unable to apply K gate");
408 void ApplyRx(
void *sim,
int qubit,
double angle)
411 fApplyRx(sim, qubit, angle);
413 throw std::runtime_error(
"QuestLibSim: Unable to apply Rx gate");
416 void ApplyRy(
void *sim,
int qubit,
double angle)
419 fApplyRy(sim, qubit, angle);
421 throw std::runtime_error(
"QuestLibSim: Unable to apply Ry gate");
424 void ApplyRz(
void *sim,
int qubit,
double angle)
427 fApplyRz(sim, qubit, angle);
429 throw std::runtime_error(
"QuestLibSim: Unable to apply Rz gate");
432 void ApplyU(
void *sim,
int qubit,
double theta,
double phi,
double lambda,
double gamma)
435 fApplyU(sim, qubit, theta, phi, lambda, gamma);
437 throw std::runtime_error(
"QuestLibSim: Unable to apply U gate");
442 void ApplyCS(
void *sim,
int control,
int target)
445 fApplyCS(sim, control, target);
447 throw std::runtime_error(
"QuestLibSim: Unable to apply CS gate");
450 void ApplyCT(
void *sim,
int control,
int target)
453 fApplyCT(sim, control, target);
455 throw std::runtime_error(
"QuestLibSim: Unable to apply CT gate");
458 void ApplyCH(
void *sim,
int control,
int target)
461 fApplyCH(sim, control, target);
463 throw std::runtime_error(
"QuestLibSim: Unable to apply CH gate");
469 fApplySwap(sim, qubit1, qubit2);
471 throw std::runtime_error(
"QuestLibSim: Unable to apply Swap gate");
474 void ApplyCX(
void *sim,
int control,
int target)
477 fApplyCX(sim, control, target);
479 throw std::runtime_error(
"QuestLibSim: Unable to apply CX gate");
482 void ApplyCY(
void *sim,
int control,
int target)
485 fApplyCY(sim, control, target);
487 throw std::runtime_error(
"QuestLibSim: Unable to apply CY gate");
490 void ApplyCZ(
void *sim,
int control,
int target)
493 fApplyCZ(sim, control, target);
495 throw std::runtime_error(
"QuestLibSim: Unable to apply CZ gate");
498 void ApplyCRx(
void *sim,
int control,
int target,
double angle)
501 fApplyCRx(sim, control, target, angle);
503 throw std::runtime_error(
"QuestLibSim: Unable to apply CRx gate");
506 void ApplyCRy(
void *sim,
int control,
int target,
double angle)
509 fApplyCRy(sim, control, target, angle);
511 throw std::runtime_error(
"QuestLibSim: Unable to apply CRy gate");
514 void ApplyCRz(
void *sim,
int control,
int target,
double angle)
517 fApplyCRz(sim, control, target, angle);
519 throw std::runtime_error(
"QuestLibSim: Unable to apply CRz gate");
522 void ApplyCP(
void *sim,
int control,
int target,
double angle)
525 fApplyCP(sim, control, target, angle);
527 throw std::runtime_error(
"QuestLibSim: Unable to apply CP gate");
530 void ApplyCU(
void *sim,
int control,
int target,
double theta,
double phi,
double lambda,
double gamma)
533 fApplyCU(sim, control, target, theta, phi, lambda, gamma);
535 throw std::runtime_error(
"QuestLibSim: Unable to apply CU gate");
541 fApplyCSx(sim, control, target);
543 throw std::runtime_error(
"QuestLibSim: Unable to apply CSx gate");
549 fApplyCSxDg(sim, control, target);
551 throw std::runtime_error(
"QuestLibSim: Unable to apply CSxDg gate");
556 void ApplyCSwap(
void *sim,
int control,
int qubit1,
int qubit2)
559 fApplyCSwap(sim, control, qubit1, qubit2);
561 throw std::runtime_error(
"QuestLibSim: Unable to apply CSwap gate");
564 void ApplyCCX(
void *sim,
int control1,
int control2,
int target)
567 fApplyCCX(sim, control1, control2, target);
569 throw std::runtime_error(
"QuestLibSim: Unable to apply CCX gate");
574 bool GetAmplitudes(
void *sim, std::vector<std::complex<double>> &litudes)
const
579 return fGetAmplitudes(sim, amplitudes.data(), amplitudes.size() *
sizeof(std::complex<double>)) == 1;
582 std::vector<std::complex<float>> amplitudesSingle(amplitudes.size());
583 if (fGetAmplitudes(sim, amplitudesSingle.data(), amplitudesSingle.size() *
sizeof(std::complex<float>)) == 1)
585 for (
size_t i = 0; i < amplitudes.size(); ++i)
586 amplitudes[i] = std::complex<double>(
static_cast<double>(amplitudesSingle[i].real()),
static_cast<double>(amplitudesSingle[i].imag()));
593 throw std::runtime_error(
"QuestLibSim: Unable to get amplitudes");
598 bool GetAmplitude(
void *sim,
long long int index, std::complex<double> &litude)
const
603 return fGetAmplitude(sim, index, &litude,
sizeof(std::complex<double>)) == 1;
606 std::complex<float> ampSingle;
607 if (fGetAmplitude(sim, index, &Single,
sizeof(std::complex<float>)) == 1)
609 amplitude = std::complex<double>(
static_cast<double>(ampSingle.real()),
static_cast<double>(ampSingle.imag()));
615 throw std::runtime_error(
"QuestLibSim: Unable to get amplitude");
622 return fIsDoublePrecision() == 1;
624 throw std::runtime_error(
"QuestLibSim: Unable to check double precision");
629 bool initialized =
false;
631 void (*fInitialize)() =
nullptr;
632 void (*fFinalize)() =
nullptr;
634 unsigned long int (*fCreateSimulator)(int) =
nullptr;
635 void (*fDestroySimulator)(
unsigned long int) =
nullptr;
636 unsigned long int (*fCloneSimulator)(
void *) =
nullptr;
637 void *(*fGetSimulator)(
unsigned long int) =
nullptr;
639 int (*fGetNumQubits)(
void *) =
nullptr;
640 double (*fGetQubitProbability0)(
void *, int) =
nullptr;
641 double (*fGetQubitProbability1)(
void *, int) =
nullptr;
642 double (*fGetOutcomeProbability)(
void *,
long long int) =
nullptr;
643 double (*fGetExpectationValue)(
void *,
const char *) =
nullptr;
645 int (*fMeasure)(
void *, int) =
nullptr;
646 long long int (*fMeasureQubits)(
void *,
int *, int) =
nullptr;
648 void (*fApplyP)(
void *, int, double) =
nullptr;
649 void (*fApplyX)(
void *, int) =
nullptr;
650 void (*fApplyY)(
void *, int) =
nullptr;
651 void (*fApplyZ)(
void *, int) =
nullptr;
652 void (*fApplyH)(
void *, int) =
nullptr;
653 void (*fApplyS)(
void *, int) =
nullptr;
654 void (*fApplyT)(
void *, int) =
nullptr;
655 void (*fApplyRx)(
void *, int, double) =
nullptr;
656 void (*fApplyRy)(
void *, int, double) =
nullptr;
657 void (*fApplyRz)(
void *, int, double) =
nullptr;
659 void (*fApplyCS)(
void *, int, int) =
nullptr;
660 void (*fApplyCT)(
void *, int, int) =
nullptr;
661 void (*fApplyCH)(
void *, int, int) =
nullptr;
662 void (*fApplySwap)(
void *, int, int) =
nullptr;
663 void (*fApplyCX)(
void *, int, int) =
nullptr;
664 void (*fApplyCY)(
void *, int, int) =
nullptr;
665 void (*fApplyCZ)(
void *, int, int) =
nullptr;
666 void (*fApplyCRx)(
void *, int, int, double) =
nullptr;
667 void (*fApplyCRy)(
void *, int, int, double) =
nullptr;
668 void (*fApplyCRz)(
void *, int, int, double) =
nullptr;
670 void (*fApplyCSwap)(
void *, int, int, int) =
nullptr;
671 void (*fApplyCCX)(
void *, int, int, int) =
nullptr;
673 void (*fApplySdg)(
void *, int) =
nullptr;
674 void (*fApplyTdg)(
void *, int) =
nullptr;
675 void (*fApplySx)(
void *, int) =
nullptr;
676 void (*fApplySxDg)(
void *, int) =
nullptr;
677 void (*fApplyK)(
void *, int) =
nullptr;
679 void (*fApplyU)(
void *, int, double, double, double, double) =
nullptr;
680 void (*fApplyCU)(
void *, int, int, double, double, double, double) =
nullptr;
681 void (*fApplyCP)(
void *, int, int, double) =
nullptr;
682 void (*fApplyCSx)(
void *, int, int) =
nullptr;
683 void (*fApplyCSxDg)(
void *, int, int) =
nullptr;
684 int (*fGetAmplitudes)(
void *,
void *,
unsigned long long int) =
nullptr;
685 int (*fGetAmplitude)(
void *,
long long int,
void *,
unsigned long long int) =
nullptr;
686 int (*fIsDoublePrecision)() =
nullptr;
unsigned long int CreateSimulator(int nrQubits)
void ApplyCRz(void *sim, int control, int target, double angle)
bool IsDoublePrecision() const
QuestLibSim(QuestLibSim &&)=default
void ApplyRy(void *sim, int qubit, double angle)
void ApplyU(void *sim, int qubit, double theta, double phi, double lambda, double gamma)
void ApplyCS(void *sim, int control, int target)
void ApplyCZ(void *sim, int control, int target)
void ApplyCH(void *sim, int control, int target)
bool GetAmplitude(void *sim, long long int index, std::complex< double > &litude) const
void ApplyCRx(void *sim, int control, int target, double angle)
void ApplyTdg(void *sim, int qubit)
int Measure(void *sim, int qubit)
void ApplyCP(void *sim, int control, int target, double angle)
void ApplyCX(void *sim, int control, int target)
void ApplyX(void *sim, int qubit)
void ApplyRz(void *sim, int qubit, double angle)
void DestroySimulator(unsigned long int simHandle)
void ApplyCSxDg(void *sim, int control, int target)
void ApplyK(void *sim, int qubit)
void ApplyH(void *sim, int qubit)
void ApplySx(void *sim, int qubit)
double GetQubitProbability1(void *sim, int qubit) const
long long int MeasureQubits(void *sim, int *qubits, int numQubits)
void ApplyCY(void *sim, int control, int target)
void ApplyCSwap(void *sim, int control, int qubit1, int qubit2)
void ApplyS(void *sim, int qubit)
void ApplyCU(void *sim, int control, int target, double theta, double phi, double lambda, double gamma)
void ApplyZ(void *sim, int qubit)
int GetNumQubits(void *sim) const
double GetQubitProbability0(void *sim, int qubit) const
void ApplySdg(void *sim, int qubit)
double GetExpectationValue(void *sim, const char *pauliStr) const
void ApplySxDg(void *sim, int qubit)
QuestLibSim(const QuestLibSim &)=delete
bool Init(const char *libName) noexcept override
void ApplySwap(void *sim, int qubit1, int qubit2)
void ApplyCSx(void *sim, int control, int target)
unsigned long int CloneSimulator(void *sim)
void ApplyCCX(void *sim, int control1, int control2, int target)
QuestLibSim & operator=(QuestLibSim &&)=default
double GetOutcomeProbability(void *sim, long long int outcome) const
static void CheckFunction(void *func, int line) noexcept
void ApplyP(void *sim, int qubit, double angle)
bool GetAmplitudes(void *sim, std::vector< std::complex< double > > &litudes) const
void ApplyT(void *sim, int qubit)
QuestLibSim & operator=(const QuestLibSim &)=delete
void ApplyRx(void *sim, int qubit, double angle)
void * GetSimulator(unsigned long int simHandle)
void ApplyY(void *sim, int qubit)
void ApplyCT(void *sim, int control, int target)
void ApplyCRy(void *sim, int control, int target, double angle)
void * GetFunction(const char *funcName) noexcept
virtual bool Init(const char *libName) noexcept
Bounded online routing based on current physical bond dimensions.