From 3c2d9c6161dda464373fa7c71d1656ae50406197 Mon Sep 17 00:00:00 2001 From: burgholzer Date: Fri, 10 Jan 2025 11:36:28 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20make=20`ancillary`=20and?= =?UTF-8?q?=20`garbage`=20protected=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit these members had getters since a while back. there is no need to keep them as public members. Signed-off-by: burgholzer --- include/mqt-core/dd/Simulation.hpp | 2 +- include/mqt-core/ir/QuantumComputation.hpp | 6 +++--- src/dd/FunctionalityConstruction.cpp | 10 +++++----- src/dd/Simulation.cpp | 2 +- src/ir/QuantumComputation.cpp | 8 ++++++++ src/python/ir/register_quantum_computation.cpp | 4 ++-- test/ir/test_qfr_functionality.cpp | 12 ++++++------ 7 files changed, 26 insertions(+), 18 deletions(-) diff --git a/include/mqt-core/dd/Simulation.hpp b/include/mqt-core/dd/Simulation.hpp index 588347fcb..58742ecb9 100644 --- a/include/mqt-core/dd/Simulation.hpp +++ b/include/mqt-core/dd/Simulation.hpp @@ -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; } diff --git a/include/mqt-core/ir/QuantumComputation.hpp b/include/mqt-core/ir/QuantumComputation.hpp index 0a4b6b1cb..f2a1301c4 100644 --- a/include/mqt-core/ir/QuantumComputation.hpp +++ b/include/mqt-core/ir/QuantumComputation.hpp @@ -52,6 +52,9 @@ class QuantumComputation { ClassicalRegisterMap cregs; QuantumRegisterMap ancregs; + std::vector ancillary; + std::vector garbage; + std::mt19937_64 mt; std::size_t seed = 0; @@ -134,9 +137,6 @@ class QuantumComputation { void setName(const std::string& n) noexcept { name = n; } - std::vector ancillary; - std::vector garbage; - [[nodiscard]] std::size_t getNindividualOps() const; [[nodiscard]] std::size_t getNsingleQubitOps() const; [[nodiscard]] std::size_t getDepth() const; diff --git a/src/dd/FunctionalityConstruction.cpp b/src/dd/FunctionalityConstruction.cpp index e5ea94506..484592260 100644 --- a/src/dd/FunctionalityConstruction.cpp +++ b/src/dd/FunctionalityConstruction.cpp @@ -26,7 +26,7 @@ MatrixDD buildFunctionality(const QuantumComputation& qc, Package& 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 @@ -40,8 +40,8 @@ MatrixDD buildFunctionality(const QuantumComputation& qc, Package& 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; } @@ -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; } diff --git a/src/dd/Simulation.cpp b/src/dd/Simulation.cpp index 30c7289bf..2f89478f7 100644 --- a/src/dd/Simulation.cpp +++ b/src/dd/Simulation.cpp @@ -111,7 +111,7 @@ sample(const QuantumComputation& qc, const VectorDD& in, Package& 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 counts{}; diff --git a/src/ir/QuantumComputation.cpp b/src/ir/QuantumComputation.cpp index be4a35c88..b75737a8a 100644 --- a/src/ir/QuantumComputation.cpp +++ b/src/ir/QuantumComputation.cpp @@ -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::count(getGarbage().cbegin(), getGarbage().cend(), true)); +} + ///--------------------------------------------------------------------------- /// \n Operations \n ///--------------------------------------------------------------------------- diff --git a/src/python/ir/register_quantum_computation.cpp b/src/python/ir/register_quantum_computation.cpp index 774e59c6a..cb4cec072 100644 --- a/src/python/ir/register_quantum_computation.cpp +++ b/src/python/ir/register_quantum_computation.cpp @@ -208,7 +208,7 @@ 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", @@ -216,7 +216,7 @@ void registerQuantumComputation(py::module& m) { "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", diff --git a/test/ir/test_qfr_functionality.cpp b/test/ir/test_qfr_functionality.cpp index a7f2e4946..c0652c496 100644 --- a/test/ir/test_qfr_functionality.cpp +++ b/test/ir/test_qfr_functionality.cpp @@ -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) {