Skip to content

Commit

Permalink
♻️ make ancillary and garbage protected functions
Browse files Browse the repository at this point in the history
these members had getters since a while back. there is no need to keep them as public members.

Signed-off-by: burgholzer <[email protected]>
  • Loading branch information
burgholzer committed Jan 10, 2025
1 parent 456dd64 commit 3c2d9c6
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion include/mqt-core/dd/Simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ VectorDD simulate(const QuantumComputation& qc, const VectorDD& in,
e = applyUnitaryOperation(*op, e, dd, permutation);
}
changePermutation(e, permutation, qc.outputPermutation, dd);
e = dd.reduceGarbage(e, qc.garbage);
e = dd.reduceGarbage(e, qc.getGarbage());
return e;
}

Expand Down
6 changes: 3 additions & 3 deletions include/mqt-core/ir/QuantumComputation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class QuantumComputation {
ClassicalRegisterMap cregs;
QuantumRegisterMap ancregs;

std::vector<bool> ancillary;
std::vector<bool> garbage;

std::mt19937_64 mt;
std::size_t seed = 0;

Expand Down Expand Up @@ -134,9 +137,6 @@ class QuantumComputation {

void setName(const std::string& n) noexcept { name = n; }

std::vector<bool> ancillary;
std::vector<bool> garbage;

[[nodiscard]] std::size_t getNindividualOps() const;
[[nodiscard]] std::size_t getNsingleQubitOps() const;
[[nodiscard]] std::size_t getDepth() const;
Expand Down
10 changes: 5 additions & 5 deletions src/dd/FunctionalityConstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ MatrixDD buildFunctionality(const QuantumComputation& qc, Package<Config>& dd) {
}

auto permutation = qc.initialLayout;
auto e = dd.createInitialMatrix(qc.ancillary);
auto e = dd.createInitialMatrix(qc.getAncillary());

for (const auto& op : qc) {
// SWAP gates can be executed virtually by changing the permutation
Expand All @@ -40,8 +40,8 @@ MatrixDD buildFunctionality(const QuantumComputation& qc, Package<Config>& dd) {
}
// correct permutation if necessary
changePermutation(e, permutation, qc.outputPermutation, dd);
e = dd.reduceAncillae(e, qc.ancillary);
e = dd.reduceGarbage(e, qc.garbage);
e = dd.reduceAncillae(e, qc.getAncillary());
e = dd.reduceGarbage(e, qc.getGarbage());

return e;
}
Expand Down Expand Up @@ -69,8 +69,8 @@ MatrixDD buildFunctionalityRecursive(const QuantumComputation& qc,

// correct permutation if necessary
changePermutation(e, permutation, qc.outputPermutation, dd);
e = dd.reduceAncillae(e, qc.ancillary);
e = dd.reduceGarbage(e, qc.garbage);
e = dd.reduceAncillae(e, qc.getAncillary());
e = dd.reduceGarbage(e, qc.getGarbage());

return e;
}
Expand Down
2 changes: 1 addition & 1 deletion src/dd/Simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ sample(const QuantumComputation& qc, const VectorDD& in, Package<Config>& dd,

// correct permutation if necessary
changePermutation(e, permutation, qc.outputPermutation, dd);
e = dd.reduceGarbage(e, qc.garbage);
e = dd.reduceGarbage(e, qc.getGarbage());

// measure all qubits
std::map<std::string, std::size_t> counts{};
Expand Down
8 changes: 8 additions & 0 deletions src/ir/QuantumComputation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,14 @@ QuantumComputation::fromCompoundOperation(const CompoundOperation& op) {
return qc;
}

std::size_t QuantumComputation::getNmeasuredQubits() const noexcept {
return getNqubits() - getNgarbageQubits();
}
std::size_t QuantumComputation::getNgarbageQubits() const {
return static_cast<std::size_t>(
std::count(getGarbage().cbegin(), getGarbage().cend(), true));
}

///---------------------------------------------------------------------------
/// \n Operations \n
///---------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/python/ir/register_quantum_computation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,15 @@ void registerQuantumComputation(py::module& m) {
/// \n Ancillary and Garbage Handling \n
///---------------------------------------------------------------------------

qc.def_readonly("ancillary", &qc::QuantumComputation::ancillary);
qc.def_property_readonly("ancillary", &qc::QuantumComputation::getAncillary);
qc.def("set_circuit_qubit_ancillary",
&qc::QuantumComputation::setLogicalQubitAncillary, "q"_a);
qc.def("se_circuit_qubits_ancillary",
&qc::QuantumComputation::setLogicalQubitsAncillary, "q_min"_a,
"q_max"_a);
qc.def("is_circuit_qubit_ancillary",
&qc::QuantumComputation::logicalQubitIsAncillary, "q"_a);
qc.def_readonly("garbage", &qc::QuantumComputation::garbage);
qc.def_property_readonly("garbage", &qc::QuantumComputation::getGarbage);
qc.def("set_circuit_qubit_garbage",
&qc::QuantumComputation::setLogicalQubitGarbage, "q"_a);
qc.def("set_circuit_qubits_garbage",
Expand Down
12 changes: 6 additions & 6 deletions test/ir/test_qfr_functionality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,12 +468,12 @@ TEST_F(QFRFunctionality, AddAncillaryQubits) {
qc.addAncillaryQubit(1, std::nullopt);
EXPECT_EQ(qc.getNqubits(), 2);
EXPECT_EQ(qc.getNancillae(), 1);
ASSERT_EQ(qc.ancillary.size(), 2U);
ASSERT_EQ(qc.garbage.size(), 2U);
EXPECT_FALSE(qc.ancillary[0]);
EXPECT_TRUE(qc.ancillary[1]);
EXPECT_FALSE(qc.garbage[0]);
EXPECT_TRUE(qc.garbage[1]);
ASSERT_EQ(qc.getAncillary().size(), 2U);
ASSERT_EQ(qc.getGarbage().size(), 2U);
EXPECT_FALSE(qc.getAncillary()[0]);
EXPECT_TRUE(qc.getAncillary()[1]);
EXPECT_FALSE(qc.getGarbage()[0]);
EXPECT_TRUE(qc.getGarbage()[1]);
}

TEST_F(QFRFunctionality, CircuitDepthEmptyCircuit) {
Expand Down

0 comments on commit 3c2d9c6

Please sign in to comment.