Qrack  10.0
General classical-emulating-quantum development framework
qinterface.hpp
Go to the documentation of this file.
1 //
3 // (C) Daniel Strano and the Qrack contributors 2017-2023. All rights reserved.
4 //
5 // This is a multithreaded, universal quantum register simulation, allowing
6 // (nonphysical) register cloning and direct measurement of probability and
7 // phase, to leverage what advantages classical emulation of qubits can have.
8 //
9 // Licensed under the GNU Lesser General Public License V3.
10 // See LICENSE.md in the project root or https://www.gnu.org/licenses/lgpl-3.0.en.html
11 // for details.
12 
13 #pragma once
14 
15 #include "common/parallel_for.hpp"
16 #include "common/pauli.hpp"
17 #include "common/rdrandwrapper.hpp"
18 #include "hamiltonian.hpp"
19 
20 #include <map>
21 #include <random>
22 
23 #if ENABLE_UINT128
24 #include <ostream>
25 #endif
26 
27 namespace Qrack {
28 
29 class QInterface;
30 typedef std::shared_ptr<QInterface> QInterfacePtr;
31 
38 
43 
48 
53 
58 
63 
68 
73 
78 
83 
92 
98 
103 
108 
113 
114 #if ENABLE_OPENCL || ENABLE_CUDA
116 #if ENABLE_OPENCL
118 #else
120 #endif
121 #else
123 
125 #endif
126 
128 
130 
132 };
133 
141 class QInterface : public ParallelFor {
142 protected:
145  bool useRDRAND;
147  uint32_t randomSeed;
151  std::uniform_real_distribution<real1_s> rand_distribution;
152  std::shared_ptr<RdRandom> hardware_rand_generator;
153 
154  // Compilers have difficulty figuring out types and overloading if the "norm" handle is passed to std::transform. If
155  // you need a safe pointer to norm(), try this:
156  static inline real1_f normHelper(const complex& c) { return (real1_f)norm(c); }
157 
158  static inline real1_f clampProb(real1_f toClamp)
159  {
160  if (toClamp < ZERO_R1_F) {
161  toClamp = ZERO_R1_F;
162  }
163  if (toClamp > ONE_R1_F) {
164  toClamp = ONE_R1_F;
165  }
166  return toClamp;
167  }
168 
170  {
171  if (randGlobalPhase) {
172  real1_f angle = Rand() * 2 * (real1_f)PI_R1;
173  return complex((real1)cos(angle), (real1)sin(angle));
174  } else {
175  return ONE_CMPLX;
176  }
177  }
178 
179  template <typename Fn> void MACWrapper(const std::vector<bitLenInt>& controls, Fn fn)
180  {
181  bitCapInt xMask = ZERO_BCI;
182  for (const bitLenInt& control : controls) {
183  bi_or_ip(&xMask, pow2(control));
184  }
185 
186  XMask(xMask);
187  fn(controls);
188  XMask(xMask);
189  }
190 
191  virtual bitCapInt SampleClone(const std::vector<bitCapInt>& qPowers)
192  {
193  QInterfacePtr clone = Clone();
194 
195  const bitCapInt rawSample = clone->MAll();
196  bitCapInt sample = ZERO_BCI;
197  for (size_t i = 0U; i < qPowers.size(); ++i) {
198  if (bi_compare_0(rawSample & qPowers[i]) != 0) {
199  bi_or_ip(&sample, pow2(i));
200  }
201  }
202 
203  return sample;
204  }
205 
206  virtual real1_f ExpVarUnitaryAll(bool isExp, const std::vector<bitLenInt>& bits,
207  const std::vector<std::shared_ptr<complex>>& basisOps, std::vector<real1_f> eigenVals = {});
208  virtual real1_f ExpVarUnitaryAll(bool isExp, const std::vector<bitLenInt>& bits,
209  const std::vector<real1_f>& basisOps, std::vector<real1_f> eigenVals = {});
210  virtual real1_f ExpVarBitsAll(bool isExp, const std::vector<bitLenInt>& bits, const bitCapInt& offset = ZERO_BCI)
211  {
212  std::vector<bitCapInt> perms;
213  perms.reserve(bits.size() << 1U);
214  for (size_t i = 0U; i < bits.size(); ++i) {
215  perms.push_back(ZERO_BCI);
216  perms.push_back(pow2(i));
217  }
218 
219  return isExp ? ExpectationBitsFactorized(bits, perms, offset) : VarianceBitsFactorized(bits, perms, offset);
220  }
221 
222  virtual void Copy(QInterfacePtr orig)
223  {
224  orig->Finish();
225  doNormalize = orig->doNormalize;
226  randGlobalPhase = orig->randGlobalPhase;
227  useRDRAND = orig->useRDRAND;
228  qubitCount = orig->qubitCount;
229  randomSeed = orig->randomSeed;
230  amplitudeFloor = orig->amplitudeFloor;
231  maxQPower = orig->maxQPower;
232  rand_generator = orig->rand_generator;
233  rand_distribution = orig->rand_distribution;
234  hardware_rand_generator = orig->hardware_rand_generator;
235  }
236 
237 public:
238  QInterface(bitLenInt n, qrack_rand_gen_ptr rgp = nullptr, bool doNorm = false, bool useHardwareRNG = true,
239  bool randomGlobalPhase = true, real1_f norm_thresh = REAL1_EPSILON);
240 
243  : doNormalize(false)
244  , randGlobalPhase(true)
245  , useRDRAND(true)
246  , qubitCount(0U)
247  , randomSeed(0)
249  , maxQPower(ONE_BCI)
250  , rand_distribution(0.0, 1.0)
251  , hardware_rand_generator{ nullptr }
252  {
253  // Intentionally left blank
254  }
255 
256  virtual ~QInterface()
257  {
258  // Virtual destructor for inheritance
259  }
260 
261  void SetRandomSeed(uint32_t seed)
262  {
263  if (!!rand_generator) {
264  rand_generator->seed(seed);
265  }
266  }
267 
268  virtual void SetQubitCount(bitLenInt qb)
269  {
270  qubitCount = qb;
272  }
273 
275  virtual void SetConcurrency(uint32_t threadsPerEngine) { SetConcurrencyLevel(threadsPerEngine); }
276 
278  virtual bitLenInt GetQubitCount() { return qubitCount; }
279 
281  virtual bitCapInt GetMaxQPower() { return maxQPower; }
282 
284  virtual bitCapInt GetAmplitudeCount() { return maxQPower; }
285 
286  virtual bool GetIsArbitraryGlobalPhase() { return randGlobalPhase; }
287 
290  {
291  if (!!hardware_rand_generator) {
292  return hardware_rand_generator->Next();
293  } else {
295  }
296  }
297 
302  virtual void LossySaveStateVector(std::string f, int p = 6, int b = 4);
307  virtual void LossyLoadStateVector(std::string f);
308 
313  virtual void SetQuantumState(const complex* inputState) = 0;
314 
319  virtual void GetQuantumState(complex* outputState) = 0;
320 
325  virtual void GetReducedDensityMatrix(const std::vector<bitLenInt>& qubits, complex* outputState);
326 
329  virtual void GetProbs(real1* outputProbs) = 0;
330 
335  virtual complex GetAmplitude(const bitCapInt& perm) = 0;
336 
341  virtual void SetAmplitude(const bitCapInt& perm, const complex& amp) = 0;
342 
344  virtual void SetPermutation(const bitCapInt& perm, const complex& phaseFac = CMPLX_DEFAULT_ARG);
345 
382  virtual bitLenInt Compose(QInterfacePtr toCopy) { return Compose(toCopy, qubitCount); }
387  virtual bitLenInt ComposeNoClone(QInterfacePtr toCopy) { return Compose(toCopy); }
391  virtual std::map<QInterfacePtr, bitLenInt> Compose(std::vector<QInterfacePtr> toCopy);
395  virtual bitLenInt Compose(QInterfacePtr toCopy, bitLenInt start);
396 
434  virtual void Decompose(bitLenInt start, QInterfacePtr dest) = 0;
435 
439  virtual QInterfacePtr Decompose(bitLenInt start, bitLenInt length) = 0;
440 
478  virtual void Dispose(bitLenInt start, bitLenInt length) = 0;
479 
483  virtual void Dispose(bitLenInt start, bitLenInt length, const bitCapInt& disposedPerm) = 0;
484 
488  virtual bitLenInt Allocate(bitLenInt length) { return Allocate(qubitCount, length); }
489 
493  virtual bitLenInt Allocate(bitLenInt start, bitLenInt length) = 0;
494 
503  virtual void Mtrx(const complex mtrx[4U], bitLenInt qubit) = 0;
504 
508  virtual void MCMtrx(const std::vector<bitLenInt>& controls, const complex mtrx[4U], bitLenInt target) = 0;
509 
513  virtual void MACMtrx(const std::vector<bitLenInt>& controls, const complex mtrx[4U], bitLenInt target)
514  {
515  if (IS_NORM_0(mtrx[1U]) && IS_NORM_0(mtrx[2U])) {
516  MACPhase(controls, mtrx[0U], mtrx[3U], target);
517  } else if (IS_NORM_0(mtrx[0U]) && IS_NORM_0(mtrx[3U])) {
518  MACInvert(controls, mtrx[1U], mtrx[2U], target);
519  } else {
520  MACWrapper(controls, [this, mtrx, target](const std::vector<bitLenInt>& lc) { MCMtrx(lc, mtrx, target); });
521  }
522  }
523 
528  virtual void UCMtrx(
529  const std::vector<bitLenInt>& controls, const complex* mtrx, bitLenInt target, const bitCapInt& controlPerm);
530 
534  virtual void Phase(const complex& topLeft, const complex& bottomRight, bitLenInt qubit)
535  {
536  if ((randGlobalPhase || IS_NORM_0(ONE_CMPLX - topLeft)) && IS_NORM_0(topLeft - bottomRight)) {
537  return;
538  }
539 
540  const complex mtrx[4U]{ topLeft, ZERO_CMPLX, ZERO_CMPLX, bottomRight };
541  Mtrx(mtrx, qubit);
542  }
543 
547  virtual void Invert(const complex& topRight, const complex& bottomLeft, bitLenInt qubit)
548  {
549  const complex mtrx[4U]{ ZERO_CMPLX, topRight, bottomLeft, ZERO_CMPLX };
550  Mtrx(mtrx, qubit);
551  }
552 
556  virtual void MCPhase(
557  const std::vector<bitLenInt>& controls, const complex& topLeft, const complex& bottomRight, bitLenInt target)
558  {
559  if (IS_NORM_0(ONE_CMPLX - topLeft) && IS_NORM_0(ONE_CMPLX - bottomRight)) {
560  return;
561  }
562 
563  const complex mtrx[4U]{ topLeft, ZERO_CMPLX, ZERO_CMPLX, bottomRight };
564  MCMtrx(controls, mtrx, target);
565  }
566 
571  virtual void MCInvert(
572  const std::vector<bitLenInt>& controls, const complex& topRight, const complex& bottomLeft, bitLenInt target)
573  {
574  const complex mtrx[4U]{ ZERO_CMPLX, topRight, bottomLeft, ZERO_CMPLX };
575  MCMtrx(controls, mtrx, target);
576  }
577 
581  virtual void MACPhase(
582  const std::vector<bitLenInt>& controls, const complex& topLeft, const complex& bottomRight, bitLenInt target)
583  {
584  if (IS_NORM_0(ONE_CMPLX - topLeft) && IS_NORM_0(ONE_CMPLX - bottomRight)) {
585  return;
586  }
587 
588  MACWrapper(controls, [this, topLeft, bottomRight, target](const std::vector<bitLenInt>& lc) {
589  MCPhase(lc, topLeft, bottomRight, target);
590  });
591  }
592 
597  virtual void MACInvert(
598  const std::vector<bitLenInt>& controls, const complex& topRight, const complex& bottomLeft, bitLenInt target)
599  {
600  MACWrapper(controls, [this, topRight, bottomLeft, target](const std::vector<bitLenInt>& lc) {
601  MCInvert(lc, topRight, bottomLeft, target);
602  });
603  }
604 
609  virtual void UCPhase(const std::vector<bitLenInt>& controls, const complex& topLeft, const complex& bottomRight,
610  bitLenInt target, const bitCapInt& perm)
611  {
612  if (IS_NORM_0(ONE_CMPLX - topLeft) && IS_NORM_0(ONE_CMPLX - bottomRight)) {
613  return;
614  }
615 
616  const complex mtrx[4U]{ topLeft, ZERO_CMPLX, ZERO_CMPLX, bottomRight };
617  UCMtrx(controls, mtrx, target, perm);
618  }
619 
624  virtual void UCInvert(const std::vector<bitLenInt>& controls, const complex& topRight, const complex& bottomLeft,
625  bitLenInt target, const bitCapInt& perm)
626  {
627  const complex mtrx[4U]{ ZERO_CMPLX, topRight, bottomLeft, ZERO_CMPLX };
628  UCMtrx(controls, mtrx, target, perm);
629  }
630 
646  const std::vector<bitLenInt>& controls, bitLenInt qubit, const complex* mtrxs)
647  {
648  UniformlyControlledSingleBit(controls, qubit, mtrxs, std::vector<bitCapInt>(), ZERO_BCI);
649  }
650  virtual void UniformlyControlledSingleBit(const std::vector<bitLenInt>& controls, bitLenInt qubit,
651  const complex* mtrxs, const std::vector<bitCapInt>& mtrxSkipPowers, const bitCapInt& mtrxSkipValueMask);
652 
668  virtual void TimeEvolve(Hamiltonian h, real1_f timeDiff);
669 
673  virtual void CSwap(const std::vector<bitLenInt>& controls, bitLenInt qubit1, bitLenInt qubit2);
674 
678  virtual void AntiCSwap(const std::vector<bitLenInt>& controls, bitLenInt qubit1, bitLenInt qubit2);
679 
683  virtual void CSqrtSwap(const std::vector<bitLenInt>& controls, bitLenInt qubit1, bitLenInt qubit2);
684 
688  virtual void AntiCSqrtSwap(const std::vector<bitLenInt>& controls, bitLenInt qubit1, bitLenInt qubit2);
689 
693  virtual void CISqrtSwap(const std::vector<bitLenInt>& controls, bitLenInt qubit1, bitLenInt qubit2);
694 
698  virtual void AntiCISqrtSwap(const std::vector<bitLenInt>& controls, bitLenInt qubit1, bitLenInt qubit2);
699 
705  virtual void CCNOT(bitLenInt control1, bitLenInt control2, bitLenInt target)
706  {
707  const std::vector<bitLenInt> controls{ control1, control2 };
708  MCInvert(controls, ONE_CMPLX, ONE_CMPLX, target);
709  }
710 
716  virtual void AntiCCNOT(bitLenInt control1, bitLenInt control2, bitLenInt target)
717  {
718  const std::vector<bitLenInt> controls{ control1, control2 };
719  MACInvert(controls, ONE_CMPLX, ONE_CMPLX, target);
720  }
721 
727  virtual void CNOT(bitLenInt control, bitLenInt target)
728  {
729  const std::vector<bitLenInt> controls{ control };
730  MCInvert(controls, ONE_CMPLX, ONE_CMPLX, target);
731  }
732 
738  virtual void AntiCNOT(bitLenInt control, bitLenInt target)
739  {
740  const std::vector<bitLenInt> controls{ control };
741  MACInvert(controls, ONE_CMPLX, ONE_CMPLX, target);
742  }
743 
750  virtual void CY(bitLenInt control, bitLenInt target)
751  {
752  const std::vector<bitLenInt> controls{ control };
753  MCInvert(controls, -I_CMPLX, I_CMPLX, target);
754  }
755 
761  virtual void AntiCY(bitLenInt control, bitLenInt target)
762  {
763  const std::vector<bitLenInt> controls{ control };
764  MACInvert(controls, -I_CMPLX, I_CMPLX, target);
765  }
766 
773  virtual void CCY(bitLenInt control1, bitLenInt control2, bitLenInt target)
774  {
775  const std::vector<bitLenInt> controls{ control1, control2 };
776  MCInvert(controls, -I_CMPLX, I_CMPLX, target);
777  }
778 
784  virtual void AntiCCY(bitLenInt control1, bitLenInt control2, bitLenInt target)
785  {
786  const std::vector<bitLenInt> controls{ control1, control2 };
787  MACInvert(controls, -I_CMPLX, I_CMPLX, target);
788  }
789 
796  virtual void CZ(bitLenInt control, bitLenInt target)
797  {
798  const std::vector<bitLenInt> controls{ control };
799  MCPhase(controls, ONE_CMPLX, -ONE_CMPLX, target);
800  }
801 
807  virtual void AntiCZ(bitLenInt control, bitLenInt target)
808  {
809  const std::vector<bitLenInt> controls{ control };
810  MACPhase(controls, ONE_CMPLX, -ONE_CMPLX, target);
811  }
812 
819  virtual void CCZ(bitLenInt control1, bitLenInt control2, bitLenInt target)
820  {
821  const std::vector<bitLenInt> controls{ control1, control2 };
822  MCPhase(controls, ONE_CMPLX, -ONE_CMPLX, target);
823  }
824 
830  virtual void AntiCCZ(bitLenInt control1, bitLenInt control2, bitLenInt target)
831  {
832  const std::vector<bitLenInt> controls{ control1, control2 };
833  MACPhase(controls, ONE_CMPLX, -ONE_CMPLX, target);
834  }
835 
842  virtual void U(bitLenInt target, real1_f theta, real1_f phi, real1_f lambda);
843 
849  virtual void U2(bitLenInt target, real1_f phi, real1_f lambda) { U(target, (real1_f)(M_PI / 2), phi, lambda); }
850 
856  virtual void IU2(bitLenInt target, real1_f phi, real1_f lambda)
857  {
858  U(target, (real1_f)(M_PI / 2), (real1_f)(-lambda - PI_R1), (real1_f)(-phi + PI_R1));
859  }
860 
866  virtual void AI(bitLenInt target, real1_f azimuth, real1_f inclination);
867 
873  virtual void IAI(bitLenInt target, real1_f azimuth, real1_f inclination);
874 
880  virtual void CAI(bitLenInt control, bitLenInt target, real1_f azimuth, real1_f inclination);
881 
888  virtual void AntiCAI(bitLenInt control, bitLenInt target, real1_f azimuth, real1_f inclination);
889 
896  virtual void CIAI(bitLenInt control, bitLenInt target, real1_f azimuth, real1_f inclination);
897 
904  virtual void AntiCIAI(bitLenInt control, bitLenInt target, real1_f azimuth, real1_f inclination);
905 
913  virtual void CU(const std::vector<bitLenInt>& controls, bitLenInt target, real1_f theta, real1_f phi,
914  real1_f lambda, real1_f gamma = ZERO_R1_F);
915 
923  virtual void AntiCU(const std::vector<bitLenInt>& controls, bitLenInt target, real1_f theta, real1_f phi,
924  real1_f lambda, real1_f gamma = ZERO_R1_F);
925 
931  virtual void H(bitLenInt qubit)
932  {
936  Mtrx(mtrx, qubit);
937  }
938 
944  virtual void SqrtH(bitLenInt qubit)
945  {
946  QRACK_CONST complex m00 =
947  complex((real1)((ONE_R1 + SQRT2_R1) / (2 * SQRT2_R1)), (real1)((-ONE_R1 + SQRT2_R1) / (2 * SQRT2_R1)));
948  QRACK_CONST complex m01 = complex((real1)(SQRT1_2_R1 / 2), (real1)(-SQRT1_2_R1 / 2));
949  QRACK_CONST complex m10 = m01;
950  QRACK_CONST complex m11 =
951  complex((real1)((-ONE_R1 + SQRT2_R1) / (2 * SQRT2_R1)), (real1)((ONE_R1 + SQRT2_R1) / (2 * SQRT2_R1)));
952  QRACK_CONST complex mtrx[4]{ m00, m01, m10, m11 };
953  Mtrx(mtrx, qubit);
954  }
955 
961  virtual void SH(bitLenInt qubit)
962  {
965  QRACK_CONST complex C_I_SQRT1_2_NEG = complex(ZERO_R1, -SQRT1_2_R1);
966  QRACK_CONST complex mtrx[4]{ C_SQRT1_2, C_SQRT1_2, C_I_SQRT1_2, C_I_SQRT1_2_NEG };
967  Mtrx(mtrx, qubit);
968  }
969 
975  virtual void HIS(bitLenInt qubit)
976  {
979  QRACK_CONST complex C_I_SQRT1_2_NEG = complex(ZERO_R1, -SQRT1_2_R1);
980  QRACK_CONST complex mtrx[4]{ C_SQRT1_2, C_I_SQRT1_2_NEG, C_SQRT1_2, C_I_SQRT1_2 };
981  Mtrx(mtrx, qubit);
982  }
983 
1031  virtual bool M(bitLenInt qubit) { return ForceM(qubit, false, false); };
1032 
1038  virtual bool ForceM(bitLenInt qubit, bool result, bool doForce = true, bool doApply = true) = 0;
1039 
1045  virtual void S(bitLenInt qubit) { Phase(ONE_CMPLX, I_CMPLX, qubit); }
1046 
1052  virtual void IS(bitLenInt qubit) { Phase(ONE_CMPLX, -I_CMPLX, qubit); }
1053 
1059  virtual void T(bitLenInt qubit) { Phase(ONE_CMPLX, complex(SQRT1_2_R1, SQRT1_2_R1), qubit); }
1060 
1066  virtual void IT(bitLenInt qubit) { Phase(ONE_CMPLX, complex(SQRT1_2_R1, -SQRT1_2_R1), qubit); }
1067 
1073  virtual void PhaseRootN(bitLenInt n, bitLenInt qubit)
1074  {
1075  if (n == 0) {
1076  return;
1077  }
1078 
1079  Phase(ONE_CMPLX, pow(-ONE_CMPLX, (real1)(ONE_R1 / pow2Ocl(n - 1U))), qubit);
1080  }
1081 
1087  virtual void IPhaseRootN(bitLenInt n, bitLenInt qubit)
1088  {
1089  if (n == 0) {
1090  return;
1091  }
1092 
1093  Phase(ONE_CMPLX, pow(-ONE_CMPLX, (real1)(-ONE_R1 / pow2Ocl(n - 1U))), qubit);
1094  }
1095 
1101  virtual void PhaseRootNMask(bitLenInt n, const bitCapInt& mask);
1102 
1108  virtual void PhaseParity(real1_f radians, const bitCapInt& mask);
1109 
1116  virtual void X(bitLenInt qubit) { Invert(ONE_CMPLX, ONE_CMPLX, qubit); }
1117 
1124  virtual void XMask(const bitCapInt& mask);
1125 
1133  virtual void Y(bitLenInt qubit) { Invert(-I_CMPLX, I_CMPLX, qubit); }
1134 
1141  virtual void YMask(const bitCapInt& mask);
1142 
1149  virtual void Z(bitLenInt qubit) { Phase(ONE_CMPLX, -ONE_CMPLX, qubit); }
1150 
1157  virtual void ZMask(const bitCapInt& mask);
1158 
1165  virtual void SqrtX(bitLenInt qubit)
1166  {
1167  QRACK_CONST complex ONE_PLUS_I_DIV_2 = complex((real1)(ONE_R1 / 2), (real1)(ONE_R1 / 2));
1168  QRACK_CONST complex ONE_MINUS_I_DIV_2 = complex((real1)(ONE_R1 / 2), (real1)(-ONE_R1 / 2));
1169  QRACK_CONST complex mtrx[4]{ ONE_PLUS_I_DIV_2, ONE_MINUS_I_DIV_2, ONE_MINUS_I_DIV_2, ONE_PLUS_I_DIV_2 };
1170  Mtrx(mtrx, qubit);
1171  }
1172 
1179  virtual void ISqrtX(bitLenInt qubit)
1180  {
1181  QRACK_CONST complex ONE_PLUS_I_DIV_2 = complex((real1)(ONE_R1 / 2), (real1)(ONE_R1 / 2));
1182  QRACK_CONST complex ONE_MINUS_I_DIV_2 = complex((real1)(ONE_R1 / 2), (real1)(-ONE_R1 / 2));
1183  QRACK_CONST complex mtrx[4]{ ONE_MINUS_I_DIV_2, ONE_PLUS_I_DIV_2, ONE_PLUS_I_DIV_2, ONE_MINUS_I_DIV_2 };
1184  Mtrx(mtrx, qubit);
1185  }
1186 
1194  virtual void SqrtY(bitLenInt qubit)
1195  {
1196  QRACK_CONST complex ONE_PLUS_I_DIV_2 = complex((real1)(ONE_R1 / 2), (real1)(ONE_R1 / 2));
1197  QRACK_CONST complex ONE_PLUS_I_DIV_2_NEG = complex((real1)(-ONE_R1 / 2), (real1)(-ONE_R1 / 2));
1198  QRACK_CONST complex mtrx[4]{ ONE_PLUS_I_DIV_2, ONE_PLUS_I_DIV_2_NEG, ONE_PLUS_I_DIV_2, ONE_PLUS_I_DIV_2 };
1199  Mtrx(mtrx, qubit);
1200  }
1201 
1209  virtual void ISqrtY(bitLenInt qubit)
1210  {
1211  QRACK_CONST complex ONE_MINUS_I_DIV_2 = complex((real1)(ONE_R1 / 2), (real1)(-ONE_R1 / 2));
1212  QRACK_CONST complex ONE_MINUS_I_DIV_2_NEG = complex((real1)(-ONE_R1 / 2), (real1)(ONE_R1 / 2));
1213  QRACK_CONST complex mtrx[4]{ ONE_MINUS_I_DIV_2, ONE_MINUS_I_DIV_2, ONE_MINUS_I_DIV_2_NEG, ONE_MINUS_I_DIV_2 };
1214  Mtrx(mtrx, qubit);
1215  }
1216 
1222  virtual void SqrtW(bitLenInt qubit)
1223  {
1225  QRACK_CONST complex m01 = complex((real1)(-ONE_R1 / 2), (real1)(-ONE_R1 / 2));
1226  QRACK_CONST complex m10 = complex((real1)(ONE_R1 / 2), (real1)(-ONE_R1 / 2));
1227  QRACK_CONST complex mtrx[4]{ diag, m01, m10, diag };
1228  Mtrx(mtrx, qubit);
1229  }
1230 
1236  virtual void ISqrtW(bitLenInt qubit)
1237  {
1239  QRACK_CONST complex m01 = complex((real1)(ONE_R1 / 2), (real1)(ONE_R1 / 2));
1240  QRACK_CONST complex m10 = complex((real1)(-ONE_R1 / 2), (real1)(ONE_R1 / 2));
1241  QRACK_CONST complex mtrx[4]{ diag, m01, m10, diag };
1242  Mtrx(mtrx, qubit);
1243  }
1244 
1251  virtual void CH(bitLenInt control, bitLenInt target)
1252  {
1253  const std::vector<bitLenInt> controls{ control };
1257  MCMtrx(controls, mtrx, target);
1258  }
1259 
1266  virtual void AntiCH(bitLenInt control, bitLenInt target)
1267  {
1268  const std::vector<bitLenInt> controls{ control };
1272  MACMtrx(controls, mtrx, target);
1273  }
1274 
1281  virtual void CS(bitLenInt control, bitLenInt target)
1282  {
1283  const std::vector<bitLenInt> controls{ control };
1284  MCPhase(controls, ONE_CMPLX, I_CMPLX, target);
1285  }
1286 
1293  virtual void AntiCS(bitLenInt control, bitLenInt target)
1294  {
1295  const std::vector<bitLenInt> controls{ control };
1296  MACPhase(controls, ONE_CMPLX, I_CMPLX, target);
1297  }
1298 
1305  virtual void CIS(bitLenInt control, bitLenInt target)
1306  {
1307  const std::vector<bitLenInt> controls{ control };
1308  MCPhase(controls, ONE_CMPLX, -I_CMPLX, target);
1309  }
1310 
1317  virtual void AntiCIS(bitLenInt control, bitLenInt target)
1318  {
1319  const std::vector<bitLenInt> controls{ control };
1320  MACPhase(controls, ONE_CMPLX, -I_CMPLX, target);
1321  }
1322 
1329  virtual void CT(bitLenInt control, bitLenInt target)
1330  {
1331  const std::vector<bitLenInt> controls{ control };
1332  MCPhase(controls, ONE_CMPLX, complex(SQRT1_2_R1, SQRT1_2_R1), target);
1333  }
1334 
1341  virtual void CIT(bitLenInt control, bitLenInt target)
1342  {
1343  const std::vector<bitLenInt> controls{ control };
1344  MCPhase(controls, ONE_CMPLX, complex(SQRT1_2_R1, -SQRT1_2_R1), target);
1345  }
1346 
1353  virtual void CPhaseRootN(bitLenInt n, bitLenInt control, bitLenInt target)
1354  {
1355  if (n == 0) {
1356  return;
1357  }
1358 
1359  const std::vector<bitLenInt> controls{ control };
1360  MCPhase(controls, ONE_CMPLX, pow(-ONE_CMPLX, (real1)(ONE_R1 / pow2Ocl(n - 1U))), target);
1361  }
1362 
1369  virtual void AntiCPhaseRootN(bitLenInt n, bitLenInt control, bitLenInt target)
1370  {
1371  if (n == 0) {
1372  return;
1373  }
1374 
1375  const std::vector<bitLenInt> controls{ control };
1376  MACPhase(controls, ONE_CMPLX, pow(-ONE_CMPLX, (real1)(ONE_R1 / pow2Ocl(n - 1U))), target);
1377  }
1378 
1385  virtual void CIPhaseRootN(bitLenInt n, bitLenInt control, bitLenInt target)
1386  {
1387  if (n == 0) {
1388  return;
1389  }
1390 
1391  const std::vector<bitLenInt> controls{ control };
1392  MCPhase(controls, ONE_CMPLX, pow(-ONE_CMPLX, (real1)(-ONE_R1 / pow2Ocl(n - 1U))), target);
1393  }
1394 
1401  virtual void AntiCIPhaseRootN(bitLenInt n, bitLenInt control, bitLenInt target)
1402  {
1403  if (n == 0) {
1404  return;
1405  }
1406 
1407  const std::vector<bitLenInt> controls{ control };
1408  MACPhase(controls, ONE_CMPLX, pow(-ONE_CMPLX, (real1)(-ONE_R1 / pow2Ocl(n - 1U))), target);
1409  }
1410 
1427  virtual void AND(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt outputBit);
1428 
1434  virtual void OR(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt outputBit);
1435 
1441  virtual void XOR(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt outputBit);
1442 
1447  virtual void CLAND(bitLenInt inputQBit, bool inputClassicalBit, bitLenInt outputBit);
1448 
1453  virtual void CLOR(bitLenInt inputQBit, bool inputClassicalBit, bitLenInt outputBit);
1454 
1459  virtual void CLXOR(bitLenInt inputQBit, bool inputClassicalBit, bitLenInt outputBit);
1460 
1466  virtual void NAND(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt outputBit);
1467 
1473  virtual void NOR(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt outputBit);
1474 
1480  virtual void XNOR(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt outputBit);
1481 
1486  virtual void CLNAND(bitLenInt inputQBit, bool inputClassicalBit, bitLenInt outputBit);
1487 
1492  virtual void CLNOR(bitLenInt inputQBit, bool inputClassicalBit, bitLenInt outputBit);
1493 
1498  virtual void CLXNOR(bitLenInt inputQBit, bool inputClassicalBit, bitLenInt outputBit);
1499 
1521  virtual void UniformlyControlledRY(const std::vector<bitLenInt>& controls, bitLenInt qubit, const real1* angles);
1522 
1533  virtual void UniformlyControlledRZ(const std::vector<bitLenInt>& controls, bitLenInt qubit, const real1* angles);
1534 
1540  virtual void RT(real1_f radians, bitLenInt qubit);
1541 
1547  virtual void RX(real1_f radians, bitLenInt qubit);
1548 
1554  virtual void RY(real1_f radians, bitLenInt qubit);
1555 
1561  virtual void RZ(real1_f radians, bitLenInt qubit);
1562 
1569  virtual void CRZ(real1_f radians, bitLenInt control, bitLenInt target);
1570 
1577  virtual void CRY(real1_f radians, bitLenInt control, bitLenInt target);
1578 
1579 #if ENABLE_ROT_API
1586  virtual void RTDyad(int numerator, int denomPower, bitLenInt qubit);
1587 
1593  virtual void RXDyad(int numerator, int denomPower, bitLenInt qubit);
1594 
1600  virtual void Exp(real1_f radians, bitLenInt qubit);
1601 
1607  virtual void Exp(
1608  const std::vector<bitLenInt>& controls, bitLenInt qubit, const complex mtrx[4U], bool antiCtrled = false);
1609 
1616  virtual void ExpDyad(int numerator, int denomPower, bitLenInt qubit);
1617 
1623  virtual void ExpX(real1_f radians, bitLenInt qubit);
1624 
1631  virtual void ExpXDyad(int numerator, int denomPower, bitLenInt qubit);
1632 
1638  virtual void ExpY(real1_f radians, bitLenInt qubit);
1639 
1646  virtual void ExpYDyad(int numerator, int denomPower, bitLenInt qubit);
1647 
1653  virtual void ExpZ(real1_f radians, bitLenInt qubit);
1654 
1661  virtual void ExpZDyad(int numerator, int denomPower, bitLenInt qubit);
1662 
1668  virtual void CRX(real1_f radians, bitLenInt control, bitLenInt target);
1669 
1675  virtual void CRXDyad(int numerator, int denomPower, bitLenInt control, bitLenInt target);
1676 
1682  virtual void RYDyad(int numerator, int denomPower, bitLenInt qubit);
1683 
1690  virtual void CRYDyad(int numerator, int denomPower, bitLenInt control, bitLenInt target);
1691 
1697  virtual void RZDyad(int numerator, int denomPower, bitLenInt qubit);
1698 
1705  virtual void CRZDyad(int numerator, int denomPower, bitLenInt control, bitLenInt target);
1706 
1714  virtual void CRT(real1_f radians, bitLenInt control, bitLenInt target);
1715 
1722  virtual void CRTDyad(int numerator, int denomPower, bitLenInt control, bitLenInt target);
1723 #endif
1724 
1737  virtual void H(bitLenInt start, bitLenInt length);
1738 
1740  virtual void X(bitLenInt start, bitLenInt length) { XMask(bitRegMask(start, length)); }
1741 
1742 #if ENABLE_REG_GATES
1743 
1745  virtual void U(bitLenInt start, bitLenInt length, real1_f theta, real1_f phi, real1_f lambda);
1746 
1748  virtual void U2(bitLenInt start, bitLenInt length, real1_f phi, real1_f lambda);
1749 
1751  virtual void SH(bitLenInt start, bitLenInt length);
1752 
1754  virtual void HIS(bitLenInt start, bitLenInt length);
1755 
1757  virtual void SqrtH(bitLenInt start, bitLenInt length);
1758 
1760  virtual void S(bitLenInt start, bitLenInt length);
1761 
1763  virtual void IS(bitLenInt start, bitLenInt length);
1764 
1766  virtual void T(bitLenInt start, bitLenInt length);
1767 
1769  virtual void IT(bitLenInt start, bitLenInt length);
1770 
1772  virtual void PhaseRootN(bitLenInt n, bitLenInt start, bitLenInt length);
1773 
1775  virtual void IPhaseRootN(bitLenInt n, bitLenInt start, bitLenInt length);
1776 
1778  virtual void Y(bitLenInt start, bitLenInt length);
1779 
1781  virtual void SqrtX(bitLenInt start, bitLenInt length);
1782 
1784  virtual void ISqrtX(bitLenInt start, bitLenInt length);
1785 
1787  virtual void SqrtY(bitLenInt start, bitLenInt length);
1788 
1790  virtual void ISqrtY(bitLenInt start, bitLenInt length);
1791 
1793  virtual void Z(bitLenInt start, bitLenInt length);
1794 
1796  virtual void CNOT(bitLenInt inputBits, bitLenInt targetBits, bitLenInt length);
1797 
1799  virtual void AntiCNOT(bitLenInt inputBits, bitLenInt targetBits, bitLenInt length);
1800 
1802  virtual void CCNOT(bitLenInt control1, bitLenInt control2, bitLenInt target, bitLenInt length);
1803 
1805  virtual void AntiCCNOT(bitLenInt control1, bitLenInt control2, bitLenInt target, bitLenInt length);
1806 
1808  virtual void CY(bitLenInt control, bitLenInt target, bitLenInt length);
1809 
1811  virtual void AntiCY(bitLenInt inputBits, bitLenInt targetBits, bitLenInt length);
1812 
1814  virtual void CCY(bitLenInt control1, bitLenInt control2, bitLenInt target, bitLenInt length);
1815 
1817  virtual void AntiCCY(bitLenInt control1, bitLenInt control2, bitLenInt target, bitLenInt length);
1818 
1820  virtual void CZ(bitLenInt control, bitLenInt target, bitLenInt length);
1821 
1823  virtual void AntiCZ(bitLenInt inputBits, bitLenInt targetBits, bitLenInt length);
1824 
1826  virtual void CCZ(bitLenInt control1, bitLenInt control2, bitLenInt target, bitLenInt length);
1827 
1829  virtual void AntiCCZ(bitLenInt control1, bitLenInt control2, bitLenInt target, bitLenInt length);
1830 
1832  virtual void Swap(bitLenInt start1, bitLenInt start2, bitLenInt length);
1833 
1835  virtual void ISwap(bitLenInt start1, bitLenInt start2, bitLenInt length);
1836 
1838  virtual void IISwap(bitLenInt start1, bitLenInt start2, bitLenInt length);
1839 
1841  virtual void SqrtSwap(bitLenInt start1, bitLenInt start2, bitLenInt length);
1842 
1844  virtual void ISqrtSwap(bitLenInt start1, bitLenInt start2, bitLenInt length);
1845 
1847  virtual void FSim(real1_f theta, real1_f phi, bitLenInt start1, bitLenInt start2, bitLenInt length);
1848 
1855  virtual void AND(bitLenInt inputStart1, bitLenInt inputStart2, bitLenInt outputStart, bitLenInt length);
1856 
1863  virtual void CLAND(bitLenInt qInputStart, const bitCapInt& classicalInput, bitLenInt outputStart, bitLenInt length);
1864 
1866  virtual void OR(bitLenInt inputStart1, bitLenInt inputStart2, bitLenInt outputStart, bitLenInt length);
1867 
1869  virtual void CLOR(bitLenInt qInputStart, const bitCapInt& classicalInput, bitLenInt outputStart, bitLenInt length);
1870 
1872  virtual void XOR(bitLenInt inputStart1, bitLenInt inputStart2, bitLenInt outputStart, bitLenInt length);
1873 
1875  virtual void CLXOR(bitLenInt qInputStart, const bitCapInt& classicalInput, bitLenInt outputStart, bitLenInt length);
1876 
1878  virtual void NAND(bitLenInt inputStart1, bitLenInt inputStart2, bitLenInt outputStart, bitLenInt length);
1879 
1881  virtual void CLNAND(
1882  bitLenInt qInputStart, const bitCapInt& classicalInput, bitLenInt outputStart, bitLenInt length);
1883 
1885  virtual void NOR(bitLenInt inputStart1, bitLenInt inputStart2, bitLenInt outputStart, bitLenInt length);
1886 
1888  virtual void CLNOR(bitLenInt qInputStart, const bitCapInt& classicalInput, bitLenInt outputStart, bitLenInt length);
1889 
1891  virtual void XNOR(bitLenInt inputStart1, bitLenInt inputStart2, bitLenInt outputStart, bitLenInt length);
1892 
1894  virtual void CLXNOR(
1895  bitLenInt qInputStart, const bitCapInt& classicalInput, bitLenInt outputStart, bitLenInt length);
1896 
1897 #if ENABLE_ROT_API
1903  virtual void RT(real1_f radians, bitLenInt start, bitLenInt length);
1904 
1911  virtual void RTDyad(int numerator, int denomPower, bitLenInt start, bitLenInt length);
1912 
1918  virtual void RX(real1_f radians, bitLenInt start, bitLenInt length);
1919 
1925  virtual void RXDyad(int numerator, int denomPower, bitLenInt start, bitLenInt length);
1926 
1932  virtual void CRX(real1_f radians, bitLenInt control, bitLenInt target, bitLenInt length);
1933 
1939  virtual void CRXDyad(int numerator, int denomPower, bitLenInt control, bitLenInt target, bitLenInt length);
1940 
1946  virtual void RY(real1_f radians, bitLenInt start, bitLenInt length);
1947 
1954  virtual void RYDyad(int numerator, int denomPower, bitLenInt start, bitLenInt length);
1955 
1962  virtual void CRY(real1_f radians, bitLenInt control, bitLenInt target, bitLenInt length);
1963 
1970  virtual void CRYDyad(int numerator, int denomPower, bitLenInt control, bitLenInt target, bitLenInt length);
1971 
1977  virtual void RZ(real1_f radians, bitLenInt start, bitLenInt length);
1978 
1984  virtual void RZDyad(int numerator, int denomPower, bitLenInt start, bitLenInt length);
1985 
1992  virtual void CRZ(real1_f radians, bitLenInt control, bitLenInt target, bitLenInt length);
1993 
2000  virtual void CRZDyad(int numerator, int denomPower, bitLenInt control, bitLenInt target, bitLenInt length);
2001 
2008  virtual void CRT(real1_f radians, bitLenInt control, bitLenInt target, bitLenInt length);
2009 
2016  virtual void CRTDyad(int numerator, int denomPower, bitLenInt control, bitLenInt target, bitLenInt length);
2017 
2023  virtual void Exp(real1_f radians, bitLenInt start, bitLenInt length);
2024 
2031  virtual void ExpDyad(int numerator, int denomPower, bitLenInt start, bitLenInt length);
2032 
2038  virtual void ExpX(real1_f radians, bitLenInt start, bitLenInt length);
2039 
2046  virtual void ExpXDyad(int numerator, int denomPower, bitLenInt start, bitLenInt length);
2047 
2053  virtual void ExpY(real1_f radians, bitLenInt start, bitLenInt length);
2054 
2061  virtual void ExpYDyad(int numerator, int denomPower, bitLenInt start, bitLenInt length);
2062 
2068  virtual void ExpZ(real1_f radians, bitLenInt start, bitLenInt length);
2069 
2076  virtual void ExpZDyad(int numerator, int denomPower, bitLenInt start, bitLenInt length);
2077 #endif
2078 
2085  virtual void CH(bitLenInt control, bitLenInt target, bitLenInt length);
2086 
2093  virtual void CS(bitLenInt control, bitLenInt target, bitLenInt length);
2094 
2101  virtual void CIS(bitLenInt control, bitLenInt target, bitLenInt length);
2102 
2109  virtual void CT(bitLenInt control, bitLenInt target, bitLenInt length);
2110 
2117  virtual void CIT(bitLenInt control, bitLenInt target, bitLenInt length);
2118 
2125  virtual void CPhaseRootN(bitLenInt n, bitLenInt control, bitLenInt target, bitLenInt length);
2126 
2133  virtual void CIPhaseRootN(bitLenInt n, bitLenInt control, bitLenInt target, bitLenInt length);
2134 
2136 #endif
2137 
2145  virtual void ROL(bitLenInt shift, bitLenInt start, bitLenInt length);
2146 
2148  virtual void ROR(bitLenInt shift, bitLenInt start, bitLenInt length);
2149 
2151  virtual void ASL(bitLenInt shift, bitLenInt start, bitLenInt length);
2152 
2154  virtual void ASR(bitLenInt shift, bitLenInt start, bitLenInt length);
2155 
2157  virtual void LSL(bitLenInt shift, bitLenInt start, bitLenInt length);
2158 
2160  virtual void LSR(bitLenInt shift, bitLenInt start, bitLenInt length);
2161 
2163  virtual void INC(const bitCapInt& toAdd, bitLenInt start, bitLenInt length);
2164 
2166  virtual void DEC(const bitCapInt& toSub, bitLenInt start, bitLenInt length)
2167  {
2168  const bitCapInt invToSub = pow2(length) - toSub;
2169  INC(invToSub, start, length);
2170  }
2171 
2173  virtual void INCDECC(const bitCapInt& toAdd, bitLenInt start, bitLenInt length, bitLenInt carryIndex);
2174 
2176  virtual void INCC(const bitCapInt& toAdd, bitLenInt start, bitLenInt length, bitLenInt carryIndex)
2177  {
2178  const bool hasCarry = M(carryIndex);
2179  if (hasCarry) {
2180  X(carryIndex);
2181  INCDECC(toAdd + 1U, start, length, carryIndex);
2182  } else {
2183  INCDECC(toAdd, start, length, carryIndex);
2184  }
2185  }
2186 
2188  virtual void DECC(const bitCapInt& toSub, bitLenInt start, bitLenInt length, bitLenInt carryIndex)
2189  {
2190  const bool hasCarry = M(carryIndex);
2191  bitCapInt invToSub = pow2(length) - toSub;
2192  if (hasCarry) {
2193  X(carryIndex);
2194  } else {
2195  --invToSub;
2196  }
2197 
2198  INCDECC(invToSub, start, length, carryIndex);
2199  }
2200 
2202  virtual void CINC(
2203  const bitCapInt& toAdd, bitLenInt inOutStart, bitLenInt length, const std::vector<bitLenInt>& controls);
2204 
2206  virtual void CDEC(
2207  const bitCapInt& toSub, bitLenInt inOutStart, bitLenInt length, const std::vector<bitLenInt>& controls)
2208  {
2209  const bitCapInt invToSub = pow2(length) - toSub;
2210  CINC(invToSub, inOutStart, length, controls);
2211  }
2212 
2214  virtual void INCS(const bitCapInt& toAdd, bitLenInt start, bitLenInt length, bitLenInt overflowIndex)
2215  {
2216  const bitCapInt signMask = pow2(length - 1U);
2217  INC(signMask, start, length);
2218  INCDECC(toAdd & ~signMask, start, length, overflowIndex);
2219  if (bi_compare_0(toAdd & signMask) == 0) {
2220  DEC(signMask, start, length);
2221  }
2222  }
2223 
2225  virtual void DECS(const bitCapInt& toSub, bitLenInt start, bitLenInt length, bitLenInt overflowIndex)
2226  {
2227  const bitCapInt invToSub = pow2(length) - toSub;
2228  INCS(invToSub, start, length, overflowIndex);
2229  }
2230 
2232  virtual void MULModNOut(
2233  const bitCapInt& toMul, const bitCapInt& modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length);
2234 
2236  virtual void IMULModNOut(
2237  const bitCapInt& toMul, const bitCapInt& modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length);
2238 
2240  virtual void CMULModNOut(const bitCapInt& toMul, const bitCapInt& modN, bitLenInt inStart, bitLenInt outStart,
2241  bitLenInt length, const std::vector<bitLenInt>& controls);
2242 
2244  virtual void CIMULModNOut(const bitCapInt& toMul, const bitCapInt& modN, bitLenInt inStart, bitLenInt outStart,
2245  bitLenInt length, const std::vector<bitLenInt>& controls);
2246 
2252  virtual void FullAdd(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt carryInSumOut, bitLenInt carryOut)
2253  {
2254  // See https://quantumcomputing.stackexchange.com/questions/1654/how-do-i-add-11-using-a-quantum-computer
2255 
2256  // Assume outputBit is in 0 state.
2257  CCNOT(inputBit1, inputBit2, carryOut);
2258  CNOT(inputBit1, inputBit2);
2259  CCNOT(inputBit2, carryInSumOut, carryOut);
2260  CNOT(inputBit2, carryInSumOut);
2261  CNOT(inputBit1, inputBit2);
2262  }
2263 
2269  virtual void IFullAdd(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt carryInSumOut, bitLenInt carryOut)
2270  {
2271  // See https://quantumcomputing.stackexchange.com/questions/1654/how-do-i-add-11-using-a-quantum-computer
2272  // Quantum computing is reversible! Simply perform the inverse operations in reverse order!
2273  // (CNOT and CCNOT are self-inverse.)
2274 
2275  // Assume outputBit is in 0 state.
2276  CNOT(inputBit1, inputBit2);
2277  CNOT(inputBit2, carryInSumOut);
2278  CCNOT(inputBit2, carryInSumOut, carryOut);
2279  CNOT(inputBit1, inputBit2);
2280  CCNOT(inputBit1, inputBit2, carryOut);
2281  }
2282 
2288  virtual void CFullAdd(const std::vector<bitLenInt>& controls, bitLenInt inputBit1, bitLenInt inputBit2,
2289  bitLenInt carryInSumOut, bitLenInt carryOut);
2290 
2296  virtual void CIFullAdd(const std::vector<bitLenInt>& controls, bitLenInt inputBit1, bitLenInt inputBit2,
2297  bitLenInt carryInSumOut, bitLenInt carryOut);
2298 
2304  virtual void ADC(bitLenInt input1, bitLenInt input2, bitLenInt output, bitLenInt length, bitLenInt carry);
2305 
2311  virtual void IADC(bitLenInt input1, bitLenInt input2, bitLenInt output, bitLenInt length, bitLenInt carry);
2312 
2318  virtual void CADC(const std::vector<bitLenInt>& controls, bitLenInt input1, bitLenInt input2, bitLenInt output,
2319  bitLenInt length, bitLenInt carry);
2320 
2326  virtual void CIADC(const std::vector<bitLenInt>& controls, bitLenInt input1, bitLenInt input2, bitLenInt output,
2327  bitLenInt length, bitLenInt carry);
2328 
2343  virtual void QFT(bitLenInt start, bitLenInt length, bool trySeparate = false);
2344 
2351  virtual void QFTR(const std::vector<bitLenInt>& qubits, bool trySeparate = false);
2352 
2359  virtual void IQFT(bitLenInt start, bitLenInt length, bool trySeparate = false);
2360 
2367  virtual void IQFTR(const std::vector<bitLenInt>& qubits, bool trySeparate = false);
2368 
2370  virtual void ZeroPhaseFlip(bitLenInt start, bitLenInt length);
2371 
2373  virtual void PhaseFlip() { Phase(-ONE_CMPLX, -ONE_CMPLX, 0); }
2374 
2376  virtual void SetReg(bitLenInt start, bitLenInt length, const bitCapInt& value);
2377 
2379  virtual bitCapInt MReg(bitLenInt start, bitLenInt length) { return ForceMReg(start, length, ZERO_BCI, false); }
2380 
2382  virtual bitCapInt MAll() { return MReg(0, qubitCount); }
2383 
2389  virtual bitCapInt ForceMReg(
2390  bitLenInt start, bitLenInt length, const bitCapInt& result, bool doForce = true, bool doApply = true);
2391 
2393  virtual bitCapInt M(const std::vector<bitLenInt>& bits) { return ForceM(bits, std::vector<bool>()); }
2394 
2396  virtual bitCapInt ForceM(const std::vector<bitLenInt>& bits, const std::vector<bool>& values, bool doApply = true);
2397 
2399  virtual void Swap(bitLenInt qubit1, bitLenInt qubit2);
2400 
2402  virtual void ISwap(bitLenInt qubit1, bitLenInt qubit2);
2403 
2405  virtual void IISwap(bitLenInt qubit1, bitLenInt qubit2);
2406 
2408  virtual void SqrtSwap(bitLenInt qubit1, bitLenInt qubit2);
2409 
2411  virtual void ISqrtSwap(bitLenInt qubit1, bitLenInt qubit2);
2412 
2414  virtual void FSim(real1_f theta, real1_f phi, bitLenInt qubit1, bitLenInt qubit2)
2415  {
2416  const std::vector<bitLenInt> controls{ qubit1 };
2417  const real1 sinTheta = (real1)sin(theta);
2418  const complex expIPhi = exp(complex(ZERO_R1, -(real1)phi));
2419 
2420  MCPhase(controls, ONE_CMPLX, expIPhi, qubit2);
2421 
2422  if ((2 * abs(sinTheta)) <= (FP_NORM_EPSILON * PI_R1)) {
2423  // ISWAP power is 0.
2424  return;
2425  }
2426 
2427  const real1 sinThetaDiffNeg = ONE_R1 + sinTheta;
2428  if ((2 * abs(sinThetaDiffNeg)) <= (FP_NORM_EPSILON * PI_R1)) {
2429  return ISwap(qubit1, qubit2);
2430  }
2431 
2432  const real1 sinThetaDiffPos = ONE_R1 - sinTheta;
2433  if ((2 * abs(sinThetaDiffPos)) <= (FP_NORM_EPSILON * PI_R1)) {
2434  return IISwap(qubit1, qubit2);
2435  }
2436 
2437  // Decompose root of ISWAP gate:
2438 
2439  // RXX
2440  H(qubit1);
2441  H(qubit2);
2442  CNOT(qubit1, qubit2);
2443  RZ(2 * theta, qubit2);
2444  CNOT(qubit1, qubit2);
2445  H(qubit2);
2446  H(qubit1);
2447 
2448  // RYY
2449  S(qubit1);
2450  S(qubit2);
2451  H(qubit1);
2452  H(qubit2);
2453  CNOT(qubit1, qubit2);
2454  RZ(2 * theta, qubit2);
2455  CNOT(qubit1, qubit2);
2456  H(qubit2);
2457  H(qubit1);
2458  IS(qubit2);
2459  IS(qubit1);
2460  }
2461 
2463  virtual void Reverse(bitLenInt first, bitLenInt last)
2464  {
2465  while ((last > 0U) && (first < (last - 1U))) {
2466  --last;
2467  Swap(first, last);
2468  ++first;
2469  }
2470  }
2471 
2483  virtual real1_f Prob(bitLenInt qubit) = 0;
2487  virtual real1_f CProb(bitLenInt control, bitLenInt target)
2488  {
2489  AntiCNOT(control, target);
2490  const real1_f prob = Prob(target);
2491  AntiCNOT(control, target);
2492 
2493  return prob;
2494  }
2498  virtual real1_f ACProb(bitLenInt control, bitLenInt target)
2499  {
2500  CNOT(control, target);
2501  const real1_f prob = Prob(target);
2502  CNOT(control, target);
2503 
2504  return prob;
2505  }
2506 
2511  {
2512  real1_f totProb = ZERO_R1_F;
2513  real1_f highestProb = ZERO_R1_F;
2514  bitCapInt bestPerm = ZERO_BCI;
2515  for (bitCapInt p = ZERO_BCI; p < maxQPower; ++p) {
2516  real1_f prob = ProbAll(p);
2517  if (prob > highestProb) {
2518  highestProb = prob;
2519  bestPerm = p;
2520  }
2521  totProb += prob;
2522  if (highestProb > (ONE_R1_F - totProb)) {
2523  break;
2524  }
2525  }
2526 
2527  return bestPerm;
2528  }
2529 
2533  virtual std::vector<bitCapInt> HighestProbAll(size_t n);
2534 
2538  virtual real1_f ProbAll(const bitCapInt& fullRegister)
2539  {
2540  return clampProb((real1_f)norm(GetAmplitude(fullRegister)));
2541  }
2542 
2546  virtual real1_f ProbReg(bitLenInt start, bitLenInt length, const bitCapInt& permutation);
2547 
2555  virtual real1_f ProbMask(const bitCapInt& mask, const bitCapInt& permutation);
2556 
2563  virtual void ProbMaskAll(const bitCapInt& mask, real1* probsArray);
2564 
2571  virtual void ProbBitsAll(const std::vector<bitLenInt>& bits, real1* probsArray);
2572 
2579  virtual real1_f VarianceBitsAll(const std::vector<bitLenInt>& bits, const bitCapInt& offset = ZERO_BCI)
2580  {
2581  return ExpVarBitsAll(false, bits, offset);
2582  }
2583 
2591  bool roundRz, const std::vector<bitLenInt>& bits, const bitCapInt& offset = ZERO_BCI)
2592  {
2593  return VarianceBitsAll(bits, offset);
2594  }
2595 
2602  virtual real1_f VariancePauliAll(std::vector<bitLenInt> bits, std::vector<Pauli> paulis);
2603 
2611  const std::vector<bitLenInt>& bits, const std::vector<real1_f>& basisOps, std::vector<real1_f> eigenVals = {})
2612  {
2613  return ExpVarUnitaryAll(false, bits, basisOps, eigenVals);
2614  }
2615 
2622  virtual real1_f VarianceUnitaryAll(const std::vector<bitLenInt>& bits,
2623  const std::vector<std::shared_ptr<complex>>& basisOps, std::vector<real1_f> eigenVals = {})
2624  {
2625  return ExpVarUnitaryAll(false, bits, basisOps, eigenVals);
2626  }
2627 
2634  virtual real1_f VarianceFloatsFactorized(const std::vector<bitLenInt>& bits, const std::vector<real1_f>& weights);
2635 
2644  bool roundRz, const std::vector<bitLenInt>& bits, const std::vector<real1_f>& weights)
2645  {
2646  return VarianceFloatsFactorized(bits, weights);
2647  }
2648 
2656  const std::vector<bitLenInt>& bits, const std::vector<bitCapInt>& perms, const bitCapInt& offset = ZERO_BCI);
2657 
2665  virtual real1_f VarianceBitsFactorizedRdm(bool roundRz, const std::vector<bitLenInt>& bits,
2666  const std::vector<bitCapInt>& perms, const bitCapInt& offset = ZERO_BCI)
2667  {
2668  return VarianceBitsFactorized(bits, perms, offset);
2669  }
2670 
2677  virtual real1_f ExpectationBitsAll(const std::vector<bitLenInt>& bits, const bitCapInt& offset = ZERO_BCI)
2678  {
2679  return ExpVarBitsAll(true, bits, offset);
2680  }
2681 
2688  virtual real1_f ExpectationPauliAll(std::vector<bitLenInt> bits, std::vector<Pauli> paulis);
2689 
2696  virtual real1_f ExpectationUnitaryAll(const std::vector<bitLenInt>& bits,
2697  const std::vector<std::shared_ptr<complex>>& basisOps, std::vector<real1_f> eigenVals = {})
2698  {
2699  return ExpVarUnitaryAll(true, bits, basisOps, eigenVals);
2700  }
2701 
2709  const std::vector<bitLenInt>& bits, const std::vector<real1_f>& basisOps, std::vector<real1_f> eigenVals = {})
2710  {
2711  return ExpVarUnitaryAll(true, bits, basisOps, eigenVals);
2712  }
2713 
2721  const std::vector<bitLenInt>& bits, const std::vector<bitCapInt>& perms, const bitCapInt& offset = ZERO_BCI);
2722 
2730  virtual real1_f ExpectationBitsFactorizedRdm(bool roundRz, const std::vector<bitLenInt>& bits,
2731  const std::vector<bitCapInt>& perms, const bitCapInt& offset = ZERO_BCI)
2732  {
2733  return ExpectationBitsFactorized(bits, perms, offset);
2734  }
2735 
2743  const std::vector<bitLenInt>& bits, const std::vector<real1_f>& weights);
2744 
2753  bool roundRz, const std::vector<bitLenInt>& bits, const std::vector<real1_f>& weights)
2754  {
2755  return ExpectationFloatsFactorized(bits, weights);
2756  }
2757 
2762  virtual real1_f ProbRdm(bitLenInt qubit) { return Prob(qubit); }
2766  virtual real1_f ProbAllRdm(bool roundRz, const bitCapInt& fullRegister) { return ProbAll(fullRegister); }
2774  virtual real1_f ProbMaskRdm(bool roundRz, const bitCapInt& mask, const bitCapInt& permutation)
2775  {
2776  return ProbMask(mask, permutation);
2777  }
2785  bool roundRz, const std::vector<bitLenInt>& bits, const bitCapInt& offset = ZERO_BCI)
2786  {
2787  return ExpectationBitsAll(bits, offset);
2788  }
2789 
2802  virtual std::map<bitCapInt, int> MultiShotMeasureMask(const std::vector<bitCapInt>& qPowers, unsigned shots);
2803 
2809  virtual void MultiShotMeasureMask(
2810  const std::vector<bitCapInt>& qPowers, unsigned shots, unsigned long long* shotsArray);
2811 
2821  virtual void SetBit(bitLenInt qubit, bool value)
2822  {
2823  if (value != M(qubit)) {
2824  X(qubit);
2825  }
2826  }
2827 
2834  virtual bool ApproxCompare(QInterfacePtr toCompare, real1_f error_tol = TRYDECOMPOSE_EPSILON)
2835  {
2836  return SumSqrDiff(toCompare) <= error_tol;
2837  }
2838 
2844  virtual real1_f SumSqrDiff(QInterfacePtr toCompare) = 0;
2845 
2851  virtual bool TryDecompose(bitLenInt start, QInterfacePtr dest, real1_f error_tol = TRYDECOMPOSE_EPSILON);
2852 
2859  virtual void UpdateRunningNorm(real1_f norm_thresh = REAL1_DEFAULT_ARG) = 0;
2860 
2867  virtual void NormalizeState(
2868  real1_f nrm = REAL1_DEFAULT_ARG, real1_f norm_thresh = REAL1_DEFAULT_ARG, real1_f phaseArg = ZERO_R1_F) = 0;
2869 
2875  virtual void Finish() {}
2876 
2881  virtual bool isFinished() { return true; }
2882 
2887  virtual void Dump() {}
2888 
2893  virtual bool isBinaryDecisionTree() { return false; }
2894 
2899  virtual bool isClifford() { return false; }
2900 
2905  virtual bool isClifford(bitLenInt qubit) { return false; }
2906 
2910  virtual bool isOpenCL() { return false; }
2911 
2925  virtual bool TrySeparate(const std::vector<bitLenInt>& qubits, real1_f error_tol) { return false; }
2929  virtual bool TrySeparate(bitLenInt qubit) { return false; }
2933  virtual bool TrySeparate(bitLenInt qubit1, bitLenInt qubit2) { return false; }
2937  virtual bool AreFactorized(std::vector<bitLenInt> a, std::vector<bitLenInt> b, bool flushCache = false)
2938  {
2939  return false;
2940  }
2950  virtual double GetUnitaryFidelity() { return 1.0; }
2954  virtual void ResetUnitaryFidelity() {}
2958  virtual void SetSdrp(real1_f sdrp) {};
2962  virtual void SetNcrp(real1_f ncrp) {};
2970  virtual void SetReactiveSeparate(bool isAggSep) {}
2978  virtual bool GetReactiveSeparate() { return false; }
2986  virtual void SetTInjection(bool useGadget) {}
2994  virtual bool GetTInjection() { return false; }
3001  virtual void SetUseExactNearClifford(bool useExact) {}
3008  virtual bool GetUseExactNearClifford() { return false; }
3014  virtual void SetNoiseParameter(real1_f lambda) {}
3020  virtual real1_f GetNoiseParameter() { return ZERO_R1_F; }
3024  virtual void SetAceMaxQubits(bitLenInt qb) {}
3028  virtual void SetSparseAceMaxMb(size_t mb) {}
3036  virtual void SetStochastic(bool s) {}
3040  virtual void SetMajorQuadrant(bool q) {}
3044  virtual void SetMajorQuadrant(bitLenInt t, bool q) {}
3048  virtual void FlipQuadrant(bitLenInt t) {}
3049 
3053  virtual QInterfacePtr Clone() = 0;
3054 
3058  virtual QInterfacePtr Copy() { return Clone(); }
3059 
3063  virtual void SetDevice(int64_t dID) {};
3064 
3068  virtual int64_t GetDevice() { return -1; }
3072  virtual void SetDeviceList(std::vector<int64_t> dIDs) {};
3073 
3077  virtual std::vector<int64_t> GetDeviceList() { return std::vector<int64_t>(); }
3078 
3082  bitCapIntOcl GetMaxSize() { return pow2Ocl(sizeof(bitCapInt) * 8); };
3083 
3088  {
3089  complex amp;
3090  bitCapInt perm = ZERO_BCI;
3091  do {
3092  amp = GetAmplitude(perm);
3093  bi_increment(&perm, 1U);
3094  } while ((abs(amp) <= REAL1_EPSILON) && (perm < maxQPower));
3095 
3096  return (real1_f)std::arg(amp);
3097  }
3098 
3104  virtual void DepolarizingChannelWeak1Qb(bitLenInt qubit, real1_f lambda);
3105 
3107 };
3108 } // namespace Qrack
void bi_or_ip(BigInteger *left, const BigInteger &right)
Definition: big_integer.hpp:445
void bi_increment(BigInteger *pBigInt, const BIG_INTEGER_WORD &value)
Definition: big_integer.hpp:221
int bi_compare_0(const BigInteger &left)
Definition: big_integer.hpp:141
Definition: parallel_for.hpp:27
void SetConcurrencyLevel(unsigned num)
Definition: parallel_for.hpp:36
A "Qrack::QInterface" is an abstract interface exposing qubit permutation state vector with methods t...
Definition: qinterface.hpp:141
virtual void GetQuantumState(complex *outputState)=0
Get the pure quantum state representation.
bitCapInt maxQPower
Definition: qinterface.hpp:149
void SetRandomSeed(uint32_t seed)
Definition: qinterface.hpp:261
virtual void SetConcurrency(uint32_t threadsPerEngine)
Set the number of threads in parallel for loops, per component QEngine.
Definition: qinterface.hpp:275
virtual bitCapInt GetAmplitudeCount()
Count of amplitudes, which might be less than state vector if sparse or compressed.
Definition: qinterface.hpp:284
real1 amplitudeFloor
Definition: qinterface.hpp:148
void MACWrapper(const std::vector< bitLenInt > &controls, Fn fn)
Definition: qinterface.hpp:179
bool useRDRAND
Definition: qinterface.hpp:145
virtual bitLenInt Allocate(bitLenInt length)
Allocate new "length" count of |0> state qubits at end of qubit index position.
Definition: qinterface.hpp:488
virtual bitCapInt GetMaxQPower()
Get the maximum number of basis states, namely for qubits.
Definition: qinterface.hpp:281
virtual void Copy(QInterfacePtr orig)
Definition: qinterface.hpp:222
virtual void Dispose(bitLenInt start, bitLenInt length, const bitCapInt &disposedPerm)=0
Dispose a a contiguous set of qubits that are already in a permutation eigenstate.
std::shared_ptr< RdRandom > hardware_rand_generator
Definition: qinterface.hpp:152
virtual bitLenInt Compose(QInterfacePtr toCopy)
Combine another QInterface with this one, after the last bit index of this one.
Definition: qinterface.hpp:382
virtual void GetReducedDensityMatrix(const std::vector< bitLenInt > &qubits, complex *outputState)
Get a reduced density matrix on the list of qubits.
Definition: qinterface.cpp:886
virtual QInterfacePtr Decompose(bitLenInt start, bitLenInt length)=0
Schmidt decompose a length of qubits.
qrack_rand_gen_ptr rand_generator
Definition: qinterface.hpp:150
virtual void LossySaveStateVector(std::string f, int p=6, int b=4)
Write the quantum state to disk with lossy compression.
Definition: qinterface.cpp:855
virtual bitLenInt ComposeNoClone(QInterfacePtr toCopy)
This is a variant of Compose() for a toCopy argument that will definitely not be reused once "Compose...
Definition: qinterface.hpp:387
bool randGlobalPhase
Definition: qinterface.hpp:144
virtual void SetPermutation(const bitCapInt &perm, const complex &phaseFac=CMPLX_DEFAULT_ARG)
Set to a specific permutation of all qubits.
Definition: qinterface.cpp:103
virtual void Decompose(bitLenInt start, QInterfacePtr dest)=0
Minimally decompose a set of contiguous bits from the separably composed unit, into "destination".
QInterface()
Default constructor, primarily for protected internal use.
Definition: qinterface.hpp:242
virtual void SetQubitCount(bitLenInt qb)
Definition: qinterface.hpp:268
std::uniform_real_distribution< real1_s > rand_distribution
Definition: qinterface.hpp:151
virtual void SetAmplitude(const bitCapInt &perm, const complex &amp)=0
Sets the representational amplitude of a full permutation.
virtual bitLenInt Allocate(bitLenInt start, bitLenInt length)=0
Allocate new "length" count of |0> state qubits at specified qubit index start position.
virtual void SetQuantumState(const complex *inputState)=0
Set an arbitrary pure quantum state representation.
virtual bitCapInt SampleClone(const std::vector< bitCapInt > &qPowers)
Definition: qinterface.hpp:191
static real1_f normHelper(const complex &c)
Definition: qinterface.hpp:156
static real1_f clampProb(real1_f toClamp)
Definition: qinterface.hpp:158
virtual real1_f ExpVarUnitaryAll(bool isExp, const std::vector< bitLenInt > &bits, const std::vector< std::shared_ptr< complex >> &basisOps, std::vector< real1_f > eigenVals={})
Definition: qinterface.cpp:478
bitLenInt qubitCount
Definition: qinterface.hpp:146
virtual bitLenInt GetQubitCount()
Get the count of bits in this register.
Definition: qinterface.hpp:278
uint32_t randomSeed
Definition: qinterface.hpp:147
real1_f Rand()
Generate a random real number between 0 and 1.
Definition: qinterface.hpp:289
virtual real1_f ExpVarBitsAll(bool isExp, const std::vector< bitLenInt > &bits, const bitCapInt &offset=ZERO_BCI)
Definition: qinterface.hpp:210
virtual void Dispose(bitLenInt start, bitLenInt length)=0
Minimally decompose a set of contiguous bits from the separably composed unit, and discard the separa...
virtual bool GetIsArbitraryGlobalPhase()
Definition: qinterface.hpp:286
bool doNormalize
Definition: qinterface.hpp:143
complex GetNonunitaryPhase()
Definition: qinterface.hpp:169
virtual void LossyLoadStateVector(std::string f)
Read the quantum state from disk with lossy compression.
Definition: qinterface.cpp:866
virtual ~QInterface()
Definition: qinterface.hpp:256
virtual void GetProbs(real1 *outputProbs)=0
Get the pure quantum state representation.
virtual complex GetAmplitude(const bitCapInt &perm)=0
Get the representational amplitude of a full permutation.
Half-precision floating-point type.
Definition: half.hpp:2206
virtual void MULModNOut(const bitCapInt &toMul, const bitCapInt &modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length)
Multiplication modulo N by integer, (out of place)
Definition: arithmetic.cpp:127
virtual void DECS(const bitCapInt &toSub, bitLenInt start, bitLenInt length, bitLenInt overflowIndex)
Subtract a classical integer from the register, with sign and without carry.
Definition: qinterface.hpp:2225
virtual void ASL(bitLenInt shift, bitLenInt start, bitLenInt length)
Arithmetic shift left, with last 2 bits as sign and carry.
Definition: qinterface.cpp:335
virtual void IFullAdd(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt carryInSumOut, bitLenInt carryOut)
Inverse of FullAdd.
Definition: qinterface.hpp:2269
virtual void INCDECC(const bitCapInt &toAdd, bitLenInt start, bitLenInt length, bitLenInt carryIndex)
Common driver method behind INCC and DECC.
Definition: arithmetic.cpp:53
virtual void CADC(const std::vector< bitLenInt > &controls, bitLenInt input1, bitLenInt input2, bitLenInt output, bitLenInt length, bitLenInt carry)
Add a quantum integer to a quantum integer, with carry and with controls.
Definition: arithmetic.cpp:371
virtual void CINC(const bitCapInt &toAdd, bitLenInt inOutStart, bitLenInt length, const std::vector< bitLenInt > &controls)
Add integer (without sign, with controls)
Definition: arithmetic.cpp:79
virtual void INCS(const bitCapInt &toAdd, bitLenInt start, bitLenInt length, bitLenInt overflowIndex)
Add a classical integer to the register, with sign and without carry.
Definition: qinterface.hpp:2214
virtual void DECC(const bitCapInt &toSub, bitLenInt start, bitLenInt length, bitLenInt carryIndex)
Subtract classical integer (without sign, with carry)
Definition: qinterface.hpp:2188
virtual void ADC(bitLenInt input1, bitLenInt input2, bitLenInt output, bitLenInt length, bitLenInt carry)
Add a quantum integer to a quantum integer, with carry.
Definition: arithmetic.cpp:330
virtual void DEC(const bitCapInt &toSub, bitLenInt start, bitLenInt length)
Subtract classical integer (without sign)
Definition: qinterface.hpp:2166
virtual void CFullAdd(const std::vector< bitLenInt > &controls, bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt carryInSumOut, bitLenInt carryOut)
Controlled quantum analog of classical "Full Adder" gate.
Definition: arithmetic.cpp:276
virtual void INC(const bitCapInt &toAdd, bitLenInt start, bitLenInt length)
Add integer (without sign)
Definition: arithmetic.cpp:20
virtual void LSR(bitLenInt shift, bitLenInt start, bitLenInt length)
Logical shift right, filling the extra bits with |0>
Definition: qinterface.cpp:386
virtual void FullAdd(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt carryInSumOut, bitLenInt carryOut)
Quantum analog of classical "Full Adder" gate.
Definition: qinterface.hpp:2252
virtual void IMULModNOut(const bitCapInt &toMul, const bitCapInt &modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length)
Inverse of multiplication modulo N by integer, (out of place)
Definition: arithmetic.cpp:165
virtual void CIFullAdd(const std::vector< bitLenInt > &controls, bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt carryInSumOut, bitLenInt carryOut)
Inverse of CFullAdd.
Definition: arithmetic.cpp:302
virtual void CIADC(const std::vector< bitLenInt > &controls, bitLenInt input1, bitLenInt input2, bitLenInt output, bitLenInt length, bitLenInt carry)
Inverse of CADC.
Definition: arithmetic.cpp:392
virtual void ROL(bitLenInt shift, bitLenInt start, bitLenInt length)
Circular shift left - shift bits left, and carry last bits.
Definition: qinterface.cpp:297
virtual void CDEC(const bitCapInt &toSub, bitLenInt inOutStart, bitLenInt length, const std::vector< bitLenInt > &controls)
Subtract classical integer (without sign, with controls)
Definition: qinterface.hpp:2206
virtual void INCC(const bitCapInt &toAdd, bitLenInt start, bitLenInt length, bitLenInt carryIndex)
Add integer (without sign, with carry)
Definition: qinterface.hpp:2176
virtual void IADC(bitLenInt input1, bitLenInt input2, bitLenInt output, bitLenInt length, bitLenInt carry)
Inverse of ADC.
Definition: arithmetic.cpp:350
virtual void CMULModNOut(const bitCapInt &toMul, const bitCapInt &modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length, const std::vector< bitLenInt > &controls)
Controlled multiplication modulo N by integer, (out of place)
Definition: arithmetic.cpp:201
virtual void CIMULModNOut(const bitCapInt &toMul, const bitCapInt &modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length, const std::vector< bitLenInt > &controls)
Inverse of controlled multiplication modulo N by integer, (out of place)
Definition: arithmetic.cpp:240
virtual void ROR(bitLenInt shift, bitLenInt start, bitLenInt length)
Circular shift right - shift bits right, and carry first bits.
Definition: qinterface.cpp:316
virtual void LSL(bitLenInt shift, bitLenInt start, bitLenInt length)
Logical shift left, filling the extra bits with |0>
Definition: qinterface.cpp:371
virtual void ASR(bitLenInt shift, bitLenInt start, bitLenInt length)
Arithmetic shift right, with last 2 bits as sign and carry.
Definition: qinterface.cpp:353
virtual void ZMask(const bitCapInt &mask)
Masked Z gate.
Definition: gates.cpp:144
virtual void CPhaseRootN(bitLenInt n, bitLenInt control, bitLenInt target)
Controlled "PhaseRootN" gate.
Definition: qinterface.hpp:1353
virtual void CIPhaseRootN(bitLenInt n, bitLenInt control, bitLenInt target)
Controlled inverse "PhaseRootN" gate.
Definition: qinterface.hpp:1385
virtual void AntiCIAI(bitLenInt control, bitLenInt target, real1_f azimuth, real1_f inclination)
(Anti-)Controlled inverse "Azimuth, Inclination" (RY-RZ)
Definition: rotational.cpp:117
virtual void ISqrtX(bitLenInt qubit)
Inverse square root of X gate.
Definition: qinterface.hpp:1179
virtual void ISqrtY(bitLenInt qubit)
Inverse square root of Y gate.
Definition: qinterface.hpp:1209
virtual void CZ(bitLenInt control, bitLenInt target)
Controlled Z gate.
Definition: qinterface.hpp:796
virtual void SH(bitLenInt qubit)
Y-basis transformation gate.
Definition: qinterface.hpp:961
virtual void AntiCS(bitLenInt control, bitLenInt target)
(Anti-)controlled S gate
Definition: qinterface.hpp:1293
virtual void CCZ(bitLenInt control1, bitLenInt control2, bitLenInt target)
Doubly-Controlled Z gate.
Definition: qinterface.hpp:819
virtual void UCPhase(const std::vector< bitLenInt > &controls, const complex &topLeft, const complex &bottomRight, bitLenInt target, const bitCapInt &perm)
Apply a single bit transformation that only effects phase, with arbitrary control bits,...
Definition: qinterface.hpp:609
virtual void CS(bitLenInt control, bitLenInt target)
Controlled S gate.
Definition: qinterface.hpp:1281
virtual void CIS(bitLenInt control, bitLenInt target)
Controlled inverse S gate.
Definition: qinterface.hpp:1305
virtual void SqrtY(bitLenInt qubit)
Square root of Y gate.
Definition: qinterface.hpp:1194
virtual void CNOT(bitLenInt control, bitLenInt target)
Controlled NOT gate.
Definition: qinterface.hpp:727
virtual void TimeEvolve(Hamiltonian h, real1_f timeDiff)
To define a Hamiltonian, give a vector of controlled single bit gates ("HamiltonianOp" instances) tha...
Definition: gates.cpp:426
virtual void SqrtW(bitLenInt qubit)
Square root of W gate.
Definition: qinterface.hpp:1222
virtual void IS(bitLenInt qubit)
Inverse S gate.
Definition: qinterface.hpp:1052
virtual void S(bitLenInt qubit)
S gate.
Definition: qinterface.hpp:1045
virtual void CT(bitLenInt control, bitLenInt target)
Controlled T gate.
Definition: qinterface.hpp:1329
virtual void UniformlyControlledSingleBit(const std::vector< bitLenInt > &controls, bitLenInt qubit, const complex *mtrxs)
Apply a "uniformly controlled" arbitrary single bit unitary transformation.
Definition: qinterface.hpp:645
virtual void Y(bitLenInt qubit)
Y gate.
Definition: qinterface.hpp:1133
virtual bool ForceM(bitLenInt qubit, bool result, bool doForce=true, bool doApply=true)=0
Act as if is a measurement was applied, except force the (usually random) result.
virtual void CY(bitLenInt control, bitLenInt target)
Controlled Y gate.
Definition: qinterface.hpp:750
virtual void AntiCIPhaseRootN(bitLenInt n, bitLenInt control, bitLenInt target)
(Anti-)controlled inverse "PhaseRootN" gate
Definition: qinterface.hpp:1401
virtual void CH(bitLenInt control, bitLenInt target)
Controlled H gate.
Definition: qinterface.hpp:1251
virtual void AntiCU(const std::vector< bitLenInt > &controls, bitLenInt target, real1_f theta, real1_f phi, real1_f lambda, real1_f gamma=ZERO_R1_F)
(Anti-)Controlled general unitary gate
Definition: rotational.cpp:42
virtual void AntiCAI(bitLenInt control, bitLenInt target, real1_f azimuth, real1_f inclination)
(Anti-)Controlled "Azimuth, Inclination" (RY-RZ)
Definition: rotational.cpp:105
virtual void H(bitLenInt qubit)
Hadamard gate.
Definition: qinterface.hpp:931
virtual void CIAI(bitLenInt control, bitLenInt target, real1_f azimuth, real1_f inclination)
Controlled inverse "Azimuth, Inclination" (RY-RZ)
Definition: rotational.cpp:91
virtual void CSqrtSwap(const std::vector< bitLenInt > &controls, bitLenInt qubit1, bitLenInt qubit2)
Apply a square root of swap with arbitrary control bits.
Definition: gates.cpp:282
virtual void AntiCSwap(const std::vector< bitLenInt > &controls, bitLenInt qubit1, bitLenInt qubit2)
Apply a swap with arbitrary (anti) control bits.
Definition: gates.cpp:270
virtual void CU(const std::vector< bitLenInt > &controls, bitLenInt target, real1_f theta, real1_f phi, real1_f lambda, real1_f gamma=ZERO_R1_F)
Controlled general unitary gate.
Definition: rotational.cpp:29
virtual void AI(bitLenInt target, real1_f azimuth, real1_f inclination)
"Azimuth, Inclination" (RY-RZ)
Definition: rotational.cpp:55
virtual void AntiCPhaseRootN(bitLenInt n, bitLenInt control, bitLenInt target)
(Anti-)controlled "PhaseRootN" gate
Definition: qinterface.hpp:1369
virtual void YMask(const bitCapInt &mask)
Masked Y gate.
Definition: gates.cpp:112
virtual void MACInvert(const std::vector< bitLenInt > &controls, const complex &topRight, const complex &bottomLeft, bitLenInt target)
Apply a single bit transformation that reverses bit probability and might effect phase,...
Definition: qinterface.hpp:597
virtual void UCMtrx(const std::vector< bitLenInt > &controls, const complex *mtrx, bitLenInt target, const bitCapInt &controlPerm)
Apply an arbitrary single bit unitary transformation, with arbitrary control bits,...
Definition: gates.cpp:23
virtual void HIS(bitLenInt qubit)
Y-basis (inverse) transformation gate.
Definition: qinterface.hpp:975
virtual void Phase(const complex &topLeft, const complex &bottomRight, bitLenInt qubit)
Apply a single bit transformation that only effects phase.
Definition: qinterface.hpp:534
virtual void Invert(const complex &topRight, const complex &bottomLeft, bitLenInt qubit)
Apply a single bit transformation that reverses bit probability and might effect phase.
Definition: qinterface.hpp:547
virtual void CAI(bitLenInt control, bitLenInt target, real1_f azimuth, real1_f inclination)
Controlled "Azimuth, Inclination" (RY-RZ)
Definition: rotational.cpp:79
virtual void CCY(bitLenInt control1, bitLenInt control2, bitLenInt target)
Doubly-Controlled Y gate.
Definition: qinterface.hpp:773
virtual void AntiCZ(bitLenInt control, bitLenInt target)
Anti controlled Z gate.
Definition: qinterface.hpp:807
virtual void CISqrtSwap(const std::vector< bitLenInt > &controls, bitLenInt qubit1, bitLenInt qubit2)
Apply an inverse square root of swap with arbitrary control bits.
Definition: gates.cpp:331
virtual void X(bitLenInt qubit)
X gate.
Definition: qinterface.hpp:1116
virtual void MACPhase(const std::vector< bitLenInt > &controls, const complex &topLeft, const complex &bottomRight, bitLenInt target)
Apply a single bit transformation that only effects phase, with arbitrary (anti-)control bits.
Definition: qinterface.hpp:581
virtual void U2(bitLenInt target, real1_f phi, real1_f lambda)
2-parameter unitary gate
Definition: qinterface.hpp:849
virtual void Z(bitLenInt qubit)
Z gate.
Definition: qinterface.hpp:1149
virtual void AntiCNOT(bitLenInt control, bitLenInt target)
Anti controlled NOT gate.
Definition: qinterface.hpp:738
virtual void AntiCY(bitLenInt control, bitLenInt target)
Anti controlled Y gate.
Definition: qinterface.hpp:761
virtual void ISqrtW(bitLenInt qubit)
Inverse square root of W gate.
Definition: qinterface.hpp:1236
virtual void PhaseParity(real1_f radians, const bitCapInt &mask)
Parity phase gate.
Definition: gates.cpp:399
virtual void MACMtrx(const std::vector< bitLenInt > &controls, const complex mtrx[4U], bitLenInt target)
Apply an arbitrary single bit unitary transformation, with arbitrary (anti-)control bits.
Definition: qinterface.hpp:513
virtual void IPhaseRootN(bitLenInt n, bitLenInt qubit)
Inverse "PhaseRootN" gate.
Definition: qinterface.hpp:1087
virtual void IAI(bitLenInt target, real1_f azimuth, real1_f inclination)
Invert "Azimuth, Inclination" (RY-RZ)
Definition: rotational.cpp:66
virtual void AntiCISqrtSwap(const std::vector< bitLenInt > &controls, bitLenInt qubit1, bitLenInt qubit2)
Apply an inverse square root of swap with arbitrary (anti) control bits.
Definition: gates.cpp:387
virtual void PhaseRootN(bitLenInt n, bitLenInt qubit)
"PhaseRootN" gate
Definition: qinterface.hpp:1073
virtual void U(bitLenInt target, real1_f theta, real1_f phi, real1_f lambda)
General unitary gate.
Definition: rotational.cpp:18
virtual void AntiCIS(bitLenInt control, bitLenInt target)
(Anti-)controlled inverse S gate
Definition: qinterface.hpp:1317
virtual void SqrtX(bitLenInt qubit)
Square root of X gate.
Definition: qinterface.hpp:1165
virtual void IU2(bitLenInt target, real1_f phi, real1_f lambda)
Inverse 2-parameter unitary gate.
Definition: qinterface.hpp:856
virtual void AntiCCNOT(bitLenInt control1, bitLenInt control2, bitLenInt target)
Anti doubly-controlled NOT gate.
Definition: qinterface.hpp:716
virtual void PhaseRootNMask(bitLenInt n, const bitCapInt &mask)
Masked PhaseRootN gate.
Definition: gates.cpp:155
virtual void SqrtH(bitLenInt qubit)
Square root of Hadamard gate.
Definition: qinterface.hpp:944
virtual void MCPhase(const std::vector< bitLenInt > &controls, const complex &topLeft, const complex &bottomRight, bitLenInt target)
Apply a single bit transformation that only effects phase, with arbitrary control bits.
Definition: qinterface.hpp:556
virtual void AntiCCY(bitLenInt control1, bitLenInt control2, bitLenInt target)
Anti doubly-controlled Y gate.
Definition: qinterface.hpp:784
virtual bool M(bitLenInt qubit)
Measurement gate.
Definition: qinterface.hpp:1031
virtual void Mtrx(const complex mtrx[4U], bitLenInt qubit)=0
Apply an arbitrary single bit unitary transformation.
virtual void MCInvert(const std::vector< bitLenInt > &controls, const complex &topRight, const complex &bottomLeft, bitLenInt target)
Apply a single bit transformation that reverses bit probability and might effect phase,...
Definition: qinterface.hpp:571
virtual void AntiCSqrtSwap(const std::vector< bitLenInt > &controls, bitLenInt qubit1, bitLenInt qubit2)
Apply a square root of swap with arbitrary (anti) control bits.
Definition: gates.cpp:375
virtual void CIT(bitLenInt control, bitLenInt target)
Controlled inverse T gate.
Definition: qinterface.hpp:1341
virtual void IT(bitLenInt qubit)
Inverse T gate.
Definition: qinterface.hpp:1066
virtual void MCMtrx(const std::vector< bitLenInt > &controls, const complex mtrx[4U], bitLenInt target)=0
Apply an arbitrary single bit unitary transformation, with arbitrary control bits.
virtual void AntiCCZ(bitLenInt control1, bitLenInt control2, bitLenInt target)
Anti doubly-controlled Z gate.
Definition: qinterface.hpp:830
virtual void CSwap(const std::vector< bitLenInt > &controls, bitLenInt qubit1, bitLenInt qubit2)
Apply a swap with arbitrary control bits.
Definition: gates.cpp:247
virtual void T(bitLenInt qubit)
T gate.
Definition: qinterface.hpp:1059
virtual void CCNOT(bitLenInt control1, bitLenInt control2, bitLenInt target)
Doubly-controlled NOT gate.
Definition: qinterface.hpp:705
virtual void XMask(const bitCapInt &mask)
Masked X gate.
Definition: gates.cpp:101
virtual void AntiCH(bitLenInt control, bitLenInt target)
(Anti-)controlled H gate
Definition: qinterface.hpp:1266
virtual void UCInvert(const std::vector< bitLenInt > &controls, const complex &topRight, const complex &bottomLeft, bitLenInt target, const bitCapInt &perm)
Apply a single bit transformation that reverses bit probability and might effect phase,...
Definition: qinterface.hpp:624
virtual void IQFTR(const std::vector< bitLenInt > &qubits, bool trySeparate=false)
Inverse Quantum Fourier Transform (random access) - Apply the inverse quantum Fourier transform to th...
Definition: qinterface.cpp:176
virtual void Reverse(bitLenInt first, bitLenInt last)
Reverse all of the bits in a sequence.
Definition: qinterface.hpp:2463
virtual void ISqrtSwap(bitLenInt qubit1, bitLenInt qubit2)
Inverse square root of Swap gate.
Definition: gates.cpp:224
virtual bitCapInt ForceMReg(bitLenInt start, bitLenInt length, const bitCapInt &result, bool doForce=true, bool doApply=true)
Act as if is a measurement was applied, except force the (usually random) result.
Definition: qinterface.cpp:215
virtual void QFTR(const std::vector< bitLenInt > &qubits, bool trySeparate=false)
Quantum Fourier Transform (random access) - Apply the quantum Fourier transform to the register.
Definition: qinterface.cpp:156
virtual void IISwap(bitLenInt qubit1, bitLenInt qubit2)
Inverse ISwap - Swap values of two bits in register, and apply phase factor of -i if bits are differe...
Definition: gates.cpp:189
virtual void ISwap(bitLenInt qubit1, bitLenInt qubit2)
Swap values of two bits in register, and apply phase factor of i if bits are different.
Definition: gates.cpp:177
virtual bitCapInt MAll()
Measure permutation state of all coherent bits.
Definition: qinterface.hpp:2382
virtual void QFT(bitLenInt start, bitLenInt length, bool trySeparate=false)
Quantum Fourier Transform - Apply the quantum Fourier transform to the register.
Definition: qinterface.cpp:114
virtual void PhaseFlip()
Phase flip always - equivalent to Z X Z X on any bit in the QInterface.
Definition: qinterface.hpp:2373
virtual bitCapInt M(const std::vector< bitLenInt > &bits)
Measure bits with indices in array, and return a mask of the results.
Definition: qinterface.hpp:2393
virtual void SqrtSwap(bitLenInt qubit1, bitLenInt qubit2)
Square root of Swap gate.
Definition: gates.cpp:201
virtual void FSim(real1_f theta, real1_f phi, bitLenInt qubit1, bitLenInt qubit2)
The 2-qubit "fSim" gate, (useful in the simulation of particles with fermionic statistics)
Definition: qinterface.hpp:2414
virtual void Swap(bitLenInt qubit1, bitLenInt qubit2)
Swap values of two bits in register.
Definition: gates.cpp:166
virtual void ZeroPhaseFlip(bitLenInt start, bitLenInt length)
Reverse the phase of the state where the register equals zero.
Definition: gates.cpp:84
virtual void SetReg(bitLenInt start, bitLenInt length, const bitCapInt &value)
Set register bits to given permutation.
Definition: qinterface.cpp:195
virtual bitCapInt MReg(bitLenInt start, bitLenInt length)
Measure permutation state of a register.
Definition: qinterface.hpp:2379
virtual void IQFT(bitLenInt start, bitLenInt length, bool trySeparate=false)
Inverse Quantum Fourier Transform - Apply the inverse quantum Fourier transform to the register.
Definition: qinterface.cpp:136
virtual void XOR(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt outputBit)
Quantum analog of classical "XOR" gate.
Definition: logic.cpp:55
virtual void OR(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt outputBit)
Quantum analog of classical "OR" gate.
Definition: logic.cpp:36
virtual void XNOR(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt outputBit)
Quantum analog of classical "XNOR" gate.
Definition: logic.cpp:83
virtual void CLXNOR(bitLenInt inputQBit, bool inputClassicalBit, bitLenInt outputBit)
Quantum analog of classical "XNOR" gate.
Definition: logic.cpp:129
virtual void CLNAND(bitLenInt inputQBit, bool inputClassicalBit, bitLenInt outputBit)
Quantum analog of classical "NAND" gate.
Definition: logic.cpp:117
virtual void AND(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt outputBit)
Quantum analog of classical "AND" gate.
Definition: logic.cpp:18
virtual void CLAND(bitLenInt inputQBit, bool inputClassicalBit, bitLenInt outputBit)
Quantum analog of classical "AND" gate.
Definition: logic.cpp:89
virtual void CLOR(bitLenInt inputQBit, bool inputClassicalBit, bitLenInt outputBit)
Quantum analog of classical "OR" gate.
Definition: logic.cpp:96
virtual void NAND(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt outputBit)
Quantum analog of classical "NAND" gate.
Definition: logic.cpp:71
virtual void CLNOR(bitLenInt inputQBit, bool inputClassicalBit, bitLenInt outputBit)
Quantum analog of classical "NOR" gate.
Definition: logic.cpp:123
virtual void CLXOR(bitLenInt inputQBit, bool inputClassicalBit, bitLenInt outputBit)
Quantum analog of classical "XOR" gate.
Definition: logic.cpp:105
virtual void NOR(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt outputBit)
Quantum analog of classical "NOR" gate.
Definition: logic.cpp:77
virtual void X(bitLenInt start, bitLenInt length)
Bitwise Pauli X (or logical "NOT") operator.
Definition: qinterface.hpp:1740
virtual void H(bitLenInt start, bitLenInt length)
Bitwise Hadamard.
virtual void ExpZDyad(int numerator, int denomPower, bitLenInt qubit)
Dyadic fraction Pauli Z exponentiation gate.
Definition: qinterface.cpp:1334
virtual void RZDyad(int numerator, int denomPower, bitLenInt qubit)
Dyadic fraction Z axis rotation gate.
Definition: qinterface.cpp:1346
virtual void ExpX(real1_f radians, bitLenInt qubit)
Pauli X exponentiation gate.
Definition: rotational.cpp:251
virtual void CRTDyad(int numerator, int denomPower, bitLenInt control, bitLenInt target)
Controlled dyadic fraction "phase shift gate".
Definition: qinterface.cpp:1350
virtual void UniformlyControlledRZ(const std::vector< bitLenInt > &controls, bitLenInt qubit, const real1 *angles)
Apply a "uniformly controlled" rotation of a bit around the Pauli Z axis.
Definition: rotational.cpp:153
virtual void ExpDyad(int numerator, int denomPower, bitLenInt qubit)
Dyadic fraction (identity) exponentiation gate.
Definition: qinterface.cpp:1316
virtual void CRZDyad(int numerator, int denomPower, bitLenInt control, bitLenInt target)
Controlled dyadic fraction Z axis rotation gate.
Definition: qinterface.cpp:1368
virtual void RX(real1_f radians, bitLenInt qubit)
X axis rotation gate.
Definition: rotational.cpp:179
virtual void CRZ(real1_f radians, bitLenInt control, bitLenInt target)
Controlled Z axis rotation gate.
Definition: rotational.cpp:206
virtual void RYDyad(int numerator, int denomPower, bitLenInt qubit)
Dyadic fraction Y axis rotation gate.
Definition: qinterface.cpp:1343
virtual void UniformlyControlledRY(const std::vector< bitLenInt > &controls, bitLenInt qubit, const real1 *angles)
Apply a "uniformly controlled" rotation of a bit around the Pauli Y axis.
Definition: rotational.cpp:132
virtual void ExpY(real1_f radians, bitLenInt qubit)
Pauli Y exponentiation gate.
Definition: rotational.cpp:258
virtual void CRT(real1_f radians, bitLenInt control, bitLenInt target)
Controlled "phase shift gate".
Definition: rotational.cpp:272
virtual void CRY(real1_f radians, bitLenInt control, bitLenInt target)
Controlled Y axis rotation gate.
Definition: rotational.cpp:215
virtual void RY(real1_f radians, bitLenInt qubit)
Y axis rotation gate.
Definition: rotational.cpp:189
virtual void CRX(real1_f radians, bitLenInt control, bitLenInt target)
Controlled X axis rotation gate.
Definition: rotational.cpp:279
virtual void ExpYDyad(int numerator, int denomPower, bitLenInt qubit)
Dyadic fraction Pauli Y exponentiation gate.
Definition: qinterface.cpp:1328
virtual void RTDyad(int numerator, int denomPower, bitLenInt qubit)
Dyadic fraction phase shift gate.
Definition: qinterface.cpp:1313
virtual void RZ(real1_f radians, bitLenInt qubit)
Z axis rotation gate.
Definition: rotational.cpp:198
virtual void RT(real1_f radians, bitLenInt qubit)
Phase shift gate.
Definition: rotational.cpp:173
virtual void ExpZ(real1_f radians, bitLenInt qubit)
Pauli Z exponentiation gate.
Definition: rotational.cpp:265
virtual void CRYDyad(int numerator, int denomPower, bitLenInt control, bitLenInt target)
Controlled dyadic fraction y axis rotation gate.
Definition: qinterface.cpp:1362
virtual void RXDyad(int numerator, int denomPower, bitLenInt qubit)
Dyadic fraction X axis rotation gate.
Definition: qinterface.cpp:1340
virtual void ExpXDyad(int numerator, int denomPower, bitLenInt qubit)
Dyadic fraction Pauli X exponentiation gate.
Definition: qinterface.cpp:1322
virtual void Exp(real1_f radians, bitLenInt qubit)
(Identity) Exponentiation gate
Definition: rotational.cpp:227
virtual void CRXDyad(int numerator, int denomPower, bitLenInt control, bitLenInt target)
Controlled dyadic fraction X axis rotation gate.
Definition: qinterface.cpp:1356
virtual void Dump()
If asynchronous work is still running, let the simulator know that it can be aborted.
Definition: qinterface.hpp:2887
virtual bool isBinaryDecisionTree()
Returns "true" if current state representation is definitely a binary decision tree,...
Definition: qinterface.hpp:2893
virtual std::vector< int64_t > GetDeviceList()
Get the device index.
Definition: qinterface.hpp:3077
virtual void SetSparseAceMaxMb(size_t mb)
Set the (sparse-simulation) "automatic circuit elision" (ACE) maximum memory megabytes.
Definition: qinterface.hpp:3028
virtual real1_f VarianceBitsFactorized(const std::vector< bitLenInt > &bits, const std::vector< bitCapInt > &perms, const bitCapInt &offset=ZERO_BCI)
Get expectation value of bits, given an array of qubit weights.
Definition: qinterface.cpp:579
virtual real1_f ProbAllRdm(bool roundRz, const bitCapInt &fullRegister)
Direct measure of full permutation probability, treating all ancillary qubits as post-selected T gate...
Definition: qinterface.hpp:2766
virtual bool ApproxCompare(QInterfacePtr toCompare, real1_f error_tol=TRYDECOMPOSE_EPSILON)
Compare state vectors approximately, to determine whether this state vector is the same as the target...
Definition: qinterface.hpp:2834
virtual real1_f ExpectationBitsFactorizedRdm(bool roundRz, const std::vector< bitLenInt > &bits, const std::vector< bitCapInt > &perms, const bitCapInt &offset=ZERO_BCI)
Get (reduced density matrix) expectation value of bits, given an array of qubit weights.
Definition: qinterface.hpp:2730
virtual bool isClifford(bitLenInt qubit)
Returns "true" if current qubit state is identifiably within the Clifford set, or "false" if it is no...
Definition: qinterface.hpp:2905
virtual real1_f VarianceUnitaryAll(const std::vector< bitLenInt > &bits, const std::vector< std::shared_ptr< complex >> &basisOps, std::vector< real1_f > eigenVals={})
Direct measure of variance of listed (2x2 operator) single-qubit tensor product probability.
Definition: qinterface.hpp:2622
virtual real1_f VarianceUnitaryAll(const std::vector< bitLenInt > &bits, const std::vector< real1_f > &basisOps, std::vector< real1_f > eigenVals={})
Direct measure of variance of listed (3-parameter) single-qubit tensor product probability.
Definition: qinterface.hpp:2610
virtual real1_f FirstNonzeroPhase()
Get phase of lowest permutation nonzero amplitude.
Definition: qinterface.hpp:3087
virtual void SetMajorQuadrant(bool q)
Set major vs.
Definition: qinterface.hpp:3040
virtual real1_f VarianceFloatsFactorized(const std::vector< bitLenInt > &bits, const std::vector< real1_f > &weights)
Direct measure of variance of listed bit string probability.
Definition: qinterface.cpp:620
virtual real1_f VarianceBitsFactorizedRdm(bool roundRz, const std::vector< bitLenInt > &bits, const std::vector< bitCapInt > &perms, const bitCapInt &offset=ZERO_BCI)
Get (reduced density matrix) expectation value of bits, given an array of qubit weights.
Definition: qinterface.hpp:2665
virtual real1_f ExpectationUnitaryAll(const std::vector< bitLenInt > &bits, const std::vector< real1_f > &basisOps, std::vector< real1_f > eigenVals={})
Get single-qubit (3-parameter) tensor product (arbitrary real) observable.
Definition: qinterface.hpp:2708
virtual real1_f CProb(bitLenInt control, bitLenInt target)
Direct measure of bit probability to be in |1> state, if control bit is |1>.
Definition: qinterface.hpp:2487
virtual double GetUnitaryFidelity()
When "Schmidt-decomposition rounding parameter" ("SDRP") is being used, starting from initial 1....
Definition: qinterface.hpp:2950
virtual void SetSdrp(real1_f sdrp)
Set the "Schmidt decomposition rounding parameter" value, (between 0 and 1)
Definition: qinterface.hpp:2958
virtual bool TrySeparate(bitLenInt qubit1, bitLenInt qubit2)
Two-qubit TrySeparate()
Definition: qinterface.hpp:2933
virtual bool isClifford()
Returns "true" if current state is identifiably within the Clifford set, or "false" if it is not or c...
Definition: qinterface.hpp:2899
virtual void SetMajorQuadrant(bitLenInt t, bool q)
Set major vs.
Definition: qinterface.hpp:3044
virtual real1_f ProbAll(const bitCapInt &fullRegister)
Direct measure of full permutation probability.
Definition: qinterface.hpp:2538
virtual void ProbMaskAll(const bitCapInt &mask, real1 *probsArray)
Direct measure of masked permutation probability.
Definition: qinterface.cpp:423
virtual bool AreFactorized(std::vector< bitLenInt > a, std::vector< bitLenInt > b, bool flushCache=false)
Check if two vectors of qubit indices are factorized, in internal representation.
Definition: qinterface.hpp:2937
virtual void SetStochastic(bool s)
Toggle probabilistic approximate near-Clifford vs.
Definition: qinterface.hpp:3036
virtual QInterfacePtr Clone()=0
Clone this QInterface.
virtual real1_f ACProb(bitLenInt control, bitLenInt target)
Direct measure of bit probability to be in |1> state, if control bit is |0>.
Definition: qinterface.hpp:2498
virtual real1_f VariancePauliAll(std::vector< bitLenInt > bits, std::vector< Pauli > paulis)
Direct measure of variance of listed Pauli tensor product probability.
Definition: qinterface.cpp:659
virtual void Finish()
If asynchronous work is still running, block until it finishes.
Definition: qinterface.hpp:2875
virtual bool GetReactiveSeparate()
Get reactive separation option.
Definition: qinterface.hpp:2978
virtual QInterfacePtr Copy()
Copy this QInterface.
Definition: qinterface.hpp:3058
virtual real1_f ExpectationBitsAll(const std::vector< bitLenInt > &bits, const bitCapInt &offset=ZERO_BCI)
Get permutation expectation value of bits.
Definition: qinterface.hpp:2677
virtual real1_f Prob(bitLenInt qubit)=0
Direct measure of bit probability to be in |1> state.
virtual std::map< bitCapInt, int > MultiShotMeasureMask(const std::vector< bitCapInt > &qPowers, unsigned shots)
Statistical measure of masked permutation probability.
Definition: qinterface.cpp:807
virtual void SetTInjection(bool useGadget)
Set the option to use T-injection gadgets (off by default)
Definition: qinterface.hpp:2986
virtual real1_f VarianceFloatsFactorizedRdm(bool roundRz, const std::vector< bitLenInt > &bits, const std::vector< real1_f > &weights)
Direct measure of (reduced density matrix) variance of bits, given an array of qubit weights.
Definition: qinterface.hpp:2643
virtual void ResetUnitaryFidelity()
Reset the internal fidelity calculation tracker to 1.0.
Definition: qinterface.hpp:2954
virtual bool TryDecompose(bitLenInt start, QInterfacePtr dest, real1_f error_tol=TRYDECOMPOSE_EPSILON)
Attempt to Decompose() a bit range.
Definition: qinterface.cpp:836
virtual real1_f GetNoiseParameter()
Get the noise level option (only for a noisy interface)
Definition: qinterface.hpp:3020
virtual void SetUseExactNearClifford(bool useExact)
Set the option to use exact-method near-Clifford simulation (on by default)
Definition: qinterface.hpp:3001
virtual void ProbBitsAll(const std::vector< bitLenInt > &bits, real1 *probsArray)
Direct measure of listed permutation probability.
Definition: qinterface.cpp:446
virtual bool GetUseExactNearClifford()
Get the option to use exact-method near-Clifford simulation (on by default)
Definition: qinterface.hpp:3008
virtual real1_f ProbRdm(bitLenInt qubit)
Direct measure of bit probability to be in |1> state, treating all ancillary qubits as post-selected ...
Definition: qinterface.hpp:2762
bitCapIntOcl GetMaxSize()
Get maximum number of amplitudes that can be allocated on current device.
Definition: qinterface.hpp:3082
virtual bool GetTInjection()
Get the option to use T-injection gadgets.
Definition: qinterface.hpp:2994
virtual void SetSparseProbabilityFloor(real1_f p)
Set the sparse-simulation amplitude probability floor, before truncation.
Definition: qinterface.hpp:3032
virtual void SetDeviceList(std::vector< int64_t > dIDs)
Set the device index list, if more than one device is available.
Definition: qinterface.hpp:3072
virtual real1_f ProbMask(const bitCapInt &mask, const bitCapInt &permutation)
Direct measure of masked permutation probability.
Definition: qinterface.cpp:280
virtual real1_f ExpectationPauliAll(std::vector< bitLenInt > bits, std::vector< Pauli > paulis)
Get Pauli tensor product observable.
Definition: qinterface.cpp:715
virtual void SetReactiveSeparate(bool isAggSep)
Set reactive separation option (on by default if available)
Definition: qinterface.hpp:2970
virtual void UpdateRunningNorm(real1_f norm_thresh=REAL1_DEFAULT_ARG)=0
Force a calculation of the norm of the state vector, in order to make it unit length before the next ...
virtual real1_f VarianceBitsAll(const std::vector< bitLenInt > &bits, const bitCapInt &offset=ZERO_BCI)
Direct measure of variance of listed permutation probability.
Definition: qinterface.hpp:2579
virtual void FlipQuadrant(bitLenInt t)
Toggle major vs.
Definition: qinterface.hpp:3048
virtual real1_f ExpectationBitsFactorized(const std::vector< bitLenInt > &bits, const std::vector< bitCapInt > &perms, const bitCapInt &offset=ZERO_BCI)
Get expectation value of bits, given an array of qubit weights.
Definition: qinterface.cpp:542
virtual real1_f ProbMaskRdm(bool roundRz, const bitCapInt &mask, const bitCapInt &permutation)
Direct measure of masked permutation probability, treating all ancillary qubits as post-selected T ga...
Definition: qinterface.hpp:2774
virtual void SetNcrp(real1_f ncrp)
Set the "Near-clifford rounding parameter" value, (between 0 and 1)
Definition: qinterface.hpp:2962
virtual void SetDevice(int64_t dID)
Set the device index, if more than one device is available.
Definition: qinterface.hpp:3063
virtual void DepolarizingChannelWeak1Qb(bitLenInt qubit, real1_f lambda)
Simulate a local qubit depolarizing noise channel, under a stochastic "weak simulation condition....
Definition: gates.cpp:487
virtual bool isOpenCL()
Returns "true" if current simulation is OpenCL-based.
Definition: qinterface.hpp:2910
virtual real1_f ExpectationFloatsFactorized(const std::vector< bitLenInt > &bits, const std::vector< real1_f > &weights)
Get expectation value of bits, given a (floating-point) array of qubit weights.
Definition: qinterface.cpp:771
virtual real1_f ProbReg(bitLenInt start, bitLenInt length, const bitCapInt &permutation)
Direct measure of register permutation probability.
Definition: qinterface.cpp:264
virtual int64_t GetDevice()
Get the device index.
Definition: qinterface.hpp:3068
virtual bool TrySeparate(const std::vector< bitLenInt > &qubits, real1_f error_tol)
Qrack::QUnit types maintain explicit separation of representations of qubits, which reduces memory us...
Definition: qinterface.hpp:2925
virtual void SetAceMaxQubits(bitLenInt qb)
Set the "automatic circuit elision" (ACE) maximum entangled subsystem qubit count.
Definition: qinterface.hpp:3024
virtual real1_f SumSqrDiff(QInterfacePtr toCompare)=0
Calculates (1 - <\psi_e|\psi_c>) between states |\psi_c> and |\psi_e>.
virtual void SetBit(bitLenInt qubit, bool value)
Set individual bit to pure |0> (false) or |1> (true) state.
Definition: qinterface.hpp:2821
virtual real1_f ExpectationBitsAllRdm(bool roundRz, const std::vector< bitLenInt > &bits, const bitCapInt &offset=ZERO_BCI)
Get permutation expectation value of bits, treating all ancillary qubits as post-selected T gate gadg...
Definition: qinterface.hpp:2784
virtual void NormalizeState(real1_f nrm=REAL1_DEFAULT_ARG, real1_f norm_thresh=REAL1_DEFAULT_ARG, real1_f phaseArg=ZERO_R1_F)=0
Apply the normalization factor found by UpdateRunningNorm() or on the fly by a single bit gate.
virtual real1_f ExpectationFloatsFactorizedRdm(bool roundRz, const std::vector< bitLenInt > &bits, const std::vector< real1_f > &weights)
Get (reduced density matrix) expectation value of bits, given a (floating-point) array of qubit weigh...
Definition: qinterface.hpp:2752
virtual real1_f VarianceBitsAllRdm(bool roundRz, const std::vector< bitLenInt > &bits, const bitCapInt &offset=ZERO_BCI)
Direct measure of (reduced density matrix) variance of listed permutation probability.
Definition: qinterface.hpp:2590
virtual bitCapInt HighestProbAll()
Get highest probability permutation.
Definition: qinterface.hpp:2510
virtual real1_f ExpectationUnitaryAll(const std::vector< bitLenInt > &bits, const std::vector< std::shared_ptr< complex >> &basisOps, std::vector< real1_f > eigenVals={})
Get single-qubit tensor product (arbitrary real) observable.
Definition: qinterface.hpp:2696
virtual bool TrySeparate(bitLenInt qubit)
Single-qubit TrySeparate()
Definition: qinterface.hpp:2929
virtual void SetNoiseParameter(real1_f lambda)
Set the noise level option (only for a noisy interface)
Definition: qinterface.hpp:3014
virtual bool isFinished()
Returns "false" if asynchronous work is still running, and "true" if all previously dispatched asynch...
Definition: qinterface.hpp:2881
GLOSSARY: bitLenInt - "bit-length integer" - unsigned integer ID of qubit position in register bitCap...
Definition: complex16x2simd.hpp:25
bitCapInt bitRegMask(const bitLenInt &start, const bitLenInt &length)
Definition: qrack_functions.hpp:170
QRACK_CONST real1 SQRT1_2_R1
Definition: qrack_types.hpp:182
QInterfaceEngine
Enumerated list of supported engines.
Definition: qinterface.hpp:37
@ QINTERFACE_OPTIMAL_BASE
Definition: qinterface.hpp:124
@ QINTERFACE_QPAGER
Create a QPager, which breaks up the work of a QEngine into equally sized "pages.".
Definition: qinterface.hpp:82
@ QINTERFACE_OPTIMAL_SCHROEDINGER
Definition: qinterface.hpp:122
@ QINTERFACE_OPTIMAL_MULTI
Definition: qinterface.hpp:129
@ QINTERFACE_CUDA
Create a QEngineCUDA, leveraging CUDA hardware to increase the speed of certain calculations.
Definition: qinterface.hpp:52
@ QINTERFACE_STABILIZER_HYBRID
Create a QStabilizerHybrid, switching between a QStabilizer and a QHybrid as efficient.
Definition: qinterface.hpp:77
@ QINTERFACE_HYBRID
Create a QHybrid, switching between QEngineCPU and QEngineOCL as efficient.
Definition: qinterface.hpp:57
@ QINTERFACE_OPTIMAL
Definition: qinterface.hpp:127
@ QINTERFACE_BDT_HYBRID
Create a QBinaryDecisionTree, (CPU-based).
Definition: qinterface.hpp:67
@ QINTERFACE_BDT
Create a QBinaryDecisionTree, (CPU-based).
Definition: qinterface.hpp:62
@ QINTERFACE_TENSOR_NETWORK
Circuit-simplification layer.
Definition: qinterface.hpp:107
@ QINTERFACE_NOISY
Noisy wrapper layer.
Definition: qinterface.hpp:112
@ QINTERFACE_QUNIT_CLIFFORD
Clifford-specialized QUnit.
Definition: qinterface.hpp:102
@ QINTERFACE_OPENCL
Create a QEngineOCL, leveraging OpenCL hardware to increase the speed of certain calculations.
Definition: qinterface.hpp:47
@ QINTERFACE_STABILIZER
Create a QStabilizer, limited to Clifford/Pauli operations, but efficient.
Definition: qinterface.hpp:72
@ QINTERFACE_QUNIT
Create a QUnit, which utilizes other QInterface classes to minimize the amount of work that's needed ...
Definition: qinterface.hpp:91
@ QINTERFACE_QUNIT_MULTI
Create a QUnitMulti, which distributes the explicitly separated "shards" of a QUnit across available ...
Definition: qinterface.hpp:97
@ QINTERFACE_MAX
Definition: qinterface.hpp:131
@ QINTERFACE_CPU
Create a QEngineCPU leveraging only local CPU and memory resources.
Definition: qinterface.hpp:42
std::shared_ptr< QInterface > QInterfacePtr
Definition: qinterface.hpp:29
QRACK_CONST real1 SQRT2_R1
Definition: qrack_types.hpp:181
QRACK_CONST real1_f TRYDECOMPOSE_EPSILON
Definition: qrack_types.hpp:265
QRACK_CONST complex C_SQRT1_2_NEG
Definition: gates.cpp:21
void seed(quid sid, unsigned s)
"Seed" random number generator (if pseudo-random Mersenne twister is in use)
Definition: wasm_api.cpp:873
half_float::half real1
Definition: qrack_types.hpp:106
std::complex< real1 > complex
Definition: qrack_types.hpp:140
QRACK_CONST real1 FP_NORM_EPSILON
Definition: qrack_types.hpp:263
bitCapInt pow2(const bitLenInt &p)
Definition: qrack_functions.hpp:156
double norm(const complex2 &c)
Definition: complex16x2simd.hpp:122
QRACK_CONST real1 REAL1_EPSILON
Definition: qrack_types.hpp:203
QRACK_CONST complex ONE_CMPLX
Definition: qrack_types.hpp:257
QRACK_CONST real1 ONE_R1
Definition: qrack_types.hpp:188
std::vector< HamiltonianOpPtr > Hamiltonian
Definition: hamiltonian.hpp:122
QRACK_CONST real1 ZERO_R1
Definition: qrack_types.hpp:186
float real1_f
Definition: qrack_types.hpp:107
QRACK_CONST complex CMPLX_DEFAULT_ARG
Definition: qrack_types.hpp:262
QRACK_CONST complex I_CMPLX
Definition: qrack_types.hpp:259
QRACK_CONST complex C_SQRT1_2
Definition: gates.cpp:17
QRACK_CONST complex ZERO_CMPLX
Definition: qrack_types.hpp:258
QRACK_CONST real1 PI_R1
Definition: qrack_types.hpp:180
const bitCapInt ONE_BCI
Definition: qrack_types.hpp:141
const bitCapInt ZERO_BCI
Definition: qrack_types.hpp:142
bitCapIntOcl pow2Ocl(const bitLenInt &p)
Definition: qrack_functions.hpp:157
unsigned int gamma(unsigned int arg)
Gamma function and postprocessing.
Definition: half.hpp:2101
HALF_CONSTEXPR half abs(half arg)
Absolute value.
Definition: half.hpp:2958
half sin(half arg)
Sine function.
Definition: half.hpp:3868
half cos(half arg)
Cosine function.
Definition: half.hpp:3905
half pow(half x, half y)
Power function.
Definition: half.hpp:3721
half exp(half arg)
Exponential function.
Definition: half.hpp:3184
#define REAL1_DEFAULT_ARG
Definition: qrack_types.hpp:179
#define QRACK_CONST
Definition: qrack_types.hpp:176
#define bitLenInt
Definition: qrack_types.hpp:41
#define ZERO_R1_F
Definition: qrack_types.hpp:162
#define qrack_rand_gen_ptr
Definition: qrack_types.hpp:158
#define bitCapInt
Definition: qrack_types.hpp:65
#define bitCapIntOcl
Definition: qrack_types.hpp:53
#define ONE_R1_F
Definition: qrack_types.hpp:165
#define IS_NORM_0(c)
Definition: qrack_types.hpp:28
#define C_I_SQRT1_2
Definition: qunitclifford.cpp:412