Qrack  10.0
General classical-emulating-quantum development framework
qengine_cpu.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 "qengine.hpp"
16 #include "statevector.hpp"
17 
18 #if ENABLE_QUNIT_CPU_PARALLEL && ENABLE_PTHREAD
19 #include "common/dispatchqueue.hpp"
20 #endif
21 
22 namespace Qrack {
23 
24 class QEngineCPU;
25 typedef std::shared_ptr<QEngineCPU> QEngineCPUPtr;
26 
27 template <class BidirectionalIterator>
28 void reverse(BidirectionalIterator first, BidirectionalIterator last, const bitCapInt& stride);
29 template <class BidirectionalIterator>
30 void rotate(
31  BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, const bitCapInt& stride);
32 
36 class QEngineCPU : public QEngine {
37 protected:
39 #if ENABLE_QUNIT_CPU_PARALLEL && ENABLE_PTHREAD
40  DispatchQueue dispatchQueue;
41 #endif
42  double logFidelity;
43  bool isSparse;
45 
46  using QEngine::Copy;
47  void Copy(QInterfacePtr orig) { Copy(std::dynamic_pointer_cast<QEngineCPU>(orig)); }
48  void Copy(QEngineCPUPtr orig)
49  {
50  QEngine::Copy(std::dynamic_pointer_cast<QEngine>(orig));
51  stateVec = orig->stateVec;
52  logFidelity = orig->logFidelity;
53  isSparse = orig->isSparse;
54  sparse_thresh = orig->sparse_thresh;
55  }
56 
57  StateVectorSparsePtr CastStateVecSparse() { return std::dynamic_pointer_cast<StateVectorSparse>(stateVec); }
58  StateVectorArrayPtr CastStateVecDense() { return std::dynamic_pointer_cast<StateVectorArray>(stateVec); }
59 
60 public:
61  QEngineCPU(bitLenInt qBitCount, const bitCapInt& initState, qrack_rand_gen_ptr rgp = nullptr,
62  const complex& phaseFac = CMPLX_DEFAULT_ARG, bool doNorm = false, bool randomGlobalPhase = true,
63  bool ignored = false, int64_t ignored2 = -1, bool useHardwareRNG = true, bool useSparseStateVec = false,
64  real1_f norm_thresh = REAL1_EPSILON, std::vector<int64_t> ignored3 = {}, bitLenInt ignored4 = 0U,
65  real1_f ignored5 = _qrack_qunit_sep_thresh);
66 
67  ~QEngineCPU() { Dump(); }
68 
69  void Finish()
70  {
71 #if ENABLE_QUNIT_CPU_PARALLEL && ENABLE_PTHREAD
72  dispatchQueue.finish();
73 #endif
74  };
75 
76  bool isFinished()
77  {
78 #if ENABLE_QUNIT_CPU_PARALLEL && ENABLE_PTHREAD
79  return dispatchQueue.isFinished();
80 #else
81  return true;
82 #endif
83  }
84 
85  void Dump()
86  {
87 #if ENABLE_QUNIT_CPU_PARALLEL && ENABLE_PTHREAD
88  dispatchQueue.dump();
89 #endif
90  }
91 
93  {
94  if (!stateVec) {
95  return ZERO_R1_F;
96  }
97 
99  }
100 
102  {
103  Dump();
104  FreeStateVec();
106  logFidelity = 0.0;
107  }
108 
110 
112  {
113  if (isSparse) {
114  logFidelity += (double)log(CastStateVecSparse()->truncate_to_size(QRACK_SPARSE_MAX_KEYS));
115  }
116  }
117 
118  void SparseRenorm(real1_f totFidelityLoss)
119  {
121  if ((totFidelityLoss > REAL1_EPSILON) && (sv->size() <= QRACK_SPARSE_MAX_KEYS)) {
122  const double f = 1.0 - (double)totFidelityLoss;
123  logFidelity += (double)log(f);
124  sv->mult((real1_f)std::sqrt(1.0 / f));
125  } else {
126  logFidelity += (double)log(sv->truncate_to_size(QRACK_SPARSE_MAX_KEYS));
127  }
128  }
129 
131  {
132  if (isSparse) {
133  return CastStateVecSparse()->size();
134  }
135 
136  return maxQPower;
137  }
138 
139  double GetUnitaryFidelity() { return exp(logFidelity); }
141 
142  bool IsZeroAmplitude() { return !stateVec; }
143  void GetAmplitudePage(complex* pagePtr, bitCapIntOcl offset, bitCapIntOcl length);
144  void SetAmplitudePage(const complex* pagePtr, bitCapIntOcl offset, bitCapIntOcl length);
145  void SetAmplitudePage(
146  QEnginePtr pageEnginePtr, bitCapIntOcl srcOffset, bitCapIntOcl dstOffset, bitCapIntOcl length);
147  void ShuffleBuffers(QEnginePtr engine);
148  void CopyStateVec(QEnginePtr src);
149 
151 
152  void QueueSetDoNormalize(bool doNorm)
153  {
154  Dispatch(1U, [this, doNorm] { doNormalize = doNorm; });
155  }
156  void QueueSetRunningNorm(real1_f runningNrm)
157  {
158  Dispatch(1U, [this, runningNrm] { runningNorm = runningNrm; });
159  }
160 
161  void LossySaveStateVector(std::string f, int p = 6, int b = 4);
162  void LossyLoadStateVector(std::string f);
163  void SetQuantumState(const complex* inputState);
164  void GetQuantumState(complex* outputState);
165  void GetProbs(real1* outputProbs);
166  complex GetAmplitude(const bitCapInt& perm);
167  void SetAmplitude(const bitCapInt& perm, const complex& amp);
168 
169  using QEngine::Compose;
171  bitLenInt Compose(QInterfacePtr toCopy) { return Compose(std::dynamic_pointer_cast<QEngineCPU>(toCopy)); }
172  std::map<QInterfacePtr, bitLenInt> Compose(std::vector<QInterfacePtr> toCopy);
173  bitLenInt Compose(QEngineCPUPtr toCopy, bitLenInt start);
175  {
176  return Compose(std::dynamic_pointer_cast<QEngineCPU>(toCopy), start);
177  }
178 
179  using QEngine::Decompose;
180  void Decompose(bitLenInt start, QInterfacePtr dest);
181 
182  void Dispose(bitLenInt start, bitLenInt length);
183  void Dispose(bitLenInt start, bitLenInt length, const bitCapInt& disposedPerm);
184 
185  using QEngine::Allocate;
186  bitLenInt Allocate(bitLenInt start, bitLenInt length);
187 
190  void XMask(const bitCapInt& mask);
191  void PhaseParity(real1_f radians, const bitCapInt& mask);
192  void PhaseRootNMask(bitLenInt n, const bitCapInt& mask);
193 
200  void ROL(bitLenInt shift, bitLenInt start, bitLenInt length);
201 #if ENABLE_ALU
202  void INC(const bitCapInt& toAdd, bitLenInt start, bitLenInt length);
203  void CINC(const bitCapInt& toAdd, bitLenInt inOutStart, bitLenInt length, const std::vector<bitLenInt>& controls);
204  void INCS(const bitCapInt& toAdd, bitLenInt start, bitLenInt length, bitLenInt overflowIndex);
205 #if ENABLE_BCD
206  void INCBCD(const bitCapInt& toAdd, bitLenInt start, bitLenInt length);
207 #endif
208  void MUL(const bitCapInt& toMul, bitLenInt inOutStart, bitLenInt carryStart, bitLenInt length);
209  void DIV(const bitCapInt& toDiv, bitLenInt inOutStart, bitLenInt carryStart, bitLenInt length);
210  void MULModNOut(
211  const bitCapInt& toMul, const bitCapInt& modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length);
212  void IMULModNOut(
213  const bitCapInt& toMul, const bitCapInt& modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length);
214  void POWModNOut(
215  const bitCapInt& base, const bitCapInt& modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length);
216  void CMUL(const bitCapInt& toMul, bitLenInt inOutStart, bitLenInt carryStart, bitLenInt length,
217  const std::vector<bitLenInt>& controls);
218  void CDIV(const bitCapInt& toDiv, bitLenInt inOutStart, bitLenInt carryStart, bitLenInt length,
219  const std::vector<bitLenInt>& controls);
220  void CMULModNOut(const bitCapInt& toMul, const bitCapInt& modN, bitLenInt inStart, bitLenInt outStart,
221  bitLenInt length, const std::vector<bitLenInt>& controls);
222  void CIMULModNOut(const bitCapInt& toMul, const bitCapInt& modN, bitLenInt inStart, bitLenInt outStart,
223  bitLenInt length, const std::vector<bitLenInt>& controls);
224  void CPOWModNOut(const bitCapInt& base, const bitCapInt& modN, bitLenInt inStart, bitLenInt outStart,
225  bitLenInt length, const std::vector<bitLenInt>& controls);
226  void FullAdd(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt carryInSumOut, bitLenInt carryOut);
227  void IFullAdd(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt carryInSumOut, bitLenInt carryOut);
228  bitCapInt IndexedLDA(bitLenInt indexStart, bitLenInt indexLength, bitLenInt valueStart, bitLenInt valueLength,
229  const unsigned char* values, bool resetValue = true);
230  bitCapInt IndexedADC(bitLenInt indexStart, bitLenInt indexLength, bitLenInt valueStart, bitLenInt valueLength,
231  bitLenInt carryIndex, const unsigned char* values);
232  bitCapInt IndexedSBC(bitLenInt indexStart, bitLenInt indexLength, bitLenInt valueStart, bitLenInt valueLength,
233  bitLenInt carryIndex, const unsigned char* values);
234  void Hash(bitLenInt start, bitLenInt length, const unsigned char* values);
235  void CPhaseFlipIfLess(const bitCapInt& greaterPerm, bitLenInt start, bitLenInt length, bitLenInt flagIndex);
236  void PhaseFlipIfLess(const bitCapInt& greaterPerm, bitLenInt start, bitLenInt length);
237 
239 #endif
240 
247  void SetPermutation(const bitCapInt& perm, const complex& phaseFac = CMPLX_DEFAULT_ARG);
249  void UniformlyControlledSingleBit(const std::vector<bitLenInt>& controls, bitLenInt qubitIndex,
250  const complex* mtrxs, const std::vector<bitCapInt>& mtrxSkipPowers, const bitCapInt& mtrxSkipValueMask);
251  void UniformParityRZ(const bitCapInt& mask, real1_f angle);
252  void CUniformParityRZ(const std::vector<bitLenInt>& controls, const bitCapInt& mask, real1_f angle);
253 
262  real1_f Prob(bitLenInt qubitIndex);
263  real1_f CtrlOrAntiProb(bool controlState, bitLenInt control, bitLenInt target);
264  real1_f ProbReg(bitLenInt start, bitLenInt length, const bitCapInt& permutation);
265  real1_f ProbMask(const bitCapInt& mask, const bitCapInt& permutation);
266  real1_f ProbParity(const bitCapInt& mask);
268  bitCapInt MAll();
269  bool ForceMParity(const bitCapInt& mask, bool result, bool doForce = true);
270  void NormalizeState(
271  real1_f nrm = REAL1_DEFAULT_ARG, real1_f norm_thresh = REAL1_DEFAULT_ARG, real1_f phaseArg = ZERO_R1_F);
272  real1_f SumSqrDiff(QInterfacePtr toCompare) { return SumSqrDiff(std::dynamic_pointer_cast<QEngineCPU>(toCompare)); }
273  real1_f SumSqrDiff(QEngineCPUPtr toCompare);
276 
279 protected:
280  real1_f GetExpectation(bitLenInt valueStart, bitLenInt valueLength);
281 
283  {
284  if (isSparse) {
285  return std::make_shared<StateVectorSparse>(elemCount);
286  } else {
287  return std::make_shared<StateVectorArray>(elemCount);
288  }
289  }
291  void FreeStateVec(complex* sv = nullptr) { stateVec = nullptr; }
292 
293  void Dispatch(bitCapIntOcl workItemCount, DispatchFn fn)
294  {
295 #if ENABLE_QUNIT_CPU_PARALLEL && ENABLE_PTHREAD
296  if ((workItemCount >= pow2Ocl(GetPreferredConcurrencyPower())) && (workItemCount < GetStride())) {
297  dispatchQueue.dispatch(fn);
298  } else {
299  Finish();
300  fn();
301  }
302 #else
303  fn();
304 #endif
305  }
306 
307  void DecomposeDispose(bitLenInt start, bitLenInt length, QEngineCPUPtr dest);
308  void Apply2x2(bitCapInt offset1, bitCapInt offset2, const complex mtrx[4U], bitLenInt bitCount,
309  const bitCapInt* qPowersSorted, bool doCalcNorm, real1_f norm_thresh = REAL1_DEFAULT_ARG);
310  void Apply2x2Sparse(bitCapInt offset1, bitCapInt offset2, const complex mtrx[4U], bitLenInt bitCount,
311  const bitCapInt* qPowersSorted, bool doCalcNorm, real1_f norm_thresh = REAL1_DEFAULT_ARG);
312  void UpdateRunningNorm(real1_f norm_thresh = REAL1_DEFAULT_ARG);
313  using QEngine::ApplyM;
314  void ApplyM(const bitCapInt& mask, const bitCapInt& result, const complex& nrm);
315 
316 #if ENABLE_ALU
317  void INCDECC(const bitCapInt& toMod, bitLenInt inOutStart, bitLenInt length, bitLenInt carryIndex);
318  void INCDECSC(const bitCapInt& toMod, bitLenInt inOutStart, bitLenInt length, bitLenInt carryIndex);
319  void INCDECSC(
320  const bitCapInt& toMod, bitLenInt inOutStart, bitLenInt length, bitLenInt overflowIndex, bitLenInt carryIndex);
321 #if ENABLE_BCD
322  void INCDECBCDC(const bitCapInt& toMod, bitLenInt inOutStart, bitLenInt length, bitLenInt carryIndex);
323 #endif
324 
325  typedef std::function<bitCapIntOcl(const bitCapIntOcl&, const bitCapIntOcl&)> IOFn;
326  void MULDIV(const IOFn& inFn, const IOFn& outFn, const bitCapInt& toMul, const bitLenInt& inOutStart,
327  const bitLenInt& carryStart, const bitLenInt& length);
328  void CMULDIV(const IOFn& inFn, const IOFn& outFn, const bitCapInt& toMul, const bitLenInt& inOutStart,
329  const bitLenInt& carryStart, const bitLenInt& length, const std::vector<bitLenInt>& controls);
330 
331  typedef std::function<bitCapIntOcl(const bitCapIntOcl&)> MFn;
332  void ModNOut(const MFn& kernelFn, const bitCapInt& modN, const bitLenInt& inStart, const bitLenInt& outStart,
333  const bitLenInt& length, const bool& inverse = false);
334  void CModNOut(const MFn& kernelFn, const bitCapInt& modN, const bitLenInt& inStart, const bitLenInt& outStart,
335  const bitLenInt& length, const std::vector<bitLenInt>& controls, const bool& inverse = false);
336 #endif
337 };
338 } // namespace Qrack
Definition: dispatchqueue.hpp:33
void finish()
Definition: dispatchqueue.cpp:40
void dispatch(const DispatchFn &op)
Definition: dispatchqueue.cpp:66
void dump()
Definition: dispatchqueue.cpp:51
bool isFinished()
Definition: dispatchqueue.hpp:51
bitLenInt GetPreferredConcurrencyPower()
Definition: parallel_for.hpp:51
bitCapIntOcl GetStride()
Definition: parallel_for.hpp:50
General purpose QEngineCPU implementation.
Definition: qengine_cpu.hpp:36
void PhaseRootNMask(bitLenInt n, const bitCapInt &mask)
Masked PhaseRootN gate.
Definition: state.cpp:1056
void SetSparseProbabilityFloor(real1_f p)
Set the sparse-simulation amplitude probability floor, before truncation.
Definition: qengine_cpu.hpp:109
bool isFinished()
Returns "false" if asynchronous work is still running, and "true" if all previously dispatched asynch...
Definition: qengine_cpu.hpp:76
bitLenInt Compose(QEngineCPUPtr toCopy)
Combine (a copy of) another QEngineCPU with this one, after the last bit index of this one.
Definition: state.cpp:1271
void DecomposeDispose(bitLenInt start, bitLenInt length, QEngineCPUPtr dest)
Minimally decompose a set of contigious bits from the separable unit.
Definition: state.cpp:1551
void Dispatch(bitCapIntOcl workItemCount, DispatchFn fn)
Definition: qengine_cpu.hpp:293
void QueueSetRunningNorm(real1_f runningNrm)
Add an operation to the (OpenCL) queue, to set the value of runningNorm, which is the normalization c...
Definition: qengine_cpu.hpp:156
StateVectorArrayPtr CastStateVecDense()
Definition: qengine_cpu.hpp:58
complex GetAmplitude(const bitCapInt &perm)
Get the representational amplitude of a full permutation.
Definition: state.cpp:187
StateVectorPtr stateVec
Definition: qengine_cpu.hpp:38
void SparseRenorm(real1_f totFidelityLoss)
Definition: qengine_cpu.hpp:118
void Copy(QInterfacePtr orig)
Definition: qengine_cpu.hpp:47
void ZeroAmplitudes()
Set all amplitudes to 0, and optionally temporarily deallocate state vector RAM.
Definition: qengine_cpu.hpp:101
void SetAmplitude(const bitCapInt &perm, const complex &amp)
Sets the representational amplitude of a full permutation.
Definition: state.cpp:203
void TruncateBySize()
Definition: qengine_cpu.hpp:111
QEnginePtr CloneEmpty()
Clone this QEngine's settings, with a zeroed state vector.
Definition: utility.cpp:35
virtual void ApplyM(const bitCapInt &qPower, bool result, const complex &nrm)
Definition: qengine.hpp:161
bool isSparse
Definition: qengine_cpu.hpp:43
void CopyStateVec(QEnginePtr src)
Exactly copy the state vector of a different QEngine instance.
Definition: state.cpp:165
void FreeStateVec(complex *sv=nullptr)
Definition: qengine_cpu.hpp:291
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: state.cpp:2250
void Dispose(bitLenInt start, bitLenInt length)
Minimally decompose a set of contiguous bits from the separably composed unit, and discard the separa...
Definition: state.cpp:1703
bitLenInt Compose(QInterfacePtr toCopy, bitLenInt start)
Compose() a QInterface peer, inserting its qubit into index order at start index.
Definition: qengine_cpu.hpp:174
void GetQuantumState(complex *outputState)
Get pure quantum state, in unsigned int permutation basis.
Definition: state.cpp:322
real1_f sparse_thresh
Definition: qengine_cpu.hpp:44
bitLenInt Compose(QInterfacePtr toCopy)
Combine another QInterface with this one, after the last bit index of this one.
Definition: qengine_cpu.hpp:171
void XMask(const bitCapInt &mask)
Masked X gate.
Definition: state.cpp:965
std::function< bitCapIntOcl(const bitCapIntOcl &, const bitCapIntOcl &)> IOFn
Definition: qengine_cpu.hpp:325
StateVectorPtr AllocStateVec(bitCapIntOcl elemCount)
Definition: qengine_cpu.hpp:282
void GetProbs(real1 *outputProbs)
Get all probabilities, in unsigned int permutation basis.
Definition: state.cpp:338
QEngineCPU(bitLenInt qBitCount, const bitCapInt &initState, qrack_rand_gen_ptr rgp=nullptr, const complex &phaseFac=CMPLX_DEFAULT_ARG, bool doNorm=false, bool randomGlobalPhase=true, bool ignored=false, int64_t ignored2=-1, bool useHardwareRNG=true, bool useSparseStateVec=false, real1_f norm_thresh=REAL1_EPSILON, std::vector< int64_t > ignored3={}, bitLenInt ignored4=0U, real1_f ignored5=_qrack_qunit_sep_thresh)
Initialize a coherent unit with qBitCount number of bits, to initState unsigned integer permutation s...
Definition: state.cpp:37
void ResetStateVec(StateVectorPtr sv)
Definition: qengine_cpu.hpp:290
void ShuffleBuffers(QEnginePtr engine)
Swap the high half of this engine with the low half of another.
Definition: state.cpp:134
void LossyLoadStateVector(std::string f)
Read the quantum state from disk with lossy compression.
Definition: state.cpp:273
real1_f GetExpectation(bitLenInt valueStart, bitLenInt valueLength)
Definition: utility.cpp:70
void CModNOut(const MFn &kernelFn, const bitCapInt &modN, const bitLenInt &inStart, const bitLenInt &outStart, const bitLenInt &length, const std::vector< bitLenInt > &controls, const bool &inverse=false)
Definition: arithmetic.cpp:670
std::function< bitCapIntOcl(const bitCapIntOcl &)> MFn
Definition: qengine_cpu.hpp:331
void Copy(QEngineCPUPtr orig)
Definition: qengine_cpu.hpp:48
~QEngineCPU()
Definition: qengine_cpu.hpp:67
void PhaseParity(real1_f radians, const bitCapInt &mask)
Parity phase gate.
Definition: state.cpp:1009
void SetAmplitudePage(const complex *pagePtr, bitCapIntOcl offset, bitCapIntOcl length)
Copy a "page" of amplitudes from pagePtr into this QEngine's internal state.
Definition: state.cpp:80
void MULDIV(const IOFn &inFn, const IOFn &outFn, const bitCapInt &toMul, const bitLenInt &inOutStart, const bitLenInt &carryStart, const bitLenInt &length)
Definition: arithmetic.cpp:422
virtual QInterfacePtr Decompose(bitLenInt start, bitLenInt length)
Definition: qengine.hpp:287
void INCDECC(const bitCapInt &toMod, bitLenInt inOutStart, bitLenInt length, bitLenInt carryIndex)
Add integer (without sign, with carry)
Definition: arithmetic.cpp:175
void Apply2x2(bitCapInt offset1, bitCapInt offset2, const complex mtrx[4U], bitLenInt bitCount, const bitCapInt *qPowersSorted, bool doCalcNorm, real1_f norm_thresh=REAL1_DEFAULT_ARG)
Definition: state.cpp:705
void CMULDIV(const IOFn &inFn, const IOFn &outFn, const bitCapInt &toMul, const bitLenInt &inOutStart, const bitLenInt &carryStart, const bitLenInt &length, const std::vector< bitLenInt > &controls)
Definition: arithmetic.cpp:488
bool IsZeroAmplitude()
Returns "true" only if amplitudes are all totally 0.
Definition: qengine_cpu.hpp:142
void QueueSetDoNormalize(bool doNorm)
Add an operation to the (OpenCL) queue, to set the value of doNormalize, which controls whether to au...
Definition: qengine_cpu.hpp:152
double logFidelity
Definition: qengine_cpu.hpp:42
void GetAmplitudePage(complex *pagePtr, bitCapIntOcl offset, bitCapIntOcl length)
Copy a "page" of amplitudes from this QEngine's internal state, into pagePtr.
Definition: state.cpp:66
void SetQuantumState(const complex *inputState)
Set arbitrary pure quantum state, in unsigned int permutation basis.
Definition: state.cpp:308
bitLenInt Allocate(bitLenInt start, bitLenInt length)
Allocate new "length" count of |0> state qubits at specified qubit index start position.
Definition: utility.cpp:54
real1_f FirstNonzeroPhase()
Get phase of lowest permutation nonzero amplitude.
Definition: qengine_cpu.hpp:92
StateVectorSparsePtr CastStateVecSparse()
Definition: qengine_cpu.hpp:57
void Dump()
If asynchronous work is still running, let the simulator know that it can be aborted.
Definition: qengine_cpu.hpp:85
double GetUnitaryFidelity()
When "Schmidt-decomposition rounding parameter" ("SDRP") is being used, starting from initial 1....
Definition: qengine_cpu.hpp:139
void Apply2x2Sparse(bitCapInt offset1, bitCapInt offset2, const complex mtrx[4U], bitLenInt bitCount, const bitCapInt *qPowersSorted, bool doCalcNorm, real1_f norm_thresh=REAL1_DEFAULT_ARG)
Definition: state.cpp:842
void INCDECBCDC(const bitCapInt &toMod, bitLenInt inOutStart, bitLenInt length, bitLenInt carryIndex)
Add BCD integer (without sign, with carry)
Definition: arithmetic.cpp:888
void ResetUnitaryFidelity()
Reset the internal fidelity calculation tracker to 1.0.
Definition: qengine_cpu.hpp:140
void LossySaveStateVector(std::string f, int p=6, int b=4)
Write the quantum state to disk with lossy compression.
Definition: state.cpp:256
void INCDECSC(const bitCapInt &toMod, bitLenInt inOutStart, bitLenInt length, bitLenInt carryIndex)
Common driver method behind INCSC and DECSC (without overflow flag)
Definition: arithmetic.cpp:312
bitCapInt GetAmplitudeCount()
Count of amplitudes, which might be less than state vector if sparse or compressed.
Definition: qengine_cpu.hpp:130
void ModNOut(const MFn &kernelFn, const bitCapInt &modN, const bitLenInt &inStart, const bitLenInt &outStart, const bitLenInt &length, const bool &inverse=false)
Definition: arithmetic.cpp:595
void Finish()
If asynchronous work is still running, block until it finishes.
Definition: qengine_cpu.hpp:69
Abstract QEngine implementation, for all "Schroedinger method" engines.
Definition: qengine.hpp:31
virtual void Copy(QInterfacePtr orig)
Copy this QInterface.
Definition: qinterface.hpp:222
virtual void ApplyM(const bitCapInt &qPower, bool result, const complex &nrm)
Definition: qengine.hpp:161
real1 runningNorm
The value stored in runningNorm should always be the total probability implied by the norm of all amp...
Definition: qengine.hpp:39
virtual void Decompose(bitLenInt start, QInterfacePtr dest)=0
Minimally decompose a set of contiguous bits from the separably composed unit, into "destination".
bitCapInt maxQPower
Definition: qinterface.hpp:149
virtual bitLenInt Allocate(bitLenInt length)
Allocate new "length" count of |0> state qubits at end of qubit index position.
Definition: qinterface.hpp:488
virtual bitLenInt Compose(QInterfacePtr toCopy)
Combine another QInterface with this one, after the last bit index of this one.
Definition: qinterface.hpp:382
bool doNormalize
Definition: qinterface.hpp:143
Half-precision floating-point type.
Definition: half.hpp:2206
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:647
void DIV(const bitCapInt &toDiv, bitLenInt inOutStart, bitLenInt carryStart, bitLenInt length)
Divide by integer.
Definition: arithmetic.cpp:474
void IFullAdd(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt carryInSumOut, bitLenInt carryOut)
Inverse of FullAdd.
Definition: arithmetic.cpp:1595
void Hash(bitLenInt start, bitLenInt length, const unsigned char *values)
Transform a length of qubit register via lookup through a hash table.
Definition: arithmetic.cpp:1449
void CINC(const bitCapInt &toAdd, bitLenInt inOutStart, bitLenInt length, const std::vector< bitLenInt > &controls)
Add integer (without sign, with controls)
Definition: arithmetic.cpp:121
bitCapInt IndexedADC(bitLenInt indexStart, bitLenInt indexLength, bitLenInt valueStart, bitLenInt valueLength, bitLenInt carryIndex, const unsigned char *values)
Add based on an indexed load from classical memory.
Definition: arithmetic.cpp:1088
void INC(const bitCapInt &toAdd, bitLenInt start, bitLenInt length)
Add integer (without sign)
Definition: arithmetic.cpp:73
void CPhaseFlipIfLess(const 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: arithmetic.cpp:1680
void CMUL(const bitCapInt &toMul, bitLenInt inOutStart, bitLenInt carryStart, bitLenInt length, const std::vector< bitLenInt > &controls)
Controlled multiplication by integer.
Definition: arithmetic.cpp:553
void CDIV(const bitCapInt &toDiv, bitLenInt inOutStart, bitLenInt carryStart, bitLenInt length, const std::vector< bitLenInt > &controls)
Controlled division by power of integer.
Definition: arithmetic.cpp:575
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:751
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:737
bitCapInt IndexedLDA(bitLenInt indexStart, bitLenInt indexLength, bitLenInt valueStart, bitLenInt valueLength, const unsigned char *values, bool resetValue=true)
Set 8 bit register bits based on read from classical memory.
Definition: arithmetic.cpp:983
void INCBCD(const bitCapInt &toAdd, bitLenInt start, bitLenInt length)
Add BCD integer (without sign)
Definition: arithmetic.cpp:777
void CPOWModNOut(const bitCapInt &base, const 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: arithmetic.cpp:763
void POWModNOut(const bitCapInt &base, const bitCapInt &modN, bitLenInt inStart, bitLenInt outStart, bitLenInt length)
Raise a classical base to a quantum power, modulo N, (out of place)
Definition: arithmetic.cpp:658
void PhaseFlipIfLess(const bitCapInt &greaterPerm, bitLenInt start, bitLenInt length)
This is an expedient for an adaptive Grover's search for a function's global minimum.
Definition: arithmetic.cpp:1705
void MUL(const bitCapInt &toMul, bitLenInt inOutStart, bitLenInt carryStart, bitLenInt length)
Multiply by integer.
Definition: arithmetic.cpp:458
void FullAdd(bitLenInt inputBit1, bitLenInt inputBit2, bitLenInt carryInSumOut, bitLenInt carryOut)
Quantum analog of classical "Full Adder" gate.
Definition: arithmetic.cpp:1511
void INCS(const bitCapInt &toAdd, bitLenInt start, bitLenInt length, bitLenInt overflowIndex)
Add an integer to the register, with sign and without carry.
Definition: arithmetic.cpp:227
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:634
bitCapInt IndexedSBC(bitLenInt indexStart, bitLenInt indexLength, bitLenInt valueStart, bitLenInt valueLength, bitLenInt carryIndex, const unsigned char *values)
Subtract based on an indexed load from classical memory.
Definition: arithmetic.cpp:1265
void ROL(bitLenInt shift, bitLenInt start, bitLenInt length)
"Circular shift left" - shift bits left, and carry last bits.
Definition: arithmetic.cpp:23
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 U(bitLenInt target, real1_f theta, real1_f phi, real1_f lambda)
General unitary gate.
Definition: rotational.cpp:18
void UniformlyControlledSingleBit(const std::vector< bitLenInt > &controls, bitLenInt qubitIndex, const complex *mtrxs, const std::vector< bitCapInt > &mtrxSkipPowers, const bitCapInt &mtrxSkipValueMask)
Definition: state.cpp:1094
void CUniformParityRZ(const std::vector< bitLenInt > &controls, const 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: state.cpp:1228
void SetPermutation(const bitCapInt &perm, const complex &phaseFac=CMPLX_DEFAULT_ARG)
Set to a specific permutation of all qubits.
Definition: state.cpp:228
void UniformParityRZ(const bitCapInt &mask, real1_f angle)
If the target qubit set parity is odd, this applies a phase factor of .
Definition: state.cpp:1200
real1_f ProbParity(const bitCapInt &mask)
Overall probability of any odd permutation of the masked set of bits.
Definition: state.cpp:1949
virtual real1_f FirstNonzeroPhase()
Get phase of lowest permutation nonzero amplitude.
Definition: qinterface.hpp:3087
real1_f CtrlOrAntiProb(bool controlState, bitLenInt control, bitLenInt target)
PSEUDO-QUANTUM Direct measure of bit probability to be in |1> state, if control is in |0>/|1>,...
Definition: state.cpp:1814
real1_f Prob(bitLenInt qubitIndex)
PSEUDO-QUANTUM Direct measure of bit probability to be in |1> state.
Definition: state.cpp:1751
bool ForceMParity(const 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: state.cpp:2052
QInterfacePtr Clone()
Clone this QInterface.
Definition: utility.cpp:17
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: state.cpp:2198
QInterfacePtr Copy()
Copy this QInterface.
Definition: utility.cpp:45
bitCapInt MAll()
Measure permutation state of all coherent bits.
Definition: state.cpp:2026
bitCapInt HighestProbAll()
Get highest probability permutation.
Definition: state.cpp:1995
real1_f ProbReg(bitLenInt start, bitLenInt length, const bitCapInt &permutation)
Direct measure of register permutation probability.
Definition: state.cpp:1872
real1_f ProbMask(const bitCapInt &mask, const bitCapInt &permutation)
Direct measure of masked permutation probability.
Definition: state.cpp:1910
real1_f SumSqrDiff(QInterfacePtr toCompare)
Calculates (1 - <\psi_e|\psi_c>) between states |\psi_c> and |\psi_e>.
Definition: qengine_cpu.hpp:272
GLOSSARY: bitLenInt - "bit-length integer" - unsigned integer ID of qubit position in register bitCap...
Definition: complex16x2simd.hpp:25
std::shared_ptr< QEngine > QEnginePtr
Definition: qrack_types.hpp:153
std::shared_ptr< QInterface > QInterfacePtr
Definition: qinterface.hpp:29
const real1_f _qrack_qunit_sep_thresh
Definition: qrack_functions.hpp:258
void rotate(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, const bitCapInt &stride)
const size_t QRACK_SPARSE_MAX_KEYS
Definition: qrack_functions.hpp:270
std::function< void(void)> DispatchFn
Definition: dispatchqueue.hpp:31
std::complex< real1 > complex
Definition: qrack_types.hpp:140
std::shared_ptr< StateVectorSparse > StateVectorSparsePtr
Definition: qrack_types.hpp:151
std::shared_ptr< QEngineCPU > QEngineCPUPtr
Definition: qengine_cpu.hpp:24
QRACK_CONST real1 REAL1_EPSILON
Definition: qrack_types.hpp:203
QRACK_CONST real1 ZERO_R1
Definition: qrack_types.hpp:186
float real1_f
Definition: qrack_types.hpp:107
std::shared_ptr< StateVectorArray > StateVectorArrayPtr
Definition: qrack_types.hpp:150
QRACK_CONST complex CMPLX_DEFAULT_ARG
Definition: qrack_types.hpp:262
std::shared_ptr< StateVector > StateVectorPtr
Definition: qrack_types.hpp:147
void reverse(BidirectionalIterator first, BidirectionalIterator last, const bitCapInt &stride)
bitCapIntOcl pow2Ocl(const bitLenInt &p)
Definition: qrack_functions.hpp:157
uint32 sqrt(uint32 &r, int &exp)
Fixed point square root.
Definition: half.hpp:1638
half log(half arg)
Natural logarithm.
Definition: half.hpp:3301
half exp(half arg)
Exponential function.
Definition: half.hpp:3184
#define REAL1_DEFAULT_ARG
Definition: qrack_types.hpp:179
#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