Skip to content

Commit

Permalink
adding random unitary failsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
TysonRayJones committed Sep 21, 2023
1 parent 956cb2c commit 6ffe631
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions tests/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,16 @@ QMatrix getOrthonormalisedRows(QMatrix matr) {
return matr;
}

QMatrix getRandomDiagonalUnitary(int numQb) {
DEMAND( numQb >= 1 );

QMatrix matr = getZeroMatrix(1 << numQb);
for (size_t i=0; i<matr.size(); i++)
matr[i][i] = expI(getRandomReal(0,4*M_PI));

return matr;
}

QMatrix getRandomUnitary(int numQb) {
DEMAND( numQb >= 1 );

Expand All @@ -624,10 +634,12 @@ QMatrix getRandomUnitary(int numQb) {
// create U = Q D
QMatrix matrU = matrQ * matrD;

// ensure matrix is indeed unitary
// in the rare scenario the result is not sufficiently precisely unitary,
// replace it with a trivially unitary diagonal matrix
QMatrix daggerProd = matrU * getConjugateTranspose(matrU);
QMatrix iden = getIdentityMatrix(dim);
DEMAND( areEqual(daggerProd, iden, 1E2*REAL_EPS) );
if( ! areEqual(daggerProd, iden) )
matrU = getRandomDiagonalUnitary(numQb);

return matrU;
}
Expand Down Expand Up @@ -662,7 +674,12 @@ std::vector<QMatrix> getRandomKrausMap(int numQb, int numOps) {
QMatrix prodSum = getZeroMatrix(1 << numQb);
for (int i=0; i<numOps; i++)
prodSum += getConjugateTranspose(ops[i]) * ops[i];
DEMAND( areEqual(prodSum, iden, 1E2*REAL_EPS) );

// in the rare scenario it is insufficiently numerically precise,
// replace the map with trivially precise diagonals
if( ! areEqual(prodSum, iden) )
for (int i=0; i<numOps; i++)
ops[i] = weights[i] * getRandomDiagonalUnitary(numQb);

return ops;
}
Expand Down

0 comments on commit 6ffe631

Please sign in to comment.