Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Commit

Permalink
🎨 assorted cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
burgholzer committed Jun 9, 2023
1 parent fa3c7e5 commit 37592a6
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 123 deletions.
6 changes: 3 additions & 3 deletions include/Executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ template <class T> class Executor {

public:
virtual ~Executor() = default;
explicit Executor() = default;

virtual json execute(const T& task) = 0;
[[nodiscard]] virtual std::string getIdentifier() const = 0;
virtual json execute(const T& task) = 0;

[[nodiscard]] virtual std::string getIdentifier() const = 0;
};
3 changes: 2 additions & 1 deletion include/Task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

class Task {
public:
virtual ~Task() = default;
virtual ~Task() = default;

[[nodiscard]] virtual std::string getIdentifier() const = 0;
};
4 changes: 1 addition & 3 deletions include/executors/AlternatingVerificationExecutor.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#pragma once

#include "EquivalenceCheckingManager.hpp"
#include "Executor.hpp"
#include "tasks/VerificationTask.hpp"

class AlternatingVerificationExecutor : public Executor<VerificationTask> {
public:
AlternatingVerificationExecutor() = default;
json execute(const VerificationTask& task) override;

json execute(const VerificationTask& task) override;
[[nodiscard]] std::string getIdentifier() const override {
return "alternating_verification";
}
Expand Down
4 changes: 1 addition & 3 deletions include/executors/CircuitSimulatorExecutor.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#pragma once

#include "CircuitSimulator.hpp"
#include "Executor.hpp"
#include "tasks/SimulationTask.hpp"

class CircuitSimulatorExecutor : public Executor<SimulationTask> {
public:
CircuitSimulatorExecutor() = default;
json execute(const SimulationTask& task) override;

json execute(const SimulationTask& task) override;
[[nodiscard]] std::string getIdentifier() const override {
return "circuit_simulator";
};
Expand Down
13 changes: 6 additions & 7 deletions include/tasks/SimulationTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@

class SimulationTask : public Task {
public:
SimulationTask() = default;
explicit SimulationTask() = default;
explicit SimulationTask(std::unique_ptr<qc::QuantumComputation> circ)
: qc(std::move(circ)) {}

explicit SimulationTask(std::unique_ptr<qc::QuantumComputation> qc)
: qc(std::move(qc)) {}
[[nodiscard]] std::string getIdentifier() const override {
return "sim_" + qc->getName();
};

[[nodiscard]] const std::unique_ptr<qc::QuantumComputation>& getQc() const {
return qc;
};

[[nodiscard]] std::string getIdentifier() const override {
return "sim_" + qc->getName();
};

protected:
std::unique_ptr<qc::QuantumComputation> qc;
};
18 changes: 9 additions & 9 deletions include/tasks/VerificationTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@

class VerificationTask : public Task {
public:
VerificationTask() = default;
VerificationTask(std::unique_ptr<qc::QuantumComputation> qc1,
std::unique_ptr<qc::QuantumComputation> qc2)
: qc1(std::move(qc1)), qc2(std::move(qc2)) {}
explicit VerificationTask() = default;
VerificationTask(std::unique_ptr<qc::QuantumComputation> circ1,
std::unique_ptr<qc::QuantumComputation> circ2)
: qc1(std::move(circ1)), qc2(std::move(circ2)) {}

[[nodiscard]] std::string getIdentifier() const override {
return "ver_" + qc1->getName() + "_" + qc2->getName();
};

protected:
std::unique_ptr<qc::QuantumComputation> qc1;
std::unique_ptr<qc::QuantumComputation> qc2;

public:
[[nodiscard]] const std::unique_ptr<qc::QuantumComputation>& getQc1() const {
return qc1;
};

[[nodiscard]] const std::unique_ptr<qc::QuantumComputation>& getQc2() const {
return qc2;
};

protected:
std::unique_ptr<qc::QuantumComputation> qc1;
std::unique_ptr<qc::QuantumComputation> qc2;
};
4 changes: 2 additions & 2 deletions src/AlternatingVerificationExecutor.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "executors/AlternatingVerificationExecutor.hpp"

#include "EquivalenceCheckingManager.hpp"

json AlternatingVerificationExecutor::execute(const VerificationTask& task) {
json result;
auto const constructionStart = std::chrono::steady_clock::now();

auto qc1 = task.getQc1()->clone();
auto qc2 = task.getQc2()->clone();
auto equivalenceCheckingManager =
std::make_unique<ec::EquivalenceCheckingManager>(*task.getQc1(),
*task.getQc2());
Expand Down
2 changes: 2 additions & 0 deletions src/CircuitSimulatorExecutor.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "executors/CircuitSimulatorExecutor.hpp"

#include "CircuitSimulator.hpp"

json CircuitSimulatorExecutor::execute(const SimulationTask& task) {
json result;
auto const constructionStart = std::chrono::steady_clock::now();
Expand Down
12 changes: 6 additions & 6 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ if(NOT TARGET gtest OR NOT TARGET gmock)
add_subdirectory("${PROJECT_SOURCE_DIR}/${GTEST_PATH}" "${GTEST_PATH}"
EXCLUDE_FROM_ALL)
endif()
configure_file(${CMAKE_SOURCE_DIR}/test/circuits.json
${CMAKE_CURRENT_BINARY_DIR}/circuits.json COPYONLY)
configure_file(${CMAKE_SOURCE_DIR}/test/equi_circuits.json
${CMAKE_CURRENT_BINARY_DIR}/equi_circuits.json COPYONLY)
package_add_test(${PROJECT_NAME}_test ${PROJECT_NAME} test_ddsim_simple.cpp
test_qcec_simple.cpp test_simexec.cpp test_qcecexec.cpp)
configure_file(${CMAKE_SOURCE_DIR}/test/sim_circuits.json
${CMAKE_CURRENT_BINARY_DIR}/sim_circuits.json COPYONLY)
configure_file(${CMAKE_SOURCE_DIR}/test/ver_circuits.json
${CMAKE_CURRENT_BINARY_DIR}/ver_circuits.json COPYONLY)
package_add_test(${PROJECT_NAME}_sim_test ${PROJECT_NAME} test_simexec.cpp)
package_add_test(${PROJECT_NAME}_ver_test ${PROJECT_NAME} test_qcecexec.cpp)
14 changes: 0 additions & 14 deletions test/equi_circuits.json

This file was deleted.

4 changes: 2 additions & 2 deletions test/circuits.json → test/sim_circuits.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[
{
"description": "two_qubit_circuit_with_two_x_gates",
"initial_circuit": "OPENQASM 2.0;include \"qelib1.inc\";qreg q[2];x q[0];x q[1];\n",
"circuit": "OPENQASM 2.0;include \"qelib1.inc\";qreg q[2];x q[0];x q[1];\n",
"expected_meas_results": {
"11": 1024
}
},
{
"description": "two_qubit_circuit_with_h_z_controlled_x_swap",
"initial_circuit": "OPENQASM 2.0;include \"qelib1.inc\";qreg q[2];cx q[0], q[1];h q[0];z q[0];h q[0];swap q[0], q[1];\n",
"circuit": "OPENQASM 2.0;include \"qelib1.inc\";qreg q[2];cx q[0], q[1];h q[0];z q[0];h q[0];swap q[0], q[1];\n",
"expected_meas_results": {
"10": 1024
}
Expand Down
20 changes: 0 additions & 20 deletions test/test_ddsim_simple.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions test/test_qcec_simple.cpp

This file was deleted.

30 changes: 13 additions & 17 deletions test/test_qcecexec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
struct TestConfigurationQCEC {
// given input
std::string description;
std::string initialCircuit1;
std::string initialCircuit2;
std::string circuit1;
std::string circuit2;

// expected output
std::string expectedEquivalence;
Expand All @@ -16,8 +16,8 @@ struct TestConfigurationQCEC {
// NOLINTNEXTLINE (readability-identifier-naming)
inline void from_json(const nlohmann::json& j, TestConfigurationQCEC& test) {
test.description = j.at("description").get<std::string>();
test.initialCircuit1 = j.at("initial_circuit1").get<std::string>();
test.initialCircuit2 = j.at("initial_circuit2").get<std::string>();
test.circuit1 = j.at("circuit1").get<std::string>();
test.circuit2 = j.at("circuit2").get<std::string>();
test.expectedEquivalence = j.at("expected_equivalence").get<std::string>();
}

Expand All @@ -33,12 +33,12 @@ class QCECExecTest : public ::testing::TestWithParam<TestConfigurationQCEC> {
void SetUp() override {
test = GetParam();

std::stringstream ss1(test.initialCircuit1);
std::stringstream ss1(test.circuit1);
auto qc1 = std::make_unique<qc::QuantumComputation>();
qc1->import(ss1, qc::Format::OpenQASM);
std::cout << "Circuit 1:\n" << *qc1 << std::endl;

std::stringstream ss2(test.initialCircuit2);
std::stringstream ss2(test.circuit2);
auto qc2 = std::make_unique<qc::QuantumComputation>();
qc2->import(ss2, qc::Format::OpenQASM);
std::cout << "Circuit 2:\n" << *qc2 << std::endl;
Expand All @@ -47,32 +47,28 @@ class QCECExecTest : public ::testing::TestWithParam<TestConfigurationQCEC> {

alternatingVerificationExecutor =
std::make_unique<AlternatingVerificationExecutor>();

result = alternatingVerificationExecutor->execute(verificationTask);
std::cout << "Results:" << std::endl;
std::cout << result.dump(2U) << std::endl;
}

void TearDown() override { std::cout << "Tearing down...\n"; }

VerificationTask verificationTask;
std::unique_ptr<AlternatingVerificationExecutor>
alternatingVerificationExecutor;
TestConfigurationQCEC test;
json result;
};

INSTANTIATE_TEST_SUITE_P(
Circuits, QCECExecTest, testing::ValuesIn(getTests("equi_circuits.json")),
Circuits, QCECExecTest, testing::ValuesIn(getTests("ver_circuits.json")),
[](const testing::TestParamInfo<QCECExecTest::ParamType>& inf) {
return inf.param.description;
});

TEST_P(QCECExecTest, Equivalence) {
TEST_P(QCECExecTest, Tests) {
const auto result =
alternatingVerificationExecutor->execute(verificationTask);
std::cout << "Results:\n" << result.dump(2U) << std::endl;

ASSERT_TRUE(result.contains("check_results"));
EXPECT_EQ(result["check_results"]["equivalence"], test.expectedEquivalence);
}

TEST_P(QCECExecTest, Entries) {
EXPECT_TRUE(result.contains("construction_time"));
EXPECT_TRUE(result.contains("execution_time"));
EXPECT_TRUE(result.contains("executor"));
Expand Down
33 changes: 16 additions & 17 deletions test/test_simexec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
struct TestConfigurationDDSIM {
// given input
std::string description;
std::string initialCircuit;
std::string circuit;

// expected output
json expectedResults{};
Expand All @@ -15,54 +15,53 @@ struct TestConfigurationDDSIM {
// NOLINTNEXTLINE (readability-identifier-naming)
inline void from_json(const nlohmann::json& j, TestConfigurationDDSIM& test) {
test.description = j.at("description").get<std::string>();
test.initialCircuit = j.at("initial_circuit").get<std::string>();
test.circuit = j.at("circuit").get<std::string>();
test.expectedResults = j.at("expected_meas_results").get<json>();
}

static std::vector<TestConfigurationDDSIM> getTests(const std::string& path) {
namespace {
std::vector<TestConfigurationDDSIM> getTests(const std::string& path) {
std::ifstream input(path);
nlohmann::json j;
input >> j;
return j;
}
} // namespace

class DDSIMExecTest : public ::testing::TestWithParam<TestConfigurationDDSIM> {
protected:
void SetUp() override {
test = GetParam();
std::stringstream ss(test.circuit);

std::stringstream ss(test.initialCircuit);
auto qc = std::make_unique<qc::QuantumComputation>();
auto qc = std::make_unique<qc::QuantumComputation>();
qc->import(ss, qc::Format::OpenQASM);
std::cout << "Circuit:\n" << *qc;

simulationTask = SimulationTask(std::move(qc));
circuitSimulatorExecutor = std::make_unique<CircuitSimulatorExecutor>();

result = circuitSimulatorExecutor->execute(simulationTask);
std::cout << "Results:" << std::endl;
std::cout << result.dump(2U) << std::endl;
}

void TearDown() override { std::cout << "Tearing down...\n"; }

SimulationTask simulationTask;
std::unique_ptr<CircuitSimulatorExecutor> circuitSimulatorExecutor;
TestConfigurationDDSIM test;
json result;
};

INSTANTIATE_TEST_SUITE_P(
Circuits, DDSIMExecTest, testing::ValuesIn(getTests("circuits.json")),
Circuits, DDSIMExecTest, testing::ValuesIn(getTests("sim_circuits.json")),
[](const testing::TestParamInfo<DDSIMExecTest::ParamType>& inf) {
return inf.param.description;
});

TEST_P(DDSIMExecTest, MeasResults) {
EXPECT_EQ(result["measurement_results"], test.expectedResults);
}
TEST_P(DDSIMExecTest, Tests) {
const auto result = circuitSimulatorExecutor->execute(simulationTask);
std::cout << "Results:\n" << result.dump(2U) << std::endl;

ASSERT_TRUE(result.contains("measurement_results"));
if (!result["measurement_results"].empty()) {
EXPECT_EQ(result["measurement_results"], test.expectedResults);
}

TEST_P(DDSIMExecTest, Entries) {
EXPECT_TRUE(result.contains("construction_time"));
EXPECT_TRUE(result.contains("execution_time"));
EXPECT_TRUE(result.contains("executor"));
Expand Down
14 changes: 14 additions & 0 deletions test/ver_circuits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"description": "simple_circuits",
"circuit1": "OPENQASM 2.0;include \"qelib1.inc\";qreg q[2];x q[0];x q[1];\n",
"circuit2": "OPENQASM 2.0;include \"qelib1.inc\";qreg q[2];x q[0];x q[1];x q[0];x q[0];\n",
"expected_equivalence": "equivalent"
},
{
"description": "non_equi_circuits",
"circuit1": "OPENQASM 2.0;include \"qelib1.inc\";qreg q[2];h q[0];t q[1];cx q[0], q[1];\n",
"circuit2": "OPENQASM 2.0;include \"qelib1.inc\";qreg q[2];h q[1];cx q[0], q[1];s q[0];\n",
"expected_equivalence": "not_equivalent"
}
]

0 comments on commit 37592a6

Please sign in to comment.