53 unit->ResetPhaseOffset();
67 std::vector<bitLenInt*>::iterator first, std::vector<bitLenInt*>::iterator last);
93 std::vector<bitLenInt> bits{ control, target };
94 std::vector<bitLenInt*> ebits{ &bits[0
U], &bits[1U] };
96 fn(unit, bits[0
U], bits[1U], mtrx);
109 throw std::invalid_argument(
110 methodName + std::string(
" target qubit index parameter must be within allocated qubit bounds!"));
117 throw std::invalid_argument(
118 methodName + std::string(
" target qubit index parameter must be within allocated qubit bounds!"));
120 if (controls.size() > 1U) {
121 throw std::invalid_argument(methodName + std::string(
" can only have one control qubit!"));
125 throw std::invalid_argument(
126 methodName + std::string(
" control qubit index parameter must be within allocated qubit bounds!"));
135 int64_t ignored3 = -1,
bool useHardwareRNG =
true,
bool ignored4 =
false,
real1_f ignored5 =
REAL1_EPSILON,
149 return std::make_shared<QUnitClifford>(
165 shards[i].unit->SetRandGlobalPhase(isRand);
174 std::map<QStabilizerPtr, QStabilizerPtr> engines;
178 if (engines.find(unit) == engines.end()) {
179 permCount *=
pow2(unit->gaussian());
188 shards = std::vector<CliffordShard>();
195 const std::vector<bitLenInt>& bits,
const std::vector<bitCapInt>& perms,
bitCapInt offset = 0
U);
216 throw std::domain_error(
"QUnitClifford::SetAmplitude() not implemented!");
242 unit->AntiCNOT(c, t);
313 if (qubit1 == qubit2) {
336 bool ForceM(
bitLenInt t,
bool result,
bool doForce =
true,
bool doApply =
true);
346 std::map<bitCapInt, int>
MultiShotMeasureMask(
const std::vector<bitCapInt>& qPowers,
unsigned shots);
348 void MultiShotMeasureMask(
const std::vector<bitCapInt>& qPowers,
unsigned shots,
unsigned long long* shotsArray);
366 std::vector<complex>
GetAmplitudes(std::vector<bitCapInt> perms);
375 return shard.
unit->IsSeparableZ(shard.
mapped);
385 return shard.
unit->IsSeparableX(shard.
mapped);
394 return shard.
unit->IsSeparableY(shard.
mapped);
412 return std::dynamic_pointer_cast<QUnitClifford>(
Clone())->EntangleAll()->CanDecomposeDispose(start, length);
421 throw std::invalid_argument(
"QUnit::Compose start index is out-of-bounds!");
425 QUnitCliffordPtr clone = std::dynamic_pointer_cast<QUnitClifford>(toCopy->Clone());
428 shards.insert(
shards.begin() + start, clone->shards.begin(), clone->shards.end());
436 return Compose(std::dynamic_pointer_cast<QUnitClifford>(toCopy), start);
440 Decompose(start, std::dynamic_pointer_cast<QUnitClifford>(dest));
462 throw std::out_of_range(
"QUnitClifford::Allocate() cannot start past end of register!");
473 return Compose(nQubits, start);
490 return SumSqrDiff(std::dynamic_pointer_cast<QUnitClifford>(toCompare));
495 return ApproxCompare(std::dynamic_pointer_cast<QUnitClifford>(toCompare), error_tol);
503 if (
this == toCompare.get()) {
507 return std::dynamic_pointer_cast<QUnitClifford>(
Clone())->EntangleAll()->ApproxCompare(
508 std::dynamic_pointer_cast<QUnitClifford>(toCompare->Clone())->EntangleAll(), error_tol);
529 shard.
unit->Phase(topLeft, bottomRight, shard.
mapped);
536 shard.
unit->Invert(topRight, bottomLeft, shard.
mapped);
541 if (!controls.size()) {
542 Phase(topLeft, bottomRight, t);
550 unit->MCPhase({ c }, mtrx[0
U], mtrx[3U], t);
555 if (!controls.size()) {
556 Phase(topLeft, bottomRight, t);
564 unit->MACPhase({ c }, mtrx[0
U], mtrx[3U], t);
569 if (!controls.size()) {
570 Invert(topRight, bottomLeft, t);
578 unit->MCInvert({ c }, mtrx[1U], mtrx[2U], t);
583 if (!controls.size()) {
584 Invert(topRight, bottomLeft, t);
592 unit->MACInvert({ c }, mtrx[1U], mtrx[2U], t);
597 if (!controls.size()) {
605 unit->MCMtrx({ c }, mtrx, t);
610 if (!controls.size()) {
618 unit->MACMtrx({ c }, mtrx, t);
628 unit->FSim((
real1_f)std::real(mtrx[0
U]), (
real1_f)std::real(mtrx[1U]), c, t);
634 for (
size_t i = 0
U; i < qubits.size(); ++i) {
645 if (qubit1 == qubit2) {
A "Qrack::QInterface" is an abstract interface exposing qubit permutation state vector with methods t...
Definition: qinterface.hpp:146
bitCapInt maxQPower
Definition: qinterface.hpp:154
bool useRDRAND
Definition: qinterface.hpp:150
virtual bitLenInt Allocate(bitLenInt length)
Allocate new "length" count of |0> state qubits at end of qubit index position.
Definition: qinterface.hpp:434
std::shared_ptr< RdRandom > hardware_rand_generator
Definition: qinterface.hpp:157
virtual bitLenInt Compose(QInterfacePtr toCopy)
Combine another QInterface with this one, after the last bit index of this one.
Definition: qinterface.hpp:338
qrack_rand_gen_ptr rand_generator
Definition: qinterface.hpp:155
bool randGlobalPhase
Definition: qinterface.hpp:149
virtual void SetQubitCount(bitLenInt qb)
Definition: qinterface.hpp:159
bitLenInt qubitCount
Definition: qinterface.hpp:151
bool doNormalize
Definition: qinterface.hpp:148
Definition: qunitclifford.hpp:41
void Mtrx(const complex *mtrx, bitLenInt t)
Apply an arbitrary single bit unitary transformation.
Definition: qunitclifford.hpp:518
bitCapInt GetMaxQPower()
Get the maximum number of basis states, namely for qubits.
Definition: qunitclifford.hpp:158
real1_f ExpectationBitsFactorized(const std::vector< bitLenInt > &bits, const std::vector< bitCapInt > &perms, bitCapInt offset=0U)
Get expectation value of bits, given an array of qubit weights.
Definition: qunitclifford.cpp:46
void Invert(complex topRight, complex bottomLeft, bitLenInt t)
Apply a single bit transformation that reverses bit probability and might effect phase.
Definition: qunitclifford.hpp:532
void AntiCNOT(bitLenInt c, bitLenInt t)
Apply an (anti-)CNOT gate with control and target.
Definition: qunitclifford.hpp:239
void MACMtrx(const std::vector< bitLenInt > &controls, const complex *mtrx, bitLenInt t)
Apply an arbitrary single bit unitary transformation, with arbitrary (anti-)control bits.
Definition: qunitclifford.hpp:608
void SetRandGlobalPhase(bool isRand)
Definition: qunitclifford.hpp:162
bool IsSeparableZ(const bitLenInt &t)
Returns "true" if target qubit is a Z basis eigenstate.
Definition: qunitclifford.hpp:371
void IISwap(bitLenInt c, bitLenInt t)
Inverse ISwap - Swap values of two bits in register, and apply phase factor of -i if bits are differe...
Definition: qunitclifford.hpp:328
QStabilizerPtr EntangleInCurrentBasis(std::vector< bitLenInt * >::iterator first, std::vector< bitLenInt * >::iterator last)
Definition: qunitclifford.cpp:257
bool IsSeparableY(const bitLenInt &t)
Returns "true" if target qubit is a Y basis eigenstate.
Definition: qunitclifford.hpp:390
void Decompose(bitLenInt start, QUnitCliffordPtr dest)
Definition: qunitclifford.hpp:442
bitLenInt Compose(QInterfacePtr toCopy, bitLenInt start)
Definition: qunitclifford.hpp:434
std::vector< CliffordShard > shards
Definition: qunitclifford.hpp:44
std::map< bitCapInt, complex > GetQuantumState()
Convert the state to sparse ket notation.
Definition: qunitclifford.cpp:407
void SetPermutation(bitCapInt perm, complex phaseFac=CMPLX_DEFAULT_ARG)
Set to a specific permutation of all qubits.
Definition: qunitclifford.cpp:169
bool TrySeparate(bitLenInt qubit1, bitLenInt qubit2)
Two-qubit TrySeparate()
Definition: qunitclifford.hpp:643
bool isClifford(bitLenInt qubit)
Returns "true" if current qubit state is identifiably within the Clifford set, or "false" if it is no...
Definition: qunitclifford.hpp:154
void Clear()
Definition: qunitclifford.hpp:186
bitLenInt ThrowIfQubitSetInvalid(const std::vector< bitLenInt > &controls, bitLenInt t, std::string methodName)
Definition: qunitclifford.hpp:114
bool IsSeparableX(const bitLenInt &t)
Returns "true" if target qubit is an X basis eigenstate.
Definition: qunitclifford.hpp:381
void MCInvert(const std::vector< bitLenInt > &controls, complex topRight, complex bottomLeft, bitLenInt t)
Apply a single bit transformation that reverses bit probability and might effect phase,...
Definition: qunitclifford.hpp:567
bitLenInt Compose(QUnitCliffordPtr toCopy)
Definition: qunitclifford.hpp:416
virtual bitLenInt Compose(QInterfacePtr toCopy)
Combine another QInterface with this one, after the last bit index of this one.
Definition: qinterface.hpp:338
void X(bitLenInt t)
X gate.
Definition: qunitclifford.hpp:293
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: qunitclifford.hpp:632
bitCapInt PermCount()
Definition: qunitclifford.hpp:172
void Detach(bitLenInt start, bitLenInt length, QUnitCliffordPtr dest)
Definition: qunitclifford.cpp:188
QStabilizerPtr EntangleAll()
Definition: qunitclifford.hpp:69
QInterfacePtr CloneBody(QUnitCliffordPtr copyPtr)
Definition: qunitclifford.cpp:29
real1_f Prob(bitLenInt qubit)
Direct measure of bit probability to be in |1> state.
Definition: qunitclifford.hpp:511
void OrderContiguous(QStabilizerPtr unit)
Definition: qunitclifford.cpp:322
std::map< bitCapInt, int > MultiShotMeasureMask(const std::vector< bitCapInt > &qPowers, unsigned shots)
Statistical measure of masked permutation probability.
Definition: qunitclifford.cpp:563
bitLenInt GetQubitCount()
Get the count of bits in this register.
Definition: qunitclifford.hpp:156
void Z(bitLenInt t)
Apply a phase gate (|0>->|0>, |1>->-|1>, or "Z") to qubit b.
Definition: qunitclifford.hpp:284
std::function< void(QStabilizerPtr unit, const bitLenInt &c, const bitLenInt &t, const complex *mtrx)> CGateFn
Definition: qunitclifford.hpp:90
void Dispose(bitLenInt start, bitLenInt length, bitCapInt disposedPerm)
Dispose a a contiguous set of qubits that are already in a permutation eigenstate.
Definition: qunitclifford.hpp:453
void CNOT(bitLenInt c, bitLenInt t)
Apply a CNOT gate with control and target.
Definition: qunitclifford.hpp:220
complex GetAmplitude(bitCapInt perm)
Get a single basis state amplitude.
Definition: qunitclifford.cpp:423
void IS(bitLenInt t)
Apply an inverse phase gate (|0>->|0>, |1>->-i|1>, or "S adjoint") to qubit b.
Definition: qunitclifford.hpp:276
void Decompose(bitLenInt start, QInterfacePtr dest)
Minimally decompose a set of contiguous bits from the separably composed unit, into "destination".
Definition: qunitclifford.hpp:438
bool ForceM(bitLenInt t, bool result, bool doForce=true, bool doApply=true)
Measure qubit t.
Definition: qunitclifford.cpp:540
bitCapInt MAll()
Measure all qubits.
Definition: qunitclifford.hpp:339
void S(bitLenInt t)
Apply a phase gate (|0>->|0>, |1>->i|1>, or "S") to qubit b.
Definition: qunitclifford.hpp:268
friend std::istream & operator>>(std::istream &is, const QUnitCliffordPtr s)
Definition: qunitclifford.cpp:830
void MCMtrx(const std::vector< bitLenInt > &controls, const complex *mtrx, bitLenInt t)
Apply an arbitrary single bit unitary transformation, with arbitrary control bits.
Definition: qunitclifford.hpp:595
void AntiCY(bitLenInt c, bitLenInt t)
Apply an (anti-)CY gate with control and target.
Definition: qunitclifford.hpp:246
real1_f ProbPermRdm(bitCapInt perm, bitLenInt ancillaeStart)
Definition: qunitclifford.cpp:103
void H(bitLenInt t)
Hadamard gate.
Definition: qunitclifford.hpp:261
~QUnitClifford()
Definition: qunitclifford.hpp:138
bool SeparateBit(bool value, bitLenInt qubit)
Definition: qunitclifford.cpp:503
void Y(bitLenInt t)
Apply a Pauli Y gate to target.
Definition: qunitclifford.hpp:300
void SetDevice(int64_t dID)
Set the device index, if more than one device is available.
Definition: qunitclifford.hpp:160
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: qunitclifford.cpp:74
std::vector< complex > GetAmplitudes(std::vector< bitCapInt > perms)
Get a single basis state amplitude.
Definition: qunitclifford.cpp:452
bool isClifford()
Returns "true" if current state is identifiably within the Clifford set, or "false" if it is not or c...
Definition: qunitclifford.hpp:153
void FSim(real1_f theta, real1_f phi, bitLenInt c, bitLenInt t)
The 2-qubit "fSim" gate, (useful in the simulation of particles with fermionic statistics)
Definition: qunitclifford.hpp:621
QUnitCliffordPtr CloneEmpty()
Definition: qunitclifford.hpp:147
void MCPhase(const std::vector< bitLenInt > &controls, complex topLeft, complex bottomRight, bitLenInt t)
Apply a single bit transformation that only effects phase, with arbitrary control bits.
Definition: qunitclifford.hpp:539
uint8_t IsSeparable(const bitLenInt &t)
Returns: 0 if target qubit is not separable 1 if target qubit is a Z basis eigenstate 2 if target qub...
Definition: qunitclifford.hpp:403
bool CanDecomposeDispose(const bitLenInt start, const bitLenInt length)
Definition: qunitclifford.hpp:410
void GetProbs(real1 *outputProbs)
Get all probabilities corresponding to ket notation.
Definition: qunitclifford.cpp:415
void ResetPhaseOffset()
Definition: qunitclifford.hpp:169
void CY(bitLenInt c, bitLenInt t)
Apply a CY gate with control and target.
Definition: qunitclifford.hpp:227
void Dispose(bitLenInt start, bitLenInt length)
Minimally decompose a set of contiguous bits from the separably composed unit, and discard the separa...
Definition: qunitclifford.hpp:452
void ThrowIfQubitInvalid(bitLenInt t, std::string methodName)
Definition: qunitclifford.hpp:106
friend std::ostream & operator<<(std::ostream &os, const QUnitCliffordPtr s)
Definition: qunitclifford.cpp:809
void MACInvert(const std::vector< bitLenInt > &controls, complex topRight, complex bottomLeft, bitLenInt t)
Apply a single bit transformation that reverses bit probability and might effect phase,...
Definition: qunitclifford.hpp:581
real1_f ProbMask(bitCapInt mask, bitCapInt permutation)
Direct measure of masked permutation probability.
Definition: qunitclifford.cpp:141
void ISwap(bitLenInt c, bitLenInt t)
Swap values of two bits in register, and apply phase factor of i if bits are different.
Definition: qunitclifford.hpp:321
void CombinePhaseOffsets(QStabilizerPtr unit)
Definition: qunitclifford.hpp:46
complex GetPhaseOffset()
Definition: qunitclifford.hpp:170
void Phase(complex topLeft, complex bottomRight, bitLenInt t)
Apply a single bit transformation that only effects phase.
Definition: qunitclifford.hpp:525
void SetAmplitude(bitCapInt perm, complex amp)
Sets the representational amplitude of a full permutation.
Definition: qunitclifford.hpp:214
QInterfacePtr Decompose(bitLenInt start, bitLenInt length)
Schmidt decompose a length of qubits.
Definition: qunitclifford.hpp:443
void AntiCZ(bitLenInt c, bitLenInt t)
Apply an (anti-)CZ gate with control and target.
Definition: qunitclifford.hpp:253
QStabilizerPtr MakeStabilizer(bitLenInt length=1U, bitCapInt perm=0U, complex phaseFac=CMPLX_DEFAULT_ARG)
Definition: qunitclifford.hpp:205
void CGate(bitLenInt control, bitLenInt target, const complex *mtrx, CGateFn fn)
Definition: qunitclifford.hpp:91
virtual real1_f SumSqrDiff(QInterfacePtr toCompare)
Definition: qunitclifford.hpp:488
complex phaseOffset
Definition: qunitclifford.hpp:43
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: qunitclifford.hpp:483
void SortUnit(QStabilizerPtr unit, std::vector< QSortEntry > &bits, bitLenInt low, bitLenInt high)
Definition: qunitclifford.cpp:348
bitLenInt Compose(QInterfacePtr toCopy)
Combine another QInterface with this one, after the last bit index of this one.
Definition: qunitclifford.hpp:417
void CZ(bitLenInt c, bitLenInt t)
Apply a CZ gate with control and target.
Definition: qunitclifford.hpp:233
void Swap(bitLenInt qubit1, bitLenInt qubit2)
Swap values of two bits in register.
Definition: qunitclifford.hpp:308
void MACPhase(const std::vector< bitLenInt > &controls, complex topLeft, complex bottomRight, bitLenInt t)
Apply a single bit transformation that only effects phase, with arbitrary (anti-)control bits.
Definition: qunitclifford.hpp:553
QUnitClifford(bitLenInt n, bitCapInt perm=0U, qrack_rand_gen_ptr rgp=nullptr, complex phasFac=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=FP_NORM_EPSILON_F)
Definition: qunitclifford.cpp:20
bool ApproxCompare(QUnitCliffordPtr toCompare, real1_f error_tol=TRYDECOMPOSE_EPSILON)
Definition: qunitclifford.hpp:497
void SetQuantumState(const complex *inputState)
Set an arbitrary pure quantum state representation.
Definition: qunitclifford.cpp:727
QInterfacePtr Clone()
Clone this QInterface.
Definition: qunitclifford.hpp:140
bitLenInt Allocate(bitLenInt start, bitLenInt length)
Allocate new "length" count of |0> state qubits at specified qubit index start position.
Definition: qunitclifford.hpp:455
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: qunitclifford.hpp:476
bitLenInt Compose(QUnitCliffordPtr toCopy, bitLenInt start)
Definition: qunitclifford.hpp:418
bool ApproxCompare(QInterfacePtr toCompare, real1_f error_tol=TRYDECOMPOSE_EPSILON)
Compare state vectors approximately, component by component, to determine whether this state vector i...
Definition: qunitclifford.hpp:493
Half-precision floating-point type.
Definition: half.hpp:2222
virtual void H(bitLenInt qubit)
Hadamard gate.
Definition: qinterface.hpp:876
virtual void X(bitLenInt qubit)
X gate.
Definition: qinterface.hpp:1054
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:2588
Definition: complex16x2simd.hpp:25
std::complex< half_float::half > complex
Definition: qrack_types.hpp:62
std::shared_ptr< QInterface > QInterfacePtr
Definition: qinterface.hpp:28
QRACK_CONST real1_f TRYDECOMPOSE_EPSILON
Definition: qrack_types.hpp:244
constexpr real1_f ZERO_R1_F
Definition: qrack_types.hpp:152
constexpr real1_f FP_NORM_EPSILON_F
Definition: qrack_types.hpp:245
const real1 ONE_R1
Definition: qrack_types.hpp:153
half_float::half real1
Definition: qrack_types.hpp:63
bitCapInt pow2(const bitLenInt &p)
Definition: qrack_functions.hpp:22
std::shared_ptr< QUnitClifford > QUnitCliffordPtr
Definition: qunitclifford.hpp:19
const real1 REAL1_DEFAULT_ARG
Definition: qrack_types.hpp:155
QRACK_CONST complex ONE_CMPLX
Definition: qrack_types.hpp:239
float real1_f
Definition: qrack_types.hpp:64
QRACK_CONST complex CMPLX_DEFAULT_ARG
Definition: qrack_types.hpp:242
std::shared_ptr< QStabilizer > QStabilizerPtr
Definition: qstabilizer.hpp:42
const real1 REAL1_EPSILON
Definition: qrack_types.hpp:157
QRACK_CONST complex ZERO_CMPLX
Definition: qrack_types.hpp:240
MICROSOFT_QUANTUM_DECL void U(_In_ uintq sid, _In_ uintq q, _In_ double theta, _In_ double phi, _In_ double lambda)
(External API) 3-parameter unitary gate
Definition: pinvoke_api.cpp:1362
#define bitLenInt
Definition: qrack_types.hpp:44
#define qrack_rand_gen_ptr
Definition: qrack_types.hpp:146
#define bitCapInt
Definition: qrack_types.hpp:105
Definition: qunitclifford.hpp:22
QStabilizerPtr unit
Definition: qunitclifford.hpp:24
CliffordShard(const CliffordShard &o)
Definition: qunitclifford.hpp:33
bitLenInt mapped
Definition: qunitclifford.hpp:23
CliffordShard(bitLenInt m=0U, QStabilizerPtr u=NULL)
Definition: qunitclifford.hpp:26
Definition: qunitclifford.hpp:56
bool operator>(const QSortEntry &rhs)
Definition: qunitclifford.hpp:60
bitLenInt bit
Definition: qunitclifford.hpp:57
bool operator<(const QSortEntry &rhs)
Definition: qunitclifford.hpp:59
bitLenInt mapped
Definition: qunitclifford.hpp:58