utilities.hpp
Go to the documentation of this file.
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 
25 extern QuESTEnv QUEST_ENV;
26 
36 #define NUM_QUBITS 5
37 
49 typedef std::vector<std::vector<qcomp>> QMatrix;
50 
60 typedef std::vector<qcomp> QVector;
61 
62 /* (Excluded from Doxygen doc)
63  *
64  * Define QVector and QMatrix operator overloads.
65  * Note that QMatrix overloads don't simply use QVector
66  * overloads, since the complex vector dot product involves
67  * conjugation, which doesn't occur in complex matrix multiplication.
68  * Note too we also avoid defining operators in terms of other operators
69  * (e.g. minus is plus(negative times)) since compiler optimisations
70  * may change the order of operations and confuse the overloads invoked.
71  * Definition of division using multiplication can furthermore
72  * heighten numerical errors.
73  *
74  * @ingroup testutilities
75  * @author Tyson Jones
76  */
77 QVector operator + (const QVector& v1, const QVector& v2);
78 QVector operator - (const QVector& v1, const QVector& v2);
79 QVector operator * (const qcomp& a, const QVector& v);
80 QVector operator * (const QVector& v, const qcomp& a);
81 QVector operator / (const QVector& v, const qcomp& a);
82 qcomp operator * (const QVector &v1, const QVector& v2);
83 void operator += (QVector& v1, const QVector& v2);
84 void operator -= (QVector& v1, const QVector& v2);
85 void operator *= (QVector& v1, const qcomp& a);
86 void operator /= (QVector& v1, const qcomp& a);
87 QMatrix operator + (const QMatrix& m1, const QMatrix& m2);
88 QMatrix operator - (const QMatrix& m1, const QMatrix& m2);
89 QMatrix operator * (const qcomp& a, const QMatrix& m);
90 QMatrix operator * (const QMatrix& m, const qcomp& a);
91 QMatrix operator / (const QMatrix& m, const qcomp& a);
92 QMatrix operator * (const QMatrix& m1, const QMatrix& m2);
93 void operator += (QMatrix& m1, const QMatrix& m2);
94 void operator -= (QMatrix& m1, const QMatrix& m2);
95 void operator *= (QMatrix& m1, const qreal& a);
96 void operator /= (QMatrix& m1, const qreal& a);
97 void operator *= (QMatrix& m1, const QMatrix& m2);
98 QVector operator * (const QMatrix& m, const QVector& v);
99 
107 QVector toQVector(Qureg qureg);
108 
117 
125 QMatrix toQMatrix(Qureg qureg);
126 
133 QMatrix toQMatrix(Complex alpha, Complex beta);
134 
141 
148 
155 
162 QMatrix toQMatrix(qreal* coeffs, pauliOpType* paulis, int numQubits, int numTerms);
163 
170 
177 
184 
192 
200 
209 
218 void toQureg(Qureg qureg, QVector vec);
219 
228 void toQureg(Qureg qureg, QMatrix mat);
229 
238 
244 QMatrix getZeroMatrix(size_t dim);
245 
251 QMatrix getIdentityMatrix(size_t dim);
252 
260 
271 
279 
287 QMatrix getSwapMatrix(int qb1, int qb2, int numQb);
288 
303 QMatrix getFullOperatorMatrix(int* ctrls, int numCtrls, int *targs, int numTargs, QMatrix op, int numQubits);
304 
313 QMatrix getKetBra(QVector ket, QVector bra);
314 
321 
329 int getRandomInt(int min, int max);
330 
338 qreal getRandomReal(qreal min, qreal max);
339 
348 
356 QVector getRandomQVector(int dim);
357 
365 QMatrix getRandomQMatrix(int dim);
366 
380 QMatrix getRandomUnitary(int numQb);
381 
389 QVector getRandomStateVector(int numQb);
390 
399 
407 
414 
421 
431 std::vector<QMatrix> getRandomKrausMap(int numQb, int numOps);
432 
438 std::vector<qreal> getRandomProbabilities(int numProbs);
439 
446 std::vector<QVector> getRandomOrthonormalVectors(int numQb, int numStates);
447 
454 QMatrix getMixedDensityMatrix(std::vector<qreal> probs, std::vector<QVector> states);
455 
462 
469 
475 QVector getDFT(QVector in, int* targs, int numTargs);
476 
483 long long int getValueOfTargets(long long int ind, int* targs, int numTargs);
484 
493 void setSubMatrix(QMatrix &dest, QMatrix sub, size_t r, size_t c);
494 
514 void applyReferenceOp(QMatrix &state, int* ctrls, int numCtrls, int *targs, int numTargs, QMatrix op);
515 
535 void applyReferenceOp(QMatrix &state, int* ctrls, int numCtrls, int targ1, int targ2, QMatrix op);
536 
555 void applyReferenceOp(QMatrix &state, int* ctrls, int numCtrls, int target, QMatrix op);
556 
575 void applyReferenceOp(QMatrix &state, int *targs, int numTargs, QMatrix op);
576 
594 void applyReferenceOp(QMatrix &state, int ctrl, int targ, QMatrix op);
595 
614 void applyReferenceOp(QMatrix &state, int ctrl, int* targs, int numTargs, QMatrix op);
615 
633 void applyReferenceOp(QMatrix &state, int ctrl, int targ1, int targ2, QMatrix op);
634 
652 void applyReferenceOp(QMatrix &state, int targ, QMatrix op);
653 
672 void applyReferenceOp(QVector &state, int* ctrls, int numCtrls, int *targs, int numTargs, QMatrix op);
673 
690 void applyReferenceOp(QVector &state, int* ctrls, int numCtrls, int targ1, int targ2, QMatrix op);
691 
708 void applyReferenceOp(QVector &state, int* ctrls, int numCtrls, int target, QMatrix op);
709 
725 void applyReferenceOp(QVector &state, int *targs, int numTargs, QMatrix op);
726 
742 void applyReferenceOp(QVector &state, int ctrl, int targ, QMatrix op);
743 
760 void applyReferenceOp(QVector &state, int ctrl, int* targs, int numTargs, QMatrix op);
761 
778 void applyReferenceOp(QVector &state, int ctrl, int targ1, int targ2, QMatrix op);
779 
795 void applyReferenceOp(QVector &state, int targ, QMatrix op);
796 
805 void applyReferenceMatrix(QVector &state, int* ctrls, int numCtrls, int *targs, int numTargs, QMatrix op);
806 
815 void applyReferenceMatrix(QMatrix &state, int* ctrls, int numCtrls, int *targs, int numTargs, QMatrix op);
816 
830 bool areEqual(Qureg qureg1, Qureg qureg2);
831 
844 bool areEqual(Qureg qureg, QVector vec);
845 
858 bool areEqual(Qureg qureg, QMatrix matr);
859 
873 bool areEqual(Qureg qureg1, Qureg qureg2, qreal precision);
874 
887 bool areEqual(Qureg qureg, QVector vec, qreal precision);
888 
901 bool areEqual(Qureg qureg, QMatrix matr, qreal precision);
902 
909 bool areEqual(QVector a, QVector b);
910 
917 bool areEqual(QMatrix a, QMatrix b);
918 
925 bool areEqual(QVector vec, qreal* reals, qreal* imags);
926 
934 bool areEqual(QVector vec, qreal* reals);
935 
940 qcomp expI(qreal phase);
941 
947 unsigned int calcLog2(long unsigned int res);
948 
955 void setRandomPauliSum(qreal* coeffs, pauliOpType* codes, int numQubits, int numTerms);
956 
962 void setRandomPauliSum(PauliHamil hamil);
963 
971 
979 long long int getTwosComplement(long long int decimal, int numBits);
980 
988 long long int getUnsigned(long long int twosComp, int numBits);
989 
1001 void setDiagMatrixOverrides(QMatrix &matr, int* numQubitsPerReg, int numRegs, enum bitEncoding encoding, long long int* overrideInds, qreal* overridePhases, int numOverrides);
1002 
1011 void setUniqueFilename(char* outFn, char* prefix);
1012 
1019 void writeToFileSynch(char* fn, const string& contents);
1020 
1027 void deleteFilesWithPrefixSynch(char* prefix);
1028 
1029 // makes below signatures more concise
1030 template<class T> using CatchGen = Catch::Generators::GeneratorWrapper<T>;
1031 
1054 CatchGen<int*> sublists(int* list, int len, int sublen);
1055 
1081 CatchGen<int*> sublists(CatchGen<int>&& gen, int numSamps, const int* exclude, int numExclude);
1082 
1108 CatchGen<int*> sublists(CatchGen<int>&& gen, int numSamps, int excluded);
1109 
1130 CatchGen<int*> sublists(CatchGen<int>&& gen, int sublen);
1131 
1146 CatchGen<int*> bitsets(int numBits);
1147 
1164 CatchGen<int*> sequences(int base, int numDigits);
1165 
1182 CatchGen<pauliOpType*> pauliseqs(int numPaulis);
1183 
1184 #endif // QUEST_TEST_UTILS_H
pauliOpType
Codes for specifying Pauli operators.
Definition: QuEST.h:96
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:304
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:1298
Catch::Generators::GeneratorWrapper< T > CatchGen
Definition: utilities.hpp:1030
QVector getKroneckerProduct(QVector b, QVector a)
Returns b (otimes) a.
Definition: utilities.cpp:143
qcomp expI(qreal phase)
Returns the unit-norm complex number exp(i*phase).
Definition: utilities.cpp:417
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:784
QMatrix getConjugateTranspose(QMatrix a)
Returns the conjugate transpose of the complex square matrix a.
Definition: utilities.cpp:189
int getRandomInt(int min, int max)
Returns a random integer between min (inclusive) and max (exclusive), from the uniform distribution.
Definition: utilities.cpp:526
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:578
QMatrix getRandomPureDensityMatrix(int numQb)
Returns a random numQb-by-numQb density matrix, from an undisclosed distribution, which is pure (corr...
Definition: utilities.cpp:511
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:640
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:230
QMatrix getRandomUnitary(int numQb)
Returns a uniformly random (under Haar) 2^numQb-by-2^numQb unitary matrix.
Definition: utilities.cpp:530
void writeToFileSynch(char *fn, const string &contents)
Writes contents to the file with filename fn, which is created and/or overwritten.
Definition: utilities.cpp:1362
qreal getRandomReal(qreal min, qreal max)
Returns a random real between min (inclusive) and max (exclusive), from the uniform distribution.
Definition: utilities.cpp:421
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:1316
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:169
QuESTEnv QUEST_ENV
The single QuESTEnv environment created before the Catch tests begin, and destroyed thereafter.
Definition: main.cpp:20
void operator/=(QVector &v1, const qcomp &a)
Definition: utilities.cpp:73
std::vector< QVector > getRandomOrthonormalVectors(int numQb, int numStates)
Returns a list of random orthonormal complex vectors, from an undisclosed distribution.
Definition: utilities.cpp:613
Represents a 4x4 matrix of complex numbers.
Definition: QuEST.h:175
Information about the environment the program is running in.
Definition: QuEST.h:362
Represents a general 2^N by 2^N matrix of complex numbers.
Definition: QuEST.h:186
#define qreal
void toQureg(Qureg qureg, QVector vec)
Initialises the state-vector qureg to have the same amplitudes as vec.
Definition: utilities.cpp:1201
unsigned int calcLog2(long unsigned int res)
Returns log2 of numbers which must be gauranteed to be 2^n.
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:216
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:1358
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:1229
void toComplexMatrixN(QMatrix qm, ComplexMatrixN cm)
Initialises cm with the values of qm.
Definition: utilities.cpp:1033
std::vector< qcomp > QVector
A complex vector, which can be zero-initialised with QVector(numAmps).
Definition: utilities.hpp:60
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:222
QVector toQVector(Qureg qureg)
Returns an equal-size copy of the given state-vector qureg.
Definition: utilities.cpp:1113
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:1562
#define qcomp
ComplexMatrix4 toComplexMatrix4(QMatrix qm)
Returns a ComplexMatrix4 copy of QMatix qm.
Definition: utilities.cpp:1027
QMatrix toDiagonalQMatrix(QVector vec)
Returns a diagonal complex matrix formed by the given vector.
Definition: utilities.cpp:1309
bool areEqual(Qureg qureg1, Qureg qureg2)
Performs a hardware-agnostic comparison of the given quregs, checking whether the difference between ...
Definition: utilities.cpp:879
Represents a diagonal complex operator on the full Hilbert state of a Qureg.
Definition: QuEST.h:297
A Pauli Hamiltonian, expressed as a real-weighted sum of pauli products, and which can hence represen...
Definition: QuEST.h:277
void operator-=(QVector &v1, const QVector &v2)
Definition: utilities.cpp:67
QVector getRandomStateVector(int numQb)
Returns a random numQb-length L2-normalised state-vector from an undisclosed distribution.
Definition: utilities.cpp:468
void setRandomDiagPauliHamil(PauliHamil hamil)
Populates hamil with random coefficients and a random amount number of PAULI_I and PAULI_Z operators.
Definition: utilities.cpp:1241
QVector operator-(const QVector &v1, const QVector &v2)
Definition: utilities.cpp:33
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:379
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:1286
Represents a system of qubits.
Definition: QuEST.h:322
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:1567
QVector operator*(const qcomp &a, const QVector &v)
Definition: utilities.cpp:40
std::vector< std::vector< qcomp > > QMatrix
A complex square matrix.
Definition: utilities.hpp:49
ComplexMatrix2 toComplexMatrix2(QMatrix qm)
Returns a ComplexMatrix2 copy of QMatix qm.
Definition: utilities.cpp:1021
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:1488
QVector getNormalised(QVector vec)
Returns an L2-normalised copy of vec, using Kahan summation for improved accuracy.
Definition: utilities.cpp:446
std::vector< qreal > getRandomProbabilities(int numProbs)
Returns a list of random real scalars, each in [0, 1], which sum to unity.
Definition: utilities.cpp:472
QMatrix getIdentityMatrix(size_t dim)
Returns a dim-by-dim identity matrix.
Definition: utilities.cpp:161
QMatrix getPureDensityMatrix(QVector state)
Returns a density matrix initialised into the given pure state.
Definition: utilities.cpp:507
void deleteFilesWithPrefixSynch(char *prefix)
Deletes all files with filename starting with prefix.
Definition: utilities.cpp:1375
QMatrix getRandomDensityMatrix(int numQb)
Returns a random numQb-by-numQb density matrix, from an undisclosed distribution, in a very mixed sta...
Definition: utilities.cpp:490
QVector operator/(const QVector &v, const qcomp &a)
Definition: utilities.cpp:49
QMatrix toQMatrix(Qureg qureg)
Returns an equal-size copy of the given density matrix qureg.
Definition: utilities.cpp:1071
QMatrix getExponentialOfDiagonalMatrix(QMatrix a)
Returns the matrix exponential of a diagonal, square, complex matrix.
Definition: utilities.cpp:197
QMatrix getZeroMatrix(size_t dim)
Returns a dim-by-dim square complex matrix, initialised to all zeroes.
Definition: utilities.cpp:153
void operator*=(QVector &v1, const qcomp &a)
Definition: utilities.cpp:70
Represents one complex number.
Definition: QuEST.h:103
void operator+=(QVector &v1, const QVector &v2)
Definition: utilities.cpp:64
QVector getRandomQVector(int dim)
Returns a dim-length vector with random complex amplitudes in the square joining {-1-i,...
Definition: utilities.cpp:435
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:841
QVector getDFT(QVector in)
Returns the discrete fourier transform of vector in.
Definition: utilities.cpp:652
QVector getMatrixDiagonal(QMatrix matr)
Returns the diagonal vector of the given matrix.
Definition: utilities.cpp:517
CatchGen< int * > bitsets(int numBits)
Returns a Catch2 generator of every numBits-length bit-set, in increasing lexographic order,...
Definition: utilities.cpp:1557
qcomp getRandomComplex()
Returns a random complex number within the square closing (-1-i) and (1+i), from a distribution unifo...
Definition: utilities.cpp:431
bitEncoding
Flags for specifying how the bits in sub-register computational basis states are mapped to indices in...
Definition: QuEST.h:269
QVector operator+(const QVector &v1, const QVector &v2)
Definition: utilities.cpp:26
Represents a 2x2 matrix of complex numbers.
Definition: QuEST.h:137
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:670