Skip to content

Commit ee6f49e

Browse files
committed
Moved static synthesis cost metrics calculation functions back into AnnotatableQuantumComputation to closer match previous Circuit class
1 parent 0feaa04 commit ee6f49e

File tree

6 files changed

+99
-121
lines changed

6 files changed

+99
-121
lines changed

include/algorithms/synthesis/quantum_computation_synthesis_cost_metrics.hpp

Lines changed: 0 additions & 96 deletions
This file was deleted.

include/core/annotatable_quantum_computation.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "ir/operations/Operation.hpp"
1717

1818
#include <cstddef>
19+
#include <cstdint>
1920
#include <functional>
2021
#include <map>
2122
#include <optional>
@@ -32,6 +33,7 @@ namespace syrec {
3233
class AnnotatableQuantumComputation: public qc::QuantumComputation {
3334
public:
3435
using QuantumOperationAnnotationsLookup = std::map<std::string, std::string, std::less<>>;
36+
using SynthesisCostMetricValue = std::uint64_t;
3537

3638
[[maybe_unused]] bool addOperationsImplementingNotGate(qc::Qubit targetQubit);
3739
[[maybe_unused]] bool addOperationsImplementingCnotGate(qc::Qubit controlQubit, qc::Qubit targetQubit);
@@ -72,6 +74,8 @@ namespace syrec {
7274
[[nodiscard]] std::vector<std::string> getQubitLabels() const;
7375
[[nodiscard]] qc::Operation* getQuantumOperation(std::size_t indexOfQuantumOperationInQuantumComputation) const;
7476
[[nodiscard]] QuantumOperationAnnotationsLookup getAnnotationsOfQuantumOperation(std::size_t indexOfQuantumOperationInQuantumComputation) const;
77+
[[nodiscard]] SynthesisCostMetricValue getQuantumCostForSynthesis() const;
78+
[[nodiscard]] SynthesisCostMetricValue getTransistorCostForSynthesis() const;
7579

7680
/**
7781
* Activate a new control qubit propagation scope.

src/core/annotatable_quantum_computation.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "ir/Definitions.hpp"
1414
#include "ir/operations/Control.hpp"
1515
#include "ir/operations/Operation.hpp"
16+
#include "ir/operations/OpType.hpp"
1617

1718
#include <algorithm>
1819
#include <cstddef>
@@ -170,6 +171,80 @@ AnnotatableQuantumComputation::QuantumOperationAnnotationsLookup AnnotatableQuan
170171
return annotationsPerQuantumOperation[indexOfQuantumOperationInQuantumComputation];
171172
}
172173

174+
AnnotatableQuantumComputation::SynthesisCostMetricValue AnnotatableQuantumComputation::getQuantumCostForSynthesis() const {
175+
SynthesisCostMetricValue cost = 0;
176+
177+
const auto numQubits = getNqubits();
178+
if (numQubits == 0) {
179+
return cost;
180+
}
181+
182+
for (const auto& quantumOperation: ops) {
183+
const std::size_t c = std::min(quantumOperation->getNcontrols() + static_cast<std::size_t>(quantumOperation->getType() == qc::OpType::SWAP), numQubits - 1);
184+
const std::size_t numEmptyLines = numQubits - c - 1U;
185+
186+
switch (c) {
187+
case 0U:
188+
case 1U:
189+
cost += 1ULL;
190+
break;
191+
case 2U:
192+
cost += 5ULL;
193+
break;
194+
case 3U:
195+
cost += 13ULL;
196+
break;
197+
case 4U:
198+
cost += (numEmptyLines >= 2U) ? 26ULL : 29ULL;
199+
break;
200+
case 5U:
201+
if (numEmptyLines >= 3U) {
202+
cost += 38ULL;
203+
} else if (numEmptyLines >= 1U) {
204+
cost += 52ULL;
205+
} else {
206+
cost += 61ULL;
207+
}
208+
break;
209+
case 6U:
210+
if (numEmptyLines >= 4U) {
211+
cost += 50ULL;
212+
} else if (numEmptyLines >= 1U) {
213+
cost += 80ULL;
214+
} else {
215+
cost += 125ULL;
216+
}
217+
break;
218+
case 7U:
219+
if (numEmptyLines >= 5U) {
220+
cost += 62ULL;
221+
} else if (numEmptyLines >= 1U) {
222+
cost += 100ULL;
223+
} else {
224+
cost += 253ULL;
225+
}
226+
break;
227+
default:
228+
if (numEmptyLines >= c - 2U) {
229+
cost += 12ULL * c - 22ULL;
230+
} else if (numEmptyLines >= 1U) {
231+
cost += 24ULL * c - 87ULL;
232+
} else {
233+
cost += (1ULL << (c + 1ULL)) - 3ULL;
234+
}
235+
}
236+
}
237+
return cost;
238+
}
239+
240+
AnnotatableQuantumComputation::SynthesisCostMetricValue AnnotatableQuantumComputation::getTransistorCostForSynthesis() const {
241+
SynthesisCostMetricValue cost = 0;
242+
for (const auto& quantumOperation: ops) {
243+
cost += quantumOperation->getNcontrols() * 8;
244+
}
245+
return cost;
246+
}
247+
173248
void AnnotatableQuantumComputation::activateControlQubitPropagationScope() {
174249
controlQubitPropgationScopes.emplace_back();
175250
}

src/python/bindings.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010

1111
#include "algorithms/simulation/simple_simulation.hpp"
12-
#include "algorithms/synthesis/quantum_computation_synthesis_cost_metrics.hpp"
1312
#include "algorithms/synthesis/syrec_cost_aware_synthesis.hpp"
1413
#include "algorithms/synthesis/syrec_line_aware_synthesis.hpp"
1514
#include "core/annotatable_quantum_computation.hpp"
@@ -32,10 +31,8 @@ PYBIND11_MODULE(pysyrec, m) {
3231
py::class_<AnnotatableQuantumComputation, qc::QuantumComputation>(m, "annotatable_quantum_computation")
3332
.def(py::init<>(), "Constructs an annotatable quantum computation")
3433
.def_property_readonly("qubit_labels", &AnnotatableQuantumComputation::getQubitLabels, "Get the label of each qubit in the quantum computation")
35-
.def(
36-
"get_quantum_cost_for_synthesis", [](const AnnotatableQuantumComputation& annotatableQuantumComputation) { return getQuantumCostForSynthesis(annotatableQuantumComputation); }, "Get the quantum cost to synthesis the quantum computation")
37-
.def(
38-
"get_transistor_cost_for_synthesis", [](const AnnotatableQuantumComputation& annotatableQuantumComputation) { return getTransistorCostForSynthesis(annotatableQuantumComputation); }, "Get the transistor cost to synthesis the quantum computation")
34+
.def("get_quantum_cost_for_synthesis", &AnnotatableQuantumComputation::getQuantumCostForSynthesis, "Get the quantum cost to synthesis the quantum computation")
35+
.def("get_transistor_cost_for_synthesis", &AnnotatableQuantumComputation::getTransistorCostForSynthesis, "Get the transistor cost to synthesis the quantum computation")
3936
.def("get_annotations_of_quantum_operation", &AnnotatableQuantumComputation::getAnnotationsOfQuantumOperation, "quantum_operation_index_in_quantum_operation"_a, "Get the annotations of a specific quantum operation in the quantum computation");
4037

4138
py::class_<NBitValuesContainer>(m, "n_bit_values_container")

test/unittests/test_cost_aware_synthesis.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* Licensed under the MIT License
99
*/
1010

11-
#include "algorithms/synthesis/quantum_computation_synthesis_cost_metrics.hpp"
1211
#include "algorithms/synthesis/syrec_cost_aware_synthesis.hpp"
1312
#include "core/annotatable_quantum_computation.hpp"
1413
#include "core/syrec/program.hpp"
@@ -27,13 +26,13 @@ using namespace syrec;
2726

2827
class SyrecCostAwareSynthesisTest: public testing::TestWithParam<std::string> {
2928
protected:
30-
std::string testConfigsDir = "./configs/";
31-
std::string testCircuitsDir = "./circuits/";
32-
std::string fileName;
33-
std::size_t expectedNumGates = 0;
34-
std::size_t expectedNumLines = 0;
35-
SynthesisCostMetricValue expectedQuantumCosts = 0;
36-
SynthesisCostMetricValue expectedTransistorCosts = 0;
29+
std::string testConfigsDir = "./configs/";
30+
std::string testCircuitsDir = "./circuits/";
31+
std::string fileName;
32+
std::size_t expectedNumGates = 0;
33+
std::size_t expectedNumLines = 0;
34+
AnnotatableQuantumComputation::SynthesisCostMetricValue expectedQuantumCosts = 0;
35+
AnnotatableQuantumComputation::SynthesisCostMetricValue expectedTransistorCosts = 0;
3736

3837
void SetUp() override {
3938
const std::string& synthesisParam = GetParam();
@@ -91,8 +90,8 @@ TEST_P(SyrecCostAwareSynthesisTest, GenericSynthesisTest) {
9190
ASSERT_EQ(expectedNumGates, annotatableQuantumComputation.getNops());
9291
ASSERT_EQ(expectedNumLines, annotatableQuantumComputation.getNqubits());
9392

94-
const SynthesisCostMetricValue actualQuantumCosts = getQuantumCostForSynthesis(annotatableQuantumComputation);
95-
const SynthesisCostMetricValue actualTransistorCosts = getTransistorCostForSynthesis(annotatableQuantumComputation);
93+
const AnnotatableQuantumComputation::SynthesisCostMetricValue actualQuantumCosts = annotatableQuantumComputation.getQuantumCostForSynthesis();
94+
const AnnotatableQuantumComputation::SynthesisCostMetricValue actualTransistorCosts = annotatableQuantumComputation.getTransistorCostForSynthesis();
9695
ASSERT_EQ(expectedQuantumCosts, actualQuantumCosts);
9796
ASSERT_EQ(expectedTransistorCosts, actualTransistorCosts);
9897
}

test/unittests/test_line_aware_synthesis.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* Licensed under the MIT License
99
*/
1010

11-
#include "algorithms/synthesis/quantum_computation_synthesis_cost_metrics.hpp"
1211
#include "algorithms/synthesis/syrec_line_aware_synthesis.hpp"
1312
#include "core/annotatable_quantum_computation.hpp"
1413
#include "core/syrec/program.hpp"
@@ -27,13 +26,13 @@ using namespace syrec;
2726

2827
class SyrecLineAwareSynthesisTest: public testing::TestWithParam<std::string> {
2928
protected:
30-
std::string testConfigsDir = "./configs/";
31-
std::string testCircuitsDir = "./circuits/";
32-
std::string fileName;
33-
std::size_t expectedNumGates = 0;
34-
std::size_t expectedNumLines = 0;
35-
SynthesisCostMetricValue expectedQuantumCosts = 0;
36-
SynthesisCostMetricValue expectedTransistorCosts = 0;
29+
std::string testConfigsDir = "./configs/";
30+
std::string testCircuitsDir = "./circuits/";
31+
std::string fileName;
32+
std::size_t expectedNumGates = 0;
33+
std::size_t expectedNumLines = 0;
34+
AnnotatableQuantumComputation::SynthesisCostMetricValue expectedQuantumCosts = 0;
35+
AnnotatableQuantumComputation::SynthesisCostMetricValue expectedTransistorCosts = 0;
3736

3837
void SetUp() override {
3938
const std::string& synthesisParam = GetParam();
@@ -91,8 +90,8 @@ TEST_P(SyrecLineAwareSynthesisTest, GenericSynthesisTest) {
9190
ASSERT_EQ(expectedNumGates, annotatableQuantumComputation.getNops());
9291
ASSERT_EQ(expectedNumLines, annotatableQuantumComputation.getNqubits());
9392

94-
const SynthesisCostMetricValue actualQuantumCosts = getQuantumCostForSynthesis(annotatableQuantumComputation);
95-
const SynthesisCostMetricValue actualTransistorCosts = getTransistorCostForSynthesis(annotatableQuantumComputation);
93+
const AnnotatableQuantumComputation::SynthesisCostMetricValue actualQuantumCosts = annotatableQuantumComputation.getQuantumCostForSynthesis();
94+
const AnnotatableQuantumComputation::SynthesisCostMetricValue actualTransistorCosts = annotatableQuantumComputation.getTransistorCostForSynthesis();
9695
ASSERT_EQ(expectedQuantumCosts, actualQuantumCosts);
9796
ASSERT_EQ(expectedTransistorCosts, actualTransistorCosts);
9897
}

0 commit comments

Comments
 (0)