Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix out-of-bounds error in ZX EdgeIterator and needless dummy register in empty circuit construction #758

Merged
merged 5 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/mqt-core/ir/QuantumComputation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ class QuantumComputation {
explicit QuantumComputation(const std::size_t nq, const std::size_t nc = 0U,
const std::size_t s = 0)
: seed(s) {
addQubitRegister(nq);
if (nq > 0) {
addQubitRegister(nq);
}
if (nc > 0) {
addClassicalRegister(nc);
}
Expand Down
11 changes: 8 additions & 3 deletions src/zx/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@ Edges::EdgeIterator::EdgeIterator(
while (v < edges.size() && !vertices[v].has_value()) {
++v;
}
currentPos = edges[v].begin();
edgesPos = edges.begin() + static_cast<int>(v);
checkNextVertex();
if (v < edges.size()) {
currentPos = edges[v].begin();
edgesPos = edges.begin() + static_cast<int>(v);
checkNextVertex();
} else {
currentPos = edges.back().end();
edgesPos = edges.end();
}
} else {
currentPos = edges.back().end();
edgesPos = edges.end();
Expand Down
9 changes: 9 additions & 0 deletions test/ir/test_qfr_functionality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1154,3 +1154,12 @@ TEST_F(QFRFunctionality, emptyPermutation) {
EXPECT_EQ(perm.maxKey(), 0U);
EXPECT_EQ(perm.maxValue(), 0U);
}

TEST_F(QFRFunctionality, NoRegisterOnEmptyCircuit) {
// This is a regression test. Previously, the following code would throw an
// exception because even zero-qubit circuits had an empty register named "q".
qc::QuantumComputation qc(0U);
qc.addQubitRegister(1U, "p");
EXPECT_NO_THROW(qc.addQubitRegister(1U, "q"));
EXPECT_EQ(qc.getQregs().size(), 2U);
}
14 changes: 14 additions & 0 deletions test/zx/test_simplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,17 @@ TEST_F(SimplifyTest, equivalenceSymbolic) {
EXPECT_EQ(d1.getNVertices(), 6);
EXPECT_TRUE(d1.isIdentity());
}

TEST_F(SimplifyTest, OnlyDeletedVertices) {
// This is a regression test. The following code should not throw an
// exception. It previously did because the code did not handle the case where
// the diagram only contains deleted vertices.
zx::ZXDiagram diag = ::makeIdentityDiagram(1, 0);
diag.makeAncilla(0);
// The following simplifies the diagram to the empty diagram.
zx::fullReduce(diag);
EXPECT_EQ(diag.getNEdges(), 0);
EXPECT_EQ(diag.getNVertices(), 0);
// A subsequent simplification should not throw an exception.
EXPECT_NO_THROW(zx::fullReduce(diag););
}
Loading