Skip to content

Commit

Permalink
🐛 Fix DD node leak (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
burgholzer authored Aug 3, 2023
2 parents 92f6ce1 + b1b16b1 commit db2299d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
4 changes: 1 addition & 3 deletions include/dd/Package.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,7 @@ template <class Config> class Package {

// all equal to zero
if (argmax == -1) {
if (!cached && !e.isTerminal()) {
// If it is not a cached computation, the node has to be put back into
// the chain
if (!e.isTerminal()) {
getMemoryManager<Node>().returnEntry(e.p);
}
return Edge<Node>::zero;
Expand Down
23 changes: 23 additions & 0 deletions test/dd/test_package.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ TEST(DDPackageTest, NearZeroNormalize) {
auto meNormalizedCached = dd->normalize(me, true);
EXPECT_EQ(meNormalizedCached, dd::mEdge::zero);

me.p = dd->mMemoryManager.get();
for (auto& edge : me.p->e) {
edge.p = dd->mMemoryManager.get();
edge.p->v = 0;
Expand Down Expand Up @@ -1824,3 +1825,25 @@ TEST(DDPackageTest, InnerProductTopNodeConjugation) {
// it will result in +0.416.
EXPECT_NEAR(dd->expectationValue(op, evolvedState), -0.416, 0.001);
}

/**
* @brief This is a regression test for a long lasting memory leak in the DD
* package.
* @details The memory leak was caused by a bug in the normalization routine
* which was not properly returning a node to the memory manager. This occurred
* whenever the multiplication of two DDs resulted in a zero terminal.
*/
TEST(DDPackageTest, DDNodeLeakRegressionTest) {
const auto nqubits = 1U;
auto matrix = dd::GateMatrix{dd::complex_one, dd::complex_zero,
dd::complex_zero, dd::complex_zero};
auto dd = std::make_unique<dd::Package<>>(nqubits);

auto dd1 = dd->makeGateDD(matrix, nqubits, 0U);
matrix[0] = dd::complex_zero;
matrix[3] = dd::complex_one;
auto dd2 = dd->makeGateDD(matrix, nqubits, 0U);
dd->multiply(dd1, dd2);
dd->garbageCollect(true);
EXPECT_EQ(dd->mMemoryManager.getUsedCount(), 0U);
}

1 comment on commit db2299d

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ✔️

No problems need attention.

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.