36 #if ENABLE_OPENCL || ENABLE_CUDA
56 throw std::invalid_argument(
"QTensorNetwork qubit index values must be within allocated qubit bounds!");
64 controls,
qubitCount,
"QTensorNetwork qubit index values must be within allocated qubit bounds!");
71 template <
typename Fn>
void RunAsAmplitudes(Fn fn, std::set<bitLenInt> qubits = std::set<bitLenInt>())
86 bool randomGlobalPhase =
true,
bool useHostMem =
false, int64_t deviceId = -1,
bool useHardwareRNG =
true,
87 bool useSparseStateVec =
false,
real1_f norm_thresh =
REAL1_EPSILON, std::vector<int64_t> ignored = {},
92 bool useHostMem =
false, int64_t deviceId = -1,
bool useHardwareRNG =
true,
bool useSparseStateVec =
false,
95 :
QTensorNetwork({}, qBitCount, initState, rgp, phaseFac, doNorm, randomGlobalPhase, useHostMem, deviceId,
96 useHardwareRNG, useSparseStateVec, norm_thresh, devList, qubitThreshold, separation_thresh)
163 layerStack->NormalizeState(nrm, norm_thresh, phaseArg);
168 return SumSqrDiff(std::dynamic_pointer_cast<QTensorNetwork>(toCompare));
182 layerStack->SetPermutation(initState, phaseFac);
200 throw std::invalid_argument(
"QTensorNetwork::SetQuantumState() argument must match in qubit count!");
204 circuit = tnEng->circuit->Clone();
226 std::dynamic_pointer_cast<QTensorNetwork>(toCopy)->RunAsAmplitudes([&](
QInterfacePtr ls) {});
228 toRet = ls->Compose(std::dynamic_pointer_cast<QTensorNetwork>(toCopy)->
layerStack, start);
235 dest->SetPermutation(0
U);
237 ls->Decompose(start, std::dynamic_pointer_cast<QTensorNetwork>(dest)->
layerStack);
261 dest->SetPermutation(0
U);
263 toRet = ls->TryDecompose(start, std::dynamic_pointer_cast<QTensorNetwork>(dest)->
layerStack, error_tol);
270 return layerStack->TrySeparate(qubits, error_tol);
279 throw std::invalid_argument(
"QTensorNetwork::Allocate() 'start' argument is out-of-bounds!");
290 for (
bitLenInt i = 0
U; i < movedQubits; ++i) {
291 const bitLenInt q = start + movedQubits - (i + 1U);
320 std::map<bitCapInt, int> toRet;
321 std::set<bitLenInt> qubits;
323 qubits.insert(
log2(qPow));
328 void MultiShotMeasureMask(
const std::vector<bitCapInt>& qPowers,
unsigned shots,
unsigned long long* shotsArray)
330 std::map<bitCapInt, int> toRet;
331 std::set<bitLenInt> qubits;
333 qubits.insert(
log2(qPow));
341 circuit->AppendGate(std::make_shared<QCircuitGate>(target, mtrx));
349 std::make_shared<QCircuitGate>(target, mtrx, std::set<bitLenInt>{ controls.begin(), controls.end() }, m));
354 circuit->AppendGate(std::make_shared<QCircuitGate>(
355 target, mtrx, std::set<bitLenInt>{ controls.begin(), controls.end() },
ZERO_BCI));
361 std::shared_ptr<complex> lMtrx(
new complex[4U], std::default_delete<
complex[]>());
362 lMtrx.get()[0
U] = topLeft;
365 lMtrx.get()[3U] = bottomRight;
368 circuit->AppendGate(std::make_shared<QCircuitGate>(
369 target, lMtrx.get(), std::set<bitLenInt>{ controls.begin(), controls.end() }, m));
375 std::shared_ptr<complex> lMtrx(
new complex[4U], std::default_delete<
complex[]>());
376 lMtrx.get()[0
U] = topLeft;
379 lMtrx.get()[3U] = bottomRight;
380 circuit->AppendGate(std::make_shared<QCircuitGate>(
381 target, lMtrx.get(), std::set<bitLenInt>{ controls.begin(), controls.end() },
ZERO_BCI));
387 std::shared_ptr<complex> lMtrx(
new complex[4U], std::default_delete<
complex[]>());
389 lMtrx.get()[1U] = topRight;
390 lMtrx.get()[2U] = bottomLeft;
394 circuit->AppendGate(std::make_shared<QCircuitGate>(
395 target, lMtrx.get(), std::set<bitLenInt>{ controls.begin(), controls.end() }, m));
401 std::shared_ptr<complex> lMtrx(
new complex[4U], std::default_delete<
complex[]>());
403 lMtrx.get()[1U] = topRight;
404 lMtrx.get()[2U] = bottomLeft;
406 circuit->AppendGate(std::make_shared<QCircuitGate>(
407 target, lMtrx.get(), std::set<bitLenInt>{ controls.begin(), controls.end() },
ZERO_BCI));
void bi_decrement(BigInteger *pBigInt, const BIG_INTEGER_WORD &value)
Definition: big_integer.hpp:237
A "Qrack::QInterface" is an abstract interface exposing qubit permutation state vector with methods t...
Definition: qinterface.hpp:141
virtual bitLenInt Allocate(bitLenInt length)
Allocate new "length" count of |0> state qubits at end of qubit index position.
Definition: qinterface.hpp:477
virtual bitLenInt Compose(QInterfacePtr toCopy)
Combine another QInterface with this one, after the last bit index of this one.
Definition: qinterface.hpp:371
virtual void SetQubitCount(bitLenInt qb)
Definition: qinterface.hpp:268
bitLenInt qubitCount
Definition: qinterface.hpp:146
Definition: qtensornetwork.hpp:29
void GetProbs(real1 *outputProbs)
Get the pure quantum state representation.
Definition: qtensornetwork.hpp:206
bitLenInt GetThresholdQb()
Definition: qtensornetwork.cpp:115
QTensorNetwork(bitLenInt qBitCount, const bitCapInt &initState=ZERO_BCI, qrack_rand_gen_ptr rgp=nullptr, const complex &phaseFac=CMPLX_DEFAULT_ARG, bool doNorm=false, bool randomGlobalPhase=true, bool useHostMem=false, int64_t deviceId=-1, bool useHardwareRNG=true, bool useSparseStateVec=false, real1_f norm_thresh=REAL1_EPSILON, std::vector< int64_t > devList={}, bitLenInt qubitThreshold=0U, real1_f separation_thresh=_qrack_qunit_sep_thresh)
Definition: qtensornetwork.hpp:90
bool useTGadget
Definition: qtensornetwork.hpp:34
void SetQuantumState(const complex *state)
Set an arbitrary pure quantum state representation.
Definition: qtensornetwork.hpp:191
void Decompose(bitLenInt start, QInterfacePtr dest)
Minimally decompose a set of contiguous bits from the separably composed unit, into "destination".
Definition: qtensornetwork.hpp:233
bool isSparse
Definition: qtensornetwork.hpp:32
void MCMtrx(const std::vector< bitLenInt > &controls, const complex *mtrx, bitLenInt target)
Apply an arbitrary single bit unitary transformation, with arbitrary control bits.
Definition: qtensornetwork.hpp:343
bool TrySeparate(bitLenInt qubit)
Single-qubit TrySeparate()
Definition: qtensornetwork.hpp:272
bool useHostRam
Definition: qtensornetwork.hpp:31
void SetTInjection(bool useGadget)
Set the option to use T-injection gadgets (off by default)
Definition: qtensornetwork.hpp:109
void SetDeviceList(std::vector< int64_t > dIDs)
Set the device index list, if more than one device is available.
Definition: qtensornetwork.hpp:148
bool TrySeparate(bitLenInt qubit1, bitLenInt qubit2)
Two-qubit TrySeparate()
Definition: qtensornetwork.hpp:273
void CheckQubitCount(bitLenInt target)
Definition: qtensornetwork.hpp:53
real1_f Prob(bitLenInt qubit)
Direct measure of bit probability to be in |1> state.
Definition: qtensornetwork.hpp:298
real1_f ncrp
Definition: qtensornetwork.hpp:47
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: qtensornetwork.hpp:383
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: qtensornetwork.hpp:357
QInterfacePtr Clone()
Clone this QInterface.
Definition: qtensornetwork.cpp:179
void Mtrx(const complex *mtrx, bitLenInt target)
Apply an arbitrary single bit unitary transformation.
Definition: qtensornetwork.hpp:338
bool isReactiveSeparate
Definition: qtensornetwork.hpp:33
bitLenInt Allocate(bitLenInt start, bitLenInt length)
Allocate new "length" count of |0> state qubits at specified qubit index start position.
Definition: qtensornetwork.hpp:276
real1_f SumSqrDiff(QInterfacePtr toCompare)
Calculates (1 - <\psi_e|\psi_c>) between states |\psi_c> and |\psi_e>.
Definition: qtensornetwork.hpp:166
void SetNcrp(real1_f rp)
Set the "Near-clifford rounding parameter" value, (between 0 and 1)
Definition: qtensornetwork.hpp:115
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: qtensornetwork.hpp:371
bool isNearClifford
Definition: qtensornetwork.hpp:35
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: qtensornetwork.hpp:397
std::map< bitCapInt, int > MultiShotMeasureMask(const std::vector< bitCapInt > &qPowers, unsigned shots)
Statistical measure of masked permutation probability.
Definition: qtensornetwork.hpp:318
bool TrySeparate(const std::vector< bitLenInt > &qubits, real1_f error_tol)
Qrack::QUnit types maintain explicit separation of representations of qubits, which reduces memory us...
Definition: qtensornetwork.hpp:268
bitLenInt aceQubits
Definition: qtensornetwork.hpp:44
complex GetAmplitude(const bitCapInt &perm)
Get the representational amplitude of a full permutation.
Definition: qtensornetwork.hpp:211
real1_f ProbAll(const bitCapInt &fullRegister)
Direct measure of full permutation probability.
Definition: qtensornetwork.hpp:304
void GetQuantumState(complex *state)
Get the pure quantum state representation.
Definition: qtensornetwork.hpp:187
void SetPermutation(const bitCapInt &initState, const complex &phaseFac=CMPLX_DEFAULT_ARG)
Set to a specific permutation of all qubits.
Definition: qtensornetwork.hpp:178
void SetAmplitude(const bitCapInt &perm, const complex &)
Sets the representational amplitude of a full permutation.
Definition: qtensornetwork.hpp:217
bitLenInt qbThreshold
Definition: qtensornetwork.hpp:45
int64_t GetDevice()
Get the device index.
Definition: qtensornetwork.hpp:149
QCircuitPtr circuit
Definition: qtensornetwork.hpp:51
QInterfacePtr Decompose(bitLenInt start, bitLenInt length)
Schmidt decompose a length of qubits.
Definition: qtensornetwork.hpp:241
void SetSparseAceMaxMb(size_t mb)
Set the (sparse-simulation) "automatic circuit elision" (ACE) maximum memory megabytes.
Definition: qtensornetwork.hpp:127
int64_t devID
Definition: qtensornetwork.hpp:42
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: qtensornetwork.hpp:160
void SetDevice(int64_t dID)
Set the device index, if more than one device is available.
Definition: qtensornetwork.hpp:147
void CheckQubitCount(bitLenInt target, const std::vector< bitLenInt > &controls)
Definition: qtensornetwork.hpp:60
void RunAsAmplitudes(Fn fn, std::set< bitLenInt > qubits=std::set< bitLenInt >())
Definition: qtensornetwork.hpp:71
void SetQuantumState(QInterfacePtr eng)
Definition: qtensornetwork.hpp:195
void SetReactiveSeparate(bool isAggSep)
Set reactive separation option (on by default if available)
Definition: qtensornetwork.hpp:107
void SetSdrp(real1_f sdrp)
Set the "Schmidt decomposition rounding parameter" value, (between 0 and 1)
Definition: qtensornetwork.hpp:100
real1_f separabilityThreshold
Definition: qtensornetwork.hpp:46
double GetUnitaryFidelity()
When "Schmidt-decomposition rounding parameter" ("SDRP") is being used, starting from initial 1....
Definition: qtensornetwork.hpp:133
void MultiShotMeasureMask(const std::vector< bitCapInt > &qPowers, unsigned shots, unsigned long long *shotsArray)
Statistical measure of masked permutation probability (returned as array)
Definition: qtensornetwork.hpp:328
bitLenInt Compose(QInterfacePtr toCopy, bitLenInt start)
Compose() a QInterface peer, inserting its qubit into index order at start index.
Definition: qtensornetwork.hpp:223
std::vector< int64_t > deviceIDs
Definition: qtensornetwork.hpp:49
bitCapInt HighestProbAll()
Get highest probability permutation.
Definition: qtensornetwork.hpp:140
QTensorNetwork(std::vector< QInterfaceEngine > eng, bitLenInt qBitCount, const bitCapInt &initState=ZERO_BCI, qrack_rand_gen_ptr rgp=nullptr, const complex &phaseFac=CMPLX_DEFAULT_ARG, bool doNorm=false, bool randomGlobalPhase=true, bool useHostMem=false, int64_t deviceId=-1, bool useHardwareRNG=true, bool useSparseStateVec=false, real1_f norm_thresh=REAL1_EPSILON, std::vector< int64_t > ignored={}, bitLenInt qubitThreshold=0, real1_f separation_thresh=_qrack_qunit_sep_thresh)
Definition: qtensornetwork.cpp:35
bool TryDecompose(bitLenInt start, QInterfacePtr dest, real1_f error_tol=TRYDECOMPOSE_EPSILON)
Attempt to Decompose() a bit range.
Definition: qtensornetwork.hpp:258
QInterfacePtr layerStack
Definition: qtensornetwork.hpp:48
real1_f SumSqrDiff(QTensorNetworkPtr toCompare)
Definition: qtensornetwork.hpp:170
void Finish()
If asynchronous work is still running, block until it finishes.
Definition: qtensornetwork.hpp:152
bool isFinished()
Returns "false" if asynchronous work is still running, and "true" if all previously dispatched asynch...
Definition: qtensornetwork.hpp:154
void Dispose(bitLenInt start, bitLenInt length)
Minimally decompose a set of contiguous bits from the separably composed unit, and discard the separa...
Definition: qtensornetwork.hpp:248
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: qtensornetwork.hpp:351
bool ForceM(bitLenInt qubit, bool result, bool doForce=true, bool doApply=true)
Act as if is a measurement was applied, except force the (usually random) result.
Definition: qtensornetwork.hpp:311
std::vector< int64_t > GetDeviceList()
Get the device index.
Definition: qtensornetwork.hpp:150
void SetAceMaxQubits(bitLenInt qb)
Set the "automatic circuit elision" (ACE) maximum entangled subsystem qubit count.
Definition: qtensornetwork.hpp:121
void Dispose(bitLenInt start, bitLenInt length, const bitCapInt &disposedPerm)
Dispose a a contiguous set of qubits that are already in a permutation eigenstate.
Definition: qtensornetwork.hpp:253
size_t aceMb
Definition: qtensornetwork.hpp:43
std::vector< QInterfaceEngine > engines
Definition: qtensornetwork.hpp:50
void MakeLayerStack()
Definition: qtensornetwork.cpp:160
void Dump()
If asynchronous work is still running, let the simulator know that it can be aborted.
Definition: qtensornetwork.hpp:156
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: qtensornetwork.hpp:158
Half-precision floating-point type.
Definition: half.hpp:2222
virtual void U(bitLenInt target, real1_f theta, real1_f phi, real1_f lambda)
General unitary gate.
Definition: rotational.cpp:18
GLOSSARY: bitLenInt - "bit-length integer" - unsigned integer ID of qubit position in register bitCap...
Definition: complex16x2simd.hpp:25
void ThrowIfQbIdArrayIsBad(const std::vector< bitLenInt > &controls, const bitLenInt &qubitCount, std::string message)
Definition: qrack_functions.hpp:185
QRACK_CONST real1_f FP_NORM_EPSILON_F
Definition: qrack_types.hpp:269
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
std::complex< real1 > complex
Definition: qrack_types.hpp:136
bitCapInt pow2(const bitLenInt &p)
Definition: qrack_functions.hpp:143
std::shared_ptr< QTensorNetwork > QTensorNetworkPtr
Definition: qtensornetwork.hpp:17
QRACK_CONST real1 REAL1_EPSILON
Definition: qrack_types.hpp:208
float real1_f
Definition: qrack_types.hpp:103
QRACK_CONST complex CMPLX_DEFAULT_ARG
Definition: qrack_types.hpp:267
std::shared_ptr< QCircuit > QCircuitPtr
Definition: qcircuit.hpp:607
QRACK_CONST complex ZERO_CMPLX
Definition: qrack_types.hpp:263
const bitCapInt ZERO_BCI
Definition: qrack_types.hpp:138
bitLenInt log2(bitCapInt n)
Definition: qrack_functions.hpp:141
#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