Qrack  10.0
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 #if BOOST_AVAILABLE
30 #include <boost/dynamic_bitset.hpp>
31 #endif
32 
33 namespace Qrack {
34 
38 
39  AmplitudeEntry(const bitCapInt& p, const complex& a)
40  : permutation(p)
41  , amplitude(a)
42  {
43  }
44 };
45 
46 class QStabilizer;
47 typedef std::shared_ptr<QStabilizer> QStabilizerPtr;
48 
49 class QStabilizer : public QInterface {
50 protected:
51  unsigned rawRandBools;
57 #if BOOST_AVAILABLE
58  bool isTransposed;
59 #endif
60 
61  // Typedef for special type std::vector<bool> compatibility
62 #if BOOST_AVAILABLE
63  typedef boost::dynamic_bitset<> BoolVector;
64 #else
65  typedef std::vector<bool> BoolVector;
66 #endif
67  // Phase bits: 0 for +1, 1 for i, 2 for -1, 3 for -i. Normally either 0 or 2.
68  std::vector<BoolVector> r;
69  // (2n+1)*n matrix for stabilizer/destabilizer x bits (there's one "scratch row" at the bottom)
70  std::vector<BoolVector> x;
71  // (2n+1)*n matrix for z bits
72  std::vector<BoolVector> z;
73 
74  typedef std::function<void(const bitLenInt&)> StabilizerParallelFunc;
75  typedef std::function<void(void)> DispatchFn;
76  void Dispatch(DispatchFn fn) { fn(); }
77 
78  void ParFor(StabilizerParallelFunc fn, std::vector<bitLenInt> qubits);
79 
80  void SetPhaseOffset(real1_f phaseArg)
81  {
82  phaseOffset = (real1)phaseArg;
83  const bool isNeg = phaseOffset < 0;
84  if (isNeg) {
86  }
87  phaseOffset -= (real1)(((size_t)(phaseOffset / (2 * PI_R1))) * (2 * PI_R1));
88  if (phaseOffset > PI_R1) {
89  phaseOffset -= 2 * PI_R1;
90  }
91  if (isNeg) {
93  }
94  }
95 
96  uint8_t GetR(size_t i) { return (r[0U][i] ? 1U : 0U) | (r[1U][i] ? 2U : 0U); }
97  void ResetR(size_t i)
98  {
99 #if BOOST_AVAILABLE
100  r[0U].reset(i);
101  r[1U].reset(i);
102 #else
103  r[0U][i] = false;
104  r[1U][i] = false;
105 #endif
106  }
107 
108  using QInterface::Copy;
109  void Copy(QInterfacePtr orig) { Copy(std::dynamic_pointer_cast<QStabilizer>(orig)); }
110  void Copy(QStabilizerPtr orig)
111  {
112  QInterface::Copy(std::dynamic_pointer_cast<QInterface>(orig));
113  rawRandBools = orig->rawRandBools;
114  rawRandBoolsRemaining = orig->rawRandBoolsRemaining;
115  phaseOffset = orig->phaseOffset;
116  isGaussianCached = orig->isGaussianCached;
117  gaussianCached = orig->gaussianCached;
118 #if BOOST_AVAILABLE
119  isTransposed = orig->isTransposed;
120 #endif
121  maxStateMapCacheQubitCount = orig->maxStateMapCacheQubitCount;
122  r = orig->r;
123  x = orig->x;
124  z = orig->z;
125  }
126 
127 #if BOOST_AVAILABLE
128  // By Elara (OpenAI custom GPT)
129  std::vector<boost::dynamic_bitset<>> FastTranspose(const std::vector<boost::dynamic_bitset<>>& matrix)
130  {
131  const size_t num_rows = matrix.size();
132  const size_t num_cols = matrix[0].size();
133 
134  std::vector<BoolVector> transposed(num_cols, BoolVector(num_rows));
135 
136  for (size_t row = 0; row < num_rows; ++row) {
137  const BoolVector& bit_row = matrix[row];
138  for (size_t col = bit_row.find_first(); col != BoolVector::npos; col = bit_row.find_next(col)) {
139  transposed[col].set(row);
140  }
141  }
142 
143  return transposed;
144  }
145 
146  void SetTransposeState(bool isTrans)
147  {
148  if (isTrans == isTransposed) {
149  return;
150  }
151 
152  if (!isTransposed) {
153  x.pop_back();
154  z.pop_back();
155  r[0U].pop_back();
156  r[1U].pop_back();
157  }
158 
159  x = FastTranspose(x);
160  z = FastTranspose(z);
161  isTransposed = isTrans;
162 
163  if (!isTransposed) {
164  x.emplace_back(qubitCount);
165  z.emplace_back(qubitCount);
166  r[0U].push_back(0U);
167  r[1U].push_back(0U);
168  }
169  }
170 #endif
171 
173  {
174  if (qubit >= qubitCount) {
175  throw std::domain_error("QStabilizer gate qubit indices are out-of-bounds!");
176  }
177  }
178 
179 public:
180  QStabilizer(bitLenInt n, const bitCapInt& perm = ZERO_BCI, qrack_rand_gen_ptr rgp = nullptr,
181  const complex& phaseFac = CMPLX_DEFAULT_ARG, bool doNorm = false, bool randomGlobalPhase = true,
182  bool ignored2 = false, int64_t ignored3 = -1, bool useHardwareRNG = true, bool ignored4 = false,
183  real1_f ignored5 = REAL1_EPSILON, std::vector<int64_t> ignored6 = {}, bitLenInt ignored7 = 0U,
184  real1_f ignored8 = _qrack_qunit_sep_thresh);
185 
187 
189 
190  bool isClifford() { return true; };
191  bool isClifford(bitLenInt qubit) { return true; };
192 
194 
196 
198  complex GetPhaseOffset() { return std::polar(ONE_R1, phaseOffset); }
199 
200  void SetPermutation(const bitCapInt& perm, const complex& phaseFac = CMPLX_DEFAULT_ARG);
201 
202  void SetRandomSeed(uint32_t seed)
203  {
204  if (!!rand_generator) {
205  rand_generator->seed(seed);
206  }
207  }
208 
209  bool Rand()
210  {
211  if (!!hardware_rand_generator) {
212  if (!rawRandBoolsRemaining) {
214  rawRandBoolsRemaining = sizeof(unsigned) * bitsInByte;
215  }
217 
218  return (bool)((rawRandBools >> rawRandBoolsRemaining) & 1U);
219  } else {
221  }
222  }
223 
224  void Clear()
225  {
226  x.clear();
227  z.clear();
228 #if BOOST_AVAILABLE
229  r[0U].resize(0U);
230  r[1U].resize(0U);
231  isTransposed = false;
232 #else
233  r[0U].clear();
234  r[1U].clear();
235 #endif
237  qubitCount = 0U;
238  maxQPower = ONE_BCI;
239  }
240 
241 protected:
243  void rowcopy(const bitLenInt& i, const bitLenInt& k)
244  {
245  if (i == k) {
246  return;
247  }
248 
249  x[i] = x[k];
250  z[i] = z[k];
251  r[0U][i] = r[0U][k];
252  r[1U][i] = r[1U][k];
253  }
255  void rowswap(const bitLenInt& i, const bitLenInt& k)
256  {
257  if (i == k) {
258  return;
259  }
260 
261  std::swap(x[k], x[i]);
262  std::swap(z[k], z[i]);
263 #if BOOST_AVAILABLE
264  bool _r = r[0U][k];
265  r[0U][k] = r[0U][i];
266  r[0U][i] = _r;
267  _r = r[1U][k];
268  r[1U][k] = r[1U][i];
269  r[1U][i] = _r;
270 #else
271  BoolVector::swap(r[0U][k], r[0U][i]);
272  BoolVector::swap(r[1U][k], r[1U][i]);
273 #endif
274  }
276  void rowset(const bitLenInt& i, bitLenInt b)
277  {
278  ResetR(i);
279 
280  BoolVector& xi = x[i];
281  BoolVector& zi = z[i];
282 #if BOOST_AVAILABLE
283  xi.reset();
284  zi.reset();
285 
286  if (b < qubitCount) {
287  xi.set(b);
288  } else {
289  b -= qubitCount;
290  zi.set(b);
291  }
292 #else
293  std::fill(xi.begin(), xi.end(), false);
294  std::fill(zi.begin(), zi.end(), false);
295 
296  if (b < qubitCount) {
297  xi[b] = true;
298  } else {
299  b -= qubitCount;
300  zi[b] = true;
301  }
302 #endif
303  }
305  void rowmult(const bitLenInt& i, const bitLenInt& k)
306  {
307  uint8_t phase = clifford(i, k);
308  r[0U][i] = (bool)(phase & 1U);
309  r[1U][i] = (bool)(phase & 2U);
310  BoolVector& xi = x[i];
311  BoolVector& zi = z[i];
312 
313 #if BOOST_AVAILABLE
314  xi ^= x[k];
315  zi ^= z[k];
316 #else
317  for (bitLenInt j = 0U; j < qubitCount; ++j) {
318  xi[j] = xi[j] ^ x[k][j];
319  zi[j] = zi[j] ^ z[k][j];
320  }
321 #endif
322  }
324  uint8_t clifford(const bitLenInt& i, const bitLenInt& k);
325 
331  void seed(const bitLenInt& g);
332 
334  AmplitudeEntry getBasisAmp(const real1_f& nrm);
335 
337  void setBasisState(const real1_f& nrm, complex* stateVec);
338 
340  void setBasisState(const real1_f& nrm, QInterfacePtr eng);
341 
343  void setBasisState(const real1_f& nrm, std::map<bitCapInt, complex>& stateMap);
344 
346  void setBasisProb(const real1_f& nrm, real1* outputProbs);
347 
349  real1_f getExpectation(const real1_f& nrm, const std::vector<bitCapInt>& bitPowers,
350  const std::vector<bitCapInt>& perms, const bitCapInt& offset);
351 
354  const real1_f& nrm, const std::vector<bitCapInt>& bitPowers, const std::vector<real1_f>& weights);
355 
357  real1_f getVariance(const real1_f& mean, const real1_f& nrm, const std::vector<bitCapInt>& bitPowers,
358  const std::vector<bitCapInt>& perms, const bitCapInt& offset);
359 
361  real1_f getVariance(const real1_f& mean, const real1_f& nrm, const std::vector<bitCapInt>& bitPowers,
362  const std::vector<real1_f>& weights);
363 
364  void DecomposeDispose(const bitLenInt start, const bitLenInt length, QStabilizerPtr toCopy);
365 
367  QStabilizerPtr toCompare, real1_f error_tol = TRYDECOMPOSE_EPSILON, bool isDiscrete = false);
368 
369 public:
376  bitLenInt gaussian(bool s = true);
377 
378  bitCapInt PermCount() { return pow2(gaussian(false)); }
379 
380  void SetQuantumState(const complex* inputState);
381  void SetAmplitude(const bitCapInt& perm, const complex& amp)
382  {
383  throw std::domain_error("QStabilizer::SetAmplitude() not implemented!");
384  }
385 
386  void SetRandGlobalPhase(bool isRand) { randGlobalPhase = isRand; }
387 
389  void CNOT(bitLenInt control, bitLenInt target);
391  void CY(bitLenInt control, bitLenInt target);
393  void CZ(bitLenInt control, bitLenInt target);
395  void AntiCNOT(bitLenInt control, bitLenInt target);
397  void AntiCY(bitLenInt control, bitLenInt target);
399  void AntiCZ(bitLenInt control, bitLenInt target);
401  using QInterface::H;
402  void H(bitLenInt qubitIndex);
404  using QInterface::X;
405  void X(bitLenInt qubitIndex);
407  void Y(bitLenInt qubitIndex);
409  void Z(bitLenInt qubitIndex);
411  void S(bitLenInt qubitIndex);
413  void IS(bitLenInt qubitIndex);
414  // Swap two bits
415  void Swap(bitLenInt qubitIndex1, bitLenInt qubitIndex2);
416  // Swap two bits and apply a phase factor of i if they are different
417  void ISwap(bitLenInt qubitIndex1, bitLenInt qubitIndex2);
418  // Swap two bits and apply a phase factor of -i if they are different
419  void IISwap(bitLenInt qubitIndex1, bitLenInt qubitIndex2);
420 
422  bool ForceM(bitLenInt t, bool result, bool doForce = true, bool doApply = true);
423 
426 
428  void GetQuantumState(complex* stateVec);
429 
431  void GetQuantumState(QInterfacePtr eng);
432 
434  std::map<bitCapInt, complex> GetQuantumState();
435 
437  void GetProbs(real1* outputProbs);
438 
440  complex GetAmplitude(const bitCapInt& perm);
441 
443  std::vector<complex> GetAmplitudes(std::vector<bitCapInt> perms);
444 
447 
450 
453  const std::vector<bitLenInt>& bits, const std::vector<bitCapInt>& perms, const bitCapInt& offset = ZERO_BCI);
454  real1_f ExpectationFloatsFactorized(const std::vector<bitLenInt>& bits, const std::vector<real1_f>& weights);
457  const std::vector<bitLenInt>& bits, const std::vector<bitCapInt>& perms, const bitCapInt& offset = ZERO_BCI);
458  real1_f VarianceFloatsFactorized(const std::vector<bitLenInt>& bits, const std::vector<real1_f>& weights);
459 
462  real1_f ProbPermRdm(const bitCapInt& perm, bitLenInt ancillaeStart);
463 
465  real1_f ProbMask(const bitCapInt& mask, const bitCapInt& permutation);
466 
468  std::vector<bitLenInt> EntangledQubits(const bitLenInt& target, const bool& g = true);
470  bool IsSeparableZ(const bitLenInt& target);
472  bool IsSeparableX(const bitLenInt& target);
474  bool IsSeparableY(const bitLenInt& target);
482  uint8_t IsSeparable(const bitLenInt& target);
483 
484  using QInterface::Compose;
485  bitLenInt Compose(QInterfacePtr toCopy) { return Compose(std::dynamic_pointer_cast<QStabilizer>(toCopy)); }
486  bitLenInt Compose(QStabilizerPtr toCopy) { return Compose(toCopy, qubitCount); }
488  {
489  return Compose(std::dynamic_pointer_cast<QStabilizer>(toCopy), start);
490  }
491  bitLenInt Compose(QStabilizerPtr toCopy, bitLenInt start);
493  {
494  DecomposeDispose(start, dest->GetQubitCount(), std::dynamic_pointer_cast<QStabilizer>(dest));
495  }
497  void Dispose(bitLenInt start, bitLenInt length) { DecomposeDispose(start, length, (QStabilizerPtr) nullptr); }
498  void Dispose(bitLenInt start, bitLenInt length, const bitCapInt& ignored)
499  {
500  DecomposeDispose(start, length, (QStabilizerPtr) nullptr);
501  }
502  bool CanDecomposeDispose(const bitLenInt start, const bitLenInt length);
503  using QInterface::Allocate;
505  {
506  if (!length) {
507  return start;
508  }
509 
510  if (start > qubitCount) {
511  throw std::out_of_range("QStabilizer::Allocate() cannot start past end of register!");
512  }
513 
514  if (!qubitCount) {
515  SetQubitCount(length);
517  return 0U;
518  }
519 
520  QStabilizerPtr nQubits = std::make_shared<QStabilizer>(length, ZERO_BCI, rand_generator, CMPLX_DEFAULT_ARG,
521  false, randGlobalPhase, false, -1, !!hardware_rand_generator);
522  return Compose(nQubits, start);
523  }
524 
526  real1_f nrm = REAL1_DEFAULT_ARG, real1_f norm_thresh = REAL1_DEFAULT_ARG, real1_f phaseArg = ZERO_R1_F)
527  {
528  if (!randGlobalPhase) {
529  SetPhaseOffset(phaseOffset + (real1)phaseArg);
530  }
531  }
533  {
534  // Intentionally left blank
535  }
536 
538  {
539  return ApproxCompareHelper(std::dynamic_pointer_cast<QStabilizer>(toCompare));
540  }
542  {
543  return ApproxCompare(std::dynamic_pointer_cast<QStabilizer>(toCompare), error_tol);
544  }
546  {
547  return error_tol >= ApproxCompareHelper(toCompare, error_tol, true);
548  }
550  {
551  return GlobalPhaseCompare(std::dynamic_pointer_cast<QStabilizer>(toCompare), error_tol);
552  }
554  {
555  const AmplitudeEntry thisAmpEntry = GetAnyAmplitude();
556  real1 argDiff = (real1)abs(
557  (std::arg(thisAmpEntry.amplitude) - std::arg(toCompare->GetAmplitude(thisAmpEntry.permutation))) /
558  (2 * PI_R1));
559  argDiff -= (real1)(size_t)argDiff;
560  if (argDiff > HALF_R1) {
561  argDiff -= ONE_R1;
562  }
563  if (FP_NORM_EPSILON >= abs(argDiff)) {
564  return false;
565  }
566  return error_tol >= ApproxCompareHelper(toCompare, error_tol, true);
567  }
568 
569  real1_f Prob(bitLenInt qubit);
570 
571  void Mtrx(const complex* mtrx, bitLenInt target);
572  void Phase(const complex& topLeft, const complex& bottomRight, bitLenInt target);
573  void Invert(const complex& topRight, const complex& bottomLeft, bitLenInt target);
574  void MCPhase(
575  const std::vector<bitLenInt>& controls, const complex& topLeft, const complex& bottomRight, bitLenInt target);
576  void MACPhase(
577  const std::vector<bitLenInt>& controls, const complex& topLeft, const complex& bottomRight, bitLenInt target);
578  void MCInvert(
579  const std::vector<bitLenInt>& controls, const complex& topRight, const complex& bottomLeft, bitLenInt target);
580  void MACInvert(
581  const std::vector<bitLenInt>& controls, const complex& topRight, const complex& bottomLeft, bitLenInt target);
582  void MCMtrx(const std::vector<bitLenInt>& controls, const complex* mtrx, bitLenInt target)
583  {
584  if (IS_NORM_0(mtrx[1U]) && IS_NORM_0(mtrx[2U])) {
585  return MCPhase(controls, mtrx[0U], mtrx[3U], target);
586  }
587 
588  if (IS_NORM_0(mtrx[0U]) && IS_NORM_0(mtrx[3U])) {
589  return MCInvert(controls, mtrx[1U], mtrx[2U], target);
590  }
591 
592  throw std::domain_error("QStabilizer::MCMtrx() not implemented for non-Clifford/Pauli cases!");
593  }
594  void MACMtrx(const std::vector<bitLenInt>& controls, const complex* mtrx, bitLenInt target)
595  {
596  if (IS_NORM_0(mtrx[1U]) && IS_NORM_0(mtrx[2U])) {
597  return MACPhase(controls, mtrx[0U], mtrx[3U], target);
598  }
599 
600  if (IS_NORM_0(mtrx[0U]) && IS_NORM_0(mtrx[3U])) {
601  return MACInvert(controls, mtrx[1U], mtrx[2U], target);
602  }
603 
604  throw std::domain_error("QStabilizer::MACMtrx() not implemented for non-Clifford/Pauli cases!");
605  }
606  void FSim(real1_f theta, real1_f phi, bitLenInt qubit1, bitLenInt qubit2);
607 
608  bool TrySeparate(const std::vector<bitLenInt>& qubits, real1_f ignored);
609  bool TrySeparate(bitLenInt qubit) { return CanDecomposeDispose(qubit, 1U); }
610  bool TrySeparate(bitLenInt qubit1, bitLenInt qubit2)
611  {
612  if (qubit2 < qubit1) {
613  std::swap(qubit1, qubit2);
614  }
615 
616  Swap(qubit1, 0U);
617  Swap(qubit2, 1U);
618 
619  const bool toRet = CanDecomposeDispose(0U, 2U);
620 
621  Swap(qubit2, 1U);
622  Swap(qubit1, 0U);
623 
624  return toRet;
625  }
626 
627  friend std::ostream& operator<<(std::ostream& os, const QStabilizerPtr s);
628  friend std::istream& operator>>(std::istream& is, const QStabilizerPtr s);
629 };
630 } // 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:477
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:371
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:49
bitCapInt PermCount()
Definition: qstabilizer.hpp:378
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:330
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:453
void S(bitLenInt qubitIndex)
Apply a phase gate (|0>->|0>, |1>->i|1>, or "S") to qubit b.
Definition: qstabilizer.cpp:1542
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:2896
bool TrySeparate(bitLenInt qubit1, bitLenInt qubit2)
Two-qubit TrySeparate()
Definition: qstabilizer.hpp:610
bool CanDecomposeDispose(const bitLenInt start, const bitLenInt length)
Definition: qstabilizer.cpp:2118
void Decompose(bitLenInt start, QInterfacePtr dest)
Minimally decompose a set of contiguous bits from the separably composed unit, into "destination".
Definition: qstabilizer.hpp:492
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:753
bool GlobalPhaseCompare(QStabilizerPtr toCompare, real1_f error_tol=TRYDECOMPOSE_EPSILON)
Definition: qstabilizer.hpp:553
void CZ(bitLenInt control, bitLenInt target)
Apply a CZ gate with control and target.
Definition: qstabilizer.cpp:1120
void ParFor(StabilizerParallelFunc fn, std::vector< bitLenInt > qubits)
Definition: qstabilizer.cpp:62
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:403
friend std::ostream & operator<<(std::ostream &os, const QStabilizerPtr s)
Definition: qstabilizer.cpp:2998
void SetQuantumState(const complex *inputState)
Set an arbitrary pure quantum state representation.
Definition: qstabilizer.cpp:2471
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:2979
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:525
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:1070
void ResetR(size_t i)
Definition: qstabilizer.hpp:97
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:1257
bool Rand()
Definition: qstabilizer.hpp:209
friend std::istream & operator>>(std::istream &is, const QStabilizerPtr s)
Definition: qstabilizer.cpp:3029
bool isGaussianCached
Definition: qstabilizer.hpp:55
uint8_t GetR(size_t i)
Definition: qstabilizer.hpp:96
bitCapInt HighestProbAll()
Get the highest-probability basis dimension in the Hilbert space.
Definition: qstabilizer.cpp:479
void SetRandGlobalPhase(bool isRand)
Definition: qstabilizer.hpp:386
void SetPhaseOffset(real1_f phaseArg)
Definition: qstabilizer.hpp:80
unsigned rawRandBoolsRemaining
Definition: qstabilizer.hpp:52
void rowmult(const bitLenInt &i, const bitLenInt &k)
Left-multiply row i by row k - does not change the logical state.
Definition: qstabilizer.hpp:305
unsigned rawRandBools
Definition: qstabilizer.hpp:51
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:717
void AntiCNOT(bitLenInt control, bitLenInt target)
Apply an (anti-)CNOT gate with control and target.
Definition: qstabilizer.cpp:978
void ValidateQubitIndex(bitLenInt qubit)
Definition: qstabilizer.hpp:172
void SetPermutation(const bitCapInt &perm, const complex &phaseFac=CMPLX_DEFAULT_ARG)
Set to a specific permutation of all qubits.
Definition: qstabilizer.cpp:97
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:532
virtual bitLenInt Compose(QInterfacePtr toCopy)
Combine another QInterface with this one, after the last bit index of this one.
Definition: qinterface.hpp:371
void IS(bitLenInt qubitIndex)
Apply an inverse phase gate (|0>->|0>, |1>->-i|1>, or "S adjoint") to qubit b.
Definition: qstabilizer.cpp:1587
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:582
void Y(bitLenInt qubitIndex)
Apply a Pauli Y gate to target.
Definition: qstabilizer.cpp:1479
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:276
virtual void H(bitLenInt qubit)
Apply a Hadamard gate to target.
Definition: qinterface.hpp:920
bool ForceM(bitLenInt t, bool result, bool doForce=true, bool doApply=true)
Measure qubit t.
Definition: qstabilizer.cpp:1785
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:498
bool IsSeparableY(const bitLenInt &target)
Returns "true" if target qubit is a Y basis eigenstate.
Definition: qstabilizer.cpp:1751
AmplitudeEntry GetQubitAmplitude(bitLenInt t, bool m)
Get any single basis state amplitude where qubit "t" has value "m".
Definition: qstabilizer.cpp:684
real1_f Prob(bitLenInt qubit)
Direct measure of bit probability to be in |1> state.
Definition: qstabilizer.cpp:2488
real1_f SumSqrDiff(QInterfacePtr toCompare)
Calculates (1 - <\psi_e|\psi_c>) between states |\psi_c> and |\psi_e>.
Definition: qstabilizer.hpp:537
bitLenInt Compose(QStabilizerPtr toCopy)
Definition: qstabilizer.hpp:486
std::function< void(void)> DispatchFn
Definition: qstabilizer.hpp:75
void Z(bitLenInt qubitIndex)
Apply a phase gate (|0>->|0>, |1>->-|1>, or "Z") to qubit b.
Definition: qstabilizer.cpp:1506
real1_f ProbMask(const bitCapInt &mask, const bitCapInt &permutation)
Direct measure of masked permutation probability.
Definition: qstabilizer.cpp:904
bitLenInt maxStateMapCacheQubitCount
Definition: qstabilizer.hpp:54
AmplitudeEntry getBasisAmp(const real1_f &nrm)
Helper for setBasisState() and setBasisProb()
Definition: qstabilizer.cpp:369
real1_f ApproxCompareHelper(QStabilizerPtr toCompare, real1_f error_tol=TRYDECOMPOSE_EPSILON, bool isDiscrete=false)
Definition: qstabilizer.cpp:2371
void DecomposeDispose(const bitLenInt start, const bitLenInt length, QStabilizerPtr toCopy)
Definition: qstabilizer.cpp:2182
void SetRandomSeed(uint32_t seed)
Definition: qstabilizer.hpp:202
void Mtrx(const complex *mtrx, bitLenInt target)
Apply an arbitrary single bit unitary transformation.
Definition: qstabilizer.cpp:2498
real1 phaseOffset
Definition: qstabilizer.hpp:53
virtual void X(bitLenInt qubit)
Apply an X (or NOT) gate to target.
Definition: qinterface.hpp:1091
bitLenInt gaussian(bool s=true)
Do Gaussian elimination to put the stabilizer generators in the following form: At the top,...
Definition: qstabilizer.cpp:248
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:2657
QInterfacePtr Clone()
Clone this QInterface.
Definition: qstabilizer.cpp:76
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:430
std::vector< bool > BoolVector
Definition: qstabilizer.hpp:65
std::function< void(const bitLenInt &)> StabilizerParallelFunc
Definition: qstabilizer.hpp:74
bool IsSeparableZ(const bitLenInt &target)
Returns "true" if target qubit is a Z basis eigenstate.
Definition: qstabilizer.cpp:1709
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:1767
bool IsSeparableX(const bitLenInt &target)
Returns "true" if target qubit is an X basis eigenstate.
Definition: qstabilizer.cpp:1739
void AntiCZ(bitLenInt control, bitLenInt target)
Apply an (anti-)CZ gate with control and target.
Definition: qstabilizer.cpp:1174
bitCapInt GetMaxQPower()
Get the maximum number of basis states, namely for qubits.
Definition: qstabilizer.hpp:195
void Phase(const complex &topLeft, const complex &bottomRight, bitLenInt target)
Apply a single bit transformation that only effects phase.
Definition: qstabilizer.cpp:2623
void CNOT(bitLenInt control, bitLenInt target)
Apply a CNOT gate with control and target.
Definition: qstabilizer.cpp:933
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:864
complex GetAmplitude(const bitCapInt &perm)
Get a single basis state amplitude.
Definition: qstabilizer.cpp:599
std::map< bitCapInt, complex > GetQuantumState()
Convert the state to sparse ket notation.
Definition: qstabilizer.cpp:546
std::vector< BoolVector > x
Definition: qstabilizer.hpp:70
void ResetPhaseOffset()
Definition: qstabilizer.hpp:197
complex GetPhaseOffset()
Definition: qstabilizer.hpp:198
std::vector< complex > GetAmplitudes(std::vector< bitCapInt > perms)
Get a single basis state amplitude.
Definition: qstabilizer.cpp:630
bitLenInt Compose(QInterfacePtr toCopy, bitLenInt start)
Compose() a QInterface peer, inserting its qubit into index order at start index.
Definition: qstabilizer.hpp:487
bool GlobalPhaseCompare(QInterfacePtr toCompare, real1_f error_tol=TRYDECOMPOSE_EPSILON)
Definition: qstabilizer.hpp:549
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:541
void rowswap(const bitLenInt &i, const bitLenInt &k)
Swaps row i and row k - does not change the logical state.
Definition: qstabilizer.hpp:255
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:2830
void GetProbs(real1 *outputProbs)
Get all probabilities corresponding to ket notation.
Definition: qstabilizer.cpp:573
bitLenInt Allocate(bitLenInt start, bitLenInt length)
Allocate new "length" count of |0> state qubits at specified qubit index start position.
Definition: qstabilizer.hpp:504
bool isClifford()
Returns "true" if current state is identifiably within the Clifford set, or "false" if it is not or c...
Definition: qstabilizer.hpp:190
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:2962
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:790
void CY(bitLenInt control, bitLenInt target)
Apply a CY gate with control and target.
Definition: qstabilizer.cpp:1020
bitLenInt Compose(QInterfacePtr toCopy)
Combine another QInterface with this one, after the last bit index of this one.
Definition: qstabilizer.hpp:485
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:827
bool ApproxCompare(QStabilizerPtr toCompare, real1_f error_tol=TRYDECOMPOSE_EPSILON)
Definition: qstabilizer.hpp:545
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:1321
std::vector< BoolVector > z
Definition: qstabilizer.hpp:72
void Copy(QStabilizerPtr orig)
Definition: qstabilizer.hpp:110
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:594
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:161
void SetAmplitude(const bitCapInt &perm, const complex &amp)
Sets the representational amplitude of a full permutation.
Definition: qstabilizer.hpp:381
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:2757
bitLenInt GetQubitCount()
Get the count of bits in this register.
Definition: qstabilizer.hpp:193
bool TrySeparate(bitLenInt qubit)
Single-qubit TrySeparate()
Definition: qstabilizer.hpp:609
std::vector< BoolVector > r
Definition: qstabilizer.hpp:68
AmplitudeEntry GetAnyAmplitude()
Get any single basis state amplitude.
Definition: qstabilizer.cpp:673
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:497
void Swap(bitLenInt qubitIndex1, bitLenInt qubitIndex2)
Swap values of two bits in register.
Definition: qstabilizer.cpp:1226
std::vector< bitLenInt > EntangledQubits(const bitLenInt &target, const bool &g=true)
Returns all qubits entangled with "target" (including itself)
Definition: qstabilizer.cpp:1634
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:424
void rowcopy(const bitLenInt &i, const bitLenInt &k)
Sets row i equal to row k.
Definition: qstabilizer.hpp:243
bitLenInt gaussianCached
Definition: qstabilizer.hpp:56
~QStabilizer()
Definition: qstabilizer.hpp:186
void Copy(QInterfacePtr orig)
Definition: qstabilizer.hpp:109
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:2694
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:191
void Dispatch(DispatchFn fn)
Definition: qstabilizer.hpp:76
void Clear()
Definition: qstabilizer.hpp:224
Half-precision floating-point type.
Definition: half.hpp:2222
virtual void H(bitLenInt qubit)
Hadamard gate.
Definition: qinterface.hpp:920
virtual void X(bitLenInt qubit)
X gate.
Definition: qinterface.hpp:1091
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:2862
virtual QInterfacePtr Copy()
Copy this QInterface.
Definition: qinterface.hpp:2992
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:249
QRACK_CONST real1_f TRYDECOMPOSE_EPSILON
Definition: qrack_types.hpp:270
QRACK_CONST real1 HALF_R1
Definition: qrack_types.hpp:192
half_float::half real1
Definition: qrack_types.hpp:102
std::complex< real1 > complex
Definition: qrack_types.hpp:136
QRACK_CONST real1 FP_NORM_EPSILON
Definition: qrack_types.hpp:268
bitCapInt pow2(const bitLenInt &p)
Definition: qrack_functions.hpp:143
QRACK_CONST real1 REAL1_EPSILON
Definition: qrack_types.hpp:208
QRACK_CONST real1 ONE_R1
Definition: qrack_types.hpp:193
QRACK_CONST real1 ZERO_R1
Definition: qrack_types.hpp:191
float real1_f
Definition: qrack_types.hpp:103
QRACK_CONST complex CMPLX_DEFAULT_ARG
Definition: qrack_types.hpp:267
std::shared_ptr< QStabilizer > QStabilizerPtr
Definition: qstabilizer.hpp:46
QRACK_CONST real1 PI_R1
Definition: qrack_types.hpp:186
const bitCapInt ONE_BCI
Definition: qrack_types.hpp:137
const bitCapInt ZERO_BCI
Definition: qrack_types.hpp:138
HALF_CONSTEXPR half abs(half arg)
Absolute value.
Definition: half.hpp:2975
#define HALF_R1_F
Definition: qrack_types.hpp:170
#define bitsInByte
Definition: qrack_types.hpp:162
#define REAL1_DEFAULT_ARG
Definition: qrack_types.hpp:185
#define bitLenInt
Definition: qrack_types.hpp:42
#define ZERO_R1_F
Definition: qrack_types.hpp:168
#define qrack_rand_gen_ptr
Definition: qrack_types.hpp:164
#define bitCapInt
Definition: qrack_types.hpp:66
#define IS_NORM_0(c)
Definition: qrack_types.hpp:29
Definition: qstabilizer.hpp:35
bitCapInt permutation
Definition: qstabilizer.hpp:36
complex amplitude
Definition: qstabilizer.hpp:37
AmplitudeEntry(const bitCapInt &p, const complex &a)
Definition: qstabilizer.hpp:39