Qrack  9.13
General classical-emulating-quantum development framework
qstabilizer.hpp
Go to the documentation of this file.
1 //
3 // (C) Daniel Strano and the Qrack contributors 2017-2023. All rights reserved.
4 //
5 // Adapted from:
6 //
7 // CHP: CNOT-Hadamard-Phase
8 // Stabilizer Quantum Computer Simulator
9 // by Scott Aaronson
10 // Last modified June 30, 2004
11 //
12 // Thanks to Simon Anders and Andrew Cross for bugfixes
13 //
14 // https://www.scottaaronson.com/chp/
15 //
16 // Daniel Strano and the Qrack contributers appreciate Scott Aaronson's open sharing of the CHP code, and we hope that
17 // vm6502q/qrack is one satisfactory framework by which CHP could be adapted to enter the C++ STL. Our project
18 // philosophy aims to raise the floor of decentralized quantum computing technology access across all modern platforms,
19 // for all people, not commercialization.
20 //
21 // Licensed under the GNU Lesser General Public License V3.
22 // See LICENSE.md in the project root or https://www.gnu.org/licenses/lgpl-3.0.en.html
23 // for details.
24 
25 #pragma once
26 
27 #include "qinterface.hpp"
28 
29 namespace Qrack {
30 
34 
35  AmplitudeEntry(const bitCapInt& p, const complex& a)
36  : permutation(p)
37  , amplitude(a)
38  {
39  }
40 };
41 
42 class QStabilizer;
43 typedef std::shared_ptr<QStabilizer> QStabilizerPtr;
44 
45 class QStabilizer : public QInterface {
46 protected:
47  unsigned rawRandBools;
51 
52  // Phase bits: 0 for +1, 1 for i, 2 for -1, 3 for -i. Normally either 0 or 2.
53  std::vector<uint8_t> r;
54  // Typedef for special type std::vector<bool> compatibility
55  typedef std::vector<bool> BoolVector;
56  // (2n+1)*n matrix for stabilizer/destabilizer x bits (there's one "scratch row" at the bottom)
57  std::vector<BoolVector> x;
58  // (2n+1)*n matrix for z bits
59  std::vector<BoolVector> z;
60 
61  typedef std::function<void(const bitLenInt&)> StabilizerParallelFunc;
62  typedef std::function<void(void)> DispatchFn;
63  void Dispatch(DispatchFn fn) { fn(); }
64 
65  void ParFor(StabilizerParallelFunc fn, std::vector<bitLenInt> qubits);
66 
67  void SetPhaseOffset(real1_f phaseArg)
68  {
69  phaseOffset = (real1)phaseArg;
70  const bool isNeg = phaseOffset < 0;
71  if (isNeg) {
73  }
74  phaseOffset -= (real1)(((size_t)(phaseOffset / (2 * PI_R1))) * (2 * PI_R1));
75  if (phaseOffset > PI_R1) {
76  phaseOffset -= 2 * PI_R1;
77  }
78  if (isNeg) {
80  }
81  }
82 
83  using QInterface::Copy;
84  void Copy(QInterfacePtr orig) { Copy(std::dynamic_pointer_cast<QStabilizer>(orig)); }
85  void Copy(QStabilizerPtr orig)
86  {
87  QInterface::Copy(std::dynamic_pointer_cast<QInterface>(orig));
88  rawRandBools = orig->rawRandBools;
89  rawRandBoolsRemaining = orig->rawRandBoolsRemaining;
90  phaseOffset = orig->phaseOffset;
91  maxStateMapCacheQubitCount = orig->maxStateMapCacheQubitCount;
92  r = orig->r;
93  x = orig->x;
94  z = orig->z;
95  }
96 
97 public:
98  QStabilizer(bitLenInt n, const bitCapInt& perm = ZERO_BCI, qrack_rand_gen_ptr rgp = nullptr,
99  const complex& phaseFac = CMPLX_DEFAULT_ARG, bool doNorm = false, bool randomGlobalPhase = true,
100  bool ignored2 = false, int64_t ignored3 = -1, bool useHardwareRNG = true, bool ignored4 = false,
101  real1_f ignored5 = REAL1_EPSILON, std::vector<int64_t> ignored6 = {}, bitLenInt ignored7 = 0U,
102  real1_f ignored8 = _qrack_qunit_sep_thresh);
103 
105 
107 
108  bool isClifford() { return true; };
109  bool isClifford(bitLenInt qubit) { return true; };
110 
112 
114 
116  complex GetPhaseOffset() { return std::polar(ONE_R1, phaseOffset); }
117 
118  void SetPermutation(const bitCapInt& perm, const complex& phaseFac = CMPLX_DEFAULT_ARG);
119 
120  void SetRandomSeed(uint32_t seed)
121  {
122  if (rand_generator != NULL) {
123  rand_generator->seed(seed);
124  }
125  }
126 
127  void SetDevice(int64_t dID) {}
128 
129  bool Rand()
130  {
131  if (hardware_rand_generator != NULL) {
132  if (!rawRandBoolsRemaining) {
134  rawRandBoolsRemaining = sizeof(unsigned) * bitsInByte;
135  }
137 
138  return (bool)((rawRandBools >> rawRandBoolsRemaining) & 1U);
139  } else {
141  }
142  }
143 
144  void Clear()
145  {
146  x.clear();
147  z.clear();
148  r.clear();
150  qubitCount = 0U;
151  maxQPower = ONE_BCI;
152  }
153 
154 protected:
156  void rowcopy(const bitLenInt& i, const bitLenInt& k)
157  {
158  if (i == k) {
159  return;
160  }
161 
162  x[i] = x[k];
163  z[i] = z[k];
164  r[i] = r[k];
165  }
167  void rowswap(const bitLenInt& i, const bitLenInt& k)
168  {
169  if (i == k) {
170  return;
171  }
172 
173  std::swap(x[k], x[i]);
174  std::swap(z[k], z[i]);
175  std::swap(r[k], r[i]);
176  }
178  void rowset(const bitLenInt& i, bitLenInt b)
179  {
180  BoolVector& xi = x[i];
181  BoolVector& zi = z[i];
182  std::fill(xi.begin(), xi.end(), false);
183  std::fill(zi.begin(), zi.end(), false);
184  r[i] = 0;
185 
186  if (b < qubitCount) {
187  xi[b] = true;
188  } else {
189  b -= qubitCount;
190  zi[b] = true;
191  }
192  }
194  void rowmult(const bitLenInt& i, const bitLenInt& k)
195  {
196  r[i] = clifford(i, k);
197  for (bitLenInt j = 0U; j < qubitCount; ++j) {
198  BoolVector& xi = x[i];
199  BoolVector& zi = z[i];
200  xi[j] = xi[j] ^ x[k][j];
201  zi[j] = zi[j] ^ z[k][j];
202  }
203  }
205  uint8_t clifford(const bitLenInt& i, const bitLenInt& k);
206 
212  void seed(const bitLenInt& g);
213 
215  AmplitudeEntry getBasisAmp(const real1_f& nrm);
216 
218  void setBasisState(const real1_f& nrm, complex* stateVec);
219 
221  void setBasisState(const real1_f& nrm, QInterfacePtr eng);
222 
224  void setBasisState(const real1_f& nrm, std::map<bitCapInt, complex>& stateMap);
225 
227  void setBasisProb(const real1_f& nrm, real1* outputProbs);
228 
230  real1_f getExpectation(const real1_f& nrm, const std::vector<bitCapInt>& bitPowers,
231  const std::vector<bitCapInt>& perms, const bitCapInt& offset);
232 
235  const real1_f& nrm, const std::vector<bitCapInt>& bitPowers, const std::vector<real1_f>& weights);
236 
238  real1_f getVariance(const real1_f& mean, const real1_f& nrm, const std::vector<bitCapInt>& bitPowers,
239  const std::vector<bitCapInt>& perms, const bitCapInt& offset);
240 
242  real1_f getVariance(const real1_f& mean, const real1_f& nrm, const std::vector<bitCapInt>& bitPowers,
243  const std::vector<real1_f>& weights);
244 
245  void DecomposeDispose(const bitLenInt start, const bitLenInt length, QStabilizerPtr toCopy);
246 
248  QStabilizerPtr toCompare, real1_f error_tol = TRYDECOMPOSE_EPSILON, bool isDiscrete = false);
249 
250 public:
258 
259  bitCapInt PermCount() { return pow2(gaussian()); }
260 
261  void SetQuantumState(const complex* inputState);
262  void SetAmplitude(const bitCapInt& perm, const complex& amp)
263  {
264  throw std::domain_error("QStabilizer::SetAmplitude() not implemented!");
265  }
266 
267  void SetRandGlobalPhase(bool isRand) { randGlobalPhase = isRand; }
268 
270  void CNOT(bitLenInt control, bitLenInt target);
272  void CY(bitLenInt control, bitLenInt target);
274  void CZ(bitLenInt control, bitLenInt target);
276  void AntiCNOT(bitLenInt control, bitLenInt target);
278  void AntiCY(bitLenInt control, bitLenInt target);
280  void AntiCZ(bitLenInt control, bitLenInt target);
282  using QInterface::H;
283  void H(bitLenInt qubitIndex);
285  using QInterface::X;
286  void X(bitLenInt qubitIndex);
288  void Y(bitLenInt qubitIndex);
290  void Z(bitLenInt qubitIndex);
292  void S(bitLenInt qubitIndex);
294  void IS(bitLenInt qubitIndex);
295  // Swap two bits
296  void Swap(bitLenInt qubitIndex1, bitLenInt qubitIndex2);
297  // Swap two bits and apply a phase factor of i if they are different
298  void ISwap(bitLenInt qubitIndex1, bitLenInt qubitIndex2);
299  // Swap two bits and apply a phase factor of -i if they are different
300  void IISwap(bitLenInt qubitIndex1, bitLenInt qubitIndex2);
301 
303  bool ForceM(bitLenInt t, bool result, bool doForce = true, bool doApply = true);
304 
306  void GetQuantumState(complex* stateVec);
307 
309  void GetQuantumState(QInterfacePtr eng);
310 
312  std::map<bitCapInt, complex> GetQuantumState();
313 
315  void GetProbs(real1* outputProbs);
316 
318  complex GetAmplitude(const bitCapInt& perm);
319 
321  std::vector<complex> GetAmplitudes(std::vector<bitCapInt> perms);
322 
325 
328 
331  const std::vector<bitLenInt>& bits, const std::vector<bitCapInt>& perms, const bitCapInt& offset = ZERO_BCI);
332  real1_f ExpectationFloatsFactorized(const std::vector<bitLenInt>& bits, const std::vector<real1_f>& weights);
335  const std::vector<bitLenInt>& bits, const std::vector<bitCapInt>& perms, const bitCapInt& offset = ZERO_BCI);
336  real1_f VarianceFloatsFactorized(const std::vector<bitLenInt>& bits, const std::vector<real1_f>& weights);
337 
340  real1_f ProbPermRdm(const bitCapInt& perm, bitLenInt ancillaeStart);
341 
343  real1_f ProbMask(const bitCapInt& mask, const bitCapInt& permutation);
344 
348  bool IsSeparableZ(const bitLenInt& target);
352  bool IsSeparableX(const bitLenInt& target);
356  bool IsSeparableY(const bitLenInt& target);
364  uint8_t IsSeparable(const bitLenInt& target);
365 
366  using QInterface::Compose;
367  bitLenInt Compose(QInterfacePtr toCopy) { return Compose(std::dynamic_pointer_cast<QStabilizer>(toCopy)); }
368  bitLenInt Compose(QStabilizerPtr toCopy) { return Compose(toCopy, qubitCount); }
370  {
371  return Compose(std::dynamic_pointer_cast<QStabilizer>(toCopy), start);
372  }
373  bitLenInt Compose(QStabilizerPtr toCopy, bitLenInt start);
375  {
376  DecomposeDispose(start, dest->GetQubitCount(), std::dynamic_pointer_cast<QStabilizer>(dest));
377  }
379  void Dispose(bitLenInt start, bitLenInt length) { DecomposeDispose(start, length, (QStabilizerPtr)NULL); }
380  void Dispose(bitLenInt start, bitLenInt length, const bitCapInt& ignored)
381  {
382  DecomposeDispose(start, length, (QStabilizerPtr)NULL);
383  }
384  bool CanDecomposeDispose(const bitLenInt start, const bitLenInt length);
385  using QInterface::Allocate;
387  {
388  if (!length) {
389  return start;
390  }
391 
392  if (start > qubitCount) {
393  throw std::out_of_range("QStabilizer::Allocate() cannot start past end of register!");
394  }
395 
396  if (!qubitCount) {
397  SetQubitCount(length);
399  return 0U;
400  }
401 
402  QStabilizerPtr nQubits = std::make_shared<QStabilizer>(length, ZERO_BCI, rand_generator, CMPLX_DEFAULT_ARG,
403  false, randGlobalPhase, false, -1, hardware_rand_generator != NULL);
404  return Compose(nQubits, start);
405  }
406 
408  real1_f nrm = REAL1_DEFAULT_ARG, real1_f norm_thresh = REAL1_DEFAULT_ARG, real1_f phaseArg = ZERO_R1_F)
409  {
410  if (!randGlobalPhase) {
411  SetPhaseOffset(phaseOffset + (real1)phaseArg);
412  }
413  }
415  {
416  // Intentionally left blank
417  }
418 
420  {
421  return ApproxCompareHelper(std::dynamic_pointer_cast<QStabilizer>(toCompare));
422  }
424  {
425  return ApproxCompare(std::dynamic_pointer_cast<QStabilizer>(toCompare), error_tol);
426  }
428  {
429  return error_tol >= ApproxCompareHelper(toCompare, error_tol, true);
430  }
432  {
433  return GlobalPhaseCompare(std::dynamic_pointer_cast<QStabilizer>(toCompare), error_tol);
434  }
436  {
437  const AmplitudeEntry thisAmpEntry = GetAnyAmplitude();
438  real1 argDiff = (real1)abs(
439  (std::arg(thisAmpEntry.amplitude) - std::arg(toCompare->GetAmplitude(thisAmpEntry.permutation))) /
440  (2 * PI_R1));
441  argDiff -= (real1)(size_t)argDiff;
442  if (argDiff > HALF_R1) {
443  argDiff -= ONE_R1;
444  }
445  if (FP_NORM_EPSILON >= abs(argDiff)) {
446  return false;
447  }
448  return error_tol >= ApproxCompareHelper(toCompare, error_tol, true);
449  }
450 
451  real1_f Prob(bitLenInt qubit);
452 
453  void Mtrx(const complex* mtrx, bitLenInt target);
454  void Phase(const complex& topLeft, const complex& bottomRight, bitLenInt target);
455  void Invert(const complex& topRight, const complex& bottomLeft, bitLenInt target);
456  void MCPhase(
457  const std::vector<bitLenInt>& controls, const complex& topLeft, const complex& bottomRight, bitLenInt target);
458  void MACPhase(
459  const std::vector<bitLenInt>& controls, const complex& topLeft, const complex& bottomRight, bitLenInt target);
460  void MCInvert(
461  const std::vector<bitLenInt>& controls, const complex& topRight, const complex& bottomLeft, bitLenInt target);
462  void MACInvert(
463  const std::vector<bitLenInt>& controls, const complex& topRight, const complex& bottomLeft, bitLenInt target);
464  void MCMtrx(const std::vector<bitLenInt>& controls, const complex* mtrx, bitLenInt target)
465  {
466  if (IS_NORM_0(mtrx[1U]) && IS_NORM_0(mtrx[2U])) {
467  MCPhase(controls, mtrx[0U], mtrx[3U], target);
468  return;
469  }
470 
471  if (IS_NORM_0(mtrx[0U]) && IS_NORM_0(mtrx[3U])) {
472  MCInvert(controls, mtrx[1U], mtrx[2U], target);
473  return;
474  }
475 
476  throw std::domain_error("QStabilizer::MCMtrx() not implemented for non-Clifford/Pauli cases!");
477  }
478  void MACMtrx(const std::vector<bitLenInt>& controls, const complex* mtrx, bitLenInt target)
479  {
480  if (IS_NORM_0(mtrx[1U]) && IS_NORM_0(mtrx[2U])) {
481  MACPhase(controls, mtrx[0U], mtrx[3U], target);
482  return;
483  }
484 
485  if (IS_NORM_0(mtrx[0U]) && IS_NORM_0(mtrx[3U])) {
486  MACInvert(controls, mtrx[1U], mtrx[2U], target);
487  return;
488  }
489 
490  throw std::domain_error("QStabilizer::MACMtrx() not implemented for non-Clifford/Pauli cases!");
491  }
492  void FSim(real1_f theta, real1_f phi, bitLenInt qubit1, bitLenInt qubit2);
493 
494  bool TrySeparate(const std::vector<bitLenInt>& qubits, real1_f ignored);
495  bool TrySeparate(bitLenInt qubit) { return CanDecomposeDispose(qubit, 1U); }
496  bool TrySeparate(bitLenInt qubit1, bitLenInt qubit2)
497  {
498  if (qubit2 < qubit1) {
499  std::swap(qubit1, qubit2);
500  }
501 
502  Swap(qubit1, 0U);
503  Swap(qubit2, 1U);
504 
505  const bool toRet = CanDecomposeDispose(0U, 2U);
506 
507  Swap(qubit2, 1U);
508  Swap(qubit1, 0U);
509 
510  return toRet;
511  }
512 
513  friend std::ostream& operator<<(std::ostream& os, const QStabilizerPtr s);
514  friend std::istream& operator>>(std::istream& is, const QStabilizerPtr s);
515 };
516 } // namespace Qrack
A "Qrack::QInterface" is an abstract interface exposing qubit permutation state vector with methods t...
Definition: qinterface.hpp:141
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:470
std::shared_ptr< RdRandom > hardware_rand_generator
Definition: qinterface.hpp:152
virtual bitLenInt Compose(QInterfacePtr toCopy)
Combine another QInterface with this one, after the last bit index of this one.
Definition: qinterface.hpp:364
qrack_rand_gen_ptr rand_generator
Definition: qinterface.hpp:150
bool randGlobalPhase
Definition: qinterface.hpp:144
virtual void SetQubitCount(bitLenInt qb)
Definition: qinterface.hpp:268
std::uniform_real_distribution< real1_s > rand_distribution
Definition: qinterface.hpp:151
bitLenInt qubitCount
Definition: qinterface.hpp:146
Definition: qstabilizer.hpp:45
bitCapInt PermCount()
Definition: qstabilizer.hpp:259
void seed(const bitLenInt &g)
Finds a Pauli operator P such that the basis state P|0...0> occurs with nonzero amplitude in q,...
Definition: qstabilizer.cpp:239
real1_f getVariance(const real1_f &mean, const real1_f &nrm, const std::vector< bitCapInt > &bitPowers, const std::vector< bitCapInt > &perms, const bitCapInt &offset)
Returns the (partial) variance from a state vector amplitude.
Definition: qstabilizer.cpp:357
void S(bitLenInt qubitIndex)
Apply a phase gate (|0>->|0>, |1>->i|1>, or "S") to qubit b.
Definition: qstabilizer.cpp:1301
void MACInvert(const std::vector< bitLenInt > &controls, const complex &topRight, const complex &bottomLeft, bitLenInt target)
Apply a single bit transformation that reverses bit probability and might effect phase,...
Definition: qstabilizer.cpp:2328
bool TrySeparate(bitLenInt qubit1, bitLenInt qubit2)
Two-qubit TrySeparate()
Definition: qstabilizer.hpp:496
bool CanDecomposeDispose(const bitLenInt start, const bitLenInt length)
Definition: qstabilizer.cpp:1637
void Decompose(bitLenInt start, QInterfacePtr dest)
Minimally decompose a set of contiguous bits from the separably composed unit, into "destination".
Definition: qstabilizer.hpp:374
real1_f ExpectationFloatsFactorized(const std::vector< bitLenInt > &bits, const std::vector< real1_f > &weights)
Get expectation value of bits, given a (floating-point) array of qubit weights.
Definition: qstabilizer.cpp:665
bool GlobalPhaseCompare(QStabilizerPtr toCompare, real1_f error_tol=TRYDECOMPOSE_EPSILON)
Definition: qstabilizer.hpp:435
std::vector< uint8_t > r
Definition: qstabilizer.hpp:53
void CZ(bitLenInt control, bitLenInt target)
Apply a CZ gate with control and target.
Definition: qstabilizer.cpp:991
void ParFor(StabilizerParallelFunc fn, std::vector< bitLenInt > qubits)
Definition: qstabilizer.cpp:57
void setBasisState(const real1_f &nrm, complex *stateVec)
Returns the result of applying the Pauli operator in the "scratch space" of q to |0....
Definition: qstabilizer.cpp:307
friend std::ostream & operator<<(std::ostream &os, const QStabilizerPtr s)
Definition: qstabilizer.cpp:2427
void SetQuantumState(const complex *inputState)
Set an arbitrary pure quantum state representation.
Definition: qstabilizer.cpp:1878
bool TrySeparate(const std::vector< bitLenInt > &qubits, real1_f ignored)
Qrack::QUnit types maintain explicit separation of representations of qubits, which reduces memory us...
Definition: qstabilizer.cpp:2408
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: qstabilizer.hpp:407
QStabilizer(bitLenInt n, const bitCapInt &perm=ZERO_BCI, qrack_rand_gen_ptr rgp=nullptr, const complex &phaseFac=CMPLX_DEFAULT_ARG, bool doNorm=false, bool randomGlobalPhase=true, bool ignored2=false, int64_t ignored3=-1, bool useHardwareRNG=true, bool ignored4=false, real1_f ignored5=REAL1_EPSILON, std::vector< int64_t > ignored6={}, bitLenInt ignored7=0U, real1_f ignored8=_qrack_qunit_sep_thresh)
Definition: qstabilizer.cpp:39
void AntiCY(bitLenInt control, bitLenInt target)
Apply an (anti-)CY gate with control and target.
Definition: qstabilizer.cpp:955
void ISwap(bitLenInt qubitIndex1, bitLenInt qubitIndex2)
Swap values of two bits in register, and apply phase factor of i if bits are different.
Definition: qstabilizer.cpp:1087
bool Rand()
Definition: qstabilizer.hpp:129
friend std::istream & operator>>(std::istream &is, const QStabilizerPtr s)
Definition: qstabilizer.cpp:2450
void SetDevice(int64_t dID)
Set the device index, if more than one device is available.
Definition: qstabilizer.hpp:127
void SetRandGlobalPhase(bool isRand)
Definition: qstabilizer.hpp:267
void SetPhaseOffset(real1_f phaseArg)
Definition: qstabilizer.hpp:67
unsigned rawRandBoolsRemaining
Definition: qstabilizer.hpp:48
void rowmult(const bitLenInt &i, const bitLenInt &k)
Left-multiply row i by row k - does not change the logical state.
Definition: qstabilizer.hpp:194
unsigned rawRandBools
Definition: qstabilizer.hpp:47
real1_f ExpectationBitsFactorized(const std::vector< bitLenInt > &bits, const std::vector< bitCapInt > &perms, const bitCapInt &offset=ZERO_BCI)
Get expectation of qubits, interpreting each permutation as an unsigned integer.
Definition: qstabilizer.cpp:627
void AntiCNOT(bitLenInt control, bitLenInt target)
Apply an (anti-)CNOT gate with control and target.
Definition: qstabilizer.cpp:887
void SetPermutation(const bitCapInt &perm, const complex &phaseFac=CMPLX_DEFAULT_ARG)
Set to a specific permutation of all qubits.
Definition: qstabilizer.cpp:89
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: qstabilizer.hpp:414
virtual bitLenInt Compose(QInterfacePtr toCopy)
Combine another QInterface with this one, after the last bit index of this one.
Definition: qinterface.hpp:364
void IS(bitLenInt qubitIndex)
Apply an inverse phase gate (|0>->|0>, |1>->-i|1>, or "S adjoint") to qubit b.
Definition: qstabilizer.cpp:1334
void MCMtrx(const std::vector< bitLenInt > &controls, const complex *mtrx, bitLenInt target)
Apply an arbitrary single bit unitary transformation, with arbitrary control bits.
Definition: qstabilizer.hpp:464
void Y(bitLenInt qubitIndex)
Apply a Pauli Y gate to target.
Definition: qstabilizer.cpp:1254
void rowset(const bitLenInt &i, bitLenInt b)
Sets row i equal to the bth observable (X_1,...X_n,Z_1,...,Z_n)
Definition: qstabilizer.hpp:178
virtual void H(bitLenInt qubit)
Apply a Hadamard gate to target.
Definition: qinterface.hpp:913
bool ForceM(bitLenInt t, bool result, bool doForce=true, bool doApply=true)
Measure qubit t.
Definition: qstabilizer.cpp:1440
bitLenInt gaussian()
Do Gaussian elimination to put the stabilizer generators in the following form: At the top,...
Definition: qstabilizer.cpp:176
void Dispose(bitLenInt start, bitLenInt length, const bitCapInt &ignored)
Dispose a a contiguous set of qubits that are already in a permutation eigenstate.
Definition: qstabilizer.hpp:380
bool IsSeparableY(const bitLenInt &target)
Returns "true" if target qubit is a Y basis eigenstate.
Definition: qstabilizer.cpp:1406
AmplitudeEntry GetQubitAmplitude(bitLenInt t, bool m)
Get any single basis state amplitude where qubit "t" has value "m".
Definition: qstabilizer.cpp:592
real1_f Prob(bitLenInt qubit)
Direct measure of bit probability to be in |1> state.
Definition: qstabilizer.cpp:1895
real1_f SumSqrDiff(QInterfacePtr toCompare)
Calculates (1 - <\psi_e|\psi_c>) between states |\psi_c> and |\psi_e>.
Definition: qstabilizer.hpp:419
bitLenInt Compose(QStabilizerPtr toCopy)
Definition: qstabilizer.hpp:368
std::function< void(void)> DispatchFn
Definition: qstabilizer.hpp:62
void Z(bitLenInt qubitIndex)
Apply a phase gate (|0>->|0>, |1>->-|1>, or "Z") to qubit b.
Definition: qstabilizer.cpp:1275
real1_f ProbMask(const bitCapInt &mask, const bitCapInt &permutation)
Direct measure of masked permutation probability.
Definition: qstabilizer.cpp:824
bitLenInt maxStateMapCacheQubitCount
Definition: qstabilizer.hpp:50
AmplitudeEntry getBasisAmp(const real1_f &nrm)
Helper for setBasisState() and setBasisProb()
Definition: qstabilizer.cpp:273
real1_f ApproxCompareHelper(QStabilizerPtr toCompare, real1_f error_tol=TRYDECOMPOSE_EPSILON, bool isDiscrete=false)
Definition: qstabilizer.cpp:1776
void DecomposeDispose(const bitLenInt start, const bitLenInt length, QStabilizerPtr toCopy)
Definition: qstabilizer.cpp:1701
void SetRandomSeed(uint32_t seed)
Definition: qstabilizer.hpp:120
void Mtrx(const complex *mtrx, bitLenInt target)
Apply an arbitrary single bit unitary transformation.
Definition: qstabilizer.cpp:1905
real1 phaseOffset
Definition: qstabilizer.hpp:49
virtual void X(bitLenInt qubit)
Apply an X (or NOT) gate to target.
Definition: qinterface.hpp:1084
void Invert(const complex &topRight, const complex &bottomLeft, bitLenInt target)
Apply a single bit transformation that reverses bit probability and might effect phase.
Definition: qstabilizer.cpp:2092
QInterfacePtr Clone()
Clone this QInterface.
Definition: qstabilizer.cpp:73
real1_f getExpectation(const real1_f &nrm, const std::vector< bitCapInt > &bitPowers, const std::vector< bitCapInt > &perms, const bitCapInt &offset)
Returns the (partial) expectation value from a state vector amplitude.
Definition: qstabilizer.cpp:334
std::vector< bool > BoolVector
Definition: qstabilizer.hpp:55
std::function< void(const bitLenInt &)> StabilizerParallelFunc
Definition: qstabilizer.hpp:61
bool IsSeparableZ(const bitLenInt &target)
Returns "true" if target qubit is a Z basis eigenstate.
Definition: qstabilizer.cpp:1369
uint8_t IsSeparable(const bitLenInt &target)
Returns: 0 if target qubit is not separable 1 if target qubit is a Z basis eigenstate 2 if target qub...
Definition: qstabilizer.cpp:1422
bool IsSeparableX(const bitLenInt &target)
Returns "true" if target qubit is an X basis eigenstate.
Definition: qstabilizer.cpp:1394
void AntiCZ(bitLenInt control, bitLenInt target)
Apply an (anti-)CZ gate with control and target.
Definition: qstabilizer.cpp:1030
bitCapInt GetMaxQPower()
Get the maximum number of basis states, namely for qubits.
Definition: qstabilizer.hpp:113
void Phase(const complex &topLeft, const complex &bottomRight, bitLenInt target)
Apply a single bit transformation that only effects phase.
Definition: qstabilizer.cpp:2054
void CNOT(bitLenInt control, bitLenInt target)
Apply a CNOT gate with control and target.
Definition: qstabilizer.cpp:855
real1_f ProbPermRdm(const bitCapInt &perm, bitLenInt ancillaeStart)
Under assumption of a QStabilizerHybrid ancillary buffer, trace out the permutation probability of th...
Definition: qstabilizer.cpp:782
complex GetAmplitude(const bitCapInt &perm)
Get a single basis state amplitude.
Definition: qstabilizer.cpp:501
std::map< bitCapInt, complex > GetQuantumState()
Convert the state to sparse ket notation.
Definition: qstabilizer.cpp:444
std::vector< BoolVector > x
Definition: qstabilizer.hpp:57
void ResetPhaseOffset()
Definition: qstabilizer.hpp:115
complex GetPhaseOffset()
Definition: qstabilizer.hpp:116
std::vector< complex > GetAmplitudes(std::vector< bitCapInt > perms)
Get a single basis state amplitude.
Definition: qstabilizer.cpp:534
bitLenInt Compose(QInterfacePtr toCopy, bitLenInt start)
Compose() a QInterface peer, inserting its qubit into index order at start index.
Definition: qstabilizer.hpp:369
bool GlobalPhaseCompare(QInterfacePtr toCompare, real1_f error_tol=TRYDECOMPOSE_EPSILON)
Definition: qstabilizer.hpp:431
bool ApproxCompare(QInterfacePtr toCompare, real1_f error_tol=TRYDECOMPOSE_EPSILON)
Compare state vectors approximately, to determine whether this state vector is the same as the target...
Definition: qstabilizer.hpp:423
void rowswap(const bitLenInt &i, const bitLenInt &k)
Swaps row i and row k - does not change the logical state.
Definition: qstabilizer.hpp:167
void MCInvert(const std::vector< bitLenInt > &controls, const complex &topRight, const complex &bottomLeft, bitLenInt target)
Apply a single bit transformation that reverses bit probability and might effect phase,...
Definition: qstabilizer.cpp:2267
void GetProbs(real1 *outputProbs)
Get all probabilities corresponding to ket notation.
Definition: qstabilizer.cpp:473
bitLenInt Allocate(bitLenInt start, bitLenInt length)
Allocate new "length" count of |0> state qubits at specified qubit index start position.
Definition: qstabilizer.hpp:386
bool isClifford()
Returns "true" if current state is identifiably within the Clifford set, or "false" if it is not or c...
Definition: qstabilizer.hpp:108
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: qstabilizer.cpp:2389
real1_f VarianceBitsFactorized(const std::vector< bitLenInt > &bits, const std::vector< bitCapInt > &perms, const bitCapInt &offset=ZERO_BCI)
Get variance of qubits, interpreting each permutation as an unsigned integer.
Definition: qstabilizer.cpp:704
void CY(bitLenInt control, bitLenInt target)
Apply a CY gate with control and target.
Definition: qstabilizer.cpp:919
bitLenInt Compose(QInterfacePtr toCopy)
Combine another QInterface with this one, after the last bit index of this one.
Definition: qstabilizer.hpp:367
real1_f VarianceFloatsFactorized(const std::vector< bitLenInt > &bits, const std::vector< real1_f > &weights)
Direct measure of variance of listed bit string probability.
Definition: qstabilizer.cpp:743
bool ApproxCompare(QStabilizerPtr toCompare, real1_f error_tol=TRYDECOMPOSE_EPSILON)
Definition: qstabilizer.hpp:427
void IISwap(bitLenInt qubitIndex1, bitLenInt qubitIndex2)
Inverse ISwap - Swap values of two bits in register, and apply phase factor of -i if bits are differe...
Definition: qstabilizer.cpp:1131
std::vector< BoolVector > z
Definition: qstabilizer.hpp:59
void Copy(QStabilizerPtr orig)
Definition: qstabilizer.hpp:85
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: qstabilizer.hpp:478
uint8_t clifford(const bitLenInt &i, const bitLenInt &k)
Return the phase (0,1,2,3) when row i is LEFT-multiplied by row k.
Definition: qstabilizer.cpp:131
void SetAmplitude(const bitCapInt &perm, const complex &amp)
Sets the representational amplitude of a full permutation.
Definition: qstabilizer.hpp:262
void MACPhase(const std::vector< bitLenInt > &controls, const complex &topLeft, const complex &bottomRight, bitLenInt target)
Apply a single bit transformation that only effects phase, with arbitrary (anti-)control bits.
Definition: qstabilizer.cpp:2200
bitLenInt GetQubitCount()
Get the count of bits in this register.
Definition: qstabilizer.hpp:111
bool TrySeparate(bitLenInt qubit)
Single-qubit TrySeparate()
Definition: qstabilizer.hpp:495
AmplitudeEntry GetAnyAmplitude()
Get any single basis state amplitude.
Definition: qstabilizer.cpp:579
void Dispose(bitLenInt start, bitLenInt length)
Minimally decompose a set of contiguous bits from the separably composed unit, and discard the separa...
Definition: qstabilizer.hpp:379
void Swap(bitLenInt qubitIndex1, bitLenInt qubitIndex2)
Swap values of two bits in register.
Definition: qstabilizer.cpp:1067
void setBasisProb(const real1_f &nrm, real1 *outputProbs)
Returns the probability from applying the Pauli operator in the "scratch space" of q to |0....
Definition: qstabilizer.cpp:328
void rowcopy(const bitLenInt &i, const bitLenInt &k)
Sets row i equal to row k.
Definition: qstabilizer.hpp:156
~QStabilizer()
Definition: qstabilizer.hpp:104
void Copy(QInterfacePtr orig)
Definition: qstabilizer.hpp:84
void MCPhase(const std::vector< bitLenInt > &controls, const complex &topLeft, const complex &bottomRight, bitLenInt target)
Apply a single bit transformation that only effects phase, with arbitrary control bits.
Definition: qstabilizer.cpp:2133
bool isClifford(bitLenInt qubit)
Returns "true" if current qubit state is identifiably within the Clifford set, or "false" if it is no...
Definition: qstabilizer.hpp:109
void Dispatch(DispatchFn fn)
Definition: qstabilizer.hpp:63
void Clear()
Definition: qstabilizer.hpp:144
Half-precision floating-point type.
Definition: half.hpp:2222
virtual void H(bitLenInt qubit)
Hadamard gate.
Definition: qinterface.hpp:913
virtual void X(bitLenInt qubit)
X gate.
Definition: qinterface.hpp:1084
virtual void U(bitLenInt target, real1_f theta, real1_f phi, real1_f lambda)
General unitary gate.
Definition: rotational.cpp:18
virtual void Dump()
If asynchronous work is still running, let the simulator know that it can be aborted.
Definition: qinterface.hpp:2843
virtual QInterfacePtr Copy()
Copy this QInterface.
Definition: qinterface.hpp:2965
GLOSSARY: bitLenInt - "bit-length integer" - unsigned integer ID of qubit position in register bitCap...
Definition: complex16x2simd.hpp:25
std::shared_ptr< QInterface > QInterfacePtr
Definition: qinterface.hpp:29
const real1_f _qrack_qunit_sep_thresh
Definition: qrack_functions.hpp:235
QRACK_CONST real1_f TRYDECOMPOSE_EPSILON
Definition: qrack_types.hpp:260
QRACK_CONST real1 HALF_R1
Definition: qrack_types.hpp:184
half_float::half real1
Definition: qrack_types.hpp:94
std::complex< real1 > complex
Definition: qrack_types.hpp:128
QRACK_CONST real1 FP_NORM_EPSILON
Definition: qrack_types.hpp:258
bitCapInt pow2(const bitLenInt &p)
Definition: qrack_functions.hpp:136
QRACK_CONST real1 REAL1_EPSILON
Definition: qrack_types.hpp:200
QRACK_CONST real1 ONE_R1
Definition: qrack_types.hpp:185
QRACK_CONST real1 ZERO_R1
Definition: qrack_types.hpp:183
float real1_f
Definition: qrack_types.hpp:95
QRACK_CONST complex CMPLX_DEFAULT_ARG
Definition: qrack_types.hpp:257
std::shared_ptr< QStabilizer > QStabilizerPtr
Definition: qstabilizer.hpp:42
QRACK_CONST real1 PI_R1
Definition: qrack_types.hpp:178
const bitCapInt ONE_BCI
Definition: qrack_types.hpp:129
const bitCapInt ZERO_BCI
Definition: qrack_types.hpp:130
HALF_CONSTEXPR half abs(half arg)
Absolute value.
Definition: half.hpp:2975
#define HALF_R1_F
Definition: qrack_types.hpp:162
#define bitsInByte
Definition: qrack_types.hpp:154
#define REAL1_DEFAULT_ARG
Definition: qrack_types.hpp:177
#define bitLenInt
Definition: qrack_types.hpp:38
#define ZERO_R1_F
Definition: qrack_types.hpp:160
#define qrack_rand_gen_ptr
Definition: qrack_types.hpp:156
#define bitCapInt
Definition: qrack_types.hpp:62
#define IS_NORM_0(c)
Definition: qrack_types.hpp:25
Definition: qstabilizer.hpp:31
bitCapInt permutation
Definition: qstabilizer.hpp:32
complex amplitude
Definition: qstabilizer.hpp:33
AmplitudeEntry(const bitCapInt &p, const complex &a)
Definition: qstabilizer.hpp:35