The Quantum Exact Simulation Toolkit v4.2.0
Loading...
Searching...
No Matches
Deprecated utilities

Utilities for testing QuEST's deprecated v3 API functions. More...

Typedefs

typedef vector< vector< qcomp > > QMatrix
 
typedef vector< qcomp > QVector
 

Functions

void applyReferenceMatrix (QMatrix &state, int *ctrls, int numCtrls, int *targs, int numTargs, QMatrix op)
 
void applyReferenceMatrix (QMatrix &state, int *targs, int numTargs, QMatrix op)
 
void applyReferenceMatrix (QVector &state, int *ctrls, int numCtrls, int *targs, int numTargs, QMatrix op)
 
void applyReferenceMatrix (QVector &state, int *targs, int numTargs, QMatrix op)
 
void applyReferenceOp (QMatrix &state, int *ctrls, int numCtrls, int *targs, int numTargs, QMatrix op)
 
void applyReferenceOp (QMatrix &state, int *ctrls, int numCtrls, int targ1, int targ2, QMatrix op)
 
void applyReferenceOp (QMatrix &state, int *ctrls, int numCtrls, int target, QMatrix op)
 
void applyReferenceOp (QMatrix &state, int *targs, int numTargs, QMatrix op)
 
void applyReferenceOp (QMatrix &state, int ctrl, int *targs, int numTargs, QMatrix op)
 
void applyReferenceOp (QMatrix &state, int ctrl, int targ, QMatrix op)
 
void applyReferenceOp (QMatrix &state, int ctrl, int targ1, int targ2, QMatrix op)
 
void applyReferenceOp (QMatrix &state, int targ, QMatrix op)
 
void applyReferenceOp (QVector &state, int *ctrls, int numCtrls, int *targs, int numTargs, QMatrix op)
 
void applyReferenceOp (QVector &state, int *ctrls, int numCtrls, int targ1, int targ2, QMatrix op)
 
void applyReferenceOp (QVector &state, int *ctrls, int numCtrls, int target, QMatrix op)
 
void applyReferenceOp (QVector &state, int *targs, int numTargs, QMatrix op)
 
void applyReferenceOp (QVector &state, int ctrl, int *targs, int numTargs, QMatrix op)
 
void applyReferenceOp (QVector &state, int ctrl, int targ, QMatrix op)
 
void applyReferenceOp (QVector &state, int ctrl, int targ1, int targ2, QMatrix op)
 
void applyReferenceOp (QVector &state, int targ, QMatrix op)
 
bool areEqual (QMatrix a, QMatrix b)
 
bool areEqual (Qureg qureg, QMatrix matr)
 
bool areEqual (Qureg qureg, QMatrix matr, qreal precision)
 
bool areEqual (Qureg qureg, QVector vec)
 
bool areEqual (Qureg qureg, QVector vec, qreal precision)
 
bool areEqual (Qureg qureg1, Qureg qureg2)
 
bool areEqual (Qureg qureg1, Qureg qureg2, qreal precision)
 
bool areEqual (QVector a, QVector b)
 
bool areEqual (QVector vec, qreal *reals)
 
bool areEqual (QVector vec, qreal *reals, qreal *imags)
 
void assertQuregAndRefInDebugState (Qureg qureg, QMatrix ref)
 
void assertQuregAndRefInDebugState (Qureg qureg, QVector ref)
 
CatchGen< int * > bitsets (int numBits)
 
unsigned int calcLog2 (long unsigned int res)
 
void deleteFilesWithPrefixSynch (char *prefix)
 
QMatrix getConjugateTranspose (QMatrix a)
 
QVector getDFT (QVector in)
 
QVector getDFT (QVector in, int *targs, int numTargs)
 
QMatrix getExponentialOfDiagonalMatrix (QMatrix a)
 
QMatrix getExponentialOfPauliMatrix (qreal angle, QMatrix a)
 
QMatrix getFullOperatorMatrix (int *ctrls, int numCtrls, int *targs, int numTargs, QMatrix op, int numQubits)
 
QMatrix getIdentityMatrix (size_t dim)
 
QMatrix getKetBra (QVector ket, QVector bra)
 
QMatrix getKroneckerProduct (QMatrix a, QMatrix b)
 
QVector getKroneckerProduct (QVector b, QVector a)
 
QVector getMatrixDiagonal (QMatrix matr)
 
QMatrix getMixedDensityMatrix (vector< qreal > probs, vector< QVector > states)
 
QVector getNormalised (QVector vec)
 
QMatrix getPureDensityMatrix (QVector state)
 
qcomp getRandomComplex ()
 
QMatrix getRandomDensityMatrix (int numQb)
 
int getRandomInt (int min, int max)
 
vector< QMatrixgetRandomKrausMap (int numQb, int numOps)
 
vector< QVectorgetRandomOrthonormalVectors (int numQb, int numStates)
 
vector< qreal > getRandomProbabilities (int numProbs)
 
QMatrix getRandomPureDensityMatrix (int numQb)
 
QMatrix getRandomQMatrix (int dim)
 
QVector getRandomQVector (int dim)
 
qreal getRandomReal (qreal min, qreal max)
 
QVector getRandomStateVector (int numQb)
 
QMatrix getRandomUnitary (int numQb)
 
QMatrix getSwapMatrix (int qb1, int qb2, int numQb)
 
long long int getTwosComplement (long long int decimal, int numBits)
 
long long int getUnsigned (long long int twosComp, int numBits)
 
long long int getValueOfTargets (long long int ind, int *targs, int numTargs)
 
QMatrix getZeroMatrix (size_t dim)
 
CatchGen< pauliOpType * > pauliseqs (int numPaulis)
 
CatchGen< int * > sequences (int base, int numDigits)
 
void setRandomDiagPauliHamil (PauliHamil hamil, int numQubits)
 
void setRandomPauliSum (PauliHamil hamil, int numQubits)
 
void setRandomPauliSum (qreal *coeffs, pauliOpType *codes, int numQubits, int numTerms)
 
void setRandomTargets (int *targs, int numTargs, int numQb)
 
void setRandomTargets (vector< int > &targs, int numQb)
 
void setRandomTestStateSeeds ()
 
void setSubMatrix (QMatrix &dest, QMatrix sub, size_t r, size_t c)
 
void setUniqueFilename (char *outFn, int maxlen, char *prefix)
 
CatchGen< int * > sublists (CatchGen< int > &&gen, int numSamps, const int *exclude, int numExclude)
 
CatchGen< int * > sublists (CatchGen< int > &&gen, int numSamps, int excluded)
 
CatchGen< int * > sublists (CatchGen< int > &&gen, int sublen)
 
CatchGen< int * > sublists (int *list, int len, int sublen)
 
ComplexMatrix2 toComplexMatrix2 (QMatrix qm)
 
ComplexMatrix4 toComplexMatrix4 (QMatrix qm)
 
void toComplexMatrixN (QMatrix qm, ComplexMatrixN cm)
 
QMatrix toDiagonalQMatrix (QVector vec)
 
QMatrix toQMatrix (CompMatr src)
 
QMatrix toQMatrix (CompMatr1 src)
 
QMatrix toQMatrix (CompMatr2 src)
 
QMatrix toQMatrix (DiagMatr matr)
 
QMatrix toQMatrix (FullStateDiagMatr matr)
 
QMatrix toQMatrix (PauliHamil hamil)
 
QMatrix toQMatrix (PauliStrSum sum)
 
QMatrix toQMatrix (qreal *coeffs, pauliOpType *paulis, int numQubits, int numTerms)
 
QMatrix toQMatrix (Qureg qureg)
 
void toQureg (Qureg qureg, QMatrix mat)
 
void toQureg (Qureg qureg, QVector vec)
 
QVector toQVector (DiagMatr op)
 
QVector toQVector (FullStateDiagMatr op)
 
QVector toQVector (Qureg qureg)
 
void writeToFileSynch (char *fn, const string &contents)
 

Detailed Description

Utilities for testing QuEST's deprecated v3 API functions.

Typedef Documentation

◆ QMatrix

typedef vector<vector<qcomp> > QMatrix

A complex square matrix. Should be initialised with getZeroMatrix(). These have all the natural linear-algebra operator overloads, including left-multiplication onto a vector.

This data-structure is not partitioned between nodes in distributed mode. That is, every node has a complete copy, allowing for safe comparisons.

Author
Tyson Jones

Definition at line 60 of file test_utilities.hpp.

◆ QVector

typedef vector<qcomp> QVector

A complex vector, which can be zero-initialised with QVector(numAmps). These have all the natural linear-algebra operator overloads.

This data-structure is not partitioned between nodes in distributed mode. That is, every node has a complete copy, allowing for safe comparisons.

Author
Tyson Jones

Definition at line 71 of file test_utilities.hpp.

Function Documentation

◆ applyReferenceMatrix() [1/4]

void applyReferenceMatrix ( QMatrix & state,
int * ctrls,
int numCtrls,
int * targs,
int numTargs,
QMatrix op )

Modifies the density matrix state to be the result of left-multiplying the multi-target operator matrix op, with the specified control and target qubits (in ctrls and targs respectively). Here, op is treated like a simple matrix and is hence left-multiplied onto the state once.

Author
Tyson Jones

Definition at line 994 of file test_utilities.cpp.

996 {
997 // for density matrices, op is left-multiplied only
998 int numQubits = calcLog2(state.size());
999 QMatrix leftOp = getFullOperatorMatrix(ctrls, numCtrls, targs, numTargs, op, numQubits);
1000 state = leftOp * state;
1001}
QMatrix getFullOperatorMatrix(int *ctrls, int numCtrls, int *targs, int numTargs, QMatrix op, int numQubits)
unsigned int calcLog2(long unsigned int res)
vector< vector< qcomp > > QMatrix

◆ applyReferenceMatrix() [2/4]

void applyReferenceMatrix ( QMatrix & state,
int * targs,
int numTargs,
QMatrix op )

Modifies the density matrix state to be the result of left-multiplying the multi-target operator matrix op, with the target qubits (assuming no control qubits). Here, op is treated like a simple matrix and is hence left-multiplied onto the state once.

Author
Tyson Jones

Definition at line 1002 of file test_utilities.cpp.

1004 {
1005 applyReferenceMatrix(state, NULL, 0, targs, numTargs, op);
1006}
void applyReferenceMatrix(QVector &state, int *ctrls, int numCtrls, int *targs, int numTargs, QMatrix op)

◆ applyReferenceMatrix() [3/4]

void applyReferenceMatrix ( QVector & state,
int * ctrls,
int numCtrls,
int * targs,
int numTargs,
QMatrix op )

Modifies the state-vector state to be the result of left-multiplying the multi-target operator matrix op, with the specified control and target qubits (in ctrls and targs respectively). This is an alias of applyReferenceOp(), since operators are always left-multiplied as matrices onto state-vectors.

Author
Tyson Jones

Definition at line 982 of file test_utilities.cpp.

984 {
985 // for state-vectors, the op is always just left-multiplied
986 applyReferenceOp(state, ctrls, numCtrls, targs, numTargs, op);
987}
void applyReferenceOp(QVector &state, int *ctrls, int numCtrls, int *targs, int numTargs, QMatrix op)

Referenced by applyReferenceMatrix(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ applyReferenceMatrix() [4/4]

void applyReferenceMatrix ( QVector & state,
int * targs,
int numTargs,
QMatrix op )

Modifies the state-vector state to be the result of left-multiplying the multi-target operator matrix op, with the specified target qubits (assuming no control qubits). T

Author
Tyson Jones

Definition at line 988 of file test_utilities.cpp.

990 {
991 // for state-vectors, the op is always just left-multiplied
992 applyReferenceOp(state, targs, numTargs, op);
993}

◆ applyReferenceOp() [1/16]

void applyReferenceOp ( QMatrix & state,
int * ctrls,
int numCtrls,
int * targs,
int numTargs,
QMatrix op )

Modifies the density matrix state to be the result of applying the multi-target operator matrix op, with the specified control and target qubits (in ctrls and targs respectively). This updates state under

\[ \text{state} \to \text{op} \, \text{state} \, \text{op}^\dagger \]

even if op is not unitary (which is useful for applying Kraus operators).

op must be a 2^numTargs-by-2^numTargs matrix. Furthermore, every element of targs must not appear in ctrls (and vice-versa), though this is not explicitly checked. Elements of targs and ctrls should be unique.

This function works by computing getFullOperatorMatrix() from the given arguments, and left-multipling it to state, then right-multiplying its conjugate transpose onto the result.

Author
Tyson Jones

Definition at line 925 of file test_utilities.cpp.

927 {
928 int numQubits = calcLog2(state.size());
929 QMatrix leftOp = getFullOperatorMatrix(ctrls, numCtrls, targs, numTargs, op, numQubits);
930 QMatrix rightOp = getConjugateTranspose(leftOp);
931 state = leftOp * state * rightOp;
932}
QMatrix getConjugateTranspose(QMatrix a)

◆ applyReferenceOp() [2/16]

void applyReferenceOp ( QMatrix & state,
int * ctrls,
int numCtrls,
int targ1,
int targ2,
QMatrix op )

Modifies the density matrix state to be the result of applying the two-target operator matrix op, with the specified control qubits (in ctrls). This updates state under

\[ \text{state} \to \text{op} \, \text{state} \, \text{op}^\dagger \]

even if op is not unitary (which is useful for applying Kraus operators).

op must be a 4-by-4 matrix. Both targ1 and targ2 must not appear in ctrls, though this is not explicitly checked. Elements of ctrls, and targ1 and targ2, should be unique.

This function works by computing getFullOperatorMatrix() from the given arguments, and left-multipling it to state, then right-multiplying its conjugate transpose onto the result.

Author
Tyson Jones

Definition at line 933 of file test_utilities.cpp.

935 {
936 int targs[2] = {targ1, targ2};
937 applyReferenceOp(state, ctrls, numCtrls, targs, 2, op);
938}

◆ applyReferenceOp() [3/16]

void applyReferenceOp ( QMatrix & state,
int * ctrls,
int numCtrls,
int target,
QMatrix op )

Modifies the density matrix state to be the result of applying the single-target operator matrix op, with the specified control qubits (in ctrls). This updates state under

\[ \text{state} \to \text{op} \, \text{state} \, \text{op}^\dagger \]

even if op is not unitary (which is useful for applying Kraus operators).

op must be a 2-by-2 matrix. target must not appear in ctrls, though this is not explicitly checked.

This function works by computing getFullOperatorMatrix() from the given arguments, and left-multipling it to state, then right-multiplying its conjugate transpose onto the result.

Author
Tyson Jones

Definition at line 939 of file test_utilities.cpp.

941 {
942 int targs[1] = {target};
943 applyReferenceOp(state, ctrls, numCtrls, targs, 1, op);
944}

◆ applyReferenceOp() [4/16]

void applyReferenceOp ( QMatrix & state,
int * targs,
int numTargs,
QMatrix op )

Modifies the density matrix state to be the result of applying the multi-target operator matrix op, with no control qubits. This updates state under

\[ \text{state} \to \text{op} \, \text{state} \, \text{op}^\dagger \]

even if op is not unitary (which is useful for applying Kraus operators).

op must be a 2^numTargs-by-2^numTargs matrix. Every element in targs should be unique, though this is not explicitly checked.

This function works by computing getFullOperatorMatrix() from the given arguments, and left-multipling it to state, then right-multiplying its conjugate transpose onto the result.

Author
Tyson Jones

Definition at line 945 of file test_utilities.cpp.

947 {
948 applyReferenceOp(state, NULL, 0, targs, numTargs, op);
949}

◆ applyReferenceOp() [5/16]

void applyReferenceOp ( QMatrix & state,
int ctrl,
int * targs,
int numTargs,
QMatrix op )

Modifies the density matrix state to be the result of applying the multi-target operator matrix op, with a single control qubit ctrl. This updates state under

\[ \text{state} \to \text{op} \, \text{state} \, \text{op}^\dagger \]

even if op is not unitary (which is useful for applying Kraus operators).

op must be a 2^numTargs-by-2^numTargs matrix, and ctrl must not appear in targs (though this is not explicitly checked).

This function works by computing getFullOperatorMatrix() from the given arguments, and left-multipling it to state, then right-multiplying its conjugate transpose onto the result.

Author
Tyson Jones

Definition at line 957 of file test_utilities.cpp.

959 {
960 int ctrls[1] = {ctrl};
961 applyReferenceOp(state, ctrls, 1, targs, numTargs, op);
962}

◆ applyReferenceOp() [6/16]

void applyReferenceOp ( QMatrix & state,
int ctrl,
int targ,
QMatrix op )

Modifies the density matrix state to be the result of applying the single-control single-target operator matrix op. This updates state under

\[ \text{state} \to \text{op} \, \text{state} \, \text{op}^\dagger \]

even if op is not unitary (which is useful for applying Kraus operators).

op must be a 2-by-2 matrix, and ctrl and targ should be different.

This function works by computing getFullOperatorMatrix() from the given arguments, and left-multipling it to state, then right-multiplying its conjugate transpose onto the result.

Author
Tyson Jones

Definition at line 950 of file test_utilities.cpp.

952 {
953 int ctrls[1] = {ctrl};
954 int targs[1] = {targ};
955 applyReferenceOp(state, ctrls, 1, targs, 1, op);
956}

◆ applyReferenceOp() [7/16]

void applyReferenceOp ( QMatrix & state,
int ctrl,
int targ1,
int targ2,
QMatrix op )

Modifies the density matrix state to be the result of applying the two-target operator matrix op, with a single control qubit ctrl. This updates state under

\[ \text{state} \to \text{op} \, \text{state} \, \text{op}^\dagger \]

even if op is not unitary (which is useful for applying Kraus operators).

op must be a 4-by-4 matrix, and ctrl, targ1 and targ2 must be unique.

This function works by computing getFullOperatorMatrix() from the given arguments, and left-multipling it to state, then right-multiplying its conjugate transpose onto the result.

Author
Tyson Jones

Definition at line 963 of file test_utilities.cpp.

965 {
966 int ctrls[1] = {ctrl};
967 int targs[2] = {targ1, targ2};
968 applyReferenceOp(state, ctrls, 1, targs, 2, op);
969}

◆ applyReferenceOp() [8/16]

void applyReferenceOp ( QMatrix & state,
int targ,
QMatrix op )

Modifies the density matrix state to be the result of applying the single-target operator matrix op, with no control qubit. This updates state under

\[ \text{state} \to \text{op} \, \text{state} \, \text{op}^\dagger \]

even if op is not unitary (which is useful for applying Kraus operators).

op must be a 2-by-2 matrix.

This function works by computing getFullOperatorMatrix() from the given arguments, and left-multipling it to state, then right-multiplying its conjugate transpose onto the result.

Author
Tyson Jones

Definition at line 970 of file test_utilities.cpp.

972 {
973 int targs[1] = {targ};
974 applyReferenceOp(state, NULL, 0, targs, 1, op);
975}

◆ applyReferenceOp() [9/16]

void applyReferenceOp ( QVector & state,
int * ctrls,
int numCtrls,
int * targs,
int numTargs,
QMatrix op )

Modifies the state-vector state to be the result of applying the multi-target operator matrix op, with the specified control and target qubits (in ctrls and targs respectively). This updates state under

\[ \text{state} \to \text{op} \, \text{state} \]

even if op is not unitary.

op must be a 2^numTargs-by-2^numTargs matrix. Furthermore, every element of targs must not appear in ctrls (and vice-versa), though this is not explicitly checked. Elements of targs and ctrls should be unique.

This function works by computing getFullOperatorMatrix() from the given arguments, and left-multiplying it onto state.

Author
Tyson Jones

Definition at line 869 of file test_utilities.cpp.

871 {
872 int numQubits = calcLog2(state.size());
873 QMatrix fullOp = getFullOperatorMatrix(ctrls, numCtrls, targs, numTargs, op, numQubits);
874 state = fullOp * state;
875}

Referenced by applyReferenceMatrix(), applyReferenceMatrix(), applyReferenceOp(), applyReferenceOp(), applyReferenceOp(), applyReferenceOp(), applyReferenceOp(), applyReferenceOp(), applyReferenceOp(), applyReferenceOp(), applyReferenceOp(), applyReferenceOp(), applyReferenceOp(), applyReferenceOp(), applyReferenceOp(), applyReferenceOp(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ applyReferenceOp() [10/16]

void applyReferenceOp ( QVector & state,
int * ctrls,
int numCtrls,
int targ1,
int targ2,
QMatrix op )

Modifies the state-vector state to be the result of applying the two-target operator matrix op, with the specified control qubits (in ctrls). This updates state under

\[ \text{state} \to \text{op} \, \text{state} \]

even if op is not unitary.

op must be a 4-by-4 matrix. Furthermore, ctrls, targ1 and targ2 should all be unique.

This function works by computing getFullOperatorMatrix() from the given arguments, and left-multiplying it onto state.

Author
Tyson Jones

Definition at line 876 of file test_utilities.cpp.

878 {
879 int targs[2] = {targ1, targ2};
880 applyReferenceOp(state, ctrls, numCtrls, targs, 2, op);
881}

◆ applyReferenceOp() [11/16]

void applyReferenceOp ( QVector & state,
int * ctrls,
int numCtrls,
int target,
QMatrix op )

Modifies the state-vector state to be the result of applying the single-target operator matrix op, with the specified control qubits (in ctrls). This updates state under

\[ \text{state} \to \text{op} \, \text{state} \]

even if op is not unitary.

op must be a 2-by-2 matrix. Furthermore, elements in ctrls and target should all be unique.

This function works by computing getFullOperatorMatrix() from the given arguments, and left-multiplying it onto state.

Author
Tyson Jones

Definition at line 882 of file test_utilities.cpp.

884 {
885 int targs[1] = {target};
886 applyReferenceOp(state, ctrls, numCtrls, targs, 1, op);
887}

◆ applyReferenceOp() [12/16]

void applyReferenceOp ( QVector & state,
int * targs,
int numTargs,
QMatrix op )

Modifies the state-vector state to be the result of applying the multi-target operator matrix op, with no contorl qubits. This updates state under

\[ \text{state} \to \text{op} \, \text{state} \]

even if op is not unitary.

op must be a 2^numTargs-by-2^numTargs matrix. Furthermore, elements in targs should be unique.

This function works by computing getFullOperatorMatrix() from the given arguments, and left-multiplying it onto state.

Author
Tyson Jones

Definition at line 888 of file test_utilities.cpp.

890 {
891 applyReferenceOp(state, NULL, 0, targs, numTargs, op);
892}

◆ applyReferenceOp() [13/16]

void applyReferenceOp ( QVector & state,
int ctrl,
int * targs,
int numTargs,
QMatrix op )

Modifies the state-vector state to be the result of applying the multi-target operator matrix op, with a single control qubit (ctrl) This updates state under

\[ \text{state} \to \text{op} \, \text{state} \]

even if op is not unitary.

op must be a 2^numTargs-by-2^numTargs matrix. Furthermore, elements in targs and ctrl should be unique.

This function works by computing getFullOperatorMatrix() from the given arguments, and left-multiplying it onto state.

Author
Tyson Jones

Definition at line 900 of file test_utilities.cpp.

902 {
903 int ctrls[1] = {ctrl};
904 applyReferenceOp(state, ctrls, 1, targs, numTargs, op);
905}

◆ applyReferenceOp() [14/16]

void applyReferenceOp ( QVector & state,
int ctrl,
int targ,
QMatrix op )

Modifies the state-vector state to be the result of applying the single-target operator matrix op, with a single control qubit (ctrl). This updates state under

\[ \text{state} \to \text{op} \, \text{state} \]

even if op is not unitary.

op must be a 2-by-2 matrix. Furthermore, ctrl and targ must be different.

This function works by computing getFullOperatorMatrix() from the given arguments, and left-multiplying it onto state.

Author
Tyson Jones

Definition at line 893 of file test_utilities.cpp.

895 {
896 int ctrls[1] = {ctrl};
897 int targs[1] = {targ};
898 applyReferenceOp(state, ctrls, 1, targs, 1, op);
899}

◆ applyReferenceOp() [15/16]

void applyReferenceOp ( QVector & state,
int ctrl,
int targ1,
int targ2,
QMatrix op )

Modifies the state-vector state to be the result of applying the two-target operator matrix op, with a single control qubit (ctrl). This updates state under

\[ \text{state} \to \text{op} \, \text{state} \]

even if op is not unitary.

op must be a 4-by-4 matrix. Furthermore, ctrl, targ1 and targ2 should all be unique.

This function works by computing getFullOperatorMatrix() from the given arguments, and left-multiplying it onto state.

Author
Tyson Jones

Definition at line 906 of file test_utilities.cpp.

908 {
909 int ctrls[1] = {ctrl};
910 int targs[2] = {targ1, targ2};
911 applyReferenceOp(state, ctrls, 1, targs, 2, op);
912}

◆ applyReferenceOp() [16/16]

void applyReferenceOp ( QVector & state,
int targ,
QMatrix op )

Modifies the state-vector state to be the result of applying the single-target operator matrix op, with no contorl qubits. This updates state under

\[ \text{state} \to \text{op} \, \text{state} \]

even if op is not unitary.

op must be a 2-by-2 matrix.

This function works by computing getFullOperatorMatrix() from the given arguments, and left-multiplying it onto state.

Author
Tyson Jones

Definition at line 913 of file test_utilities.cpp.

915 {
916 int targs[1] = {targ};
917 applyReferenceOp(state, NULL, 0, targs, 1, op);
918}

◆ areEqual() [1/10]

bool areEqual ( QMatrix a,
QMatrix b )

Returns true if the absolute value of the difference between every amplitude in matrices a and b is less than REAL_EPS.

Author
Tyson Jones

Definition at line 521 of file test_utilities.cpp.

521 {
522 DEMAND( a.size() == b.size() );
523
524 for (size_t i=0; i<a.size(); i++)
525 for (size_t j=0; j<b.size(); j++)
526 if (abs(a[i][j] - b[i][j]) > REAL_EPS)
527 return false;
528 return true;
529}

◆ areEqual() [2/10]

bool areEqual ( Qureg qureg,
QMatrix matr )

Performs a hardware-agnostic comparison of density-matrix qureg to matr, checking whether the difference between the real and imaginary components of every amplitude is smaller than the QuEST_PREC-specific REAL_EPS (defined in QuEST_precision) precision. This function demands qureg is a density matrix, and that qureg and matr have equal dimensions.

In GPU mode, this function involves a GPU to CPU memory copy overhead. In distributed mode, it involves a all-to-all single-int broadcast.

Author
Tyson Jones

Definition at line 1136 of file test_utilities.cpp.

1136 {
1137 return areEqual(qureg, matr, REAL_EPS);
1138}
bool areEqual(QVector a, QVector b)

◆ areEqual() [3/10]

bool areEqual ( Qureg qureg,
QMatrix matr,
qreal precision )

Performs a hardware-agnostic comparison of density-matrix qureg to matr, checking whether the difference between the real and imaginary components of every amplitude is smaller than precision. This function demands qureg is a density matrix, and that qureg and matr have equal dimensions.

In GPU mode, this function involves a GPU to CPU memory copy overhead. In distributed mode, it involves a all-to-all single-int broadcast.

Author
Tyson Jones

Definition at line 1077 of file test_utilities.cpp.

1077 {
1078 DEMAND( qureg.isDensityMatrix );
1079 DEMAND( (long long int) (matr.size()*matr.size()) == qureg.numAmps );
1080
1081 // ensure local qureg amps is up to date
1082 copyStateFromGPU(qureg);
1083 syncQuESTEnv();
1084
1085 // the starting index in vec of this node's qureg partition.
1086 long long int startInd = qureg.rank * qureg.numAmpsPerNode;
1087 long long int globalInd, row, col, i;
1088 int ampsAgree;
1089
1090 // compare each of this node's amplitude to the corresponding matr sub-matrix
1091 for (i=0; i<qureg.numAmpsPerNode; i++) {
1092 globalInd = startInd + i;
1093 row = globalInd % matr.size();
1094 col = globalInd / matr.size();
1095
1096 qreal realDif = absReal(real(qureg.cpuAmps[i]) - real(matr[row][col]));
1097 qreal imagDif = absReal(imag(qureg.cpuAmps[i]) - imag(matr[row][col]));
1098 ampsAgree = (realDif < precision && imagDif < precision);
1099
1100 // DEBUG
1101 if (!ampsAgree) {
1102 char buff[200];
1103 snprintf(buff, 200, "[msg from utilities.cpp] node %d has a disagreement at %lld of (%s) + i(%s):\n\t[qureg] %s + i(%s) VS [ref] %s + i(%s)\n",
1104 qureg.rank, startInd+i,
1107 printf(buff,
1108 realDif, imagDif,
1109 real(qureg.cpuAmps[i]), imag(qureg.cpuAmps[i]),
1110 real(matr[row][col]), imag(matr[row][col]));
1111 }
1112
1113 // break loop as soon as amplitudes disagree
1114 if (!ampsAgree)
1115 break;
1116
1117 /* TODO:
1118 * of the nodes which disagree, the lowest-rank should send its
1119 * disagreeing (i, row, col, stateVec[i]) to rank 0 which should
1120 * report it immediately (before the impending DEMAND failure)
1121 * using FAIL_CHECK, so users can determine nature of disagreement
1122 * (e.g. numerical precision).
1123 * Note FAIL_CHECK accepts << like cout, e.g.
1124 * FAIL_CHECK( "Amp at (" << row << ", " << col ") disagreed" );
1125 */
1126 }
1127
1128 // if one node's partition wasn't equal, all-nodes must report not-equal
1129 int allAmpsAgree = ampsAgree;
1130#if COMPILE_MPI
1131 MPI_Allreduce(&ampsAgree, &allAmpsAgree, 1, MPI_INT, MPI_LAND, MPI_COMM_WORLD);
1132#endif
1133
1134 return allAmpsAgree;
1135}
void syncQuESTEnv()
const char * QREAL_FORMAT_SPECIFIER
Definition precision.h:161

◆ areEqual() [4/10]

bool areEqual ( Qureg qureg,
QVector vec )

Performs a hardware-agnostic comparison of state-vector qureg to vec, checking whether the difference between the real and imaginary components of every amplitude is smaller than the QuEST_PREC-specific REAL_EPS (defined in QuEST_precision) precision. This function demands qureg is a state-vector, and that qureg and vec have the same number of amplitudes.

In GPU mode, this function involves a GPU to CPU memory copy overhead. In distributed mode, it involves a all-to-all single-int broadcast.

Author
Tyson Jones

Definition at line 1073 of file test_utilities.cpp.

1073 {
1074 return areEqual(qureg, vec, REAL_EPS);
1075}

◆ areEqual() [5/10]

bool areEqual ( Qureg qureg,
QVector vec,
qreal precision )

Performs a hardware-agnostic comparison of state-vector qureg to vec, checking whether the difference between the real and imaginary components of every amplitude is smaller than precision. This function demands qureg is a state-vector, and that qureg and vec have the same number of amplitudes.

In GPU mode, this function involves a GPU to CPU memory copy overhead. In distributed mode, it involves a all-to-all single-int broadcast.

Author
Tyson Jones

Definition at line 1033 of file test_utilities.cpp.

1033 {
1034 DEMAND( !qureg.isDensityMatrix );
1035 DEMAND( (int) vec.size() == qureg.numAmps );
1036
1037 copyStateFromGPU(qureg);
1038 syncQuESTEnv();
1039
1040 // the starting index in vec of this node's qureg partition.
1041 long long int startInd = qureg.rank * qureg.numAmpsPerNode;
1042
1043 int ampsAgree = 1;
1044 for (long long int i=0; i<qureg.numAmpsPerNode; i++) {
1045 qcomp dif = (qureg.cpuAmps[i] - vec[startInd+i]);
1046
1047 if (absComp(dif) > precision) {
1048 ampsAgree = 0;
1049
1050 // debug
1051 char buff[200];
1052 snprintf(buff, 200, "Disagreement at %lld of (%s) + i(%s):\n\t%s + i(%s) VS %s + i(%s)\n",
1053 startInd+i,
1056 printf(buff,
1057 real(dif), imag(dif),
1058 real(qureg.cpuAmps[i]), imag(qureg.cpuAmps[i]),
1059 real(vec[startInd+i]), imag(vec[startInd+i]));
1060
1061 break;
1062 }
1063 }
1064
1065 // if one node's partition wasn't equal, all-nodes must report not-equal
1066 int allAmpsAgree = ampsAgree;
1067#if COMPILE_MPI
1068 MPI_Allreduce(&ampsAgree, &allAmpsAgree, 1, MPI_INT, MPI_LAND, MPI_COMM_WORLD);
1069#endif
1070
1071 return allAmpsAgree;
1072}

◆ areEqual() [6/10]

bool areEqual ( Qureg qureg1,
Qureg qureg2 )

Performs a hardware-agnostic comparison of the given quregs, checking whether the difference between the real and imaginary components of every amplitude is smaller than the QuEST_PREC-specific REAL_EPS (defined in QuEST_precision) precision. This function demands that qureg1 and qureg2 are of the same type (i.e. both state-vectors or both density matrices), and of an equal number of qubits.

In GPU mode, this function involves a GPU to CPU memory copy overhead. In distributed mode, it involves a all-to-all single-int broadcast.

Author
Tyson Jones

Definition at line 1029 of file test_utilities.cpp.

1029 {
1030 return areEqual(qureg1, qureg2, REAL_EPS);
1031}

◆ areEqual() [7/10]

bool areEqual ( Qureg qureg1,
Qureg qureg2,
qreal precision )

Performs a hardware-agnostic comparison of the given quregs, checking whether the difference between the real and imaginary components of every amplitude is smaller than precision. This function demands that qureg1 and qureg2 are of the same type (i.e. both state-vectors or both density matrices), and of an equal number of qubits.

In GPU mode, this function involves a GPU to CPU memory copy overhead. In distributed mode, it involves a all-to-all single-int broadcast.

Author
Tyson Jones

Definition at line 1008 of file test_utilities.cpp.

1008 {
1009 DEMAND( qureg1.isDensityMatrix == qureg2.isDensityMatrix );
1010 DEMAND( qureg1.numAmps == qureg2.numAmps );
1011
1012 copyStateFromGPU(qureg1);
1013 copyStateFromGPU(qureg2);
1014 syncQuESTEnv();
1015
1016 // loop terminates when areEqual = 0
1017 int ampsAgree = 1;
1018 for (long long int i=0; ampsAgree && i<qureg1.numAmpsPerNode; i++)
1019 ampsAgree = absComp(qureg1.cpuAmps[i] - qureg2.cpuAmps[i]) < precision;
1020
1021 // if one node's partition wasn't equal, all-nodes must report not-equal
1022 int allAmpsAgree = ampsAgree;
1023#if COMPILE_MPI
1024 MPI_Allreduce(&ampsAgree, &allAmpsAgree, 1, MPI_INT, MPI_LAND, MPI_COMM_WORLD);
1025#endif
1026
1027 return allAmpsAgree;
1028}

◆ areEqual() [8/10]

bool areEqual ( QVector a,
QVector b )

Returns true if the absolute value of the difference between every amplitude in vectors a and b is less than REAL_EPS.

Author
Tyson Jones

Definition at line 512 of file test_utilities.cpp.

512 {
513 DEMAND( a.size() == b.size() );
514
515 for (size_t i=0; i<a.size(); i++)
516 if (abs(a[i] - b[i]) > REAL_EPS)
517 return false;
518 return true;
519}

Referenced by areEqual(), areEqual(), areEqual(), assertQuregAndRefInDebugState(), assertQuregAndRefInDebugState(), getRandomKrausMap(), getRandomUnitary(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ areEqual() [9/10]

bool areEqual ( QVector vec,
qreal * reals )

Returns true if the absolute value of the difference between every element in vec (which must be strictly real) and those implied by reals, is less than REAL_EPS.

Author
Tyson Jones

Definition at line 1154 of file test_utilities.cpp.

1154 {
1155 for (size_t i=0; i<vec.size(); i++) {
1156 DEMAND( imag(vec[i]) == 0. );
1157
1158 qreal dif = abs(real(vec[i]) - reals[i]);
1159 if (dif > REAL_EPS)
1160 return false;
1161 }
1162 return true;
1163}

◆ areEqual() [10/10]

bool areEqual ( QVector vec,
qreal * reals,
qreal * imags )

Returns true if the absolute value of the difference between every element in vec and those implied by reals and imags, is less than REAL_EPS.

Author
Tyson Jones

Definition at line 1140 of file test_utilities.cpp.

1140 {
1141
1142 qreal dif;
1143 for (size_t i=0; i<vec.size(); i++) {
1144 dif = absReal(real(vec[i]) - reals[i]);
1145 if (dif > REAL_EPS)
1146 return false;
1147 dif = absReal(imag(vec[i]) - imags[i]);
1148 if (dif > REAL_EPS)
1149 return false;
1150 }
1151 return true;
1152}

◆ assertQuregAndRefInDebugState() [1/2]

void assertQuregAndRefInDebugState ( Qureg qureg,
QMatrix ref )

Asserts the given density qureg and reference agree, and are properly initialised in the debug state. Assertion uses the DEMAND() macro, calling Catch2's FAIL() if unsatisfied, so does not contribute toward unit test statistics. This should be called within every PREPARE_TEST macro, to ensure that the test states themselves are initially correct, and do not accidentally agree by (e.g.) being all-zero.

Author
Tyson Jones

Definition at line 230 of file test_utilities.cpp.

230 {
231 DEMAND( qureg.isDensityMatrix == 1 );
232 DEMAND( (1LL << qureg.numQubits) == (long long int) ref.size() );
233
234 // assert ref is in the (column-wise) debug state (else initDebugState failed)
235 size_t i = 0;
236 for (size_t c=0; c<ref.size(); c++) {
237 for (size_t r=0; r<ref.size(); r++) {
238 qcomp val = qcomp(.2*i, .2*i+.1);
239 DEMAND( abs(ref[r][c] - val) < REAL_EPS );
240 i++;
241 }
242 }
243
244 // check qureg and ref agree
245 DEMAND( areEqual(qureg, ref) );
246}

◆ assertQuregAndRefInDebugState() [2/2]

void assertQuregAndRefInDebugState ( Qureg qureg,
QVector ref )

Asserts the given statevector qureg and reference agree, and are properly initialised in the debug state. Assertion uses the DEMAND() macro, calling Catch2's FAIL() if unsatisfied, so does not contribute toward unit test statistics. This should be called within every PREPARE_TEST macro, to ensure that the test states themselves are initially correct, and do not accidentally agree by (e.g.) being all-zero.

Author
Tyson Jones

Definition at line 216 of file test_utilities.cpp.

216 {
217 DEMAND( qureg.isDensityMatrix == 0 );
218 DEMAND( qureg.numAmps == (long long int) ref.size() );
219
220 // assert ref is in the debug state (else initDebugState failed)
221 for (size_t i=0; i<ref.size(); i++) {
222 qcomp val = qcomp(.2*i, .2*i+.1);
223 DEMAND( abs(ref[i] - val) < REAL_EPS );
224 }
225
226 // check qureg and ref agree
227 DEMAND( areEqual(qureg, ref) );
228}

◆ bitsets()

CatchGen< int * > bitsets ( int numBits)

Returns a Catch2 generator of every numBits-length bit-set, in increasing lexographic order, where left-most (zero index) bit is treated as LEAST significant (opposite typical convention). Note that the produced bitset must not be modified during generation.

This function can be used like

int* bits = GENERATE( bitsets(3) );

to produce {0,0,0}, {1,0,0}, {0,1,0}, {1,1,0}, {0,0,1}, {1,0,1}, {0,1,1}, {1,1,1}.

Author
Tyson Jones

Definition at line 1720 of file test_utilities.cpp.

1720 {
1721 return Catch::Generators::GeneratorWrapper<int*>(
1722 Catch::Detail::make_unique<SequenceGenerator<int>>(1, numBits));
1723}

Referenced by TEST_CASE().

◆ calcLog2()

unsigned int calcLog2 ( long unsigned int res)

Returns log2 of numbers which must be gauranteed to be 2^n

Author
Tyson Jones

Definition at line 485 of file test_utilities.cpp.

485 {
486 unsigned int n = 0;
487 while (res >>= 1)
488 n++;
489 return n;
490}

Referenced by applyReferenceMatrix(), applyReferenceOp(), applyReferenceOp(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ deleteFilesWithPrefixSynch()

void deleteFilesWithPrefixSynch ( char * prefix)

Deletes all files with filename starting with prefix. In distributed mode, the master node deletes while the other nodes wait until complete.

Author
Tyson Jones

Definition at line 1540 of file test_utilities.cpp.

1540 {
1541
1542 // master node deletes all files
1543 if (getQuESTEnv().rank == 0) {
1544 char cmd[200];
1545 snprintf(cmd, 200, "exec rm %s*", prefix);
1546 system(cmd);
1547 }
1548
1549 // other nodes wait
1550 syncQuESTEnv();
1551}
QuESTEnv getQuESTEnv()

◆ getConjugateTranspose()

QMatrix getConjugateTranspose ( QMatrix a)

Returns the conjugate transpose of the complex square matrix a

Author
Tyson Jones

Definition at line 291 of file linalg.cpp.

291 {
292 DEMAND( m.size() > 0 );
293
294 // unlike most functions which assume qmatrix
295 // is square, this one cheekily handles when
296 // 'm' is non-square, since necessary for
297 // computing partial traces
298
299 qmatrix out(m[0].size(), qvector(m.size()));
300
301 for (size_t r=0; r<out.size(); r++)
302 for (size_t c=0; c<out[0].size(); c++)
303 out[r][c] = std::conj(m[c][r]);
304
305 return out;
306}

Referenced by applyReferenceOp(), getRandomKrausMap(), getRandomUnitary(), TEST_CASE(), and TEST_CASE().

◆ getDFT() [1/2]

QVector getDFT ( QVector in)

Returns the discrete fourier transform of vector in

Author
Tyson Jones

Definition at line 792 of file test_utilities.cpp.

792 {
793 REQUIRE( in.size() > 0 );
794
795 size_t dim = in.size();
796 qreal ampFac = 1 / sqrt( dim );
797 qreal phaseFac = 2 * M_PI / dim;
798
799 QVector dftVec = QVector(dim);
800
801 for (size_t x=0; x<dim; x++) {
802 dftVec[x] = 0;
803 for (size_t y=0; y<dim; y++)
804 dftVec[x] += expI(phaseFac * x * y) * in[y];
805 dftVec[x] *= ampFac;
806 }
807 return dftVec;
808}
vector< qcomp > QVector

Referenced by TEST_CASE(), and TEST_CASE().

◆ getDFT() [2/2]

QVector getDFT ( QVector in,
int * targs,
int numTargs )

Returns the discrete fourier transform of a sub-partition of the vector in.

Author
Tyson Jones

Definition at line 840 of file test_utilities.cpp.

840 {
841
842 QVector out = QVector(in.size());
843 long long int inDim = (long long int) in.size();
844 long long int targDim = (1LL << numTargs);
845
846 for (long long int j=0; j<inDim; j++) {
847
848 // |j> = |x> (x) |...>, but mixed (not separated)
849 long long int x = getValueOfTargets(j, targs, numTargs);
850
851 for (long long int y=0; y<targDim; y++) {
852
853 // modifies sum_y |y> (x) ...
854 long long int outInd = getIndexOfTargetValues(j, targs, numTargs, y);
855
856 qcomp elem = (in[j] / sqrt(pow(2,numTargs))) * expI(2*M_PI * x * y / pow(2,numTargs));
857 out[outInd] += elem;
858 }
859 }
860
861 return out;
862}
long long int getValueOfTargets(long long int ind, int *targs, int numTargs)

◆ getExponentialOfDiagonalMatrix()

QMatrix getExponentialOfDiagonalMatrix ( QMatrix a)

Returns the matrix exponential of a diagonal, square, complex matrix. This method explicitly checks that the passed matrix a is diagonal.

Author
Tyson Jones

Definition at line 336 of file linalg.cpp.

336 {
337 DEMAND( isDiagonal(m) );
338
339 qmatrix out = getZeroMatrix(m.size());
340
341 for (size_t i=0; i<m.size(); i++)
342 out[i][i] = std::exp(m[i][i]);
343
344 return out;
345}
qmatrix getZeroMatrix(size_t dim)
Definition qmatrix.cpp:18

Referenced by TEST_CASE(), and TEST_CASE().

◆ getExponentialOfPauliMatrix()

QMatrix getExponentialOfPauliMatrix ( qreal angle,
QMatrix a )

Returns the matrix exponential of a kronecker product of pauli matrices (or of any involutory matrices), with exponent factor (-i angle / 2). This method will not explicitly check that the passed matrix a is kronecker product of involutory matrices, but will otherwise return an incorrect exponential.

Author
Tyson Jones

Definition at line 329 of file test_utilities.cpp.

329 {
330 QMatrix iden = getIdentityMatrix(a.size());
331 QMatrix expo = (cos(angle/2) * iden) + ((qcomp) -1i * sin(angle/2) * a);
332 return expo;
333}
QMatrix getIdentityMatrix(size_t dim)

◆ getFullOperatorMatrix()

QMatrix getFullOperatorMatrix ( int * ctrls,
int numCtrls,
int * targs,
int numTargs,
QMatrix op,
int numQubits )

Takes a 2^numTargs-by-2^numTargs matrix op and a returns a 2^numQubits-by-2^numQubits matrix where op is controlled on the given ctrls qubits. The union of {ctrls} and {targs} must be unique (though this is not explicitly checked), and every element must be >= 0 (not checked). The passed {ctrls} and {targs} arrays are unmodified.

This funciton works by first swapping {targs} and {ctrls} (via swap unitaries) to be strictly increasing {0,1,...}, building controlled(op), tensoring it to the full Hilbert space, and then 'unswapping'. The returned matrix has form: swap1 ... swapN . controlled(op) . swapN ... swap1

Author
Tyson Jones

Definition at line 417 of file test_utilities.cpp.

419 {
420 DEMAND( numCtrls >= 0 );
421 DEMAND( numTargs >= 0 );
422 DEMAND( numQubits >= (numCtrls+numTargs) );
423 DEMAND( op.size() == (1u << numTargs) );
424
425 // copy {ctrls} and {targs}to restore at end
426 vector<int> ctrlsCopy(ctrls, ctrls+numCtrls);
427 vector<int> targsCopy(targs, targs+numTargs);
428
429 // full-state matrix of qubit swaps
430 QMatrix swaps = getIdentityMatrix(1 << numQubits);
431 QMatrix unswaps = getIdentityMatrix(1 << numQubits);
432 QMatrix matr;
433
434 // swap targs to {0, ..., numTargs-1}
435 for (int i=0; i<numTargs; i++) {
436 if (i != targs[i]) {
437 matr = getSwapMatrix(i, targs[i], numQubits);
438 swaps = matr * swaps;
439 unswaps = unswaps * matr;
440
441 // even if this is the last targ, ctrls might still need updating
442 updateIndices(
443 i, targs[i], (i < numTargs-1)? &targs[i+1] : NULL,
444 numTargs-i-1, ctrls, numCtrls);
445 }
446 }
447
448 // swap ctrls to {numTargs, ..., numTargs+numCtrls-1}
449 for (int i=0; i<numCtrls; i++) {
450 int newInd = numTargs+i;
451 if (newInd != ctrls[i]) {
452 matr = getSwapMatrix(newInd, ctrls[i], numQubits);
453 swaps = matr * swaps;
454 unswaps = unswaps * matr;
455
456 // update remaining ctrls (if any exist)
457 if (i < numCtrls-1)
458 updateIndices(newInd, ctrls[i], NULL, 0, &ctrls[i+1], numCtrls-i-1);
459 }
460 }
461
462 // construct controlled-op matrix for qubits {0, ..., numCtrls+numTargs-1}
463 size_t dim = 1 << (numCtrls+numTargs);
464 QMatrix fullOp = getIdentityMatrix(dim);
465 setSubMatrix(fullOp, op, dim-op.size(), dim-op.size());
466
467 // create full-state controlled-op matrix (left-pad identities)
468 if (numQubits > numCtrls+numTargs) {
469 size_t pad = 1 << (numQubits - numCtrls - numTargs);
470 fullOp = getKroneckerProduct(getIdentityMatrix(pad), fullOp);
471 }
472
473 // apply swap to either side (to swap qubits back and forth)
474 fullOp = unswaps * fullOp * swaps;
475
476 // restore {ctrls and targs}
477 for (int i=0; i<numCtrls; i++)
478 ctrls[i] = ctrlsCopy[i];
479 for (int i=0; i<numTargs; i++)
480 targs[i] = targsCopy[i];
481
482 return fullOp;
483}
QMatrix getSwapMatrix(int qb1, int qb2, int numQb)
QVector getKroneckerProduct(QVector b, QVector a)
void setSubMatrix(QMatrix &dest, QMatrix sub, size_t r, size_t c)

Referenced by applyReferenceMatrix(), applyReferenceOp(), applyReferenceOp(), TEST_CASE(), and TEST_CASE().

◆ getIdentityMatrix()

QMatrix getIdentityMatrix ( size_t dim)

Returns a dim-by-dim identity matrix

Author
Tyson Jones

Definition at line 30 of file qmatrix.cpp.

30 {
31 DEMAND( dim >= 1 );
32
33 qmatrix out = getZeroMatrix(dim);
34
35 for (size_t i=0; i<dim; i++)
36 out[i][i] = 1;
37
38 return out;
39}

Referenced by getExponentialOfPauliMatrix(), getFullOperatorMatrix(), getRandomKrausMap(), getRandomUnitary(), getSwapMatrix(), and TEST_CASE().

◆ getKetBra()

QMatrix getKetBra ( QVector ket,
QVector bra )

Returns the matrix |ket><bra|, with ith-jth element ket(i) conj(bra(j)), since |ket><bra| = sum_i a_i|i> sum_j b_j* <j| = sum_{ij} a_i b_j* |i><j|. The dimensions of bra and ket must agree, and the returned square complex matrix has dimensions size(bra) x size(bra).

Author
Tyson Jones

Definition at line 274 of file test_utilities.cpp.

274 {
275 DEMAND( ket.size() == bra.size() );
276 QMatrix mat = getZeroMatrix(ket.size());
277
278 for (size_t r=0; r<ket.size(); r++)
279 for (size_t c=0; c<ket.size(); c++)
280 mat[r][c] = ket[r] * conj(bra[c]);
281 return mat;
282}

Referenced by getPureDensityMatrix(), getRandomDensityMatrix(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ getKroneckerProduct() [1/2]

QMatrix getKroneckerProduct ( QMatrix a,
QMatrix b )

Returns the kronecker product of a and b, where a and b are square but possibly differently-sized complex matrices.

Author
Tyson Jones

Definition at line 523 of file linalg.cpp.

523 {
524
525 // we permit the matrices to be non-square which is
526 // pretty cheeky (since qmatrix is assumed square with
527 // a 2^N dimension by most other functions), but is
528 // necessary for us to compute partial traces
529
530 size_t aRows = a.size();
531 size_t bRows = b.size();
532 size_t aCols = a[0].size();
533 size_t bCols = b[0].size();
534
535 qmatrix out(aRows * bRows, qvector(aCols * bCols));
536
537 for (size_t r=0; r<bRows; r++)
538 for (size_t c=0; c<bCols; c++)
539 for (size_t i=0; i<aRows; i++)
540 for (size_t j=0; j<aCols; j++)
541 out[r+bRows*i][c+bCols*j] = a[i][j] * b[r][c];
542
543 return out;
544}

Referenced by getSwapMatrix(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ getKroneckerProduct() [2/2]

QVector getKroneckerProduct ( QVector b,
QVector a )

Returns b (otimes) a. If b and a are state-vectors, the resulting kronecker product is the seperable state formed by joining the qubits in the state-vectors, producing |b>|a> (a is least significant)

Author
Tyson Jones

Definition at line 248 of file test_utilities.cpp.

248 {
249
250 QVector prod = QVector(a.size() * b.size());
251
252 for (size_t i=0; i<prod.size(); i++)
253 prod[i] = b[i / a.size()] * a[i % a.size()];
254
255 return prod;
256}

Referenced by getFullOperatorMatrix(), getSwapMatrix(), and toQMatrix().

◆ getMatrixDiagonal()

QVector getMatrixDiagonal ( QMatrix matr)

Returns the diagonal vector of the given matrix

Author
Tyson Jones

Definition at line 630 of file test_utilities.cpp.

630 {
631
632 QVector vec = QVector(matr.size());
633 for (size_t i=0; i<vec.size(); i++)
634 vec[i] = matr[i][i];
635
636 return vec;
637}

◆ getMixedDensityMatrix()

QMatrix getMixedDensityMatrix ( vector< qreal > probs,
vector< QVector > states )

Returns a mixed density matrix formed from mixing the given pure states, which are assumed normalised, but not necessarily orthogonal.

Author
Tyson Jones

Definition at line 780 of file test_utilities.cpp.

780 {
781 DEMAND( probs.size() == states.size() );
782 DEMAND( probs.size() >= 1 );
783
784 QMatrix matr = getZeroMatrix(states[0].size());
785
786 for (size_t i=0; i<probs.size(); i++)
787 matr += probs[i] * getPureDensityMatrix(states[i]);
788
789 return matr;
790}
QMatrix getPureDensityMatrix(QVector state)

Referenced by TEST_CASE(), and TEST_CASE().

◆ getNormalised()

QVector getNormalised ( QVector vec)

Returns an L2-normalised copy of vec, using Kahan summation for improved accuracy.

Author
Tyson Jones

Definition at line 557 of file test_utilities.cpp.

557 {
558
559 // compute the vec norm via Kahan summation to suppress numerical error
560 qreal norm = 0;
561 qreal y, t, c;
562 c = 0;
563
564 for (size_t i=0; i<vec.size(); i++) {
565 y = real(vec[i])*real(vec[i]) - c;
566 t = norm + y;
567 c = ( t - norm ) - y;
568 norm = t;
569
570 y = imag(vec[i])*imag(vec[i]) - c;
571 t = norm + y;
572 c = ( t - norm ) - y;
573 norm = t;
574 }
575
576 for (size_t i=0; i<vec.size(); i++)
577 vec[i] /= sqrt(norm);
578 return vec;
579}

Referenced by getRandomOrthonormalVectors(), and getRandomStateVector().

◆ getPureDensityMatrix()

QMatrix getPureDensityMatrix ( QVector state)

Returns a density matrix initialised into the given pure state

Author
Tyson Jones

Definition at line 620 of file test_utilities.cpp.

620 {
621 return getKetBra(state, state);
622}
QMatrix getKetBra(QVector ket, QVector bra)

Referenced by getMixedDensityMatrix(), getRandomPureDensityMatrix(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ getRandomComplex()

qcomp getRandomComplex ( )

Returns a random complex number within the square closing (-1-i) and (1+i), from a distribution uniformly randomising the individual real and imaginary components in their domains.

Author
Tyson Jones

Definition at line 107 of file random.cpp.

107 {
108 qreal re = getRandomReal(-1,1);
109 qreal im = getRandomReal(-1,1);
110 return qcomp(re, im);
111}
qreal getRandomReal(qreal min, qreal maxExcl)
Definition random.cpp:63

Referenced by getRandomQVector(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ getRandomDensityMatrix()

QMatrix getRandomDensityMatrix ( int numQb)

Returns a random numQb-by-numQb density matrix, from an undisclosed distribution, in a very mixed state. This function works by generating 2^numQb random pure states, and mixing them with random probabilities.

Author
Tyson Jones

Definition at line 308 of file random.cpp.

308 {
309 DEMAND( numQb > 0 );
310
311 // generate random probabilities to weight random pure states
312 int dim = getPow2(numQb);
313 vector<qreal> probs = getRandomProbabilities(dim);
314
315 // add random pure states
316 qmatrix dens = getZeroMatrix(dim);
317 for (int i=0; i<dim; i++) {
318 qvector pure = getRandomStateVector(numQb);
319 dens += probs[i] * getOuterProduct(pure, pure);
320 }
321
322 return dens;
323}
qvector getRandomStateVector(int numQb)
Definition random.cpp:296
vector< qreal > getRandomProbabilities(int numProbs)
Definition random.cpp:160

Referenced by TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ getRandomInt()

int getRandomInt ( int min,
int max )

Returns a random integer between min (inclusive) and max (exclusive), from the uniform distribution. Demands that max > min.

Author
Tyson Jones

Definition at line 90 of file random.cpp.

90 {
91 DEMAND( maxExcl >= min );
92
93 // permit this out of convenience
94 // for some test generators
95 if (min == maxExcl)
96 return min;
97
98 qreal r = std::floor(getRandomReal(min, maxExcl));
99 int out = static_cast<int>(r);
100
101 DEMAND( out >= min );
102 DEMAND( out < maxExcl );
103 return out;
104}

Referenced by setRandomPauliSum(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ getRandomKrausMap()

vector< QMatrix > getRandomKrausMap ( int numQb,
int numOps )

Returns a random Kraus map of #numOps 2^numQb-by-2^numQb operators, from an undisclosed distribution. Note this method is very simple and cannot generate all possible Kraus maps. It works by generating numOps random unitary matrices, and randomly re-normalising them, such that the sum of ops[j]^dagger ops[j] = 1

Author
Tyson Jones

Definition at line 405 of file random.cpp.

405 {
406 DEMAND( numOps >= 1 );
407
408 // generate random unitaries
409 vector<qmatrix> ops(numOps);
410 for (auto& u : ops)
411 u = getRandomUnitary(numQb);
412
413 // generate random weights
414 vector<qreal> weights(numOps);
415 for (auto& w : weights)
416 w = getRandomReal(0, 1);
417
418 // normalise random weights
419 qreal sum = 0;
420 for (auto& w : weights)
421 sum += w;
422 for (auto& w : weights)
423 w = std::sqrt(w/sum);
424
425 // normalise unitaries according to weights
426 for (int i=0; i<numOps; i++)
427 ops[i] *= weights[i];
428
429 DEMAND( isApproxCPTP(ops) );
430 return ops;
431}
qmatrix getRandomUnitary(int numQb)
Definition random.cpp:348

Referenced by TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ getRandomOrthonormalVectors()

vector< QVector > getRandomOrthonormalVectors ( int numQb,
int numStates )

Returns a list of random orthonormal complex vectors, from an undisclosed distribution.

Author
Tyson Jones

Definition at line 753 of file test_utilities.cpp.

753 {
754 DEMAND( numQb >= 1 );
755 DEMAND( numStates >= 1);
756
757 // set of orthonormal vectors
758 vector<QVector> vecs;
759
760 for (int n=0; n<numStates; n++) {
761
762 QVector vec = getRandomStateVector(numQb);
763
764 // orthogonalise by substracting projections of existing vectors
765 for (int m=0; m<n; m++) {
766 qcomp prod = vec * vecs[m];
767 vec -= (prod * vecs[m]);
768 }
769
770 // renormalise
771 vec = getNormalised(vec);
772
773 // add to orthonormal set
774 vecs.push_back(vec);
775 }
776
777 return vecs;
778}
QVector getNormalised(QVector vec)

◆ getRandomProbabilities()

vector< qreal > getRandomProbabilities ( int numProbs)

Returns a list of random real scalars, each in [0, 1], which sum to unity.

Author
Tyson Jones

Definition at line 160 of file random.cpp.

160 {
161
162 vector<qreal> probs(numProbs, 0);
163
164 // generate random unnormalised scalars
165 for (auto& p : probs)
166 p = getRandomReal(0, 1);
167
168 // normalise
169 qreal total = 0;
170 for (auto& p : probs)
171 total += p;
172
173 for (auto& p : probs)
174 p /= total;
175
176 return probs;
177}

Referenced by getRandomDensityMatrix(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ getRandomPureDensityMatrix()

QMatrix getRandomPureDensityMatrix ( int numQb)

Returns a random numQb-by-numQb density matrix, from an undisclosed distribution, which is pure (corresponds to a random state-vector)

Author
Tyson Jones

Definition at line 326 of file random.cpp.

326 {
327
328 qvector vec = getRandomStateVector(numQb);
329 qmatrix mat = getOuterProduct(vec, vec);
330 return mat;
331}

◆ getRandomQMatrix()

QMatrix getRandomQMatrix ( int dim)

Returns a dim-by-dim complex matrix, where the real and imaginary value of each element are independently random, under the standard normal distribution (mean 0, standard deviation 1).

Author
Tyson Jones

Definition at line 492 of file test_utilities.cpp.

492 {
493 DEMAND( dim > 1 );
494
495 QMatrix matr = getZeroMatrix(dim);
496 for (int i=0; i<dim; i++) {
497 for (int j=0; j<dim; j++) {
498
499 // generate 2 normally-distributed random numbers via Box-Muller
500 qreal a = rand()/(qreal) RAND_MAX;
501 qreal b = rand()/(qreal) RAND_MAX;
502 if (a == 0) a = REAL_EPS; // prevent rand()=0 creation of NaN
503 qreal r1 = sqrt(-2 * log(a)) * cos(2 * 3.14159265 * b);
504 qreal r2 = sqrt(-2 * log(a)) * sin(2 * 3.14159265 * b);
505
506 matr[i][j] = r1 + r2 * (qcomp) 1i;
507 }
508 }
509 return matr;
510}

Referenced by getRandomUnitary(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ getRandomQVector()

QVector getRandomQVector ( int dim)

Returns a dim-length vector with random complex amplitudes in the square joining {-1-i, 1+i}, of an undisclosed distribution. The resulting vector is NOT L2-normalised.

Author
Tyson Jones

Definition at line 549 of file test_utilities.cpp.

549 {
550 QVector vec = QVector(dim);
551 for (int i=0; i<dim; i++)
552 vec[i] = getRandomComplex();
553
554 return vec;
555}
qcomp getRandomComplex()

Referenced by getRandomStateVector(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ getRandomReal()

qreal getRandomReal ( qreal min,
qreal max )

Returns a random real between min (inclusive) and max (exclusive), from the uniform distribution. Demands that max > min.

Author
Tyson Jones

Definition at line 63 of file random.cpp.

63 {
64 DEMAND( min < maxExcl );
65
66 // advance RNG on every node, identically
67 std::uniform_real_distribution<qreal> dist(min,maxExcl);
68 qreal out = dist(RNG);
69
70 // note despite the doc asserting maxExcl is exclusive,
71 // uniform_real_distribution() can indeed return it! In that
72 // case, we substract machine-eps for caller integrity
73 if (out >= maxExcl)
74 out = std::nextafter(maxExcl, min);
75
76 DEMAND( out >= min );
77 DEMAND( out < maxExcl );
78 return out;
79}

Referenced by getRandomComplex(), getRandomInt(), getRandomInt(), getRandomKrausMap(), getRandomProbabilities(), setRandomDiagPauliHamil(), setRandomPauliSum(), setRandomPauliSum(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ getRandomStateVector()

QVector getRandomStateVector ( int numQb)

Returns a random numQb-length L2-normalised state-vector from an undisclosed distribution. This function works by randomly generating each complex amplitude, then L2-normalising.

Author
Tyson Jones

Definition at line 296 of file random.cpp.

296 {
297
298 return getNormalised(getRandomVector(getPow2(numQb)));
299}

Referenced by getRandomDensityMatrix(), getRandomOrthonormalVectors(), getRandomPureDensityMatrix(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ getRandomUnitary()

QMatrix getRandomUnitary ( int numQb)

Returns a uniformly random (under Haar) 2^numQb-by-2^numQb unitary matrix. This function works by first generating a complex matrix where each element is independently random; the real and imaginary component thereof are independent standard normally-distributed (mean 0, standard-dev 1). Then, the matrix is orthonormalised via the Gram Schmidt algorithm. The resulting unitary matrix MAY be uniformly distributed under the Haar measure, but we make no assurance. This routine may return an identity matrix if it was unable to sufficiently precisely produce a unitary of the given size.

Author
Tyson Jones

Definition at line 348 of file random.cpp.

348 {
349 DEMAND( numQb >= 1 );
350
351 // create Z ~ random complex matrix (distribution not too important)
352 size_t dim = getPow2(numQb);
353 qmatrix matrZ = getRandomMatrix(dim);
354 qmatrix matrZT = getTranspose(matrZ);
355
356 // create Z = Q R (via QR decomposition) ...
357 qmatrix matrQT = getOrthonormalisedRows(matrZ);
358 qmatrix matrQ = getTranspose(matrQT);
359 qmatrix matrR = getZeroMatrix(dim);
360
361 // ... where R_rc = (columm c of Z) . (column r of Q) = (row c of ZT) . (row r of QT) = <r|c>
362 for (size_t r=0; r<dim; r++)
363 for (size_t c=r; c<dim; c++)
364 matrR[r][c] = getInnerProduct(matrQT[r], matrZT[c]);
365
366 // create D = normalised diagonal of R
367 qmatrix matrD = getZeroMatrix(dim);
368 for (size_t i=0; i<dim; i++)
369 matrD[i][i] = matrR[i][i] / std::abs(matrR[i][i]);
370
371 // create U = Q D
372 qmatrix matrU = matrQ * matrD;
373
374 DEMAND( isApproxUnitary(matrU) );
375 return matrU;
376}

Referenced by getRandomKrausMap(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ getSwapMatrix()

QMatrix getSwapMatrix ( int qb1,
int qb2,
int numQb )

Returns the 2^numQb-by-2^numQb unitary matrix which swaps qubits qb1 and qb2; the SWAP gate of not-necessarily-adjacent qubits. If qb1 == qb2, returns the identity matrix.

Author
Tyson Jones

Definition at line 28 of file evolve.cpp.

28 {
29 DEMAND( numQb > 1 );
30 DEMAND( (qb1 >= 0 && qb1 < numQb) );
31 DEMAND( (qb2 >= 0 && qb2 < numQb) );
32
33 if (qb1 == qb2)
34 return getIdentityMatrix(getPow2(numQb));
35
36 if (qb1 > qb2)
37 std::swap(qb1, qb2);
38
39 qmatrix out;
40
41 // qubits are either adjacent
42 if (qb2 == qb1 + 1) {
43 out = qmatrix{{1,0,0,0},{0,0,1,0},{0,1,0,0},{0,0,0,1}};
44
45 // or distant
46 } else {
47 int block = getPow2(qb2 - qb1);
48 out = getZeroMatrix(block*2);
49 qmatrix iden = getIdentityMatrix(block/2);
50
51 // Lemma 3.1 of arxiv.org/pdf/1711.09765.pdf
52 qmatrix p0{{1,0},{0,0}};
53 qmatrix l0{{0,1},{0,0}};
54 qmatrix l1{{0,0},{1,0}};
55 qmatrix p1{{0,0},{0,1}};
56
57 // notating a^(n+1) = identity(getPow2(n)) (otimes) a, we construct the matrix
58 // [ p0^(N) l1^N ]
59 // [ l0^(N) p1^N ]
60 // where N = qb2 - qb1 */
61 setSubMatrix(out, getKroneckerProduct(iden, p0), 0, 0);
62 setSubMatrix(out, getKroneckerProduct(iden, l0), block, 0);
63 setSubMatrix(out, getKroneckerProduct(iden, l1), 0, block);
64 setSubMatrix(out, getKroneckerProduct(iden, p1), block, block);
65 }
66
67 // pad swap with outer identities
68 if (qb1 > 0)
69 out = getKroneckerProduct(out, getIdentityMatrix(getPow2(qb1)));
70
71 if (qb2 < numQb-1)
72 out = getKroneckerProduct(getIdentityMatrix(getPow2(numQb-qb2-1)), out);
73
74 return out;
75}
qmatrix getKroneckerProduct(qmatrix a, qmatrix b)
Definition linalg.cpp:523

Referenced by getFullOperatorMatrix().

◆ getTwosComplement()

long long int getTwosComplement ( long long int decimal,
int numBits )

Returns the two's complement signed encoding of the unsigned number decimal, which must be a number between 0 and 2^numBits (exclusive). The returned number lies in [-2^(numBits-1), 2^(numBits-1)-1]

Author
Tyson Jones

Definition at line 1451 of file test_utilities.cpp.

1451 {
1452 DEMAND( decimal >= 0 );
1453 DEMAND( numBits >= 2 );
1454 DEMAND( decimal < (1LL << numBits) );
1455
1456 long long int maxMag = 1LL << (numBits-1);
1457 if (decimal >= maxMag)
1458 return -maxMag + (decimal - maxMag);
1459 else
1460 return decimal;
1461}

◆ getUnsigned()

long long int getUnsigned ( long long int twosComp,
int numBits )

Return the unsigned value of a number, made of #numBits bits, which under two's complement, encodes the signed number twosComp. The returned number lies in [0, 2^(numBits)-1]

Author
Tyson Jones

Definition at line 1463 of file test_utilities.cpp.

1463 {
1464 DEMAND( numBits >= 2 );
1465 DEMAND( twosComp < (1LL << (numBits-1)) );
1466 DEMAND( twosComp >= - (1LL << (numBits-1)) );
1467
1468 if (twosComp >= 0)
1469 return twosComp;
1470 else
1471 return (1<<numBits) + twosComp;
1472}

◆ getValueOfTargets()

long long int getValueOfTargets ( long long int ind,
int * targs,
int numTargs )

Returns the integer value of the targeted sub-register for the given full state index ind.

Author
Tyson Jones

Definition at line 810 of file test_utilities.cpp.

810 {
811 DEMAND( ind >= 0 );
812
813 long long int val = 0;
814
815 for (int t=0; t<numTargs; t++)
816 val += ((ind >> targs[t]) & 1) * (1LL << t);
817
818 return val;
819}

Referenced by getDFT().

◆ getZeroMatrix()

◆ pauliseqs()

CatchGen< pauliOpType * > pauliseqs ( int numPaulis)

Returns a Catch2 generator of every numPaulis-length set of Pauli-matrix types (or base-4 integers). Generates in increasing lexographic order, where the left-most (zero index) pauli is treated as LEAST significant. Note that the sequence must not be modified during generation.

This function can be used like

pauliOpType* set = GENERATE( pauliseqs(2) );

to produce {I,I}, {X,I}, {Y,I}, {Z,I}, {I,X}, {X,X}, {Y,X}, {Z,X}, {I,Y}, {X,Y}, {Y,Y}, {Z,Y}, {I,Z}, {X,Z}, {Y,Z}, {Z,Z}/

Author
Tyson Jones

Definition at line 1728 of file test_utilities.cpp.

1728 {
1729 return Catch::Generators::GeneratorWrapper<pauliOpType*>(
1730 Catch::Detail::make_unique<SequenceGenerator<pauliOpType>>(PAULI_Z, numPaulis));
1731}

◆ sequences()

CatchGen< int * > sequences ( int base,
int numDigits )

Returns a Catch2 generator of every numDigits-length sequence in the given base, in increasing lexographic order, where left-most (zero index) bit is treated as LEAST significant (opposite typical convention). Note that the sequence must not be modified during generation.

This function can be used like

int base = 3;
int numDigits = 2;
int* seq = GENERATE_COPY( sequences(base, numDigits) );

to produce {0,0}, {1,0}, {2,0}, {0,1}, {1,1}, {2,1}, {0,2}, {1,2}, {2,2}.

Author
Tyson Jones

Definition at line 1724 of file test_utilities.cpp.

1724 {
1725 return Catch::Generators::GeneratorWrapper<int*>(
1726 Catch::Detail::make_unique<SequenceGenerator<int>>(base-1, numDigits));
1727}

◆ setRandomDiagPauliHamil()

void setRandomDiagPauliHamil ( PauliHamil hamil,
int numQubits )

Populates hamil with random coefficients and a random amount number of PAULI_I and PAULI_Z operators.

Author
Tyson Jones

Definition at line 1387 of file test_utilities.cpp.

1387 {
1388 for (int n=0; n<hamil.numTerms; n++) {
1389 hamil.coeffs[n] = getRandomReal(-5, 5);
1390 hamil.strings[n] = getRandomDiagPauliStr(numQubits);
1391 }
1392}

◆ setRandomPauliSum() [1/2]

void setRandomPauliSum ( PauliHamil hamil,
int numQubits )

Populates hamil with random coefficients and pauli codes

Author
Tyson Jones

Definition at line 1379 of file test_utilities.cpp.

1379 {
1380
1381 for (int n=0; n<hamil.numTerms; n++) {
1382 hamil.coeffs[n] = getRandomReal(-5, 5);
1383 hamil.strings[n] = getRandomPauliStr(numQubits);
1384 }
1385}

◆ setRandomPauliSum() [2/2]

void setRandomPauliSum ( qreal * coeffs,
pauliOpType * codes,
int numQubits,
int numTerms )

Populates the coeffs array with random qreals in (-5, 5), and populates codes with random Pauli codes

Author
Tyson Jones

Definition at line 1370 of file test_utilities.cpp.

1370 {
1371 int i=0;
1372 for (int n=0; n<numTerms; n++) {
1373 coeffs[n] = getRandomReal(-5, 5);
1374 for (int q=0; q<numQubits; q++)
1375 codes[i++] = (pauliOpType) getRandomInt(0,4);
1376 }
1377}
int getRandomInt(int min, int max)

Referenced by TEST_CASE().

◆ setRandomTargets() [1/2]

void setRandomTargets ( int * targs,
int numTargs,
int numQb )

Populates targs with a random selection of numTargs elements from [0,numQb-1]. List targs does not need to be initialised and its elements are overwritten.

Author
Tyson Jones

Definition at line 1394 of file test_utilities.cpp.

1394 {
1395 DEMAND( numQb >= 1 );
1396 DEMAND( numTargs >= 1);
1397 DEMAND( numTargs <= numQb );
1398
1399 // create an ordered list of all possible qubits
1400 vector<int> allQb(numQb);
1401 for (int q=0; q<numQb; q++)
1402 allQb[q] = q;
1403
1404 // shuffle all qubits (must be consistent on each node)
1405 std::shuffle(&allQb[0], &allQb[numQb], randomGenerator);
1406
1407 // select numTargs of all qubits
1408 for (int i=0; i<numTargs; i++)
1409 targs[i] = allQb[i];
1410}

Referenced by setRandomTargets(), TEST_CASE(), and TEST_CASE().

◆ setRandomTargets() [2/2]

void setRandomTargets ( vector< int > & targs,
int numQb )

Populates targs with a random selection of elements from [0,numQb-1]. List targs does not need to be initialised and its elements are overwritten.

Author
Tyson Jones

Definition at line 1411 of file test_utilities.cpp.

1411 {
1412
1413 setRandomTargets(targs.data(), targs.size(), numQb);
1414}
void setRandomTargets(int *targs, int numTargs, int numQb)

◆ setRandomTestStateSeeds()

void setRandomTestStateSeeds ( )

Seed the C and C++ RNGs using hardware CSPRNG

Author
Tyson Jones

Definition at line 39 of file random.cpp.

39 {
40 DEMAND( isQuESTEnvInit() );
41
42 // generate a random seed from hardware rng
43 std::random_device cspnrg;
44 unsigned seed = cspnrg();
45
46 // seed QuEST, which uses only the root node's seed
47 setSeeds(&seed, 1);
48
49 // broadcast root node seed to all nodes
50 getSeeds(&seed);
51
52 // seed RNG
53 RNG.seed(seed);
54}
std::vector< unsigned > getSeeds()
Definition debug.cpp:213
void setSeeds(unsigned *seeds, int numSeeds)
Definition debug.cpp:37
int isQuESTEnvInit()

◆ setSubMatrix()

void setSubMatrix ( QMatrix & dest,
QMatrix sub,
size_t r,
size_t c )

Modifies dest by overwriting its submatrix (from top-left corner (r, c) to bottom-right corner (r + dest.size(), c + dest.size()) with the complete elements of sub. This demands that dest.size() >= sub.size() + max(r,c).

Author
Tyson Jones

Definition at line 203 of file qmatrix.cpp.

203 {
204 DEMAND( sub.size() > 0 );
205 DEMAND( sub .size() + r <= dest.size() );
206 DEMAND( sub[0].size() + c <= dest.size() );
207
208 // this function cheekily supports when 'sub' is non-square,
209 // which is inconsistent with the preconditions of most of
210 // the qmatrix functions, but is needed by setDensityQuregAmps()
211
212 for (size_t i=0; i<sub.size(); i++)
213 for (size_t j=0; j<sub[i].size(); j++)
214 dest[r+i][c+j] = sub[i][j];
215}

Referenced by getFullOperatorMatrix(), getSwapMatrix(), TEST_CASE(), and TEST_CASE().

◆ setUniqueFilename()

void setUniqueFilename ( char * outFn,
int maxlen,
char * prefix )

Modifies the given diagonal matrix such that the diagonal elements which correspond to the coordinates in overrideInds are replaced with exp(i phase), as prescribed by overridePhases. This function assumes that the given registers are contiguous, are in order of increasing significance, and that the matrix is proportionately sized and structured to act on the space of all registers combined. Overrides can be repeated, and only the first encountered for a given index will be effected (much like applyMultiVarPhaseFuncOverrides()).

Author
Tyson Jones Modifies outFn to be a filename of format prefix_NUM.txt where NUM is a new unique integer so far. This is useful for getting unique filenames for independent test cases of functions requiring reading/writing to file, to avoid IO locks (especially common in distributed mode).
Tyson Jones

Definition at line 1523 of file test_utilities.cpp.

1523 {
1524 snprintf(outFn, maxlen, "%s_%d.txt", prefix, fn_unique_suffix_id++);
1525}

◆ sublists() [1/4]

CatchGen< int * > sublists ( CatchGen< int > && gen,
int numSamps,
const int * exclude,
int numExclude )

Returns a Catch2 generator of every length-sublen sublist of the elements generated by gen, which exclude all elements in exclude, in increasing lexographic order. This generates every fixed-length combination of gen's elements the nominated exclusions, and every permutation of each.

There is on need for the elements of exclude to actually appear in those of gen. sublen must less than or equal to the number of elements in gen, after the nominated exclusions.

Note that the sublist must not be modified, else further generation may break (QuEST's internal functions will indeed modify but restore the qubit index lists given to them, which is ok). Assumes list contains no duplicates, otherwise the generated sublists may be duplicated.

This function can be used like

int sublen = 2;
int exclude[2] = {3,4};
int* sublist = GENERATE_COPY( sublists(range(1,6), sublen, exclude, 2) );

to generate {1,2}, {1,5}, {2,1}, {2,5}, {5,1}, {5,2}

Author
Tyson Jones

◆ sublists() [2/4]

CatchGen< int * > sublists ( CatchGen< int > && gen,
int numSamps,
int excluded )

Returns a Catch2 generator of every length-sublen sublist of the elements generated by gen which exclude element excluded, in increasing lexographic order. This generates every fixed-length combination of gen's elements the nominated exclusions, and every permutation of each.

sublen must less than or equal to the number of elements in gen, after the nominated exclusion. There is no need for excluded to actually appear in the elements of gen.

Note that the sublist must not be modified, else further generation may break (QuEST's internal functions will indeed modify but restore the qubit index lists given to them, which is ok). Assumes list contains no duplicates, otherwise the generated sublists may be duplicated.

This function can be used like

int sublen = 2;
int excluded = 1;
int* sublist = GENERATE_COPY( sublists(range(1,4), sublen, excluded) );

to generate {2,3}, {3,2}.

Author
Tyson Jones

◆ sublists() [3/4]

CatchGen< int * > sublists ( CatchGen< int > && gen,
int sublen )

Returns a Catch2 generator of every length-sublen sublist of the elements generated by gen, in increasing lexographic order. This generates every fixed-length combination of gen's elements, and every permutation of each. Note that the produced sublist must not be modified, else further generation may break (QuEST's internal functions will indeed modify but restore the qubit index lists given to them, which is ok). Assumes list contains no duplicates, otherwise the generated sublists may be duplicated.

This function can be used like

int sublen = 2;
int* sublist = GENERATE_COPY( sublists(list, 4, sublen) );

to generate {1,2}, {1,3}, {1,4}, {2,1}, {2,3}, {2,4}, {3,1}, {3,2}, {3, 4}, {4,1}, {4,2}, {4, 3}.

Author
Tyson Jones

◆ sublists() [4/4]

CatchGen< int * > sublists ( int * list,
int len,
int sublen )

Returns a Catch2 generator of every length-sublen sublist of length-len list, in increasing lexographic order. This generates every fixed-length combination of the given list and every permutation of each. & If the sublist length is the full list length, this generator produces every permutation correctly. Note that the sublist must not be modified, else further & generation may break (QuEST's internal functions will indeed modify but restore the qubit index lists given to them, which is ok). Assumes list contains no duplicates, otherwise the generated sublists may be duplicated.

This function can be used like

int list[4] = {1,2,3,4};
int sublen = 2;
int* sublist = GENERATE_COPY( sublists(list, 4, sublen) );

to generate {1,2}, {1,3}, {1,4}, {2,1}, {2,3}, {2,4}, {3,1}, {3,2}, {3, 4}, {4,1}, {4,2}, {4, 3}.

Author
Tyson Jones

Definition at line 1654 of file test_utilities.cpp.

1656 {
1657 return Catch::Generators::GeneratorWrapper<int*>(
1658 Catch::Detail::make_unique<SubListGenerator>(list, len, sublen));
1659}

◆ toComplexMatrix2()

ComplexMatrix2 toComplexMatrix2 ( QMatrix qm)

Returns a ComplexMatrix2 copy of QMatix qm. Demands that qm is a 2-by-2 matrix.

Author
Tyson Jones

Definition at line 1174 of file test_utilities.cpp.

1174 {
1175 DEMAND( qm.size() == 2 );
1176 ComplexMatrix2 cm;
1177 macro_copyQMatrixToDeprecatedComplexMatrix(cm, qm);
1178 return cm;
1179}

Referenced by TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ toComplexMatrix4()

ComplexMatrix4 toComplexMatrix4 ( QMatrix qm)

Returns a ComplexMatrix4 copy of QMatix qm. Demands that qm is a 4-by-4 matrix.

Author
Tyson Jones

Definition at line 1180 of file test_utilities.cpp.

1180 {
1181 DEMAND( qm.size() == 4 );
1182 ComplexMatrix4 cm;
1183 macro_copyQMatrixToDeprecatedComplexMatrix(cm, qm);
1184 return cm;
1185}

Referenced by TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ toComplexMatrixN()

void toComplexMatrixN ( QMatrix qm,
ComplexMatrixN cm )

Populates the ComplexMatrixN with the contents of a QMatrix. In GPU-mode, this will then sync the elements ot the matrix's persistent GPU memory

Author
Tyson Jones

Definition at line 1193 of file test_utilities.cpp.

1193 {
1194 DEMAND( qm.size() == (1u<<cm.numQubits) );
1195 macro_copyComplexMatrix(cm.cpuElems, qm, qm.size());
1196 syncCompMatr(cm);
1197}
void syncCompMatr(CompMatr matr)
Definition matrices.cpp:373

Referenced by TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ toDiagonalQMatrix()

QMatrix toDiagonalQMatrix ( QVector vec)

Returns a diagonal complex matrix formed by the given vector

Author
Tyson Jones

Definition at line 1474 of file test_utilities.cpp.

1474 {
1475 QMatrix mat = getZeroMatrix(vec.size());
1476 for (size_t i=0; i<vec.size(); i++)
1477 mat[i][i] = vec[i];
1478 return mat;
1479}

◆ toQMatrix() [1/9]

QMatrix toQMatrix ( CompMatr src)

Returns a copy of the given matrix

Author
Tyson Jones

Definition at line 1209 of file test_utilities.cpp.

1209 {
1210 QMatrix dest = getZeroMatrix(1 << src.numQubits);
1211 macro_copyComplexMatrix(dest, src.cpuElems, dest.size());
1212 return dest;
1213}

◆ toQMatrix() [2/9]

QMatrix toQMatrix ( CompMatr1 src)

Returns a copy of the given 2-by-2 matrix.

Author
Tyson Jones

Definition at line 1199 of file test_utilities.cpp.

1199 {
1200 QMatrix dest = getZeroMatrix(2);
1201 macro_copyComplexMatrix(dest, src.elems, dest.size());
1202 return dest;
1203}

Referenced by TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ toQMatrix() [3/9]

QMatrix toQMatrix ( CompMatr2 src)

Returns a copy of the given 4-by-4 matrix.

Author
Tyson Jones

Definition at line 1204 of file test_utilities.cpp.

1204 {
1205 QMatrix dest = getZeroMatrix(4);
1206 macro_copyComplexMatrix(dest, src.elems, dest.size());
1207 return dest;
1208}

◆ toQMatrix() [4/9]

QMatrix toQMatrix ( DiagMatr matr)

Returns a dense matrix equivalent to the given diagonal

Author
Tyson Jones

Definition at line 1320 of file test_utilities.cpp.

1320 {
1321 QMatrix mat = getZeroMatrix(in.numElems);
1322 for (size_t i=0; i<mat.size(); i++)
1323 mat[i][i] = in.cpuElems[i];
1324 return mat;
1325}

◆ toQMatrix() [5/9]

QMatrix toQMatrix ( FullStateDiagMatr matr)

Returns a dense matrix equivalent to the given diagonal

Author
Tyson Jones

Definition at line 1312 of file test_utilities.cpp.

1312 {
1313 QVector vec = toQVector(in);
1314 QMatrix mat = getZeroMatrix(in.numElems);
1315 for (size_t i=0; i<mat.size(); i++)
1316 mat[i][i] = vec[i];
1317 return mat;
1318}
QVector toQVector(Qureg qureg)

◆ toQMatrix() [6/9]

QMatrix toQMatrix ( PauliHamil hamil)

Returns a 2^N-by-2^N Hermitian matrix form of the PauliHamil

Author
Tyson Jones

◆ toQMatrix() [7/9]

QMatrix toQMatrix ( PauliStrSum sum)

Returns a 2^N-by-2^N Hermitian Z-basis matrix of the given complex-weighted sum of Pauli strings, where N is the number of non-Identity operators.

Author
Tyson Jones

◆ toQMatrix() [8/9]

QMatrix toQMatrix ( qreal * coeffs,
pauliOpType * paulis,
int numQubits,
int numTerms )

Returns a 2^N-by-2^N Hermitian matrix form of the specified weighted sum of Pauli products

Author
Tyson Jones

Definition at line 1416 of file test_utilities.cpp.

1416 {
1417
1418 // produce a numTargs-big matrix 'pauliSum' by pauli-matrix tensoring and summing
1419 QMatrix iMatr{{1,0},{0,1}};
1420 QMatrix xMatr{{0,1},{1,0}};
1421 QMatrix yMatr{{0,-qcomp(0,1)},{qcomp(0,1),0}};
1422 QMatrix zMatr{{1,0},{0,-1}};
1423 QMatrix pauliSum = getZeroMatrix(1<<numQubits);
1424
1425 for (int t=0; t<numTerms; t++) {
1426 QMatrix pauliProd = QMatrix{{1}};
1427
1428 for (int q=0; q<numQubits; q++) {
1429 int i = q + t*numQubits;
1430
1431 QMatrix fac;
1432 pauliOpType code = paulis[i];
1433 if (code == PAULI_I) fac = iMatr;
1434 if (code == PAULI_X) fac = xMatr;
1435 if (code == PAULI_Y) fac = yMatr;
1436 if (code == PAULI_Z) fac = zMatr;
1437 pauliProd = getKroneckerProduct(fac, pauliProd);
1438 }
1439 pauliSum += coeffs[t] * pauliProd;
1440 }
1441
1442 // a now 2^numQubits by 2^numQubits Hermitian matrix
1443 return pauliSum;
1444}

◆ toQMatrix() [9/9]

QMatrix toQMatrix ( Qureg qureg)

Returns an equal-size copy of the given density matrix qureg. In GPU mode, this function involves a copy of qureg from GPU memory to RAM. In distributed mode, this involves an all-to-all broadcast of qureg.

Author
Tyson Jones

Definition at line 1215 of file test_utilities.cpp.

1215 {
1216 DEMAND( qureg.isDensityMatrix );
1217#if COMPILE_MPI
1218 DEMAND( qureg.numAmps < MPI_MAX_AMPS_IN_MSG );
1219#endif
1220
1221 // ensure local qureg amps are up to date
1222 copyStateFromGPU(qureg);
1223 syncQuESTEnv();
1224
1225 // collect all amps between all nodes
1226 qcomp* allAmps = qureg.cpuAmps;
1227
1228 // in distributed mode, give every node the full state vector
1229#if COMPILE_MPI
1230 if (qureg.isDistributed) {
1231 allAmps = (qcomp*) malloc(qureg.numAmps * sizeof *allAmps);
1232 MPI_Allgather(
1233 qureg.cpuAmps, qureg.numAmpsPerNode, MPI_QCOMP,
1234 allAmps, qureg.numAmpsPerNode, MPI_QCOMP, MPI_COMM_WORLD);
1235 }
1236#endif
1237
1238 // copy full state vector into a QVector
1239 long long int dim = (1LL << qureg.numQubits);
1240 QMatrix matr = getZeroMatrix(dim);
1241 for (long long int n=0; n<qureg.numAmps; n++)
1242 matr[n%dim][n/dim] = allAmps[n];
1243
1244 // clean up if we malloc'd the distributed array
1245 if (qureg.isDistributed)
1246 free(allAmps);
1247 return matr;
1248}

◆ toQureg() [1/2]

void toQureg ( Qureg qureg,
QMatrix mat )

Initialises the density matrix qureg to have the same amplitudes as mat. Demands qureg is a density matrix of equal dimensions to mat. In GPU mode, this function involves a copy from RAM to GPU memory. This function has no communication cost in distributed mode.

Author
Tyson Jones

Definition at line 1339 of file test_utilities.cpp.

1339 {
1340 DEMAND( qureg.isDensityMatrix );
1341 DEMAND( (1LL << qureg.numQubits) == (long long int) mat.size() );
1342
1343 syncQuESTEnv();
1344
1345 int len = (1 << qureg.numQubits);
1346 for (int i=0; i<qureg.numAmpsPerNode; i++) {
1347 int ind = qureg.rank*qureg.numAmpsPerNode + i;
1348 qureg.cpuAmps[i] = mat[ind%len][ind/len];
1349 }
1350 copyStateToGPU(qureg);
1351}

◆ toQureg() [2/2]

void toQureg ( Qureg qureg,
QVector vec )

Initialises the state-vector qureg to have the same amplitudes as vec. Demands qureg is a state-vector of an equal size to vec. In GPU mode, this function involves a copy from RAM to GPU memory. This function has no communication cost in distributed mode.

Author
Tyson Jones

Definition at line 1327 of file test_utilities.cpp.

1327 {
1328 DEMAND( !qureg.isDensityMatrix );
1329 DEMAND( qureg.numAmps == (long long int) vec.size() );
1330
1331 syncQuESTEnv();
1332
1333 for (int i=0; i<qureg.numAmpsPerNode; i++) {
1334 int ind = qureg.rank*qureg.numAmpsPerNode + i;
1335 qureg.cpuAmps[i] = vec[ind];
1336 }
1337 copyStateToGPU(qureg);
1338}

Referenced by TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().

◆ toQVector() [1/3]

QVector toQVector ( DiagMatr op)

Returns a vector with the given diagonal's elements. In distributed mode, this involves an all-to-all broadcast of op.

Author
Tyson Jones

Definition at line 1285 of file test_utilities.cpp.

1285 {
1286
1287 return vector<qcomp>(matr.cpuElems, matr.cpuElems + matr.numElems);
1288}

◆ toQVector() [2/3]

QVector toQVector ( FullStateDiagMatr op)

Returns a vector with the given diagonal's elements. In distributed mode, this involves an all-to-all broadcast of op.

Author
Tyson Jones

Definition at line 1290 of file test_utilities.cpp.

1290 {
1291
1292#if COMPILE_MPI
1293 DEMAND( matr.numElems < MPI_MAX_AMPS_IN_MSG );
1294#endif
1295
1296 vector<qcomp> vec(matr.numElems);
1297
1298 // in distributed mode, give every node the full diagonal operator
1299 if (matr.isDistributed) {
1300 #if COMPILE_MPI
1301 MPI_Allgather(
1302 matr.cpuElems, matr.numElemsPerNode, MPI_QCOMP,
1303 vec.data(), matr.numElemsPerNode, MPI_QCOMP, MPI_COMM_WORLD);
1304 #endif
1305 } else {
1306 vec.assign(matr.cpuElems, matr.cpuElems + matr.numElems);
1307 }
1308
1309 return vec;
1310}

◆ toQVector() [3/3]

QVector toQVector ( Qureg qureg)

Returns an equal-size copy of the given state-vector qureg. In GPU mode, this function involves a copy of qureg from GPU memory to RAM. In distributed mode, this involves an all-to-all broadcast of qureg.

Author
Tyson Jones

Definition at line 1250 of file test_utilities.cpp.

1250 {
1251 DEMAND( !qureg.isDensityMatrix );
1252#if COMPILE_MPI
1253 DEMAND( qureg.numAmps < MPI_MAX_AMPS_IN_MSG );
1254#endif
1255
1256 // ensure local qureg amps are up to date
1257 copyStateFromGPU(qureg);
1258 syncQuESTEnv();
1259
1260 qcomp* allAmps = qureg.cpuAmps;
1261
1262 // in distributed mode, give every node the full state vector
1263#if COMPILE_MPI
1264 if (qureg.isDistributed) {
1265 allAmps = (qcomp*) malloc(qureg.numAmps * sizeof *allAmps);
1266
1267 MPI_Allgather(
1268 qureg.cpuAmps, qureg.numAmpsPerNode, MPI_QCOMP,
1269 allAmps, qureg.numAmpsPerNode, MPI_QCOMP, MPI_COMM_WORLD);
1270 }
1271#endif
1272
1273 // copy full state vector into a QVector
1274 QVector vec = QVector(qureg.numAmps);
1275 for (long long int i=0; i<qureg.numAmps; i++)
1276 vec[i] = allAmps[i];
1277
1278 // clean up if we malloc'd distrib array
1279 if (qureg.isDistributed)
1280 free(allAmps);
1281
1282 return vec;
1283}

Referenced by TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and toQMatrix().

◆ writeToFileSynch()

void writeToFileSynch ( char * fn,
const string & contents )

Writes contents to the file with filename fn, which is created and/or overwritten. In distributed mode, the master node writes while the other nodes wait until complete.

Author
Tyson Jones

Definition at line 1527 of file test_utilities.cpp.

1527 {
1528
1529 // master node writes
1530 if (getQuESTEnv().rank == 0) {
1531 FILE* file = fopen(fn, "w");
1532 fputs(contents.c_str(), file);
1533 fclose(file);
1534 }
1535
1536 // other nodes wait
1537 syncQuESTEnv();
1538}