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

🚸🚨 Eliminate reference members and improve TaskManager interface #349

Merged
merged 4 commits into from
Jan 5, 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: 2 additions & 2 deletions include/checker/dd/DDEquivalenceChecker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class DDEquivalenceChecker : public EquivalenceChecker {
Configuration config)
: EquivalenceChecker(circ1, circ2, std::move(config)),
dd(std::make_unique<dd::Package<Config>>(nqubits)),
taskManager1(TaskManager<DDType, Config>(circ1, dd)),
taskManager2(TaskManager<DDType, Config>(circ2, dd)) {}
taskManager1(TaskManager<DDType, Config>(circ1, *dd)),
taskManager2(TaskManager<DDType, Config>(circ2, *dd)) {}

EquivalenceCriterion run() override;

Expand Down
14 changes: 7 additions & 7 deletions include/checker/dd/TaskManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ template <class DDType, class Config = dd::DDPackageConfig> class TaskManager {
using DDPackage = typename dd::Package<Config>;

public:
TaskManager(const qc::QuantumComputation& circ,
std::unique_ptr<DDPackage>& dd, const ec::Direction& dir) noexcept
: qc(&circ), package(dd), direction(dir), permutation(circ.initialLayout),
iterator(circ.begin()), end(circ.end()) {}
TaskManager(const qc::QuantumComputation& circ,
std::unique_ptr<DDPackage>& dd) noexcept
TaskManager(const qc::QuantumComputation& circ, DDPackage& dd,
const ec::Direction& dir) noexcept
: qc(&circ), package(&dd), direction(dir),
permutation(circ.initialLayout), iterator(circ.begin()),
end(circ.end()) {}
TaskManager(const qc::QuantumComputation& circ, DDPackage& dd) noexcept
: TaskManager(circ, dd, Direction::Left) {}

[[nodiscard]] bool finished() const noexcept { return iterator == end; }
Expand Down Expand Up @@ -126,7 +126,7 @@ template <class DDType, class Config = dd::DDPackageConfig> class TaskManager {

private:
const qc::QuantumComputation* qc{};
std::unique_ptr<DDPackage>& package;
DDPackage* package;
ec::Direction direction = Direction::Left;
qc::Permutation permutation{};
decltype(qc->begin()) iterator;
Expand Down
6 changes: 3 additions & 3 deletions include/checker/dd/applicationscheme/ApplicationScheme.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ template <class DDType, class Config> class ApplicationScheme {

public:
ApplicationScheme(TM& tm1, TM& tm2) noexcept
: taskManager1(tm1), taskManager2(tm2){};
: taskManager1(&tm1), taskManager2(&tm2){};

virtual ~ApplicationScheme() = default;

// get how many gates from either circuit shall be applied next
virtual std::pair<std::size_t, std::size_t> operator()() = 0;

protected:
TM& taskManager1;
TM& taskManager2;
TM* taskManager1;
TM* taskManager2;
};

} // namespace ec
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class GateCostApplicationScheme final
return {1U, 1U};
}

const auto& op = this->taskManager1();
const auto& op = (*this->taskManager1)();
const auto key =
GateCostLookupTableKeyType{op->getType(), op->getNcontrols()};
std::size_t cost = 1U;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class LookaheadApplicationScheme final

if (!cached1) {
// cache the current operation
op1 = this->taskManager1.getDD();
op1 = this->taskManager1->getDD();
package->incRef(op1);
cached1 = true;
}

if (!cached2) {
// cache the current operation
op2 = this->taskManager2.getInverseDD();
op2 = this->taskManager2->getInverseDD();
package->incRef(op2);
cached2 = true;
}
Expand All @@ -50,17 +50,17 @@ class LookaheadApplicationScheme final

// greedily chose the smaller resulting decision diagram
if (const auto size2 = dd2.size(); size1 <= size2) {
assert(!this->taskManager1.finished());
assert(!this->taskManager1->finished());
*internalState = dd1;
package->decRef(op1);
cached1 = false;
this->taskManager1.advanceIterator();
this->taskManager1->advanceIterator();
} else {
assert(!this->taskManager2.finished());
assert(!this->taskManager2->finished());
*internalState = dd2;
package->decRef(op2);
cached2 = false;
this->taskManager2.advanceIterator();
this->taskManager2->advanceIterator();
}

// properly track reference counts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class ProportionalApplicationScheme final

private:
[[nodiscard]] std::size_t computeGateRatio() const noexcept {
const std::size_t size1 = this->taskManager1.getCircuit()->size();
const std::size_t size2 = this->taskManager2.getCircuit()->size();
const std::size_t size1 = this->taskManager1->getCircuit()->size();
const std::size_t size2 = this->taskManager2->getCircuit()->size();
if (size1 == 0U) {
return size2;
}
Expand Down
8 changes: 4 additions & 4 deletions test/test_gate_cost_application_scheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ TEST_F(GateCostApplicationSchemeTest, SchemeFromProfile) {
// apply Toffoli gate
qc.mcx({1_pc, 2_pc}, 0);

auto tm = ec::TaskManager<qc::MatrixDD>(qc, dd);
auto tm = ec::TaskManager<qc::MatrixDD>(qc, *dd);

auto scheme = ec::GateCostApplicationScheme(tm, tm, filename);

Expand All @@ -52,13 +52,13 @@ TEST_F(GateCostApplicationSchemeTest, SchemeFromProfile) {
ec::EquivalenceCheckingManager ecm(qc, qc, config);
ecm.run();
EXPECT_TRUE(ecm.getResults().consideredEquivalent());
std::cout << ecm.toString() << std::endl;
std::cout << ecm.toString() << "\n";
}

TEST_F(GateCostApplicationSchemeTest, iSWAP) {
qc.iswap(0, 1);

auto tm = ec::TaskManager<qc::MatrixDD>(qc, dd);
auto tm = ec::TaskManager<qc::MatrixDD>(qc, *dd);

auto scheme = ec::GateCostApplicationScheme(tm, tm, &ec::legacyCostFunction);

Expand All @@ -73,7 +73,7 @@ TEST_F(GateCostApplicationSchemeTest, Peres) {

qc.cperes(0_pc, 1, 2);

auto tm = ec::TaskManager<qc::MatrixDD>(qc, dd);
auto tm = ec::TaskManager<qc::MatrixDD>(qc, *dd);

auto scheme = ec::GateCostApplicationScheme(tm, tm, &ec::legacyCostFunction);

Expand Down