Skip to content

Commit

Permalink
♻️ automatically increase ref count on state creation
Browse files Browse the repository at this point in the history
Signed-off-by: burgholzer <burgholzer@me.com>
  • Loading branch information
burgholzer committed Aug 19, 2024
1 parent 83fe4f3 commit 7860cb8
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 18 deletions.
16 changes: 13 additions & 3 deletions include/mqt-core/dd/Package.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ template <class Config> class Package {
static_cast<Qubit>(p),
std::array{f, dEdge::zero(), dEdge::zero(), dEdge::zero()});
}
incRef(f);
return f;
}

Expand All @@ -319,6 +320,7 @@ template <class Config> class Package {
for (std::size_t p = start; p < n + start; p++) {
f = makeDDNode(static_cast<Qubit>(p), std::array{f, vEdge::zero()});
}
incRef(f);
return f;
}
// generate computational basis state |i> with n qubits
Expand All @@ -339,6 +341,7 @@ template <class Config> class Package {
f = makeDDNode(static_cast<Qubit>(p), std::array{vEdge::zero(), f});
}
}
incRef(f);
return f;
}
// generate general basis state with n qubits
Expand Down Expand Up @@ -391,7 +394,9 @@ template <class Config> class Package {
break;
}
}
return {f.p, cn.lookup(f.w)};
vEdge e{f.p, cn.lookup(f.w)};
incRef(e);
return e;
}

// generate general GHZ state with n qubits
Expand All @@ -418,11 +423,13 @@ template <class Config> class Package {
std::array{vEdge::zero(), rightSubtree});
}

return makeDDNode(
vEdge e = makeDDNode(
static_cast<Qubit>(n - 1),
std::array<vEdge, RADIX>{
{{leftSubtree.p, {&constants::sqrt2over2, &constants::zero}},
{rightSubtree.p, {&constants::sqrt2over2, &constants::zero}}}});
incRef(e);
return e;
}

// generate general W state with n qubits
Expand Down Expand Up @@ -459,6 +466,7 @@ template <class Config> class Package {
std::array{rightSubtree, vEdge::zero()});
}
}
incRef(leftSubtree);
return leftSubtree;
}

Expand All @@ -480,7 +488,9 @@ template <class Config> class Package {
const auto level = static_cast<Qubit>(std::log2(length) - 1);
const auto state =
makeStateFromVector(stateVector.begin(), stateVector.end(), level);
return {state.p, cn.lookup(state.w)};
vEdge e{state.p, cn.lookup(state.w)};
incRef(e);
return e;
}

/**
Expand Down
1 change: 0 additions & 1 deletion include/mqt-core/dd/Simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ VectorDD simulate(const QuantumComputation* qc, const VectorDD& in,
// measurements are currently not supported here
auto permutation = qc->initialLayout;
auto e = in;
dd.incRef(e);

for (const auto& op : *qc) {
e = applyUnitaryOperation(op.get(), e, dd, permutation);
Expand Down
8 changes: 6 additions & 2 deletions src/dd/Simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ simulate(const QuantumComputation* qc, const VectorDD& in, Package<Config>& dd,
// simulate once and measure all qubits repeatedly
auto permutation = qc->initialLayout;
auto e = in;
dd.incRef(e);

for (const auto& op : *qc) {
// simply skip any non-unitary
Expand Down Expand Up @@ -135,11 +134,13 @@ simulate(const QuantumComputation* qc, const VectorDD& in, Package<Config>& dd,
std::map<std::string, std::size_t> counts{};

for (std::size_t i = 0U; i < shots; i++) {
// increase reference count of input state so that it is collected
dd.incRef(in);

std::vector<bool> measurements(qc->getNcbits(), false);

auto permutation = qc->initialLayout;
auto e = in;
dd.incRef(e);

for (const auto& op : *qc) {
if (op->getType() == Measure) {
Expand Down Expand Up @@ -171,6 +172,9 @@ simulate(const QuantumComputation* qc, const VectorDD& in, Package<Config>& dd,
counts[shot]++;
}

// decrease reference count of input state so that it can be garbage collected
dd.decRef(in);

return counts;
}

Expand Down
3 changes: 0 additions & 3 deletions test/dd/test_dd_noise_functionality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ TEST_F(DDNoiseFunctionalityTest, DetSimulateAdder4TrackAPD) {
auto dd = std::make_unique<DensityMatrixTestPackage>(qc.getNqubits());

auto rootEdge = dd->makeZeroDensityOperator(qc.getNqubits());
dd->incRef(rootEdge);

const auto* const noiseEffects = "APDI";

Expand Down Expand Up @@ -109,7 +108,6 @@ TEST_F(DDNoiseFunctionalityTest, DetSimulateAdder4TrackD) {
auto dd = std::make_unique<DensityMatrixTestPackage>(qc.getNqubits());

auto rootEdge = dd->makeZeroDensityOperator(qc.getNqubits());
dd->incRef(rootEdge);

const auto* const noiseEffects = "D";

Expand Down Expand Up @@ -141,7 +139,6 @@ TEST_F(DDNoiseFunctionalityTest, testingMeasure) {
auto dd = std::make_unique<DensityMatrixTestPackage>(qcOp.getNqubits());

auto rootEdge = dd->makeZeroDensityOperator(qcOp.getNqubits());
dd->incRef(rootEdge);

auto deterministicNoiseFunctionality = dd::DeterministicNoiseFunctionality(
dd, qcOp.getNqubits(), 0.01, 0.02, 0.02, 0.04, {});
Expand Down
3 changes: 0 additions & 3 deletions test/dd/test_edge_functionality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ TEST(DensityMatrixFunctionality, GetValueByIndexProperDensityMatrix) {
const auto nqubits = 1U;
auto dd = std::make_unique<dd::Package<>>(nqubits);
auto zero = dd->makeZeroDensityOperator(nqubits);
dd->incRef(zero);
const auto op1 = dd->makeGateDD(dd::H_MAT, 0U);
const auto op2 = dd->makeGateDD(dd::rzMat(dd::PI_4), 0U);
auto state = dd->applyOperationToDensity(zero, op1);
Expand Down Expand Up @@ -399,7 +398,6 @@ TEST(DensityMatrixFunctionality, GetSparseMatrixConsistency) {
const auto nqubits = 1U;
auto dd = std::make_unique<dd::Package<>>(nqubits);
auto zero = dd->makeZeroDensityOperator(nqubits);
dd->incRef(zero);
const auto op1 = dd->makeGateDD(dd::H_MAT, 0U);
const auto op2 = dd->makeGateDD(dd::rzMat(dd::PI_4), 0U);
auto state = dd->applyOperationToDensity(zero, op1);
Expand Down Expand Up @@ -430,7 +428,6 @@ TEST(DensityMatrixFunctionality, PrintMatrix) {
const auto nqubits = 1U;
auto dd = std::make_unique<dd::Package<>>(nqubits);
auto zero = dd->makeZeroDensityOperator(nqubits);
dd->incRef(zero);
const auto op1 = dd->makeGateDD(dd::H_MAT, 0U);
const auto op2 = dd->makeGateDD(dd::rzMat(dd::PI_4), 0U);
auto state = dd->applyOperationToDensity(zero, op1);
Expand Down
6 changes: 0 additions & 6 deletions test/dd/test_package.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,6 @@ TEST(DDPackageTest, StateGenerationManipulation) {
{dd::BasisStates::zero, dd::BasisStates::one,
dd::BasisStates::plus, dd::BasisStates::minus,
dd::BasisStates::left, dd::BasisStates::right});
dd->incRef(e);
dd->incRef(f);
dd->vUniqueTable.print();
dd->decRef(e);
dd->decRef(f);
Expand Down Expand Up @@ -1497,7 +1495,6 @@ TEST(DDPackageTest, dNodeMultiply) {
nrQubits);
// Make zero density matrix
auto state = dd->makeZeroDensityOperator(dd->qubits());
dd->incRef(state);
std::vector<dd::mEdge> operations = {};
operations.emplace_back(dd->makeGateDD(dd::H_MAT, 0));
operations.emplace_back(dd->makeGateDD(dd::H_MAT, 1));
Expand Down Expand Up @@ -1546,7 +1543,6 @@ TEST(DDPackageTest, dNodeMultiply2) {
nrQubits);
// Make zero density matrix
auto state = dd->makeZeroDensityOperator(dd->qubits());
dd->incRef(state);
std::vector<dd::mEdge> operations = {};
operations.emplace_back(dd->makeGateDD(dd::H_MAT, 0));
operations.emplace_back(dd->makeGateDD(dd::H_MAT, 1));
Expand Down Expand Up @@ -1588,7 +1584,6 @@ TEST(DDPackageTest, dNodeMulCache1) {
nrQubits);
// Make zero density matrix
auto state = dd->makeZeroDensityOperator(nrQubits);
dd->incRef(state);

const auto operation = dd->makeGateDD(dd::H_MAT, 0);
dd->applyOperationToDensity(state, operation);
Expand Down Expand Up @@ -1635,7 +1630,6 @@ TEST(DDPackageTest, dNoiseCache) {
auto dd = std::make_unique<dd::Package<>>(nrQubits);
// Make zero density matrix
const auto initialState = dd->makeZeroDensityOperator(nrQubits);
dd->incRef(initialState);

// nothing pre-cached
const std::vector<dd::Qubit> target = {0};
Expand Down

0 comments on commit 7860cb8

Please sign in to comment.