Skip to content

Commit

Permalink
🐛 Properly reset task managers (#348)
Browse files Browse the repository at this point in the history
## Description

This PR fixes an issue that `TaskManager` class was only ever being
reliably usable once due to its internal iterator and permutation not
being reset in further invocations.
 
Fixes #347

## Checklist:

<!---
This checklist serves as a reminder of a couple of things that ensure
your pull request will be merged swiftly.
-->

- [x] The pull request only contains commits that are related to it.
- [x] I have added appropriate tests and documentation.
- [x] I have made sure that all CI jobs on GitHub pass.
- [x] The pull request introduces no new warnings and follows the
project's style guidelines.

---------

Signed-off-by: burgholzer <[email protected]>
  • Loading branch information
burgholzer authored Jan 5, 2024
1 parent 4d1caad commit b867ac4
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 6 deletions.
5 changes: 0 additions & 5 deletions include/checker/dd/DDAlternatingChecker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ class DDAlternatingChecker final
private:
qc::MatrixDD functionality{};

void initializeTask(
[[maybe_unused]] TaskManager<qc::MatrixDD, AlternatingDDPackageConfig>&
taskManager) override{
// task initialization is conducted separately for this checker
};
void initialize() override;
void execute() override;
void finish() override;
Expand Down
1 change: 1 addition & 0 deletions include/checker/dd/DDConstructionChecker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class DDConstructionChecker final
private:
void initializeTask(TaskManager<qc::MatrixDD, ConstructionDDPackageConfig>&
taskManager) override {
DDEquivalenceChecker::initializeTask(taskManager);
const auto initial = dd->makeIdent(nqubits);
taskManager.setInternalState(initial);
taskManager.incRef();
Expand Down
2 changes: 1 addition & 1 deletion include/checker/dd/DDEquivalenceChecker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class DDEquivalenceChecker : public EquivalenceChecker {
// in some form
EquivalenceCriterion equals(const DDType& e, const DDType& f);

virtual void initializeTask(TaskManager<DDType, Config>& taskManager) = 0;
virtual void initializeTask(TaskManager<DDType, Config>& taskManager);
virtual void initialize();
virtual void execute();
virtual void finish();
Expand Down
5 changes: 5 additions & 0 deletions include/checker/dd/TaskManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ template <class DDType, class Config = dd::DDPackageConfig> class TaskManager {
TaskManager(const qc::QuantumComputation& circ, DDPackage& dd) noexcept
: TaskManager(circ, dd, Direction::Left) {}

void reset() noexcept {
iterator = qc->begin();
permutation = qc->initialLayout;
}

[[nodiscard]] bool finished() const noexcept { return iterator == end; }

const std::unique_ptr<qc::Operation>& operator()() const { return *iterator; }
Expand Down
6 changes: 6 additions & 0 deletions src/checker/dd/DDEquivalenceChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ EquivalenceCriterion DDEquivalenceChecker<DDType, Config>::run() {
return equivalence;
}

template <class DDType, class Config>
void DDEquivalenceChecker<DDType, Config>::initializeTask(
TaskManager<DDType, Config>& taskManager) {
taskManager.reset();
}

template <class DDType, class Config>
void DDEquivalenceChecker<DDType, Config>::initialize() {
initializeTask(taskManager1);
Expand Down
1 change: 1 addition & 0 deletions src/checker/dd/DDSimulationChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ DDSimulationChecker::DDSimulationChecker(const qc::QuantumComputation& circ1,

void DDSimulationChecker::initializeTask(
TaskManager<qc::VectorDD, SimulationDDPackageConfig>& taskManager) {
DDEquivalenceChecker::initializeTask(taskManager);
taskManager.setInternalState(initialState);
taskManager.incRef();
}
Expand Down
26 changes: 26 additions & 0 deletions test/test_equality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@ TEST_F(EqualityTest, GlobalPhase) {
ec::EquivalenceCriterion::EquivalentUpToGlobalPhase);
}

/**
* @brief The following is a regression test for
* https://github.com/cda-tum/mqt-qcec/issues/347
*/
TEST_F(EqualityTest, GlobalPhaseSimulation) {
qc1.x(0);
qc2.x(0);

// add a global phase of -1
qc2.z(0);
qc2.x(0);
qc2.z(0);
qc2.x(0);

config.execution.runAlternatingChecker = false;
config.execution.runConstructionChecker = false;
config.execution.runZXChecker = false;
config.execution.runSimulationChecker = true;
config.execution.parallel = false;
ec::EquivalenceCheckingManager ecm(qc1, qc2, config);
ecm.run();
const auto json = ecm.json();
const auto simChecker = json["checkers"].front();
EXPECT_EQ(simChecker["equivalence"], "equivalent_up_to_phase");
}

TEST_F(EqualityTest, CloseButNotEqualAlternating) {
qc1.x(0);

Expand Down

0 comments on commit b867ac4

Please sign in to comment.