51 if (m.find(target) != m.end()) {
53 circuit.push_back(std::make_shared<QCircuit>());
59 for (
size_t j = 0
U; j < controls.size(); ++j) {
60 if (m.find(controls[j]) != m.end()) {
62 circuit.push_back(std::make_shared<QCircuit>());
76 throw std::invalid_argument(
"QTensorNetwork qubit index values must be within allocated qubit bounds!");
84 controls,
qubitCount,
"QTensorNetwork qubit index values must be within allocated qubit bounds!");
89 const std::map<bitLenInt, bool>& mLayer =
measurements[layerId];
90 std::vector<bitLenInt> bits;
91 bits.reserve(mLayer.size());
92 std::vector<bool> values;
93 values.reserve(mLayer.size());
95 for (
const auto& m : mLayer) {
96 bits.push_back(m.first);
97 values.push_back(m.second);
106 return getenv(
"QRACK_QTENSORNETWORK_THRESHOLD_QB")
107 ? (
bitLenInt)std::stoi(std::string(getenv(
"QRACK_QTENSORNETWORK_THRESHOLD_QB")))
114 void MakeLayerStack(std::set<bitLenInt> qubits = std::set<bitLenInt>());
116 template <
typename Fn>
void RunAsAmplitudes(Fn fn,
const std::set<bitLenInt>& qubits = std::set<bitLenInt>())
118 if (!qubits.size()) {
148 bool randomGlobalPhase =
true,
bool useHostMem =
false, int64_t deviceId = -1,
bool useHardwareRNG =
true,
149 bool useSparseStateVec =
false,
real1_f norm_thresh =
REAL1_EPSILON, std::vector<int64_t> ignored = {},
154 bool useHostMem =
false, int64_t deviceId = -1,
bool useHardwareRNG =
true,
bool useSparseStateVec =
false,
157 :
QTensorNetwork({}, qBitCount, initState, rgp, phaseFac, doNorm, randomGlobalPhase, useHostMem, deviceId,
158 useHardwareRNG, useSparseStateVec, norm_thresh, devList, qubitThreshold, separation_thresh)
198 layerStack->NormalizeState(nrm, norm_thresh, phaseArg);
204 return SumSqrDiff(std::dynamic_pointer_cast<QTensorNetwork>(toCompare));
209 toCompare->MakeLayerStack();
220 circuit.push_back(std::make_shared<QCircuit>());
223 if (initState &
pow2(i)) {
242 throw std::domain_error(
"QTensorNetwork::SetQuantumState() not implemented!");
246 throw std::domain_error(
"QTensorNetwork::SetQuantumState() not implemented!");
261 throw std::domain_error(
"QTensorNetwork::SetAmplitude() not implemented!");
267 throw std::domain_error(
"QTensorNetwork::Compose() not implemented!");
271 throw std::domain_error(
"QTensorNetwork::Decompose() not implemented!");
275 throw std::domain_error(
"QTensorNetwork::Decompose() not implemented!");
279 throw std::domain_error(
"QTensorNetwork::Dispose() not implemented!");
283 throw std::domain_error(
"QTensorNetwork::Dispose() not implemented!");
290 throw std::invalid_argument(
"QTensorNetwork::Allocate() 'start' argument is out-of-bounds!");
303 for (
bitLenInt i = 0
U; i < movedQubits; ++i) {
304 const bitLenInt q = start + movedQubits - (i + 1U);
324 bool ForceM(
bitLenInt qubit,
bool result,
bool doForce =
true,
bool doApply =
true);
349 std::set<bitLenInt> qubits;
351 qubits.insert(
log2(qPow));
353 std::map<bitCapInt, int> toRet;
358 void MultiShotMeasureMask(
const std::vector<bitCapInt>& qPowers,
unsigned shots,
unsigned long long* shotsArray)
360 std::set<bitLenInt> qubits;
362 qubits.insert(
log2(qPow));
371 GetCircuit(target)->AppendGate(std::make_shared<QCircuitGate>(target, mtrx));
378 ->AppendGate(std::make_shared<QCircuitGate>(
379 target, mtrx, std::set<bitLenInt>{ controls.begin(), controls.end() },
pow2(controls.size()) - 1U));
386 ->AppendGate(std::make_shared<QCircuitGate>(
387 target, mtrx, std::set<bitLenInt>{ controls.begin(), controls.end() }, 0
U));
393 std::shared_ptr<complex> lMtrx(
new complex[4U], std::default_delete<
complex[]>());
394 lMtrx.get()[0
U] = topLeft;
397 lMtrx.get()[3U] = bottomRight;
399 ->AppendGate(std::make_shared<QCircuitGate>(target, lMtrx.get(),
400 std::set<bitLenInt>{ controls.begin(), controls.end() },
pow2(controls.size()) - 1U));
406 std::shared_ptr<complex> lMtrx(
new complex[4U], std::default_delete<
complex[]>());
407 lMtrx.get()[0
U] = topLeft;
410 lMtrx.get()[3U] = bottomRight;
412 ->AppendGate(std::make_shared<QCircuitGate>(
413 target, lMtrx.get(), std::set<bitLenInt>{ controls.begin(), controls.end() }, 0
U));
419 std::shared_ptr<complex> lMtrx(
new complex[4U], std::default_delete<
complex[]>());
421 lMtrx.get()[1U] = topRight;
422 lMtrx.get()[2U] = bottomLeft;
425 ->AppendGate(std::make_shared<QCircuitGate>(target, lMtrx.get(),
426 std::set<bitLenInt>{ controls.begin(), controls.end() },
pow2(controls.size()) - 1U));
432 std::shared_ptr<complex> lMtrx(
new complex[4U], std::default_delete<
complex[]>());
434 lMtrx.get()[1U] = topRight;
435 lMtrx.get()[2U] = bottomLeft;
438 ->AppendGate(std::make_shared<QCircuitGate>(
439 target, lMtrx.get(), std::set<bitLenInt>{ controls.begin(), controls.end() }, 0
U));
A "Qrack::QInterface" is an abstract interface exposing qubit permutation state vector with methods t...
Definition: qinterface.hpp:146
virtual bitLenInt Allocate(bitLenInt length)
Allocate new "length" count of |0> state qubits at end of qubit index position.
Definition: qinterface.hpp:434
virtual bitLenInt Compose(QInterfacePtr toCopy)
Combine another QInterface with this one, after the last bit index of this one.
Definition: qinterface.hpp:338
bool randGlobalPhase
Definition: qinterface.hpp:149
virtual void SetQubitCount(bitLenInt qb)
Definition: qinterface.hpp:159
bitLenInt qubitCount
Definition: qinterface.hpp:151
real1_f Rand()
Generate a random real number between 0 and 1.
Definition: qinterface.hpp:260
Definition: qtensornetwork.hpp:29
void GetProbs(real1 *outputProbs)
Get the pure quantum state representation.
Definition: qtensornetwork.hpp:248
bitLenInt GetThresholdQb()
Definition: qtensornetwork.hpp:103
bool useTGadget
Definition: qtensornetwork.hpp:34
void SetQuantumState(const complex *state)
Set an arbitrary pure quantum state representation.
Definition: qtensornetwork.hpp:240
void Decompose(bitLenInt start, QInterfacePtr dest)
Minimally decompose a set of contiguous bits from the separably composed unit, into "destination".
Definition: qtensornetwork.hpp:269
QTensorNetwork(std::vector< QInterfaceEngine > eng, bitLenInt qBitCount, bitCapInt initState=0, qrack_rand_gen_ptr rgp=nullptr, 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=FP_NORM_EPSILON_F)
Definition: qtensornetwork.cpp:20
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:373
bool useHostRam
Definition: qtensornetwork.hpp:31
QCircuitPtr GetCircuit(bitLenInt target, std::vector< bitLenInt > controls=std::vector< bitLenInt >())
Definition: qtensornetwork.hpp:44
void CheckQubitCount(bitLenInt target)
Definition: qtensornetwork.hpp:73
real1_f Prob(bitLenInt qubit)
Direct measure of bit probability to be in |1> state.
Definition: qtensornetwork.hpp:311
QInterfacePtr Clone()
Clone this QInterface.
Definition: qtensornetwork.cpp:113
void Mtrx(const complex *mtrx, bitLenInt target)
Apply an arbitrary single bit unitary transformation.
Definition: qtensornetwork.hpp:367
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:287
real1_f SumSqrDiff(QInterfacePtr toCompare)
Definition: qtensornetwork.hpp:202
bool isNearClifford
Definition: qtensornetwork.hpp:35
void MACInvert(const std::vector< bitLenInt > &controls, complex topRight, complex bottomLeft, bitLenInt target)
Apply a single bit transformation that reverses bit probability and might effect phase,...
Definition: qtensornetwork.hpp:428
void SetAmplitude(bitCapInt perm, complex amp)
Sets the representational amplitude of a full permutation.
Definition: qtensornetwork.hpp:259
QTensorNetwork(bitLenInt qBitCount, bitCapInt initState=0U, qrack_rand_gen_ptr rgp=nullptr, 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=FP_NORM_EPSILON_F)
Definition: qtensornetwork.hpp:152
std::map< bitCapInt, int > MultiShotMeasureMask(const std::vector< bitCapInt > &qPowers, unsigned shots)
Statistical measure of masked permutation probability.
Definition: qtensornetwork.hpp:347
void MACPhase(const std::vector< bitLenInt > &controls, complex topLeft, complex bottomRight, bitLenInt target)
Apply a single bit transformation that only effects phase, with arbitrary (anti-)control bits.
Definition: qtensornetwork.hpp:402
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: qtensornetwork.cpp:242
complex globalPhase
Definition: qtensornetwork.hpp:37
void GetQuantumState(complex *state)
Get the pure quantum state representation.
Definition: qtensornetwork.hpp:236
void MCPhase(const std::vector< bitLenInt > &controls, complex topLeft, complex bottomRight, bitLenInt target)
Apply a single bit transformation that only effects phase, with arbitrary control bits.
Definition: qtensornetwork.hpp:389
QInterfacePtr Decompose(bitLenInt start, bitLenInt length)
Schmidt decompose a length of qubits.
Definition: qtensornetwork.hpp:273
int64_t devID
Definition: qtensornetwork.hpp:36
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:194
void SetDevice(int64_t dID)
Set the device index, if more than one device is available.
Definition: qtensornetwork.hpp:169
void CheckQubitCount(bitLenInt target, const std::vector< bitLenInt > &controls)
Definition: qtensornetwork.hpp:80
void Dispose(bitLenInt start, bitLenInt length, bitCapInt disposedPerm)
Dispose a a contiguous set of qubits that are already in a permutation eigenstate.
Definition: qtensornetwork.hpp:281
void SetQuantumState(QInterfacePtr eng)
Definition: qtensornetwork.hpp:244
std::vector< std::map< bitLenInt, bool > > measurements
Definition: qtensornetwork.hpp:42
complex GetAmplitude(bitCapInt perm)
Get the representational amplitude of a full permutation.
Definition: qtensornetwork.hpp:253
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:358
bitLenInt Compose(QInterfacePtr toCopy, bitLenInt start)
Definition: qtensornetwork.hpp:265
void RunMeasurmentLayer(size_t layerId)
Definition: qtensornetwork.hpp:87
std::vector< int64_t > deviceIDs
Definition: qtensornetwork.hpp:39
void MakeLayerStack(std::set< bitLenInt > qubits=std::set< bitLenInt >())
Definition: qtensornetwork.cpp:60
void MCInvert(const std::vector< bitLenInt > &controls, complex topRight, complex bottomLeft, bitLenInt target)
Apply a single bit transformation that reverses bit probability and might effect phase,...
Definition: qtensornetwork.hpp:415
bitCapInt MAll()
Measure permutation state of all coherent bits.
Definition: qtensornetwork.hpp:326
QInterfacePtr layerStack
Definition: qtensornetwork.hpp:38
real1_f SumSqrDiff(QTensorNetworkPtr toCompare)
Definition: qtensornetwork.hpp:206
void Finish()
If asynchronous work is still running, block until it finishes.
Definition: qtensornetwork.hpp:171
bool isFinished()
Returns "false" if asynchronous work is still running, and "true" if all previously dispatched asynch...
Definition: qtensornetwork.hpp:178
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:277
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:381
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.cpp:134
std::vector< QInterfaceEngine > engines
Definition: qtensornetwork.hpp:40
virtual double GetUnitaryFidelity()
When "Schmidt-decomposition rounding parameter" ("SDRP") is being used, starting from initial 1....
Definition: qtensornetwork.hpp:162
std::vector< QCircuitPtr > circuit
Definition: qtensornetwork.hpp:41
real1_f ProbAll(bitCapInt fullRegister)
Direct measure of full permutation probability.
Definition: qtensornetwork.hpp:317
void SetPermutation(bitCapInt initState, complex phaseFac=CMPLX_DEFAULT_ARG)
Set to a specific permutation of all qubits.
Definition: qtensornetwork.hpp:214
void RunAsAmplitudes(Fn fn, const std::set< bitLenInt > &qubits=std::set< bitLenInt >())
Definition: qtensornetwork.hpp:116
void Dump()
If asynchronous work is still running, let the simulator know that it can be aborted.
Definition: qtensornetwork.hpp:180
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:187
Half-precision floating-point type.
Definition: half.hpp:2222
virtual bool M(bitLenInt qubitIndex)
Measurement gate.
Definition: qinterface.hpp:976
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
Definition: complex16x2simd.hpp:25
void ThrowIfQbIdArrayIsBad(const std::vector< bitLenInt > &controls, const bitLenInt &qubitCount, std::string message)
Definition: qrack_functions.hpp:71
std::complex< half_float::half > complex
Definition: qrack_types.hpp:62
std::shared_ptr< QInterface > QInterfacePtr
Definition: qinterface.hpp:28
constexpr real1_f ZERO_R1_F
Definition: qrack_types.hpp:152
constexpr real1_f FP_NORM_EPSILON_F
Definition: qrack_types.hpp:245
bitCapInt pow2(const bitLenInt &p)
Definition: qrack_functions.hpp:22
std::shared_ptr< QTensorNetwork > QTensorNetworkPtr
Definition: qtensornetwork.hpp:17
const real1 REAL1_DEFAULT_ARG
Definition: qrack_types.hpp:155
const real1 PI_R1
Definition: qrack_types.hpp:158
float real1_f
Definition: qrack_types.hpp:64
QRACK_CONST complex CMPLX_DEFAULT_ARG
Definition: qrack_types.hpp:242
std::shared_ptr< QCircuit > QCircuitPtr
Definition: qcircuit.hpp:570
const real1 REAL1_EPSILON
Definition: qrack_types.hpp:157
QRACK_CONST complex ZERO_CMPLX
Definition: qrack_types.hpp:240
bitLenInt log2(bitCapInt n)
Definition: qrack_functions.hpp:26
half sin(half arg)
Sine function.
Definition: half.hpp:3885
half cos(half arg)
Cosine function.
Definition: half.hpp:3922
#define bitLenInt
Definition: qrack_types.hpp:44
#define qrack_rand_gen_ptr
Definition: qrack_types.hpp:146
#define bitCapInt
Definition: qrack_types.hpp:105