Qrack  9.0
General classical-emulating-quantum development framework
qunit.hpp
Go to the documentation of this file.
1 //
3 // (C) Daniel Strano and the Qrack contributors 2017-2023. All rights reserved.
4 //
5 // QUnit maintains explicit separability of qubits as an optimization on a QEngine.
6 // See https://arxiv.org/abs/1710.05867
7 // (The makers of Qrack have no affiliation with the authors of that paper.)
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 "qengineshard.hpp"
16 #include "qparity.hpp"
17 
18 #if ENABLE_ALU
19 #include "qalu.hpp"
20 #endif
21 
22 namespace Qrack {
23 
24 class QUnit;
25 typedef std::shared_ptr<QUnit> QUnitPtr;
26 
27 #if ENABLE_ALU
28 class QUnit : public QAlu, public QParity, public QInterface {
29 #else
30 class QUnit : public QParity, public QInterface {
31 #endif
32 protected:
34  bool useHostRam;
35  bool isSparse;
37  bool useTGadget;
40  double logFidelity;
41  int64_t devID;
44  std::vector<int64_t> deviceIDs;
45  std::vector<QInterfaceEngine> engines;
46 
48 
49 public:
50  QUnit(std::vector<QInterfaceEngine> eng, bitLenInt qBitCount, bitCapInt initState = 0U,
51  qrack_rand_gen_ptr rgp = nullptr, complex phaseFac = CMPLX_DEFAULT_ARG, bool doNorm = false,
52  bool randomGlobalPhase = true, bool useHostMem = false, int64_t deviceId = -1, bool useHardwareRNG = true,
53  bool useSparseStateVec = false, real1_f norm_thresh = REAL1_EPSILON, std::vector<int64_t> devIDs = {},
54  bitLenInt qubitThreshold = 0U, real1_f separation_thresh = FP_NORM_EPSILON_F);
55 
56  QUnit(bitLenInt qBitCount, bitCapInt initState = 0U, qrack_rand_gen_ptr rgp = nullptr,
57  complex phaseFac = CMPLX_DEFAULT_ARG, bool doNorm = false, bool randomGlobalPhase = true,
58  bool useHostMem = false, int64_t deviceId = -1, bool useHardwareRNG = true, bool useSparseStateVec = false,
59  real1_f norm_thresh = REAL1_EPSILON, std::vector<int64_t> devIDs = {}, bitLenInt qubitThreshold = 0U,
60  real1_f separation_thresh = FP_NORM_EPSILON_F)
61  : QUnit({ QINTERFACE_STABILIZER_HYBRID }, qBitCount, initState, rgp, phaseFac, doNorm, randomGlobalPhase,
62  useHostMem, deviceId, useHardwareRNG, useSparseStateVec, norm_thresh, devIDs, qubitThreshold,
63  separation_thresh)
64  {
65  }
66 
67  virtual ~QUnit() { Dump(); }
68 
69  virtual void SetConcurrency(uint32_t threadsPerEngine)
70  {
71  QInterface::SetConcurrency(threadsPerEngine);
73  [](QInterfacePtr unit, real1_f unused1, real1_f unused2, real1_f unused3, int64_t threadsPerEngine) {
74  unit->SetConcurrency((uint32_t)threadsPerEngine);
75  return true;
76  },
77  ZERO_R1_F, ZERO_R1_F, ZERO_R1_F, threadsPerEngine);
78  }
79 
80  virtual void SetTInjection(bool useGadget)
81  {
82  useTGadget = useGadget;
84  [](QInterfacePtr unit, real1_f unused1, real1_f unused2, real1_f unused3, int64_t useGadget) {
85  unit->SetTInjection((bool)useGadget);
86  return true;
87  },
88  ZERO_R1_F, ZERO_R1_F, ZERO_R1_F, useGadget ? 1U : 0U);
89  }
90 
91  virtual void SetReactiveSeparate(bool isAggSep) { isReactiveSeparate = isAggSep; }
92  virtual bool GetReactiveSeparate() { return isReactiveSeparate; }
93 
94  virtual void SetDevice(int64_t dID);
95  virtual int64_t GetDevice() { return devID; }
96 
98  {
99  const QEngineShard& shard = shards[qubit];
100  if (!shard.unit) {
101  return Prob(qubit);
102  }
103 
104  return shard.unit->ProbRdm(qubit);
105  }
106  virtual real1_f CProbRdm(bitLenInt control, bitLenInt target)
107  {
108  AntiCNOT(control, target);
109  const real1_f prob = ProbRdm(target);
110  AntiCNOT(control, target);
111 
112  return prob;
113  }
114  virtual real1_f ACProbRdm(bitLenInt control, bitLenInt target)
115  {
116  CNOT(control, target);
117  const real1_f prob = ProbRdm(target);
118  CNOT(control, target);
119 
120  return prob;
121  }
122 
123  virtual void SetQuantumState(const complex* inputState);
124  virtual void GetQuantumState(complex* outputState);
125  virtual void GetProbs(real1* outputProbs);
126  virtual complex GetAmplitude(bitCapInt perm);
127  virtual void SetAmplitude(bitCapInt perm, complex amp)
128  {
129  if (perm >= maxQPower) {
130  throw std::invalid_argument("QUnit::SetAmplitude argument out-of-bounds!");
131  }
132 
133  EntangleAll();
134  shards[0U].unit->SetAmplitude(perm, amp);
135  }
136  virtual void SetPermutation(bitCapInt perm, complex phaseFac = CMPLX_DEFAULT_ARG);
137  using QInterface::Compose;
138  virtual bitLenInt Compose(QUnitPtr toCopy) { return Compose(toCopy, qubitCount); }
139  virtual bitLenInt Compose(QInterfacePtr toCopy) { return Compose(std::dynamic_pointer_cast<QUnit>(toCopy)); }
140  virtual bitLenInt Compose(QUnitPtr toCopy, bitLenInt start)
141  {
142  if (start > qubitCount) {
143  throw std::invalid_argument("QUnit::Compose start index is out-of-bounds!");
144  }
145 
146  /* Create a clone of the quantum state in toCopy. */
147  QUnitPtr clone = std::dynamic_pointer_cast<QUnit>(toCopy->Clone());
148 
149  /* Insert the new shards in the middle */
150  shards.insert(start, clone->shards);
151 
152  SetQubitCount(qubitCount + toCopy->GetQubitCount());
153 
154  return start;
155  }
156  virtual bitLenInt Compose(QInterfacePtr toCopy, bitLenInt start)
157  {
158  return Compose(std::dynamic_pointer_cast<QUnit>(toCopy), start);
159  }
160  virtual void Decompose(bitLenInt start, QInterfacePtr dest)
161  {
162  Decompose(start, std::dynamic_pointer_cast<QUnit>(dest));
163  }
164  virtual void Decompose(bitLenInt start, QUnitPtr dest) { Detach(start, dest->GetQubitCount(), dest); }
166  {
167  QUnitPtr dest = std::make_shared<QUnit>(engines, length, 0U, rand_generator, phaseFactor, doNormalize,
170 
171  Decompose(start, dest);
172 
173  return dest;
174  }
175  virtual void Dispose(bitLenInt start, bitLenInt length) { Detach(start, length, nullptr); }
176  virtual void Dispose(bitLenInt start, bitLenInt length, bitCapInt disposedPerm) { Detach(start, length, nullptr); }
177  using QInterface::Allocate;
178  virtual bitLenInt Allocate(bitLenInt start, bitLenInt length);
179 
185  using QInterface::H;
186  virtual void H(bitLenInt target);
187  using QInterface::S;
188  virtual void S(bitLenInt target);
189  using QInterface::IS;
190  virtual void IS(bitLenInt target);
191 
192  virtual void ZMask(bitCapInt mask) { PhaseParity(PI_R1, mask); }
193  virtual void PhaseParity(real1 radians, bitCapInt mask);
194 
195  virtual void Phase(complex topLeft, complex bottomRight, bitLenInt qubitIndex);
196  virtual void Invert(complex topRight, complex bottomLeft, bitLenInt qubitIndex);
197  virtual void MCPhase(const std::vector<bitLenInt>& controls, complex topLeft, complex bottomRight, bitLenInt target)
198  {
199  UCPhase(controls, topLeft, bottomRight, target, pow2(controls.size()) - 1U);
200  }
201  virtual void MCInvert(
202  const std::vector<bitLenInt>& controls, complex topRight, complex bottomLeft, bitLenInt target)
203  {
204  UCInvert(controls, topRight, bottomLeft, target, pow2(controls.size()) - 1U);
205  }
206  virtual void MACPhase(
207  const std::vector<bitLenInt>& controls, complex topLeft, complex bottomRight, bitLenInt target)
208  {
209  UCPhase(controls, topLeft, bottomRight, target, 0U);
210  }
211  virtual void MACInvert(
212  const std::vector<bitLenInt>& controls, complex topRight, complex bottomLeft, bitLenInt target)
213  {
214  UCInvert(controls, topRight, bottomLeft, target, 0U);
215  }
216  virtual void UCPhase(const std::vector<bitLenInt>& controls, complex topLeft, complex bottomRight, bitLenInt target,
217  bitCapInt controlPerm);
218  virtual void UCInvert(const std::vector<bitLenInt>& controls, complex topRight, complex bottomLeft,
219  bitLenInt target, bitCapInt controlPerm);
220  virtual void Mtrx(const complex* mtrx, bitLenInt qubit);
221  virtual void MCMtrx(const std::vector<bitLenInt>& controls, const complex* mtrx, bitLenInt target)
222  {
223  UCMtrx(controls, mtrx, target, pow2(controls.size()) - 1U);
224  }
225  virtual void MACMtrx(const std::vector<bitLenInt>& controls, const complex* mtrx, bitLenInt target)
226  {
227  UCMtrx(controls, mtrx, target, 0U);
228  }
229  virtual void UCMtrx(
230  const std::vector<bitLenInt>& controls, const complex* mtrx, bitLenInt target, bitCapInt controlPerm);
232  virtual void UniformlyControlledSingleBit(const std::vector<bitLenInt>& controls, bitLenInt qubitIndex,
233  const complex* mtrxs, const std::vector<bitCapInt>& mtrxSkipPowers, bitCapInt mtrxSkipValueMask);
234  virtual void CUniformParityRZ(const std::vector<bitLenInt>& controls, bitCapInt mask, real1_f angle);
235  virtual void CSwap(const std::vector<bitLenInt>& controls, bitLenInt qubit1, bitLenInt qubit2);
236  virtual void AntiCSwap(const std::vector<bitLenInt>& controls, bitLenInt qubit1, bitLenInt qubit2);
237  virtual void CSqrtSwap(const std::vector<bitLenInt>& controls, bitLenInt qubit1, bitLenInt qubit2);
238  virtual void AntiCSqrtSwap(const std::vector<bitLenInt>& controls, bitLenInt qubit1, bitLenInt qubit2);
239  virtual void CISqrtSwap(const std::vector<bitLenInt>& controls, bitLenInt qubit1, bitLenInt qubit2);
240  virtual void AntiCISqrtSwap(const std::vector<bitLenInt>& controls, bitLenInt qubit1, bitLenInt qubit2);
241  using QInterface::ForceM;
242  virtual bool ForceM(bitLenInt qubitIndex, bool result, bool doForce = true, bool doApply = true);
243  using QInterface::ForceMReg;
244  virtual bitCapInt ForceMReg(
245  bitLenInt start, bitLenInt length, bitCapInt result, bool doForce = true, bool doApply = true);
246  virtual bitCapInt MAll();
247  virtual std::map<bitCapInt, int> MultiShotMeasureMask(const std::vector<bitCapInt>& qPowers, unsigned shots);
248  virtual void MultiShotMeasureMask(
249  const std::vector<bitCapInt>& qPowers, unsigned shots, unsigned long long* shotsArray);
250 
253 #if ENABLE_ALU
254  using QInterface::M;
255  virtual bool M(bitLenInt q) { return QInterface::M(q); }
256  using QInterface::X;
257  virtual void X(bitLenInt q) { QInterface::X(q); }
258 
265  virtual void DEC(bitCapInt toSub, bitLenInt start, bitLenInt length) { QInterface::DEC(toSub, start, length); }
266  virtual void DECS(bitCapInt toSub, bitLenInt start, bitLenInt length, bitLenInt overflowIndex)
267  {
268  QInterface::DECS(toSub, start, length, overflowIndex);
269  }
270  virtual void CDEC(bitCapInt toSub, bitLenInt inOutStart, bitLenInt length, const std::vector<bitLenInt>& controls)
271  {
272  QInterface::CDEC(toSub, inOutStart, length, controls);
273  }
274  virtual void INCDECC(bitCapInt toAdd, bitLenInt start, bitLenInt length, bitLenInt carryIndex)
275  {
276  QInterface::INCDECC(toAdd, start, length, carryIndex);
277  }
278  virtual void MULModNOut(bitCapInt toMul, bitCapInt modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length)
279  {
280  QInterface::MULModNOut(toMul, modN, inStart, outStart, length);
281  }
282  virtual void IMULModNOut(bitCapInt toMul, bitCapInt modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length)
283  {
284  QInterface::IMULModNOut(toMul, modN, inStart, outStart, length);
285  }
286  virtual void CMULModNOut(bitCapInt toMul, bitCapInt modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length,
287  const std::vector<bitLenInt>& controls)
288  {
289  QInterface::CMULModNOut(toMul, modN, inStart, outStart, length, controls);
290  }
291  virtual void CIMULModNOut(bitCapInt toMul, bitCapInt modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length,
292  const std::vector<bitLenInt>& controls)
293  {
294  QInterface::CIMULModNOut(toMul, modN, inStart, outStart, length, controls);
295  }
296 
297  virtual void INC(bitCapInt toAdd, bitLenInt start, bitLenInt length);
298  virtual void CINC(bitCapInt toAdd, bitLenInt inOutStart, bitLenInt length, const std::vector<bitLenInt>& controls);
299  virtual void INCC(bitCapInt toAdd, bitLenInt start, bitLenInt length, bitLenInt carryIndex);
300  virtual void INCS(bitCapInt toAdd, bitLenInt start, bitLenInt length, bitLenInt overflowIndex);
301  virtual void INCDECSC(
302  bitCapInt toAdd, bitLenInt start, bitLenInt length, bitLenInt overflowIndex, bitLenInt carryIndex);
303  virtual void INCDECSC(bitCapInt toAdd, bitLenInt start, bitLenInt length, bitLenInt carryIndex);
304  virtual void DECC(bitCapInt toSub, bitLenInt start, bitLenInt length, bitLenInt carryIndex);
305 #if ENABLE_BCD
306  virtual void INCBCD(bitCapInt toAdd, bitLenInt start, bitLenInt length);
307  virtual void DECBCD(bitCapInt toAdd, bitLenInt start, bitLenInt length);
308  virtual void INCDECBCDC(bitCapInt toSub, bitLenInt start, bitLenInt length, bitLenInt carryIndex);
309 #endif
310  virtual void MUL(bitCapInt toMul, bitLenInt inOutStart, bitLenInt carryStart, bitLenInt length);
311  virtual void DIV(bitCapInt toDiv, bitLenInt inOutStart, bitLenInt carryStart, bitLenInt length);
312  virtual void POWModNOut(bitCapInt base, bitCapInt modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length);
313  virtual void CMUL(bitCapInt toMul, bitLenInt inOutStart, bitLenInt carryStart, bitLenInt length,
314  const std::vector<bitLenInt>& controls);
315  virtual void CDIV(bitCapInt toDiv, bitLenInt inOutStart, bitLenInt carryStart, bitLenInt length,
316  const std::vector<bitLenInt>& controls);
317  virtual void CPOWModNOut(bitCapInt base, bitCapInt modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length,
318  const std::vector<bitLenInt>& controls);
319  virtual bitCapInt IndexedLDA(bitLenInt indexStart, bitLenInt indexLength, bitLenInt valueStart,
320  bitLenInt valueLength, const unsigned char* values, bool resetValue = true);
321  virtual bitCapInt IndexedADC(bitLenInt indexStart, bitLenInt indexLength, bitLenInt valueStart,
322  bitLenInt valueLength, bitLenInt carryIndex, const unsigned char* values);
323  virtual bitCapInt IndexedSBC(bitLenInt indexStart, bitLenInt indexLength, bitLenInt valueStart,
324  bitLenInt valueLength, bitLenInt carryIndex, const unsigned char* values);
325  virtual void Hash(bitLenInt start, bitLenInt length, const unsigned char* values);
326  virtual void CPhaseFlipIfLess(bitCapInt greaterPerm, bitLenInt start, bitLenInt length, bitLenInt flagIndex);
327  virtual void PhaseFlipIfLess(bitCapInt greaterPerm, bitLenInt start, bitLenInt length);
328 
330 #endif
331 
338  virtual void SetReg(bitLenInt start, bitLenInt length, bitCapInt value);
339  virtual void Swap(bitLenInt qubit1, bitLenInt qubit2)
340  {
341  if (qubit1 >= qubitCount) {
342  throw std::invalid_argument("QUnit::Swap qubit index parameter must be within allocated qubit bounds!");
343  }
344 
345  if (qubit2 >= qubitCount) {
346  throw std::invalid_argument("QUnit::Swap qubit index parameter must be within allocated qubit bounds!");
347  }
348 
349  if (qubit1 == qubit2) {
350  return;
351  }
352 
353  // Simply swap the bit mapping.
354  shards.swap(qubit1, qubit2);
355  }
356  virtual void ISwap(bitLenInt qubit1, bitLenInt qubit2) { EitherISwap(qubit1, qubit2, false); }
357  virtual void IISwap(bitLenInt qubit1, bitLenInt qubit2) { EitherISwap(qubit1, qubit2, true); }
358  virtual void SqrtSwap(bitLenInt qubit1, bitLenInt qubit2);
359  virtual void ISqrtSwap(bitLenInt qubit1, bitLenInt qubit2);
360  virtual void FSim(real1_f theta, real1_f phi, bitLenInt qubit1, bitLenInt qubit2);
361 
370  virtual real1_f Prob(bitLenInt qubit)
371  {
372  if (qubit >= qubitCount) {
373  throw std::invalid_argument("QUnit::Prob target parameter must be within allocated qubit bounds!");
374  }
375 
376  ToPermBasisProb(qubit);
377  return ProbBase(qubit);
378  }
379  virtual real1_f ProbAll(bitCapInt perm) { return clampProb((real1_f)norm(GetAmplitudeOrProb(perm, true))); }
380  virtual real1_f ProbAllRdm(bool roundRz, bitCapInt perm)
381  {
382  if (shards[0U].unit && (shards[0U].unit->GetQubitCount() == qubitCount)) {
383  OrderContiguous(shards[0U].unit);
384  return shards[0U].unit->ProbAllRdm(roundRz, perm);
385  }
386 
387  QUnitPtr clone = std::dynamic_pointer_cast<QUnit>(Clone());
388  QInterfacePtr unit = clone->EntangleAll(true);
389  clone->OrderContiguous(unit);
390 
391  return unit->ProbAllRdm(roundRz, perm);
392  }
393  virtual real1_f ProbParity(bitCapInt mask);
394  virtual bool ForceMParity(bitCapInt mask, bool result, bool doForce = true);
395  virtual real1_f SumSqrDiff(QInterfacePtr toCompare)
396  {
397  return SumSqrDiff(std::dynamic_pointer_cast<QUnit>(toCompare));
398  }
399  virtual real1_f SumSqrDiff(QUnitPtr toCompare);
401  const std::vector<bitLenInt>& bits, const std::vector<bitCapInt>& perms, bitCapInt offset = 0U)
402  {
403  return ExpectationFactorized(false, false, bits, perms, std::vector<real1_f>(), offset, false);
404  }
406  bool roundRz, const std::vector<bitLenInt>& bits, const std::vector<bitCapInt>& perms, bitCapInt offset = 0U)
407  {
408  return ExpectationFactorized(true, false, bits, perms, std::vector<real1_f>(), offset, roundRz);
409  }
410  virtual void UpdateRunningNorm(real1_f norm_thresh = REAL1_DEFAULT_ARG);
411  virtual void NormalizeState(
412  real1_f nrm = REAL1_DEFAULT_ARG, real1_f norm_thresh = REAL1_DEFAULT_ARG, real1_f phaseArg = ZERO_R1_F);
413  virtual void Finish();
414  virtual bool isFinished();
415  virtual void Dump()
416  {
417  for (size_t i = 0U; i < shards.size(); ++i) {
418  shards[i].unit = NULL;
419  }
420  }
422  virtual bool isClifford(bitLenInt qubit) { return shards[qubit].isClifford(); };
423 
424  virtual bool TrySeparate(const std::vector<bitLenInt>& qubits, real1_f error_tol);
425  virtual bool TrySeparate(bitLenInt qubit);
426  virtual bool TrySeparate(bitLenInt qubit1, bitLenInt qubit2);
427  virtual double GetUnitaryFidelity() { return exp(logFidelity); }
428  virtual void ResetUnitaryFidelity() { logFidelity = 0.0; }
429  virtual void SetSdrp(real1_f sdrp) { separabilityThreshold = sdrp; };
430 
431  virtual QInterfacePtr Clone();
432 
435 protected:
436  virtual complex GetAmplitudeOrProb(bitCapInt perm, bool isProb);
437 
438  virtual void XBase(bitLenInt target);
439  virtual void YBase(bitLenInt target);
440  virtual void ZBase(bitLenInt target);
441  virtual real1_f ProbBase(bitLenInt qubit);
442 
443  virtual bool TrySeparateClifford(bitLenInt qubit);
444 
445  virtual void EitherISwap(bitLenInt qubit1, bitLenInt qubit2, bool isInverse);
446 
447 #if ENABLE_ALU
450  typedef void (QAlu::*CMULFn)(bitCapInt toMod, bitLenInt start, bitLenInt carryStart, bitLenInt length,
451  const std::vector<bitLenInt>& controls);
452  typedef void (QAlu::*CMULModFn)(bitCapInt toMod, bitCapInt modN, bitLenInt start, bitLenInt carryStart,
453  bitLenInt length, const std::vector<bitLenInt>& controls);
454  void INT(bitCapInt toMod, bitLenInt start, bitLenInt length, bitLenInt carryIndex, bool hasCarry,
455  std::vector<bitLenInt> controlVec = std::vector<bitLenInt>());
456  void INTS(bitCapInt toMod, bitLenInt start, bitLenInt length, bitLenInt overflowIndex, bitLenInt carryIndex,
457  bool hasCarry);
458  void INCx(INCxFn fn, bitCapInt toMod, bitLenInt start, bitLenInt length, bitLenInt flagIndex);
459  void INCxx(
460  INCxxFn fn, bitCapInt toMod, bitLenInt start, bitLenInt length, bitLenInt flag1Index, bitLenInt flag2Index);
461  QInterfacePtr CMULEntangle(std::vector<bitLenInt> controlVec, bitLenInt start, bitLenInt carryStart,
462  bitLenInt length, std::vector<bitLenInt>* controlsMapped);
463  std::vector<bitLenInt> CMULEntangle(
464  std::vector<bitLenInt> controlVec, bitLenInt start, bitCapInt carryStart, bitLenInt length);
465  void CMULx(CMULFn fn, bitCapInt toMod, bitLenInt start, bitLenInt carryStart, bitLenInt length,
466  std::vector<bitLenInt> controlVec);
467  void CMULModx(CMULModFn fn, bitCapInt toMod, bitCapInt modN, bitLenInt start, bitLenInt carryStart,
468  bitLenInt length, std::vector<bitLenInt> controlVec);
469  bool INTCOptimize(bitCapInt toMod, bitLenInt start, bitLenInt length, bool isAdd, bitLenInt carryIndex);
470  bool INTSOptimize(bitCapInt toMod, bitLenInt start, bitLenInt length, bool isAdd, bitLenInt overflowIndex);
471  bool INTSCOptimize(
472  bitCapInt toMod, bitLenInt start, bitLenInt length, bool isAdd, bitLenInt carryIndex, bitLenInt overflowIndex);
473  bitCapInt GetIndexedEigenstate(bitLenInt indexStart, bitLenInt indexLength, bitLenInt valueStart,
474  bitLenInt valueLength, const unsigned char* values);
475  bitCapInt GetIndexedEigenstate(bitLenInt start, bitLenInt length, const unsigned char* values);
476 #endif
477 
478  real1_f ExpectationFactorized(bool isRdm, bool isFloat, const std::vector<bitLenInt>& bits,
479  const std::vector<bitCapInt>& perms, const std::vector<real1_f>& weights, bitCapInt offset, bool roundRz)
480  {
481  if ((isFloat && (weights.size() < bits.size())) || (!isFloat && (perms.size() < bits.size()))) {
482  throw std::invalid_argument("QUnit::ExpectationFactorized() must supply at least as many weights as bits!");
483  }
484 
486  "QUnit::ExpectationFactorized parameter qubits vector values must be within allocated qubit bounds!");
487 
488  if (shards[0U].unit && (shards[0U].unit->GetQubitCount() == qubitCount)) {
489  OrderContiguous(shards[0U].unit);
490  return isFloat ? (isRdm ? shards[0U].unit->ExpectationFloatsFactorizedRdm(roundRz, bits, weights)
491  : shards[0U].unit->ExpectationFloatsFactorized(bits, weights))
492  : (isRdm ? shards[0U].unit->ExpectationBitsFactorizedRdm(roundRz, bits, perms, offset)
493  : shards[0U].unit->ExpectationBitsFactorized(bits, perms, offset));
494  }
495 
496  QUnitPtr clone = std::dynamic_pointer_cast<QUnit>(Clone());
497  QInterfacePtr unit = clone->EntangleAll(true);
498  clone->OrderContiguous(unit);
499 
500  return isFloat ? (isRdm ? unit->ExpectationFloatsFactorizedRdm(roundRz, bits, weights)
501  : unit->ExpectationFloatsFactorized(bits, weights))
502  : (isRdm ? unit->ExpectationBitsFactorizedRdm(roundRz, bits, perms, offset)
503  : unit->ExpectationBitsFactorized(bits, perms, offset));
504  }
505 
506  virtual QInterfacePtr Entangle(std::vector<bitLenInt> bits);
507  virtual QInterfacePtr Entangle(std::vector<bitLenInt*> bits);
508  virtual QInterfacePtr EntangleRange(bitLenInt start, bitLenInt length, bool isForProb = false);
509  virtual QInterfacePtr EntangleRange(bitLenInt start, bitLenInt length, bitLenInt start2, bitLenInt length2);
511  bitLenInt start, bitLenInt length, bitLenInt start2, bitLenInt length2, bitLenInt start3, bitLenInt length3);
512  virtual QInterfacePtr EntangleAll(bool isForProb = false)
513  {
514  QInterfacePtr toRet = EntangleRange(0, qubitCount, isForProb);
515  OrderContiguous(toRet);
516  return toRet;
517  }
518 
519  virtual QInterfacePtr CloneBody(QUnitPtr copyPtr);
520 
521  virtual bool CheckBitsPermutation(bitLenInt start, bitLenInt length = 1);
522  virtual bitCapInt GetCachedPermutation(bitLenInt start, bitLenInt length);
523  virtual bitCapInt GetCachedPermutation(const std::vector<bitLenInt>& bitArray);
524  virtual bool CheckBitsPlus(bitLenInt qubitIndex, bitLenInt length);
525 
527  std::vector<bitLenInt*>::iterator first, std::vector<bitLenInt*>::iterator last);
528 
529  typedef bool (*ParallelUnitFn)(QInterfacePtr unit, real1_f param1, real1_f param2, real1_f param3, int64_t param4);
531  real1_f param3 = ZERO_R1_F, int64_t param4 = 0);
532 
533  virtual bool SeparateBit(bool value, bitLenInt qubit);
534 
535  void OrderContiguous(QInterfacePtr unit);
536 
537  virtual void Detach(bitLenInt start, bitLenInt length, QUnitPtr dest);
538 
539  struct QSortEntry {
542  bool operator<(const QSortEntry& rhs) { return mapped < rhs.mapped; }
543  bool operator>(const QSortEntry& rhs) { return mapped > rhs.mapped; }
544  };
545  void SortUnit(QInterfacePtr unit, std::vector<QSortEntry>& bits, bitLenInt low, bitLenInt high);
546 
547  bool TrimControls(const std::vector<bitLenInt>& controls, std::vector<bitLenInt>& controlVec, bitCapInt* perm);
548 
549  template <typename CF>
551  std::vector<bitLenInt> controlVec, const std::vector<bitLenInt> targets, CF cfn, bool isPhase);
552 
553  void ClampShard(bitLenInt qubit)
554  {
555  QEngineShard& shard = shards[qubit];
556  if (!shard.ClampAmps() || !shard.unit) {
557  return;
558  }
559 
560  if (IS_NORM_0(shard.amp1)) {
561  logFidelity += (double)log(clampProb(ONE_R1_F - norm(shard.amp1)));
562  SeparateBit(false, qubit);
563  } else if (IS_NORM_0(shard.amp0)) {
564  logFidelity += (double)log(clampProb(ONE_R1_F - norm(shard.amp0)));
565  SeparateBit(true, qubit);
566  }
567  }
568 
569  void TransformX2x2(const complex* mtrxIn, complex* mtrxOut)
570  {
571  mtrxOut[0U] = (real1)(ONE_R1 / 2) * (complex)(mtrxIn[0U] + mtrxIn[1U] + mtrxIn[2U] + mtrxIn[3U]);
572  mtrxOut[1U] = (real1)(ONE_R1 / 2) * (complex)(mtrxIn[0U] - mtrxIn[1U] + mtrxIn[2U] - mtrxIn[3U]);
573  mtrxOut[2U] = (real1)(ONE_R1 / 2) * (complex)(mtrxIn[0U] + mtrxIn[1U] - mtrxIn[2U] - mtrxIn[3U]);
574  mtrxOut[3U] = (real1)(ONE_R1 / 2) * (complex)(mtrxIn[0U] - mtrxIn[1U] - mtrxIn[2U] + mtrxIn[3U]);
575  }
576 
577  void TransformXInvert(complex topRight, complex bottomLeft, complex* mtrxOut)
578  {
579  mtrxOut[0U] = (real1)(ONE_R1 / 2) * (complex)(topRight + bottomLeft);
580  mtrxOut[1U] = (real1)(ONE_R1 / 2) * (complex)(-topRight + bottomLeft);
581  mtrxOut[2U] = -mtrxOut[1U];
582  mtrxOut[3U] = -mtrxOut[0U];
583  }
584 
585  void TransformY2x2(const complex* mtrxIn, complex* mtrxOut)
586  {
587  mtrxOut[0U] = (real1)(ONE_R1 / 2) * (complex)(mtrxIn[0U] + I_CMPLX * (mtrxIn[1U] - mtrxIn[2U]) + mtrxIn[3U]);
588  mtrxOut[1U] = (real1)(ONE_R1 / 2) * (complex)(mtrxIn[0U] - I_CMPLX * (mtrxIn[1U] + mtrxIn[2U]) - mtrxIn[3U]);
589  mtrxOut[2U] = (real1)(ONE_R1 / 2) * (complex)(mtrxIn[0U] + I_CMPLX * (mtrxIn[1U] + mtrxIn[2U]) - mtrxIn[3U]);
590  mtrxOut[3U] = (real1)(ONE_R1 / 2) * (complex)(mtrxIn[0U] - I_CMPLX * (mtrxIn[1U] - mtrxIn[2U]) + mtrxIn[3U]);
591  }
592 
593  void TransformYInvert(complex topRight, complex bottomLeft, complex* mtrxOut)
594  {
595  mtrxOut[0U] = I_CMPLX * (real1)(ONE_R1 / 2) * (complex)(topRight - bottomLeft);
596  mtrxOut[1U] = I_CMPLX * (real1)(ONE_R1 / 2) * (complex)(-topRight - bottomLeft);
597  mtrxOut[2U] = -mtrxOut[1U];
598  mtrxOut[3U] = -mtrxOut[0U];
599  }
600 
601  void TransformPhase(complex topLeft, complex bottomRight, complex* mtrxOut)
602  {
603  mtrxOut[0U] = (real1)(ONE_R1 / 2) * (complex)(topLeft + bottomRight);
604  mtrxOut[1U] = (real1)(ONE_R1 / 2) * (complex)(topLeft - bottomRight);
605  mtrxOut[2U] = mtrxOut[1U];
606  mtrxOut[3U] = mtrxOut[0U];
607  }
608 
610  {
611  QEngineShard& shard = shards[i];
612  if (shard.pauliBasis != PauliX) {
613  // Recursive call that should be blocked,
614  // or already in target basis.
615  return;
616  }
617 
618  ConvertZToX(i);
619  }
620 
622  {
623  QEngineShard& shard = shards[i];
624 
625  if (shard.pauliBasis != PauliY) {
626  // Recursive call that should be blocked,
627  // or already in target basis.
628  return;
629  }
630 
631  shard.pauliBasis = PauliX;
632 
633  QRACK_CONST complex diag = complex((real1)(ONE_R1 / 2), (real1)(ONE_R1 / 2));
634  QRACK_CONST complex cross = complex((real1)(ONE_R1 / 2), (real1)(-ONE_R1 / 2));
635  QRACK_CONST complex mtrx[4U]{ diag, cross, cross, diag };
636 
637  if (shard.unit) {
638  shard.unit->Mtrx(mtrx, shard.mapped);
639  }
640 
641  if (shard.isPhaseDirty || shard.isProbDirty) {
642  shard.isProbDirty = true;
643  return;
644  }
645 
646  complex Y0 = shard.amp0;
647 
648  shard.amp0 = (mtrx[0U] * Y0) + (mtrx[1U] * shard.amp1);
649  shard.amp1 = (mtrx[2U] * Y0) + (mtrx[3U] * shard.amp1);
650  ClampShard(i);
651  }
652 
654  {
655  QEngineShard& shard = shards[i];
656 
657  if (shard.pauliBasis == PauliY) {
658  ConvertYToZ(i);
659  } else {
660  RevertBasisX(i);
661  }
662  }
663 
665  {
666  QEngineShard& shard = shards[i];
667  if (shard.pauliBasis == PauliZ) {
668  ConvertZToX(i);
669  } else if (shard.pauliBasis == PauliY) {
670  RevertBasisY(i);
671  }
672  }
673 
675  {
676  QEngineShard& shard = shards[i];
677  if (shard.pauliBasis == PauliZ) {
678  ConvertZToY(i);
679  } else if (shard.pauliBasis == PauliX) {
680  ConvertXToY(i);
681  }
682  }
683 
684  void ConvertZToX(bitLenInt i);
685  void ConvertXToY(bitLenInt i);
686  void ConvertYToZ(bitLenInt i);
687  void ConvertZToY(bitLenInt i);
688  void ShardAI(bitLenInt qubit, real1_f azimuth, real1_f inclination);
689 
692  enum RevertAnti { CTRL_AND_ANTI = 0, ONLY_CTRL = 1, ONLY_ANTI = 2 };
693 
694  void ApplyBuffer(PhaseShardPtr phaseShard, bitLenInt control, bitLenInt target, bool isAnti);
695  void ApplyBufferMap(bitLenInt bitIndex, ShardToPhaseMap bufferMap, RevertExclusivity exclusivity, bool isControl,
696  bool isAnti, const std::set<bitLenInt>& exceptPartners, bool dumpSkipped);
698  RevertControl controlExclusivity = CONTROLS_AND_TARGETS, RevertAnti antiExclusivity = CTRL_AND_ANTI,
699  const std::set<bitLenInt>& exceptControlling = {}, const std::set<bitLenInt>& exceptTargetedBy = {},
700  bool dumpSkipped = false, bool skipOptimized = false);
701 
702  void Flush0Eigenstate(bitLenInt i);
703  void Flush1Eigenstate(bitLenInt i);
704  void ToPermBasis(bitLenInt i);
705  void ToPermBasis(bitLenInt start, bitLenInt length);
708  void ToPermBasisProb(bitLenInt qubit);
709  void ToPermBasisProb(bitLenInt start, bitLenInt length);
710  void ToPermBasisMeasure(bitLenInt qubit);
711  void ToPermBasisMeasure(bitLenInt start, bitLenInt length);
712  void ToPermBasisAllMeasure();
713 
714  void DirtyShardRange(bitLenInt start, bitLenInt length)
715  {
716  for (bitLenInt i = 0U; i < length; ++i) {
717  shards[start + i].MakeDirty();
718  }
719  }
720 
722  {
723  for (bitLenInt i = 0U; i < length; ++i) {
724  shards[start + i].isPhaseDirty = true;
725  }
726  }
727 
728  void DirtyShardIndexVector(std::vector<bitLenInt> bitIndices)
729  {
730  for (bitLenInt i = 0U; i < (bitLenInt)bitIndices.size(); ++i) {
731  shards[bitIndices[i]].MakeDirty();
732  }
733  }
734 
735  void EndEmulation(bitLenInt target)
736  {
737  QEngineShard& shard = shards[target];
738  if (shard.unit) {
739  return;
740  }
741 
742  if (norm(shard.amp1) <= FP_NORM_EPSILON) {
743  shard.unit = MakeEngine(1U, 0U);
744  } else if (norm(shard.amp0) <= FP_NORM_EPSILON) {
745  shard.unit = MakeEngine(1U, 1U);
746  } else {
747  complex bitState[2U]{ shard.amp0, shard.amp1 };
748  shard.unit = MakeEngine(1U, 0U);
749  shard.unit->SetQuantumState(bitState);
750  }
751  }
752 
754  {
755  shard->found = true;
756  for (bitLenInt i = 0U; i < shards.size(); ++i) {
757  if (shards[i].found) {
758  shard->found = false;
759  return i;
760  }
761  }
762  shard->found = false;
763  return shards.size();
764  }
765 
766  void CommuteH(bitLenInt bitIndex);
767 
768  void OptimizePairBuffers(bitLenInt control, bitLenInt target, bool anti);
769 };
770 
771 } // namespace Qrack
Definition: qalu.hpp:22
Definition: qengineshard.hpp:315
bitLenInt size()
Definition: qengineshard.hpp:343
void swap(bitLenInt qubit1, bitLenInt qubit2)
Definition: qengineshard.hpp:379
void insert(bitLenInt start, QEngineShardMap &toInsert)
Definition: qengineshard.hpp:351
Associates a QInterface object with a set of bits.
Definition: qengineshard.hpp:50
complex amp0
Definition: qengineshard.hpp:62
bool ClampAmps()
Definition: qengineshard.cpp:29
bitLenInt mapped
Definition: qengineshard.hpp:59
bool isProbDirty
Definition: qengineshard.hpp:60
QInterfacePtr unit
Definition: qengineshard.hpp:58
Pauli pauliBasis
Definition: qengineshard.hpp:64
complex amp1
Definition: qengineshard.hpp:63
bool found
Definition: qengineshard.hpp:74
bool isPhaseDirty
Definition: qengineshard.hpp:61
A "Qrack::QInterface" is an abstract interface exposing qubit permutation state vector with methods t...
Definition: qinterface.hpp:146
bitCapInt maxQPower
Definition: qinterface.hpp:154
virtual void SetConcurrency(uint32_t threadsPerEngine)
Set the number of threads in parallel for loops, per component QEngine.
Definition: qinterface.hpp:249
real1 amplitudeFloor
Definition: qinterface.hpp:153
bool useRDRAND
Definition: qinterface.hpp:150
virtual bitLenInt Allocate(bitLenInt length)
Allocate new "length" count of |0> state qubits at end of qubit index position.
Definition: qinterface.hpp:434
virtual bitLenInt Compose(QInterfacePtr toCopy)
Combine another QInterface with this one, after the last bit index of this one.
Definition: qinterface.hpp:338
qrack_rand_gen_ptr rand_generator
Definition: qinterface.hpp:155
bool randGlobalPhase
Definition: qinterface.hpp:149
virtual void SetQubitCount(bitLenInt qb)
Definition: qinterface.hpp:159
static real1_f clampProb(real1_f toClamp)
Definition: qinterface.hpp:169
bitLenInt qubitCount
Definition: qinterface.hpp:151
bool doNormalize
Definition: qinterface.hpp:148
Definition: qparity.hpp:22
Definition: qunit.hpp:28
void ConvertYToZ(bitLenInt i)
Definition: qunit.cpp:2846
virtual complex GetAmplitude(bitCapInt perm)
Get the representational amplitude of a full permutation.
Definition: qunit.cpp:224
void ToPermBasisAll()
Definition: qunit.hpp:706
bool INTCOptimize(bitCapInt toMod, bitLenInt start, bitLenInt length, bool isAdd, bitLenInt carryIndex)
Check if carry arithmetic can be optimized.
Definition: qunit.cpp:3073
void(QAlu::* INCxxFn)(bitCapInt, bitLenInt, bitLenInt, bitLenInt, bitLenInt)
Definition: qunit.hpp:449
virtual void SetReactiveSeparate(bool isAggSep)
Set reactive separation option (on by default if available)
Definition: qunit.hpp:91
std::vector< int64_t > deviceIDs
Definition: qunit.hpp:44
bool(* ParallelUnitFn)(QInterfacePtr unit, real1_f param1, real1_f param2, real1_f param3, int64_t param4)
Definition: qunit.hpp:529
void OptimizePairBuffers(bitLenInt control, bitLenInt target, bool anti)
Definition: qunit.cpp:4322
void OrderContiguous(QInterfacePtr unit)
Definition: qunit.cpp:857
void INT(bitCapInt toMod, bitLenInt start, bitLenInt length, bitLenInt carryIndex, bool hasCarry, std::vector< bitLenInt > controlVec=std::vector< bitLenInt >())
Definition: qunit.cpp:3124
void Flush1Eigenstate(bitLenInt i)
Definition: qunit.cpp:2918
void SortUnit(QInterfacePtr unit, std::vector< QSortEntry > &bits, bitLenInt low, bitLenInt high)
Sort a container of bits, calling Swap() on each.
Definition: qunit.cpp:883
virtual void SetPermutation(bitCapInt perm, complex phaseFac=CMPLX_DEFAULT_ARG)
Set to a specific permutation of all qubits.
Definition: qunit.cpp:109
virtual QInterfacePtr EntangleInCurrentBasis(std::vector< bitLenInt * >::iterator first, std::vector< bitLenInt * >::iterator last)
Definition: qunit.cpp:384
virtual bool CheckBitsPermutation(bitLenInt start, bitLenInt length=1)
Check if all qubits in the range have cached probabilities indicating that they are in permutation ba...
Definition: qunit.cpp:924
bool useTGadget
Definition: qunit.hpp:37
virtual void X(bitLenInt q)
Definition: qunit.hpp:257
virtual QInterfacePtr Entangle(std::vector< bitLenInt > bits)
Definition: qunit.cpp:468
virtual bitLenInt Allocate(bitLenInt length)
Allocate new "length" count of |0> state qubits at end of qubit index position.
Definition: qinterface.hpp:434
virtual void EitherISwap(bitLenInt qubit1, bitLenInt qubit2, bool isInverse)
Definition: qunit.cpp:1797
void ApplyEitherControlled(std::vector< bitLenInt > controlVec, const std::vector< bitLenInt > targets, CF cfn, bool isPhase)
Definition: qunit.cpp:2735
void RevertBasis1Qb(bitLenInt i)
Definition: qunit.hpp:653
virtual QInterfacePtr Decompose(bitLenInt start, bitLenInt length)
Schmidt decompose a length of qubits.
Definition: qunit.hpp:165
virtual void IS(bitLenInt qubit)
Inverse S gate.
Definition: qinterface.hpp:997
virtual void S(bitLenInt qubit)
S gate.
Definition: qinterface.hpp:990
virtual real1_f CProbRdm(bitLenInt control, bitLenInt target)
Definition: qunit.hpp:106
real1_f separabilityThreshold
Definition: qunit.hpp:39
void ToPermBasisProb()
Definition: qunit.hpp:707
void CMULModx(CMULModFn fn, bitCapInt toMod, bitCapInt modN, bitLenInt start, bitLenInt carryStart, bitLenInt length, std::vector< bitLenInt > controlVec)
Definition: qunit.cpp:3584
virtual real1_f ProbBase(bitLenInt qubit)
Definition: qunit.cpp:976
virtual bitLenInt Compose(QInterfacePtr toCopy)
Combine another QInterface with this one, after the last bit index of this one.
Definition: qinterface.hpp:338
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 bool CheckBitsPlus(bitLenInt qubitIndex, bitLenInt length)
Definition: qunit.cpp:963
virtual bitLenInt Compose(QInterfacePtr toCopy)
Combine another QInterface with this one, after the last bit index of this one.
Definition: qunit.hpp:139
virtual void GetQuantumState(complex *outputState)
Get the pure quantum state representation.
Definition: qunit.cpp:168
virtual void GetProbs(real1 *outputProbs)
Get the pure quantum state representation.
Definition: qunit.cpp:196
void ToPermBasisAllMeasure()
Definition: qunit.cpp:2982
virtual void XBase(bitLenInt target)
Definition: qunit.cpp:2140
complex phaseFactor
Definition: qunit.hpp:42
virtual bitCapInt ForceMReg(bitLenInt start, bitLenInt length, 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:212
virtual void H(bitLenInt qubit)
Hadamard gate.
Definition: qinterface.hpp:876
virtual bool SeparateBit(bool value, bitLenInt qubit)
Definition: qunit.cpp:1347
bool isReactiveSeparate
Definition: qunit.hpp:36
real1_f ProbRdm(bitLenInt qubit)
Direct measure of bit probability to be in |1> state, treating all ancillary qubits as post-selected ...
Definition: qunit.hpp:97
void(QAlu::* CMULFn)(bitCapInt toMod, bitLenInt start, bitLenInt carryStart, bitLenInt length, const std::vector< bitLenInt > &controls)
Definition: qunit.hpp:450
virtual complex GetAmplitudeOrProb(bitCapInt perm, bool isProb)
Definition: qunit.cpp:226
virtual void SetTInjection(bool useGadget)
Set the option to use T-injection gadgets (off by default)
Definition: qunit.hpp:80
virtual QInterfacePtr EntangleRange(bitLenInt start, bitLenInt length, bool isForProb=false)
Definition: qunit.cpp:488
void ShardAI(bitLenInt qubit, real1_f azimuth, real1_f inclination)
Definition: qunit.cpp:2892
void ClampShard(bitLenInt qubit)
Definition: qunit.hpp:553
virtual void Decompose(bitLenInt start, QInterfacePtr dest)
Minimally decompose a set of contiguous bits from the separably composed unit, into "destination".
Definition: qunit.hpp:160
virtual bool M(bitLenInt q)
Definition: qunit.hpp:255
virtual void SetDevice(int64_t dID)
Set the device index, if more than one device is available.
Definition: qunit.cpp:3961
bitLenInt thresholdQubits
Definition: qunit.hpp:38
void DirtyShardIndexVector(std::vector< bitLenInt > bitIndices)
Definition: qunit.hpp:728
void ConvertZToY(bitLenInt i)
Definition: qunit.cpp:2869
std::vector< QInterfaceEngine > engines
Definition: qunit.hpp:45
void(QAlu::* INCxFn)(bitCapInt, bitLenInt, bitLenInt, bitLenInt)
Definition: qunit.hpp:448
void RevertBasisY(bitLenInt i)
Definition: qunit.hpp:621
virtual void UniformlyControlledSingleBit(const std::vector< bitLenInt > &controls, bitLenInt qubitIndex, const complex *mtrxs)
Apply a "uniformly controlled" arbitrary single bit unitary transformation.
Definition: qinterface.hpp:590
virtual bool GetReactiveSeparate()
Get reactive separation option.
Definition: qunit.hpp:92
bool TrimControls(const std::vector< bitLenInt > &controls, std::vector< bitLenInt > &controlVec, bitCapInt *perm)
Definition: qunit.cpp:2621
virtual ~QUnit()
Definition: qunit.hpp:67
double logFidelity
Definition: qunit.hpp:40
virtual void Detach(bitLenInt start, bitLenInt length, QUnitPtr dest)
Definition: qunit.cpp:268
RevertAnti
Definition: qunit.hpp:692
@ ONLY_ANTI
Definition: qunit.hpp:692
@ CTRL_AND_ANTI
Definition: qunit.hpp:692
@ ONLY_CTRL
Definition: qunit.hpp:692
void RevertBasisToX1Qb(bitLenInt i)
Definition: qunit.hpp:664
virtual void YBase(bitLenInt target)
Definition: qunit.cpp:2155
virtual void ZBase(bitLenInt target)
Definition: qunit.cpp:2172
void INCx(INCxFn fn, bitCapInt toMod, bitLenInt start, bitLenInt length, bitLenInt flagIndex)
Definition: qunit.cpp:3019
void TransformYInvert(complex topRight, complex bottomLeft, complex *mtrxOut)
Definition: qunit.hpp:593
void(QAlu::* CMULModFn)(bitCapInt toMod, bitCapInt modN, bitLenInt start, bitLenInt carryStart, bitLenInt length, const std::vector< bitLenInt > &controls)
Definition: qunit.hpp:452
void ApplyBuffer(PhaseShardPtr phaseShard, bitLenInt control, bitLenInt target, bool isAnti)
Definition: qunit.cpp:4081
virtual void Dispose(bitLenInt start, bitLenInt length)
Minimally decompose a set of contiguous bits from the separably composed unit, and discard the separa...
Definition: qunit.hpp:175
int64_t devID
Definition: qunit.hpp:41
void TransformPhase(complex topLeft, complex bottomRight, complex *mtrxOut)
Definition: qunit.hpp:601
void RevertBasis2Qb(bitLenInt i, RevertExclusivity exclusivity=INVERT_AND_PHASE, RevertControl controlExclusivity=CONTROLS_AND_TARGETS, RevertAnti antiExclusivity=CTRL_AND_ANTI, const std::set< bitLenInt > &exceptControlling={}, const std::set< bitLenInt > &exceptTargetedBy={}, bool dumpSkipped=false, bool skipOptimized=false)
Definition: qunit.cpp:4167
virtual bool TrySeparateClifford(bitLenInt qubit)
Definition: qunit.cpp:586
virtual void SetAmplitude(bitCapInt perm, complex amp)
Sets the representational amplitude of a full permutation.
Definition: qunit.hpp:127
bool freezeBasis2Qb
Definition: qunit.hpp:33
virtual bitCapInt GetCachedPermutation(bitLenInt start, bitLenInt length)
Assuming all bits in the range are in cached |0>/|1> eigenstates, read the unsigned integer value of ...
Definition: qunit.cpp:941
virtual int64_t GetDevice()
Get the device index.
Definition: qunit.hpp:95
void RevertBasisToY1Qb(bitLenInt i)
Definition: qunit.hpp:674
void DirtyShardRange(bitLenInt start, bitLenInt length)
Definition: qunit.hpp:714
QInterfacePtr CMULEntangle(std::vector< bitLenInt > controlVec, bitLenInt start, bitLenInt carryStart, bitLenInt length, std::vector< bitLenInt > *controlsMapped)
Definition: qunit.cpp:3537
void CMULx(CMULFn fn, bitCapInt toMod, bitLenInt start, bitLenInt carryStart, bitLenInt length, std::vector< bitLenInt > controlVec)
Definition: qunit.cpp:3571
virtual bitLenInt Compose(QInterfacePtr toCopy, bitLenInt start)
Definition: qunit.hpp:156
bool INTSCOptimize(bitCapInt toMod, bitLenInt start, bitLenInt length, bool isAdd, bitLenInt carryIndex, bitLenInt overflowIndex)
Check if arithmetic with both carry and overflow can be optimized.
Definition: qunit.cpp:3079
QUnit(std::vector< QInterfaceEngine > eng, bitLenInt qBitCount, bitCapInt initState=0U, qrack_rand_gen_ptr rgp=nullptr, complex phaseFac=CMPLX_DEFAULT_ARG, bool doNorm=false, bool randomGlobalPhase=true, bool useHostMem=false, int64_t deviceId=-1, bool useHardwareRNG=true, bool useSparseStateVec=false, real1_f norm_thresh=REAL1_EPSILON, std::vector< int64_t > devIDs={}, bitLenInt qubitThreshold=0U, real1_f separation_thresh=FP_NORM_EPSILON_F)
Definition: qunit.cpp:65
virtual void Dispose(bitLenInt start, bitLenInt length, bitCapInt disposedPerm)
Dispose a a contiguous set of qubits that are already in a permutation eigenstate.
Definition: qunit.hpp:176
virtual QInterfacePtr CloneBody(QUnitPtr copyPtr)
Definition: qunit.cpp:4060
void ToPermBasis(bitLenInt i)
Definition: qunit.cpp:2926
void TransformX2x2(const complex *mtrxIn, complex *mtrxOut)
Definition: qunit.hpp:569
virtual real1_f ACProbRdm(bitLenInt control, bitLenInt target)
Definition: qunit.hpp:114
bitLenInt FindShardIndex(QEngineShardPtr shard)
Definition: qunit.hpp:753
RevertControl
Definition: qunit.hpp:691
@ ONLY_TARGETS
Definition: qunit.hpp:691
@ CONTROLS_AND_TARGETS
Definition: qunit.hpp:691
@ ONLY_CONTROLS
Definition: qunit.hpp:691
QInterfacePtr MakeEngine(bitLenInt length, bitCapInt perm)
Definition: qunit.cpp:98
virtual QInterfacePtr EntangleAll(bool isForProb=false)
Definition: qunit.hpp:512
void ToPermBasisMeasure(bitLenInt qubit)
Definition: qunit.cpp:2954
bitCapInt GetIndexedEigenstate(bitLenInt indexStart, bitLenInt indexLength, bitLenInt valueStart, bitLenInt valueLength, const unsigned char *values)
Definition: qunit.cpp:3681
virtual bitLenInt Compose(QUnitPtr toCopy)
Definition: qunit.hpp:138
virtual void Decompose(bitLenInt start, QUnitPtr dest)
Definition: qunit.hpp:164
bool INTSOptimize(bitCapInt toMod, bitLenInt start, bitLenInt length, bool isAdd, bitLenInt overflowIndex)
Check if overflow arithmetic can be optimized.
Definition: qunit.cpp:3067
virtual void SetConcurrency(uint32_t threadsPerEngine)
Set the number of threads in parallel for loops, per component QEngine.
Definition: qunit.hpp:69
void ConvertZToX(bitLenInt i)
Definition: qunit.cpp:2801
bool isSparse
Definition: qunit.hpp:35
void CommuteH(bitLenInt bitIndex)
Definition: qunit.cpp:4219
void INCxx(INCxxFn fn, bitCapInt toMod, bitLenInt start, bitLenInt length, bitLenInt flag1Index, bitLenInt flag2Index)
Definition: qunit.cpp:3038
void ApplyBufferMap(bitLenInt bitIndex, ShardToPhaseMap bufferMap, RevertExclusivity exclusivity, bool isControl, bool isAnti, const std::set< bitLenInt > &exceptPartners, bool dumpSkipped)
Definition: qunit.cpp:4105
void TransformY2x2(const complex *mtrxIn, complex *mtrxOut)
Definition: qunit.hpp:585
void ConvertXToY(bitLenInt i)
Definition: qunit.cpp:2822
real1_f ExpectationFactorized(bool isRdm, bool isFloat, const std::vector< bitLenInt > &bits, const std::vector< bitCapInt > &perms, const std::vector< real1_f > &weights, bitCapInt offset, bool roundRz)
Definition: qunit.hpp:478
void TransformXInvert(complex topRight, complex bottomLeft, complex *mtrxOut)
Definition: qunit.hpp:577
RevertExclusivity
Definition: qunit.hpp:690
@ ONLY_INVERT
Definition: qunit.hpp:690
@ ONLY_PHASE
Definition: qunit.hpp:690
@ INVERT_AND_PHASE
Definition: qunit.hpp:690
std::vector< bitLenInt > CMULEntangle(std::vector< bitLenInt > controlVec, bitLenInt start, bitCapInt carryStart, bitLenInt length)
virtual bitLenInt Compose(QUnitPtr toCopy, bitLenInt start)
Definition: qunit.hpp:140
void DirtyShardRangePhase(bitLenInt start, bitLenInt length)
Definition: qunit.hpp:721
void Flush0Eigenstate(bitLenInt i)
Definition: qunit.cpp:2910
QUnit(bitLenInt qBitCount, bitCapInt initState=0U, qrack_rand_gen_ptr rgp=nullptr, complex phaseFac=CMPLX_DEFAULT_ARG, bool doNorm=false, bool randomGlobalPhase=true, bool useHostMem=false, int64_t deviceId=-1, bool useHardwareRNG=true, bool useSparseStateVec=false, real1_f norm_thresh=REAL1_EPSILON, std::vector< int64_t > devIDs={}, bitLenInt qubitThreshold=0U, real1_f separation_thresh=FP_NORM_EPSILON_F)
Definition: qunit.hpp:56
QEngineShardMap shards
Definition: qunit.hpp:43
bool useHostRam
Definition: qunit.hpp:34
bool ParallelUnitApply(ParallelUnitFn fn, real1_f param1=ZERO_R1_F, real1_f param2=ZERO_R1_F, real1_f param3=ZERO_R1_F, int64_t param4=0)
Definition: qunit.cpp:3911
void EndEmulation(bitLenInt target)
Definition: qunit.hpp:735
virtual void SetQuantumState(const complex *inputState)
Set an arbitrary pure quantum state representation.
Definition: qunit.cpp:123
void INTS(bitCapInt toMod, bitLenInt start, bitLenInt length, bitLenInt overflowIndex, bitLenInt carryIndex, bool hasCarry)
Definition: qunit.cpp:3333
void RevertBasisX(bitLenInt i)
Definition: qunit.hpp:609
Half-precision floating-point type.
Definition: half.hpp:2222
virtual bitCapInt IndexedLDA(bitLenInt indexStart, bitLenInt indexLength, bitLenInt valueStart, bitLenInt valueLength, const unsigned char *values, bool resetValue=true)
Set 8 bit register bits by a superposed index-offset-based read from classical memory.
Definition: qunit.cpp:3706
virtual void INC(bitCapInt toAdd, bitLenInt start, bitLenInt length)
Add integer (without sign)
Definition: qunit.cpp:3304
virtual void POWModNOut(bitCapInt base, bitCapInt modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length)
Raise a classical base to a quantum power, modulo N, (out of place)
Definition: qunit.cpp:3506
virtual void INCDECBCDC(bitCapInt toSub, bitLenInt start, bitLenInt length, bitLenInt carryIndex)
Common driver method behind INCSC and DECSC (without overflow flag)
Definition: qunit.cpp:3428
virtual void CMULModNOut(bitCapInt toMul, bitCapInt modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length, const std::vector< bitLenInt > &controls)
Controlled multiplication modulo N by integer, (out of place)
Definition: qunit.hpp:286
virtual void CMULModNOut(bitCapInt toMul, 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:259
virtual bitCapInt IndexedADC(bitLenInt indexStart, bitLenInt indexLength, bitLenInt valueStart, bitLenInt valueLength, bitLenInt carryIndex, const unsigned char *values)
Add to entangled 8 bit register state with a superposed index-offset-based read from classical memory...
Definition: qunit.cpp:3742
virtual void CIMULModNOut(bitCapInt toMul, 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:296
virtual void INCC(bitCapInt toAdd, bitLenInt start, bitLenInt length, bitLenInt carryIndex)
Add integer (without sign, with carry)
Definition: qunit.cpp:3310
virtual void INCBCD(bitCapInt toAdd, bitLenInt start, bitLenInt length)
Add classical BCD integer (without sign)
Definition: qunit.cpp:3406
virtual void CIMULModNOut(bitCapInt toMul, 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: qunit.hpp:291
virtual void Hash(bitLenInt start, bitLenInt length, const unsigned char *values)
Transform a length of qubit register via lookup through a hash table.
Definition: qunit.cpp:3844
virtual void MULModNOut(bitCapInt toMul, bitCapInt modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length)
Multiplication modulo N by integer, (out of place)
Definition: qunit.hpp:278
virtual void INCDECC(bitCapInt toAdd, bitLenInt start, bitLenInt length, bitLenInt carryIndex)
Common driver method behind INCC and DECC (without sign, with carry)
Definition: qunit.hpp:274
virtual void INCDECC(bitCapInt toAdd, bitLenInt start, bitLenInt length, bitLenInt carryIndex)
Common driver method behind INCC and DECC.
Definition: arithmetic.cpp:63
virtual void DECS(bitCapInt toSub, bitLenInt start, bitLenInt length, bitLenInt overflowIndex)
Add a classical integer to the register, with sign and without carry.
Definition: qunit.hpp:266
virtual void CDEC(bitCapInt toSub, bitLenInt inOutStart, bitLenInt length, const std::vector< bitLenInt > &controls)
Subtract integer (without sign, with controls)
Definition: qunit.hpp:270
virtual void PhaseFlipIfLess(bitCapInt greaterPerm, bitLenInt start, bitLenInt length)
This is an expedient for an adaptive Grover's search for a function's global minimum.
Definition: qunit.cpp:3865
virtual void IMULModNOut(bitCapInt toMul, bitCapInt modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length)
Inverse of multiplication modulo N by integer, (out of place)
Definition: arithmetic.cpp:226
virtual void INCS(bitCapInt toAdd, bitLenInt start, bitLenInt length, bitLenInt overflowIndex)
Add a classical integer to the register, with sign and without carry.
Definition: qunit.cpp:3389
virtual void IMULModNOut(bitCapInt toMul, bitCapInt modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length)
Inverse of multiplication modulo N by integer, (out of place)
Definition: qunit.hpp:282
virtual void CINC(bitCapInt toAdd, bitLenInt inOutStart, bitLenInt length, const std::vector< bitLenInt > &controls)
Add integer (without sign, with controls)
Definition: qunit.cpp:2995
virtual bitCapInt IndexedSBC(bitLenInt indexStart, bitLenInt indexLength, bitLenInt valueStart, bitLenInt valueLength, bitLenInt carryIndex, const unsigned char *values)
Subtract from an entangled 8 bit register state with a superposed index-offset-based read from classi...
Definition: qunit.cpp:3793
virtual void DIV(bitCapInt toDiv, bitLenInt inOutStart, bitLenInt carryStart, bitLenInt length)
Divide by integer.
Definition: qunit.cpp:3471
virtual void DECBCD(bitCapInt toAdd, bitLenInt start, bitLenInt length)
Subtract classical BCD integer (without sign)
Definition: qunit.cpp:3417
virtual void CPOWModNOut(bitCapInt base, bitCapInt modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length, const std::vector< bitLenInt > &controls)
Controlled, raise a classical base to a quantum power, modulo N, (out of place)
Definition: qunit.cpp:3654
virtual void CPhaseFlipIfLess(bitCapInt greaterPerm, bitLenInt start, bitLenInt length, bitLenInt flagIndex)
The 6502 uses its carry flag also as a greater-than/less-than flag, for the CMP operation.
Definition: qunit.cpp:3885
virtual void DEC(bitCapInt toSub, bitLenInt start, bitLenInt length)
Subtract classical integer (without sign)
Definition: arithmetic.cpp:57
virtual void DEC(bitCapInt toSub, bitLenInt start, bitLenInt length)
Add integer (without sign)
Definition: qunit.hpp:265
virtual void CMUL(bitCapInt toMul, bitLenInt inOutStart, bitLenInt carryStart, bitLenInt length, const std::vector< bitLenInt > &controls)
Controlled multiplication by integer.
Definition: qunit.cpp:3596
virtual void MUL(bitCapInt toMul, bitLenInt inOutStart, bitLenInt carryStart, bitLenInt length)
Multiply by integer.
Definition: qunit.cpp:3435
virtual void INCDECSC(bitCapInt toAdd, bitLenInt start, bitLenInt length, bitLenInt overflowIndex, bitLenInt carryIndex)
Common driver method behind INCSC and DECSC (with overflow flag)
Definition: qunit.cpp:3394
virtual void DECC(bitCapInt toSub, bitLenInt start, bitLenInt length, bitLenInt carryIndex)
Subtract integer (without sign, with carry)
Definition: qunit.cpp:3321
virtual void DECS(bitCapInt toSub, bitLenInt start, bitLenInt length, bitLenInt overflowIndex)
Subtract a classical integer from the register, with sign and without carry.
Definition: arithmetic.cpp:182
virtual void MULModNOut(bitCapInt toMul, bitCapInt modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length)
Multiplication modulo N by integer, (out of place)
Definition: arithmetic.cpp:191
virtual void CDEC(bitCapInt toSub, bitLenInt inOutStart, bitLenInt length, const std::vector< bitLenInt > &controls)
Subtract classical integer (without sign, with controls)
Definition: arithmetic.cpp:160
virtual void CDIV(bitCapInt toDiv, bitLenInt inOutStart, bitLenInt carryStart, bitLenInt length, const std::vector< bitLenInt > &controls)
Controlled division by power of integer.
Definition: qunit.cpp:3625
virtual void CSwap(const std::vector< bitLenInt > &controls, bitLenInt qubit1, bitLenInt qubit2)
Apply a swap with arbitrary control bits.
Definition: qunit.cpp:2591
virtual void MCPhase(const std::vector< bitLenInt > &controls, complex topLeft, complex bottomRight, bitLenInt target)
Apply a single bit transformation that only effects phase, with arbitrary control bits.
Definition: qunit.hpp:197
virtual void ZMask(bitCapInt mask)
Masked Z gate.
Definition: qunit.hpp:192
virtual void CNOT(bitLenInt control, bitLenInt target)
Controlled NOT gate.
Definition: qinterface.hpp:672
virtual void IS(bitLenInt qubit)
Inverse S gate.
Definition: qinterface.hpp:997
virtual void MACPhase(const std::vector< bitLenInt > &controls, complex topLeft, complex bottomRight, bitLenInt target)
Apply a single bit transformation that only effects phase, with arbitrary (anti-)control bits.
Definition: qunit.hpp:206
virtual void S(bitLenInt qubit)
S gate.
Definition: qinterface.hpp:990
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 UCMtrx(const std::vector< bitLenInt > &controls, const complex *mtrx, bitLenInt target, bitCapInt controlPerm)
Apply an arbitrary single bit unitary transformation, with arbitrary control bits,...
Definition: qunit.cpp:2559
virtual void H(bitLenInt qubit)
Hadamard gate.
Definition: qinterface.hpp:876
virtual bool M(bitLenInt qubitIndex)
Measurement gate.
Definition: qinterface.hpp:976
virtual void AntiCSqrtSwap(const std::vector< bitLenInt > &controls, bitLenInt qubit1, bitLenInt qubit2)
Apply a square root of swap with arbitrary (anti) control bits.
Definition: qunit.cpp:2606
virtual void UCPhase(const std::vector< bitLenInt > &controls, complex topLeft, complex bottomRight, bitLenInt target, bitCapInt controlPerm)
Apply a single bit transformation that only effects phase, with arbitrary control bits,...
Definition: qunit.cpp:2372
virtual void MCInvert(const std::vector< bitLenInt > &controls, complex topRight, complex bottomLeft, bitLenInt target)
Apply a single bit transformation that reverses bit probability and might effect phase,...
Definition: qunit.hpp:201
virtual void CSqrtSwap(const std::vector< bitLenInt > &controls, bitLenInt qubit1, bitLenInt qubit2)
Apply a square root of swap with arbitrary control bits.
Definition: qunit.cpp:2601
virtual void UniformlyControlledSingleBit(const std::vector< bitLenInt > &controls, bitLenInt qubitIndex, const complex *mtrxs)
Apply a "uniformly controlled" arbitrary single bit unitary transformation.
Definition: qinterface.hpp:590
virtual void Invert(complex topRight, complex bottomLeft, bitLenInt qubitIndex)
Apply a single bit transformation that reverses bit probability and might effect phase.
Definition: qunit.cpp:2321
virtual void X(bitLenInt qubit)
X gate.
Definition: qinterface.hpp:1054
virtual void PhaseParity(real1 radians, bitCapInt mask)
Definition: qunit.cpp:1040
virtual void AntiCNOT(bitLenInt control, bitLenInt target)
Anti controlled NOT gate.
Definition: qinterface.hpp:683
virtual void AntiCSwap(const std::vector< bitLenInt > &controls, bitLenInt qubit1, bitLenInt qubit2)
Apply a swap with arbitrary (anti) control bits.
Definition: qunit.cpp:2596
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: qunit.cpp:2616
virtual void U(bitLenInt target, real1_f theta, real1_f phi, real1_f lambda)
General unitary gate.
Definition: rotational.cpp:18
virtual void CUniformParityRZ(const std::vector< bitLenInt > &controls, bitCapInt mask, real1_f angle)
If the controls are set and the target qubit set parity is odd, this applies a phase factor of .
Definition: qunit.cpp:1240
virtual void UCInvert(const std::vector< bitLenInt > &controls, complex topRight, complex bottomLeft, bitLenInt target, bitCapInt controlPerm)
Apply a single bit transformation that reverses bit probability and might effect phase,...
Definition: qunit.cpp:2439
virtual void Mtrx(const complex *mtrx, bitLenInt qubit)
Apply an arbitrary single bit unitary transformation.
Definition: qunit.cpp:2500
virtual void MCMtrx(const std::vector< bitLenInt > &controls, const complex *mtrx, bitLenInt target)
Apply an arbitrary single bit unitary transformation, with arbitrary control bits.
Definition: qunit.hpp:221
virtual void MACMtrx(const std::vector< bitLenInt > &controls, const complex *mtrx, bitLenInt target)
Apply an arbitrary single bit unitary transformation, with arbitrary (anti-)control bits.
Definition: qunit.hpp:225
virtual std::map< bitCapInt, int > MultiShotMeasureMask(const std::vector< bitCapInt > &qPowers, unsigned shots)
Statistical measure of masked permutation probability.
Definition: qunit.cpp:1547
virtual bitCapInt MAll()
Measure permutation state of all coherent bits.
Definition: qunit.cpp:1496
virtual void MACInvert(const std::vector< bitLenInt > &controls, complex topRight, complex bottomLeft, bitLenInt target)
Apply a single bit transformation that reverses bit probability and might effect phase,...
Definition: qunit.hpp:211
virtual void CISqrtSwap(const std::vector< bitLenInt > &controls, bitLenInt qubit1, bitLenInt qubit2)
Apply an inverse square root of swap with arbitrary control bits.
Definition: qunit.cpp:2611
virtual void Phase(complex topLeft, complex bottomRight, bitLenInt qubitIndex)
Apply a single bit transformation that only effects phase.
Definition: qunit.cpp:2260
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: qunit.cpp:1908
virtual void SetReg(bitLenInt start, bitLenInt length, bitCapInt value)
Set register bits to given permutation.
Definition: qunit.cpp:1787
virtual bitCapInt ForceMReg(bitLenInt start, bitLenInt length, 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:212
virtual void Swap(bitLenInt qubit1, bitLenInt qubit2)
Swap values of two bits in register.
Definition: qunit.hpp:339
virtual void SqrtSwap(bitLenInt qubit1, bitLenInt qubit2)
Square root of Swap gate.
Definition: qunit.cpp:1840
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: qunit.hpp:357
virtual void ISqrtSwap(bitLenInt qubit1, bitLenInt qubit2)
Inverse square root of Swap gate.
Definition: qunit.cpp:1874
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: qunit.hpp:356
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:2600
virtual bool ForceMParity(bitCapInt mask, bool result, bool doForce=true)
Act as if is a measurement of parity of the masked set of qubits was applied, except force the (usual...
Definition: qunit.cpp:1175
virtual void UpdateRunningNorm(real1_f norm_thresh=REAL1_DEFAULT_ARG)
Force a calculation of the norm of the state vector, in order to make it unit length before the next ...
Definition: qunit.cpp:3927
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: qunit.cpp:618
virtual real1_f ExpectationBitsFactorizedRdm(bool roundRz, const std::vector< bitLenInt > &bits, const std::vector< bitCapInt > &perms, bitCapInt offset=0U)
Get (reduced density matrix) expectation value of bits, given an array of qubit weights.
Definition: qunit.hpp:405
virtual real1_f ExpectationBitsFactorized(const std::vector< bitLenInt > &bits, const std::vector< bitCapInt > &perms, bitCapInt offset=0U)
Get expectation value of bits, given an array of qubit weights.
Definition: qunit.hpp:400
virtual real1_f SumSqrDiff(QInterfacePtr toCompare)
Definition: qunit.hpp:395
virtual bool isFinished()
Returns "false" if asynchronous work is still running, and "true" if all previously dispatched asynch...
Definition: qunit.cpp:3955
virtual bool isClifford(bitLenInt qubit)
Returns "true" if current qubit state is identifiably within the Clifford set, or "false" if it is no...
Definition: qunit.hpp:422
virtual QInterfacePtr Clone()
Clone this QInterface.
Definition: qunit.cpp:4042
virtual void NormalizeState(real1_f nrm=REAL1_DEFAULT_ARG, real1_f norm_thresh=REAL1_DEFAULT_ARG, real1_f phaseArg=ZERO_R1_F)
Apply the normalization factor found by UpdateRunningNorm() or on the fly by a single bit gate.
Definition: qunit.cpp:3937
virtual void SetSdrp(real1_f sdrp)
Set the "Schmidt decomposition rounding parameter" value, (between 0 and 1)
Definition: qunit.hpp:429
virtual real1_f Prob(bitLenInt qubit)
Direct measure of bit probability to be in |1> state.
Definition: qunit.hpp:370
virtual real1_f ProbParity(bitCapInt mask)
Overall probability of any odd permutation of the masked set of bits.
Definition: qunit.cpp:1117
virtual void ResetUnitaryFidelity()
Reset the internal fidelity calculation tracker to 1.0.
Definition: qunit.hpp:428
virtual void Dump()
If asynchronous work is still running, let the simulator know that it can be aborted.
Definition: qunit.hpp:415
virtual void Finish()
If asynchronous work is still running, block until it finishes.
Definition: qunit.cpp:3947
virtual double GetUnitaryFidelity()
When "Schmidt-decomposition rounding parameter" ("SDRP") is being used, starting from initial 1....
Definition: qunit.hpp:427
virtual real1_f ProbAllRdm(bool roundRz, bitCapInt perm)
Direct measure of full permutation probability, treating all ancillary qubits as post-selected T gate...
Definition: qunit.hpp:380
virtual real1_f ProbAll(bitCapInt perm)
Direct measure of full permutation probability.
Definition: qunit.hpp:379
Definition: complex16x2simd.hpp:25
void ThrowIfQbIdArrayIsBad(const std::vector< bitLenInt > &controls, const bitLenInt &qubitCount, std::string message)
Definition: qrack_functions.hpp:71
std::complex< half_float::half > complex
Definition: qrack_types.hpp:62
@ QINTERFACE_STABILIZER_HYBRID
Create a QStabilizerHybrid, switching between a QStabilizer and a QHybrid as efficient.
Definition: qinterface.hpp:90
std::shared_ptr< QUnit > QUnitPtr
Definition: qunit.hpp:24
std::shared_ptr< QInterface > QInterfacePtr
Definition: qinterface.hpp:28
constexpr real1_f ZERO_R1_F
Definition: qrack_types.hpp:152
constexpr real1_f FP_NORM_EPSILON_F
Definition: qrack_types.hpp:245
std::shared_ptr< PhaseShard > PhaseShardPtr
Definition: qengineshard.hpp:46
const real1 ONE_R1
Definition: qrack_types.hpp:153
constexpr real1_f ONE_R1_F
Definition: qrack_types.hpp:154
half_float::half real1
Definition: qrack_types.hpp:63
QRACK_CONST real1 FP_NORM_EPSILON
Definition: qrack_types.hpp:243
bitCapInt pow2(const bitLenInt &p)
Definition: qrack_functions.hpp:22
double norm(const complex2 &c)
Definition: complex16x2simd.hpp:101
const real1 REAL1_DEFAULT_ARG
Definition: qrack_types.hpp:155
const real1 PI_R1
Definition: qrack_types.hpp:158
float real1_f
Definition: qrack_types.hpp:64
QRACK_CONST complex CMPLX_DEFAULT_ARG
Definition: qrack_types.hpp:242
QRACK_CONST complex I_CMPLX
Definition: qrack_types.hpp:241
std::map< QEngineShardPtr, PhaseShardPtr > ShardToPhaseMap
Definition: qengineshard.hpp:47
const real1 REAL1_EPSILON
Definition: qrack_types.hpp:157
@ PauliX
Pauli X operator. Corresponds to Q# constant "PauliX.".
Definition: qinterface.hpp:38
@ PauliY
Pauli Y operator. Corresponds to Q# constant "PauliY.".
Definition: qinterface.hpp:40
@ PauliZ
Pauli Z operator. Corresponds to Q# constant "PauliZ.".
Definition: qinterface.hpp:42
half log(half arg)
Natural logarithm.
Definition: half.hpp:3318
half exp(half arg)
Exponential function.
Definition: half.hpp:3201
#define QRACK_CONST
Definition: qrack_types.hpp:150
#define bitLenInt
Definition: qrack_types.hpp:44
#define qrack_rand_gen_ptr
Definition: qrack_types.hpp:146
#define bitCapInt
Definition: qrack_types.hpp:105
#define IS_NORM_0(c)
Definition: qrack_types.hpp:27
Definition: qunit.hpp:539
bool operator<(const QSortEntry &rhs)
Definition: qunit.hpp:542
bitLenInt mapped
Definition: qunit.hpp:541
bool operator>(const QSortEntry &rhs)
Definition: qunit.hpp:543
bitLenInt bit
Definition: qunit.hpp:540