Skip to content

Commit

Permalink
Introduce HALF_R1 macro
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathfulSpatula committed Nov 13, 2024
1 parent 7752f28 commit fdd3158
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 32 deletions.
5 changes: 5 additions & 0 deletions include/common/qrack_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,11 @@ typedef std::shared_ptr<QEngine> QEnginePtr;

#if FPPOW < 6
#define ZERO_R1_F 0.0f
#define HALF_R1_F 0.5f
#define ONE_R1_F 1.0f
#else
#define ZERO_R1_F 0.0
#define HALF_R1_F 0.5
#define ONE_R1_F 1.0
#endif

Expand All @@ -173,12 +175,15 @@ QRACK_CONST real1 SQRT1_2_R1 = (real1)M_SQRT1_2;

#if (FPPOW < 5) || (FPPOW > 6)
QRACK_CONST real1 ZERO_R1 = (real1)0.0f;
QRACK_CONST real1 HALF_R1 = (real1)0.5f;
QRACK_CONST real1 ONE_R1 = (real1)1.0f;
#elif FPPOW == 5
#define ZERO_R1 0.0f
#define HALF_R1 0.5f
#define ONE_R1 1.0f
#else
#define ZERO_R1 0.0
#define HALF_R1 0.5
#define ONE_R1 1.0
#endif

Expand Down
4 changes: 2 additions & 2 deletions include/qstabilizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class QStabilizer : public QInterface {

return (bool)((rawRandBools >> rawRandBoolsRemaining) & 1U);
} else {
return (rand_distribution(*rand_generator) >= (ONE_R1_F / 2));
return (rand_distribution(*rand_generator) >= HALF_R1_F);
}
}

Expand Down Expand Up @@ -435,7 +435,7 @@ class QStabilizer : public QInterface {
(std::arg(thisAmpEntry.amplitude) - std::arg(toCompare->GetAmplitude(thisAmpEntry.permutation))) /
(2 * PI_R1));
argDiff -= (real1)(size_t)argDiff;
if (argDiff > (ONE_R1 / 2)) {
if (argDiff > HALF_R1) {
argDiff -= ONE_R1;
}
if (FP_NORM_EPSILON >= abs(argDiff)) {
Expand Down
4 changes: 2 additions & 2 deletions include/qstabilizerhybrid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class QStabilizerHybrid : public QParity, public QInterface {
QStabilizerHybridPtr RdmCloneHelper()
{
QStabilizerHybridPtr clone = std::dynamic_pointer_cast<QStabilizerHybrid>(Clone());
clone->RdmCloneFlush(ONE_R1 / 2);
clone->RdmCloneFlush(HALF_R1);

return clone;
}
Expand Down Expand Up @@ -410,7 +410,7 @@ class QStabilizerHybrid : public QParity, public QInterface {
QRACK_CONST complex pauliZ[4]{ ONE_CMPLX, ZERO_CMPLX, ZERO_CMPLX, ONE_CMPLX_NEG };
complex pMtrx[4];
mul2x2(dMtrx.get(), pauliZ, pMtrx);
return (ONE_R1 - std::real(pMtrx[0] + pMtrx[1])) / 2;
return (ONE_R1 - std::real(pMtrx[0]) + std::real(pMtrx[1])) / 2;
}

real1_f CProbRdm(bitLenInt control, bitLenInt target)
Expand Down
32 changes: 16 additions & 16 deletions include/qunit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,40 +734,40 @@ class QUnit : public QParity, public QInterface {

void TransformX2x2(const complex* mtrxIn, complex* mtrxOut)
{
mtrxOut[0U] = (real1)(ONE_R1 / 2) * (complex)(mtrxIn[0U] + mtrxIn[1U] + mtrxIn[2U] + mtrxIn[3U]);
mtrxOut[1U] = (real1)(ONE_R1 / 2) * (complex)(mtrxIn[0U] - mtrxIn[1U] + mtrxIn[2U] - mtrxIn[3U]);
mtrxOut[2U] = (real1)(ONE_R1 / 2) * (complex)(mtrxIn[0U] + mtrxIn[1U] - mtrxIn[2U] - mtrxIn[3U]);
mtrxOut[3U] = (real1)(ONE_R1 / 2) * (complex)(mtrxIn[0U] - mtrxIn[1U] - mtrxIn[2U] + mtrxIn[3U]);
mtrxOut[0U] = HALF_R1 * (mtrxIn[0U] + mtrxIn[1U] + mtrxIn[2U] + mtrxIn[3U]);
mtrxOut[1U] = HALF_R1 * (mtrxIn[0U] - mtrxIn[1U] + mtrxIn[2U] - mtrxIn[3U]);
mtrxOut[2U] = HALF_R1 * (mtrxIn[0U] + mtrxIn[1U] - mtrxIn[2U] - mtrxIn[3U]);
mtrxOut[3U] = HALF_R1 * (mtrxIn[0U] - mtrxIn[1U] - mtrxIn[2U] + mtrxIn[3U]);
}

void TransformXInvert(const complex& topRight, const complex& bottomLeft, complex* mtrxOut)
{
mtrxOut[0U] = (real1)(ONE_R1 / 2) * (complex)(topRight + bottomLeft);
mtrxOut[1U] = (real1)(ONE_R1 / 2) * (complex)(-topRight + bottomLeft);
mtrxOut[0U] = HALF_R1 * (topRight + bottomLeft);
mtrxOut[1U] = HALF_R1 * (-topRight + bottomLeft);
mtrxOut[2U] = -mtrxOut[1U];
mtrxOut[3U] = -mtrxOut[0U];
}

void TransformY2x2(const complex* mtrxIn, complex* mtrxOut)
{
mtrxOut[0U] = (real1)(ONE_R1 / 2) * (complex)(mtrxIn[0U] + I_CMPLX * (mtrxIn[1U] - mtrxIn[2U]) + mtrxIn[3U]);
mtrxOut[1U] = (real1)(ONE_R1 / 2) * (complex)(mtrxIn[0U] - I_CMPLX * (mtrxIn[1U] + mtrxIn[2U]) - mtrxIn[3U]);
mtrxOut[2U] = (real1)(ONE_R1 / 2) * (complex)(mtrxIn[0U] + I_CMPLX * (mtrxIn[1U] + mtrxIn[2U]) - mtrxIn[3U]);
mtrxOut[3U] = (real1)(ONE_R1 / 2) * (complex)(mtrxIn[0U] - I_CMPLX * (mtrxIn[1U] - mtrxIn[2U]) + mtrxIn[3U]);
mtrxOut[0U] = HALF_R1 * (mtrxIn[0U] + I_CMPLX * (mtrxIn[1U] - mtrxIn[2U]) + mtrxIn[3U]);
mtrxOut[1U] = HALF_R1 * (mtrxIn[0U] - I_CMPLX * (mtrxIn[1U] + mtrxIn[2U]) - mtrxIn[3U]);
mtrxOut[2U] = HALF_R1 * (mtrxIn[0U] + I_CMPLX * (mtrxIn[1U] + mtrxIn[2U]) - mtrxIn[3U]);
mtrxOut[3U] = HALF_R1 * (mtrxIn[0U] - I_CMPLX * (mtrxIn[1U] - mtrxIn[2U]) + mtrxIn[3U]);
}

void TransformYInvert(const complex& topRight, const complex& bottomLeft, complex* mtrxOut)
{
mtrxOut[0U] = I_CMPLX * (real1)(ONE_R1 / 2) * (complex)(topRight - bottomLeft);
mtrxOut[1U] = I_CMPLX * (real1)(ONE_R1 / 2) * (complex)(-topRight - bottomLeft);
mtrxOut[0U] = I_CMPLX * HALF_R1 * (topRight - bottomLeft);
mtrxOut[1U] = I_CMPLX * HALF_R1 * (-topRight - bottomLeft);
mtrxOut[2U] = -mtrxOut[1U];
mtrxOut[3U] = -mtrxOut[0U];
}

void TransformPhase(const complex& topLeft, const complex& bottomRight, complex* mtrxOut)
{
mtrxOut[0U] = (real1)(ONE_R1 / 2) * (complex)(topLeft + bottomRight);
mtrxOut[1U] = (real1)(ONE_R1 / 2) * (complex)(topLeft - bottomRight);
mtrxOut[0U] = HALF_R1 * (topLeft + bottomRight);
mtrxOut[1U] = HALF_R1 * (topLeft - bottomRight);
mtrxOut[2U] = mtrxOut[1U];
mtrxOut[3U] = mtrxOut[0U];
}
Expand Down Expand Up @@ -805,8 +805,8 @@ class QUnit : public QParity, public QInterface {
return;
}

QRACK_CONST complex diag = complex((real1)(ONE_R1 / 2), (real1)(ONE_R1 / 2));
QRACK_CONST complex cross = complex((real1)(ONE_R1 / 2), (real1)(-ONE_R1 / 2));
QRACK_CONST complex diag = complex(HALF_R1, HALF_R1);
QRACK_CONST complex cross = complex(HALF_R1, -HALF_R1);
QRACK_CONST complex mtrx[4U]{ diag, cross, cross, diag };

const complex Y0 = shard.amp0;
Expand Down
2 changes: 1 addition & 1 deletion src/pinvoke_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2147,7 +2147,7 @@ MICROSOFT_QUANTUM_DECL uintq Measure(_In_ uintq sid, _In_ uintq n, _In_reads_(n)

double jointProb = _JointEnsembleProbabilityHelper(simulator, n, b, q, true);

toRet = (jointProb < (ONE_R1 / 2)) ? 0U : 1U;
toRet = (jointProb < HALF_R1) ? 0U : 1U;

RevertPauliBasis(simulator, n, b, q);
} catch (const std::exception& ex) {
Expand Down
2 changes: 1 addition & 1 deletion src/qstabilizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1833,7 +1833,7 @@ real1_f QStabilizer::Prob(bitLenInt qubit)
}

// Otherwise, state appears locally maximally mixed.
return ONE_R1_F / 2;
return HALF_R1_F;
}

void QStabilizer::Mtrx(const complex* mtrx, bitLenInt target)
Expand Down
4 changes: 2 additions & 2 deletions src/qstabilizerhybrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1481,15 +1481,15 @@ real1_f QStabilizerHybrid::Prob(bitLenInt qubit)
}

// Otherwise, buffer will not change the fact that state appears maximally mixed.
return ONE_R1_F / 2;
return HALF_R1_F;
}

if (stabilizer->IsSeparableZ(qubit)) {
return stabilizer->M(qubit) ? ONE_R1_F : ZERO_R1_F;
}

// Otherwise, state appears locally maximally mixed.
return ONE_R1_F / 2;
return HALF_R1_F;
}

bool QStabilizerHybrid::ForceM(bitLenInt qubit, bool result, bool doForce, bool doApply)
Expand Down
12 changes: 6 additions & 6 deletions src/qunit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ void QUnit::SortUnit(QInterfacePtr unit, std::vector<QSortEntry>& bits, bitLenIn
}
return;
}
QSortEntry pivot = bits[(low + high) / 2U];
QSortEntry pivot = bits[(low + high) >> 1U];

while (i <= j) {
while (bits[i] < pivot) {
Expand Down Expand Up @@ -1347,10 +1347,10 @@ bool QUnit::SeparateBit(bool value, bitLenInt qubit)
return true;
}

const real1_f prob = ONE_R1_F / 2 - unit->Prob(shard.mapped);
const real1_f prob = HALF_R1_F - unit->Prob(shard.mapped);
unit->Dispose(mapped, 1U, value ? ONE_BCI : ZERO_BCI);

if (!unit->isBinaryDecisionTree() && ((ONE_R1 / 2 - abs(prob)) > FP_NORM_EPSILON)) {
if (!unit->isBinaryDecisionTree() && ((HALF_R1 - abs(prob)) > FP_NORM_EPSILON)) {
unit->UpdateRunningNorm();
if (!doNormalize) {
unit->NormalizeState();
Expand Down Expand Up @@ -4017,7 +4017,7 @@ void QUnit::ApplyBuffer(PhaseShardPtr phaseShard, bitLenInt control, bitLenInt t
bool ptHi = pt > pc;
real1_f pHi = ptHi ? pt : pc;
real1_f pLo = ptHi ? pc : pt;
bool pState = abs(pHi - (ONE_R1_F / 2)) >= abs(pLo - (ONE_R1_F / 2));
bool pState = abs(pHi - HALF_R1) >= abs(pLo - HALF_R1);

logFidelity += angleFrac(polarBottom) * log(pState ? pHi : (ONE_R1_F - pLo));
if (logFidelity <= FIDELITY_MIN) {
Expand All @@ -4038,7 +4038,7 @@ void QUnit::ApplyBuffer(PhaseShardPtr phaseShard, bitLenInt control, bitLenInt t
ptHi = pt > pc;
pHi = ptHi ? pt : pc;
pLo = ptHi ? pc : pt;
pState = abs(pHi - (ONE_R1_F / 2)) >= abs(pLo - (ONE_R1_F / 2));
pState = abs(pHi - HALF_R1) >= abs(pLo - HALF_R1);

logFidelity += angleFrac(polarTop) * log(pState ? pHi : (ONE_R1_F - pLo));
if (logFidelity <= FIDELITY_MIN) {
Expand All @@ -4063,7 +4063,7 @@ void QUnit::ApplyBuffer(PhaseShardPtr phaseShard, bitLenInt control, bitLenInt t
ptHi = pth > pch;
pHi = ptHi ? pth : pch;
pLo = ptHi ? pch : pth;
pState = abs(pHi - (ONE_R1_F / 2)) >= abs(pLo - (ONE_R1_F / 2));
pState = abs(pHi - HALF_R1) >= abs(pLo - HALF_R1);

logFidelity += log(pState ? pHi : (ONE_R1_F - pLo));
if (logFidelity <= FIDELITY_MIN) {
Expand Down
2 changes: 1 addition & 1 deletion src/qunitclifford.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ void QUnitClifford::SortUnit(QStabilizerPtr unit, std::vector<QSortEntry>& bits,
}
return;
}
QSortEntry pivot = bits[(low + high) / 2U];
QSortEntry pivot = bits[(low + high) >> 1U];

while (i <= j) {
while (bits[i] < pivot) {
Expand Down
2 changes: 1 addition & 1 deletion src/wasm_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ bool Measure(quid sid, std::vector<QubitPauliBasis> q)
SIMULATOR_LOCK_GUARD_INT(sid)

TransformPauliBasis(simulator, q);
const bool toRet = (ONE_R1 / 2) <= _JointEnsembleProbabilityHelper(simulator, q, true);
const bool toRet = HALF_R1 <= _JointEnsembleProbabilityHelper(simulator, q, true);
RevertPauliBasis(simulator, q);

return toRet;
Expand Down

0 comments on commit fdd3158

Please sign in to comment.