Skip to content

Commit

Permalink
Debug QUnitMulti::TrySeparate()
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathfulSpatula committed Nov 6, 2024
1 parent 610b9cf commit c4594a5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
9 changes: 9 additions & 0 deletions include/qunit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class QUnit : public QParity, public QInterface {
bool useHostRam;
bool isReactiveSeparate;
bool useTGadget;
bool isBdt;
bitLenInt thresholdQubits;
real1_f separabilityThreshold;
real1_f roundingThreshold;
Expand Down Expand Up @@ -119,6 +120,14 @@ class QUnit : public QParity, public QInterface {
}

const bitLenInt length = dest->GetQubitCount();

if ((!length) || (length == qubitCount)) {
return true;
}

if (!isBdt) {
return QInterface::TryDecompose(start, dest, error_tol);
}

if ((start + length) > qubitCount) {
throw std::invalid_argument("QUnit::TryDecompose qubit range out-of-bounds!");
Expand Down
12 changes: 12 additions & 0 deletions src/qunit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ QUnit::QUnit(std::vector<QInterfaceEngine> eng, bitLenInt qBitCount, const bitCa
, freezeBasis2Qb(false)
, useHostRam(useHostMem)
, useTGadget(true)
, isBdt(false)
, thresholdQubits(qubitThreshold)
, separabilityThreshold(sep_thresh)
, logFidelity(0.0)
Expand All @@ -80,6 +81,17 @@ QUnit::QUnit(std::vector<QInterfaceEngine> eng, bitLenInt qBitCount, const bitCa
{
if (engines.empty()) {
engines.push_back(QINTERFACE_STABILIZER_HYBRID);
} else {
for (size_t i = 0U; i < engines.size(); ++i) {
QInterfaceEngine e = engines[i];
if ((e == QINTERFACE_CPU) || (e == QINTERFACE_OPENCL) || (e == QINTERFACE_CUDA) || (e == QINTERFACE_HYBRID) || (e == QINTERFACE_BDT_HYBRID)) {
break;
}
if (e == QINTERFACE_BDT) {
isBdt = true;
break;
}
}
}

isReactiveSeparate = (separabilityThreshold > FP_NORM_EPSILON_F);
Expand Down
37 changes: 27 additions & 10 deletions test/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2833,9 +2833,6 @@ TEST_CASE_METHOD(QInterfaceTestFixture, "test_isfinished")

TEST_CASE_METHOD(QInterfaceTestFixture, "test_tryseparate")
{
const bool isStabilizerQBdt =
(testSubSubEngineType == QINTERFACE_BDT) && (testSubEngineType == QINTERFACE_STABILIZER_HYBRID);

qftReg->SetPermutation(85);

int i;
Expand All @@ -2846,10 +2843,6 @@ TEST_CASE_METHOD(QInterfaceTestFixture, "test_tryseparate")

for (i = 0; i < 8; i++) {
qftReg->TrySeparate(i);
if (!isStabilizerQBdt) {
const std::vector<bitLenInt> toSep{ (bitLenInt)i };
qftReg->TrySeparate(toSep, FP_NORM_EPSILON_F);
}
}

REQUIRE_THAT(qftReg, HasProbability(0, 8, 85));
Expand All @@ -2860,10 +2853,34 @@ TEST_CASE_METHOD(QInterfaceTestFixture, "test_tryseparate")
qftReg->CNOT(0, 2);
qftReg->CNOT(0, 2);
qftReg->TrySeparate(0, 1);
if (!isStabilizerQBdt) {
const std::vector<bitLenInt> toSep{ 0, 1 };
qftReg->TrySeparate(toSep, FP_NORM_EPSILON_F);
qftReg->CNOT(0, 1);
qftReg->Z(0);
qftReg->H(0);
REQUIRE_THAT(qftReg, HasProbability(0, 8, 1));
}

TEST_CASE_METHOD(QInterfaceTestFixture, "test_tryseparate_tolerance")
{
qftReg->SetPermutation(85);

bitLenInt i;

qftReg->QFT(0, 8);

qftReg->IQFT(0, 8);

for (i = 0; i < 8; i++) {
qftReg->TrySeparate(std::vector<bitLenInt>({ i }), FP_NORM_EPSILON);
}

REQUIRE_THAT(qftReg, HasProbability(0, 8, 85));

qftReg->SetPermutation(0);
qftReg->H(0);
qftReg->CNOT(0, 1);
qftReg->CNOT(0, 2);
qftReg->CNOT(0, 2);
qftReg->TrySeparate({ 0, 1 }, FP_NORM_EPSILON);
qftReg->CNOT(0, 1);
qftReg->Z(0);
qftReg->H(0);
Expand Down

0 comments on commit c4594a5

Please sign in to comment.