utilities.hpp
1
14#ifndef QUEST_TEST_UTILS_H
15#define QUEST_TEST_UTILS_H
16
17#include "QuEST.h"
18#include "QuEST_complex.h"
19#include "catch.hpp"
20#include <vector>
21
25extern QuESTEnv QUEST_ENV;
26
36#define NUM_QUBITS 5
37
38#ifndef M_PI
39#define M_PI 3.141592653589793238
40#endif
41
51#define VLA(type, name, len) \
52 std::vector<type> name##_vla_hack_vec(len); \
53 type* name = name##_vla_hack_vec.data();
54
66typedef std::vector<std::vector<qcomp>> QMatrix;
67
77typedef std::vector<qcomp> QVector;
78
88
98
99/* (Excluded from Doxygen doc)
100 *
101 * Define QVector and QMatrix operator overloads.
102 * Note that QMatrix overloads don't simply use QVector
103 * overloads, since the complex vector dot product involves
104 * conjugation, which doesn't occur in complex matrix multiplication.
105 * Note too we also avoid defining operators in terms of other operators
106 * (e.g. minus is plus(negative times)) since compiler optimisations
107 * may change the order of operations and confuse the overloads invoked.
108 * Definition of division using multiplication can furthermore
109 * heighten numerical errors.
110 *
111 * @ingroup testutilities
112 * @author Tyson Jones
113 */
114QVector operator + (const QVector& v1, const QVector& v2);
115QVector operator - (const QVector& v1, const QVector& v2);
116QVector operator * (const qcomp& a, const QVector& v);
117QVector operator * (const QVector& v, const qcomp& a);
118QVector operator / (const QVector& v, const qcomp& a);
119qcomp operator * (const QVector &v1, const QVector& v2);
120void operator += (QVector& v1, const QVector& v2);
121void operator -= (QVector& v1, const QVector& v2);
122void operator *= (QVector& v1, const qcomp& a);
123void operator /= (QVector& v1, const qcomp& a);
124QMatrix operator + (const QMatrix& m1, const QMatrix& m2);
125QMatrix operator - (const QMatrix& m1, const QMatrix& m2);
126QMatrix operator * (const qcomp& a, const QMatrix& m);
127QMatrix operator * (const QMatrix& m, const qcomp& a);
128QMatrix operator / (const QMatrix& m, const qcomp& a);
129QMatrix operator * (const QMatrix& m1, const QMatrix& m2);
130void operator += (QMatrix& m1, const QMatrix& m2);
131void operator -= (QMatrix& m1, const QMatrix& m2);
132void operator *= (QMatrix& m1, const qreal& a);
133void operator /= (QMatrix& m1, const qreal& a);
134void operator *= (QMatrix& m1, const QMatrix& m2);
135QVector operator * (const QMatrix& m, const QVector& v);
136
144QVector toQVector(Qureg qureg);
145
154
162QMatrix toQMatrix(Qureg qureg);
163
170QMatrix toQMatrix(Complex alpha, Complex beta);
171
178
185
192
199QMatrix toQMatrix(qreal* coeffs, pauliOpType* paulis, int numQubits, int numTerms);
200
207
214
222
229
237
245
254
263void toQureg(Qureg qureg, QVector vec);
264
273void toQureg(Qureg qureg, QMatrix mat);
274
283
289QMatrix getZeroMatrix(size_t dim);
290
296QMatrix getIdentityMatrix(size_t dim);
297
305
316
324
332QMatrix getSwapMatrix(int qb1, int qb2, int numQb);
333
348QMatrix getFullOperatorMatrix(int* ctrls, int numCtrls, int *targs, int numTargs, QMatrix op, int numQubits);
349
359
366
374int getRandomInt(int min, int max);
375
384
393
402
411
425QMatrix getRandomUnitary(int numQb);
426
435
444
452
459
466
476std::vector<QMatrix> getRandomKrausMap(int numQb, int numOps);
477
483std::vector<qreal> getRandomProbabilities(int numProbs);
484
491std::vector<QVector> getRandomOrthonormalVectors(int numQb, int numStates);
492
499QMatrix getMixedDensityMatrix(std::vector<qreal> probs, std::vector<QVector> states);
500
507
514
520QVector getDFT(QVector in, int* targs, int numTargs);
521
528long long int getValueOfTargets(long long int ind, int* targs, int numTargs);
529
538void setSubMatrix(QMatrix &dest, QMatrix sub, size_t r, size_t c);
539
559void applyReferenceOp(QMatrix &state, int* ctrls, int numCtrls, int *targs, int numTargs, QMatrix op);
560
580void applyReferenceOp(QMatrix &state, int* ctrls, int numCtrls, int targ1, int targ2, QMatrix op);
581
600void applyReferenceOp(QMatrix &state, int* ctrls, int numCtrls, int target, QMatrix op);
601
620void applyReferenceOp(QMatrix &state, int *targs, int numTargs, QMatrix op);
621
639void applyReferenceOp(QMatrix &state, int ctrl, int targ, QMatrix op);
640
659void applyReferenceOp(QMatrix &state, int ctrl, int* targs, int numTargs, QMatrix op);
660
678void applyReferenceOp(QMatrix &state, int ctrl, int targ1, int targ2, QMatrix op);
679
697void applyReferenceOp(QMatrix &state, int targ, QMatrix op);
698
717void applyReferenceOp(QVector &state, int* ctrls, int numCtrls, int *targs, int numTargs, QMatrix op);
718
735void applyReferenceOp(QVector &state, int* ctrls, int numCtrls, int targ1, int targ2, QMatrix op);
736
753void applyReferenceOp(QVector &state, int* ctrls, int numCtrls, int target, QMatrix op);
754
770void applyReferenceOp(QVector &state, int *targs, int numTargs, QMatrix op);
771
787void applyReferenceOp(QVector &state, int ctrl, int targ, QMatrix op);
788
805void applyReferenceOp(QVector &state, int ctrl, int* targs, int numTargs, QMatrix op);
806
823void applyReferenceOp(QVector &state, int ctrl, int targ1, int targ2, QMatrix op);
824
840void applyReferenceOp(QVector &state, int targ, QMatrix op);
841
850void applyReferenceMatrix(QVector &state, int* ctrls, int numCtrls, int *targs, int numTargs, QMatrix op);
851
858void applyReferenceMatrix(QVector &state, int *targs, int numTargs, QMatrix op);
859
868void applyReferenceMatrix(QMatrix &state, int* ctrls, int numCtrls, int *targs, int numTargs, QMatrix op);
869
877void applyReferenceMatrix(QMatrix &state, int *targs, int numTargs, QMatrix op);
878
892bool areEqual(Qureg qureg1, Qureg qureg2);
893
906bool areEqual(Qureg qureg, QVector vec);
907
920bool areEqual(Qureg qureg, QMatrix matr);
921
935bool areEqual(Qureg qureg1, Qureg qureg2, qreal precision);
936
949bool areEqual(Qureg qureg, QVector vec, qreal precision);
950
963bool areEqual(Qureg qureg, QMatrix matr, qreal precision);
964
971bool areEqual(QVector a, QVector b);
972
979bool areEqual(QMatrix a, QMatrix b);
980
987bool areEqual(QVector vec, qreal* reals, qreal* imags);
988
996bool areEqual(QVector vec, qreal* reals);
997
1002qcomp expI(qreal phase);
1003
1009unsigned int calcLog2(long unsigned int res);
1010
1017void setRandomPauliSum(qreal* coeffs, pauliOpType* codes, int numQubits, int numTerms);
1018
1024void setRandomPauliSum(PauliHamil hamil);
1025
1033
1040void setRandomTargets(int* targs, int numTargs, int numQb);
1041
1049long long int getTwosComplement(long long int decimal, int numBits);
1050
1058long long int getUnsigned(long long int twosComp, int numBits);
1059
1071void setDiagMatrixOverrides(QMatrix &matr, int* numQubitsPerReg, int numRegs, enum bitEncoding encoding, long long int* overrideInds, qreal* overridePhases, int numOverrides);
1072
1081void setUniqueFilename(char* outFn, char* prefix);
1082
1089void writeToFileSynch(char* fn, const string& contents);
1090
1097void deleteFilesWithPrefixSynch(char* prefix);
1098
1099// makes below signatures more concise
1100template<class T> using CatchGen = Catch::Generators::GeneratorWrapper<T>;
1101
1124CatchGen<int*> sublists(int* list, int len, int sublen);
1125
1151CatchGen<int*> sublists(CatchGen<int>&& gen, int numSamps, const int* exclude, int numExclude);
1152
1178CatchGen<int*> sublists(CatchGen<int>&& gen, int numSamps, int excluded);
1179
1200CatchGen<int*> sublists(CatchGen<int>&& gen, int sublen);
1201
1216CatchGen<int*> bitsets(int numBits);
1217
1234CatchGen<int*> sequences(int base, int numDigits);
1235
1252CatchGen<pauliOpType*> pauliseqs(int numPaulis);
1253
1254#endif // QUEST_TEST_UTILS_H
QMatrix getExponentialOfPauliMatrix(qreal angle, QMatrix a)
Returns the matrix exponential of a kronecker product of pauli matrices (or of any involutory matrice...
Definition: utilities.cpp:256
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...
Definition: utilities.cpp:912
std::vector< QMatrix > getRandomKrausMap(int numQb, int numOps)
Returns a random Kraus map of #numOps 2^numQb-by-2^numQb operators, from an undisclosed distribution.
Definition: utilities.cpp:643
QMatrix getRandomPureDensityMatrix(int numQb)
Returns a random numQb-by-numQb density matrix, from an undisclosed distribution, which is pure (corr...
Definition: utilities.cpp:554
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...
Definition: utilities.cpp:1321
QMatrix getRandomUnitary(int numQb)
Returns a uniformly random (under Haar) 2^numQb-by-2^numQb unitary matrix.
Definition: utilities.cpp:607
CatchGen< int * > sequences(int base, int numDigits)
Returns a Catch2 generator of every numDigits-length sequence in the given base, in increasing lexogr...
Definition: utilities.cpp:1672
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...
Definition: utilities.cpp:344
ComplexMatrix4 toComplexMatrix4(QMatrix qm)
Returns a ComplexMatrix4 copy of QMatix qm.
Definition: utilities.cpp:1112
unsigned int calcLog2(long unsigned int res)
Returns log2 of numbers which must be gauranteed to be 2^n.
Definition: QuEST_validation.c:347
QVector getRandomQVector(int dim)
Returns a dim-length vector with random complex amplitudes in the square joining {-1-i,...
Definition: utilities.cpp:476
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,...
Definition: utilities.cpp:855
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...
Definition: utilities.cpp:201
CatchGen< int * > sublists(int *list, int len, int sublen)
Returns a Catch2 generator of every length-sublen sublist of length-len list, in increasing lexograph...
Definition: utilities.cpp:1598
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-ne...
Definition: utilities.cpp:270
QMatrix getConjugateTranspose(QMatrix a)
Returns the conjugate transpose of the complex square matrix a.
Definition: utilities.cpp:229
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 b...
Definition: utilities.cpp:1396
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.
Definition: utilities.cpp:740
QMatrix toQMatrix(Qureg qureg)
Returns an equal-size copy of the given density matrix qureg.
Definition: utilities.cpp:1156
QVector getNormalised(QVector vec)
Returns an L2-normalised copy of vec, using Kahan summation for improved accuracy.
Definition: utilities.cpp:487
QMatrix getRandomDensityMatrix(int numQb)
Returns a random numQb-by-numQb density matrix, from an undisclosed distribution, in a very mixed sta...
Definition: utilities.cpp:533
QMatrix getZeroMatrix(size_t dim)
Returns a dim-by-dim square complex matrix, initialised to all zeroes.
Definition: utilities.cpp:185
void assertQuregAndRefInDebugState(Qureg qureg, QVector ref)
Asserts the given statevector qureg and reference agree, and are properly initialised in the debug st...
Definition: utilities.cpp:143
int getRandomInt(int min, int max)
Returns a random integer between min (inclusive) and max (exclusive), from the uniform distribution.
Definition: utilities.cpp:569
qcomp getRandomComplex()
Returns a random complex number within the square closing (-1-i) and (1+i), from a distribution unifo...
Definition: utilities.cpp:472
ComplexMatrix2 toComplexMatrix2(QMatrix qm)
Returns a ComplexMatrix2 copy of QMatix qm.
Definition: utilities.cpp:1106
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 + d...
Definition: utilities.cpp:262
CatchGen< pauliOpType * > pauliseqs(int numPaulis)
Returns a Catch2 generator of every numPaulis-length set of Pauli-matrix types (or base-4 integers).
Definition: utilities.cpp:1677
void setUniqueFilename(char *outFn, char *prefix)
Modifies outFn to be a filename of format prefix_NUM.txt where NUM is a new unique integer so far.
Definition: utilities.cpp:1468
QVector getDFT(QVector in)
Returns the discrete fourier transform of vector in.
Definition: utilities.cpp:722
void toComplexMatrixN(QMatrix qm, ComplexMatrixN cm)
Initialises cm with the values of qm.
Definition: utilities.cpp:1118
void setRandomDiagPauliHamil(PauliHamil hamil)
Populates hamil with random coefficients and a random amount number of PAULI_I and PAULI_Z operators.
Definition: utilities.cpp:1333
QVector getMatrixDiagonal(QMatrix matr)
Returns the diagonal vector of the given matrix.
Definition: utilities.cpp:560
void writeToFileSynch(char *fn, const string &contents)
Writes contents to the file with filename fn, which is created and/or overwritten.
Definition: utilities.cpp:1472
std::vector< qreal > getRandomProbabilities(int numProbs)
Returns a list of random real scalars, each in [0, 1], which sum to unity.
Definition: utilities.cpp:515
QMatrix getRandomQMatrix(int dim)
Returns a dim-by-dim complex matrix, where the real and imaginary value of each element are independe...
Definition: utilities.cpp:419
std::vector< QVector > getRandomOrthonormalVectors(int numQb, int numStates)
Returns a list of random orthonormal complex vectors, from an undisclosed distribution.
Definition: utilities.cpp:683
void setRandomTargets(int *targs, int numTargs, int numQb)
Populates targs with a random selection of numTargs elements from [0,numQb-1].
Definition: utilities.cpp:1345
QMatrix toDiagonalQMatrix(QVector vec)
Returns a diagonal complex matrix formed by the given vector.
Definition: utilities.cpp:1419
QVector getKroneckerProduct(QVector b, QVector a)
Returns b (otimes) a.
Definition: utilities.cpp:175
std::vector< std::vector< qcomp > > QMatrix
A complex square matrix.
Definition: utilities.hpp:66
bool areEqual(Qureg qureg1, Qureg qureg2)
Performs a hardware-agnostic comparison of the given quregs, checking whether the difference between ...
Definition: utilities.cpp:961
QMatrix getIdentityMatrix(size_t dim)
Returns a dim-by-dim identity matrix.
Definition: utilities.cpp:193
QMatrix getExponentialOfDiagonalMatrix(QMatrix a)
Returns the matrix exponential of a diagonal, square, complex matrix.
Definition: utilities.cpp:237
std::vector< qcomp > QVector
A complex vector, which can be zero-initialised with QVector(numAmps).
Definition: utilities.hpp:77
qreal getRandomReal(qreal min, qreal max)
Returns a random real between min (inclusive) and max (exclusive), from the uniform distribution.
Definition: utilities.cpp:462
void deleteFilesWithPrefixSynch(char *prefix)
Deletes all files with filename starting with prefix.
Definition: utilities.cpp:1485
QMatrix getMixedDensityMatrix(std::vector< qreal > probs, std::vector< QVector > states)
Returns a mixed density matrix formed from mixing the given pure states, which are assumed normalised...
Definition: utilities.cpp:710
QVector toQVector(Qureg qureg)
Returns an equal-size copy of the given state-vector qureg.
Definition: utilities.cpp:1198
QVector getRandomStateVector(int numQb)
Returns a random numQb-length L2-normalised state-vector from an undisclosed distribution.
Definition: utilities.cpp:511
QMatrix getPureDensityMatrix(QVector state)
Returns a density matrix initialised into the given pure state.
Definition: utilities.cpp:550
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,...
Definition: utilities.cpp:1408
CatchGen< int * > bitsets(int numBits)
Returns a Catch2 generator of every numBits-length bit-set, in increasing lexographic order,...
Definition: utilities.cpp:1667
void setDiagMatrixOverrides(QMatrix &matr, int *numQubitsPerReg, int numRegs, enum bitEncoding encoding, long long int *overrideInds, qreal *overridePhases, int numOverrides)
Modifies the given diagonal matrix such that the diagonal elements which correspond to the coordinate...
Definition: utilities.cpp:1426
void toQureg(Qureg qureg, QVector vec)
Initialises the state-vector qureg to have the same amplitudes as vec.
Definition: utilities.cpp:1293
pauliOpType
Codes for specifying Pauli operators.
Definition: QuEST.h:113
#define qreal
A precision-agnostic floating point number, as determined by QuEST_PREC.
bitEncoding
Flags for specifying how the bits in sub-register computational basis states are mapped to indices in...
Definition: QuEST.h:288
#define qcomp
A precision-agnostic operator-overloaded complex number type.
Represents a 2x2 matrix of complex numbers.
Definition: QuEST.h:155
Represents a 4x4 matrix of complex numbers.
Definition: QuEST.h:193
Represents a general 2^N by 2^N matrix of complex numbers.
Definition: QuEST.h:204
Represents one complex number.
Definition: QuEST.h:121
Represents a diagonal complex operator on the full Hilbert state of a Qureg.
Definition: QuEST.h:317
A Pauli Hamiltonian, expressed as a real-weighted sum of pauli products, and which can hence represen...
Definition: QuEST.h:297
Information about the environment the program is running in.
Definition: QuEST.h:406
Represents a system of qubits.
Definition: QuEST.h:361
Represents a diagonal complex operator of a smaller dimension than the full Hilbert state of a Qureg.
Definition: QuEST.h:341