Skip to content

Commit

Permalink
Rounding (#764)
Browse files Browse the repository at this point in the history
* FP_NORM_EPSILON comparison for equality

* Revert stabilizer
  • Loading branch information
WrathfulSpatula authored May 12, 2021
1 parent 5d7db1e commit 020e38b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/qinterface/protected.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ bool QInterface::IsIdentity(const complex* mtrx, bool isControlled)
{
// If the effect of applying the buffer would be (approximately or exactly) that of applying the identity
// operator, then we can discard this buffer without applying it.
if ((mtrx[0] != mtrx[3]) || (mtrx[1] != ZERO_CMPLX) || (mtrx[2] != ZERO_CMPLX)) {
if ((norm(mtrx[0] - mtrx[3]) > FP_NORM_EPSILON) || (norm(mtrx[1]) > FP_NORM_EPSILON) ||
(norm(mtrx[2]) > FP_NORM_EPSILON)) {
return false;
}

Expand All @@ -238,7 +239,7 @@ bool QInterface::IsIdentity(const complex* mtrx, bool isControlled)
// the user's purposes. If the global phase offset has not been randomized, user code might explicitly depend on
// the global phase offset.

if ((isControlled || !randGlobalPhase) && (mtrx[0] != ONE_CMPLX)) {
if ((isControlled || !randGlobalPhase) && (norm(mtrx[0] - ONE_CMPLX) > FP_NORM_EPSILON)) {
return false;
}

Expand Down
6 changes: 4 additions & 2 deletions src/qpager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "qfactory.hpp"
#include "qpager.hpp"

#define IS_NORM_0(c) (norm(c) <= FP_NORM_EPSILON)

namespace Qrack {

QPager::QPager(QInterfaceEngine eng, bitLenInt qBitCount, bitCapInt initState, qrack_rand_gen_ptr rgp, complex phaseFac,
Expand Down Expand Up @@ -286,12 +288,12 @@ void QPager::MetaControlled(bool anti, std::vector<bitLenInt> controls, bitLenIn

bool isSpecial, isInvert;
complex top, bottom;
if ((mtrx[1] == ZERO_CMPLX) && (mtrx[2] == ZERO_CMPLX)) {
if (IS_NORM_0(mtrx[1]) && IS_NORM_0(mtrx[2])) {
isSpecial = true;
isInvert = false;
top = mtrx[0];
bottom = mtrx[3];
} else if ((mtrx[0] == ZERO_CMPLX) && (mtrx[3] == ZERO_CMPLX)) {
} else if (IS_NORM_0(mtrx[0]) && IS_NORM_0(mtrx[3])) {
isSpecial = true;
isInvert = true;
top = mtrx[1];
Expand Down
3 changes: 1 addition & 2 deletions src/qstabilizerhybrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,7 @@ void QStabilizerHybrid::Dispose(bitLenInt start, bitLenInt length)
return;
}

if (stabilizer && !stabilizer->CanDecomposeDispose(start, length))
{
if (stabilizer && !stabilizer->CanDecomposeDispose(start, length)) {
SwitchToEngine();
}

Expand Down
2 changes: 1 addition & 1 deletion src/qunit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ complex QUnit::GetAmplitude(bitCapInt perm)
}
}

if ((shards[0].GetQubitCount() > 1) && IS_1_R1(norm(result)) && (randGlobalPhase || (result == ONE_CMPLX))) {
if ((shards[0].GetQubitCount() > 1) && IS_1_R1(norm(result)) && (randGlobalPhase || IS_AMP_0(result - ONE_CMPLX))) {
SetPermutation(perm);
}

Expand Down

0 comments on commit 020e38b

Please sign in to comment.