Skip to content

Commit

Permalink
Remove redundant QCEC exception class and increase coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Burgholzer <[email protected]>
  • Loading branch information
burgholzer committed Mar 10, 2021
1 parent 1003348 commit b64d1e4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 37 deletions.
10 changes: 0 additions & 10 deletions include/EquivalenceChecker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,6 @@ namespace ec {
}
};

class QCECException : public std::invalid_argument {
std::string msg;
public:
explicit QCECException(std::string msg) : std::invalid_argument("QCEC Exception"), msg(std::move(msg)) { }

[[nodiscard]] const char *what() const noexcept override {
return msg.c_str();
}
};

class EquivalenceChecker {
protected:
qc::QuantumComputation& qc1;
Expand Down
2 changes: 1 addition & 1 deletion src/EquivalenceChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ namespace ec {
// Measurements at the end of the circuit are considered NOPs.
if ((*opIt)->getType() == qc::Measure) {
if (!qc::QuantumComputation::isLastOperationOnQubit(opIt, end)) {
throw QCECException("Intermediate measurements currently not supported. Defer your measurements to the end.");
throw std::invalid_argument("Intermediate measurements currently not supported. Defer your measurements to the end.");
}
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ImprovedDDEquivalenceChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace ec {
case ec::Strategy::Lookahead: checkLookahead(results.result, perm1, perm2);
break;
default:
throw QCECException("Strategy " + toString(config.strategy) + " not supported by ImprovedDDEquivalenceChecker");
throw std::invalid_argument("Strategy " + toString(config.strategy) + " not supported by ImprovedDDEquivalenceChecker");
}

// finish first circuit
Expand Down
35 changes: 34 additions & 1 deletion test/test_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ TEST_F(GeneralTest, IntermediateMeasurementNotSupported) {
std::stringstream ss2{bell_circuit};
ASSERT_NO_THROW(qc_alternative.import(ss2, qc::OpenQASM));
ec::EquivalenceChecker ec(qc_original, qc_alternative);
EXPECT_THROW(ec.check(), ec::QCECException);
EXPECT_THROW(ec.check(), std::invalid_argument);
}

TEST_F(GeneralTest, RemoveDiagonalGatesBeforeMeasure) {
Expand Down Expand Up @@ -186,3 +186,36 @@ TEST_F(GeneralTest, RemoveDiagonalGatesBeforeMeasure) {
results = ec3.check(config);
EXPECT_TRUE(results.consideredEquivalent());
}

TEST_F(GeneralTest, EquivalenceUpToGlobalPhase) {
qc_original.addQubitRegister(1);
qc_original.emplace_back<qc::StandardOperation>(1, 0, qc::X);
qc_original.emplace_back<qc::StandardOperation>(1, 0, qc::Z);
qc_original.emplace_back<qc::StandardOperation>(1, 0, qc::X);
qc_original.emplace_back<qc::StandardOperation>(1, 0, qc::Z);
qc_original.emplace_back<qc::StandardOperation>(1, 0, qc::X);
std::cout << qc_original << std::endl;

qc_alternative.addQubitRegister(1);
qc_alternative.emplace_back<qc::StandardOperation>(1, 0, qc::X);
std::cout << qc_alternative << std::endl;

ec::EquivalenceChecker ec(qc_original, qc_alternative);
auto results = ec.check();
EXPECT_EQ(results.equivalence, ec::Equivalence::EquivalentUpToGlobalPhase);
results.print();

ec::ImprovedDDEquivalenceChecker ec2(qc_original, qc_alternative);
auto results2 = ec2.check();
EXPECT_EQ(results2.equivalence, ec::Equivalence::EquivalentUpToGlobalPhase);
results2.print();
}

TEST_F(GeneralTest, InvalidStrategy) {
qc_original.addQubitRegister(1);
qc_original.emplace_back<qc::StandardOperation>(1, 0, qc::X);

ec::ImprovedDDEquivalenceChecker ec2(qc_original, qc_original);
ec::Configuration config{.strategy=ec::Strategy::CompilationFlow};
EXPECT_THROW(ec2.check(config), std::invalid_argument);
}
32 changes: 8 additions & 24 deletions test/test_journal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,7 @@ class JournalTestNonEQ: public testing::TestWithParam<std::tuple<std::string, un

void SetUp() override {
std::stringstream ss{};
ss << test_transpiled_dir << std::get<0>(GetParam()) << "_transpiled.";
if (std::get<0>(GetParam()) == "add6_196" || std::get<0>(GetParam()) == "cm150a_210") {
ss << "real";
} else {
ss << "qasm";
}
ss << test_transpiled_dir << std::get<0>(GetParam()) << "_transpiled.qasm";
transpiled_file = ss.str();

std::array<std::mt19937_64::result_type , std::mt19937_64::state_size> random_data{};
Expand Down Expand Up @@ -162,24 +157,13 @@ TEST_P(JournalTestNonEQ, PowerOfSimulation) {
} while (setExists(already_removed, removed));
already_removed.insert(removed);

try {
ec::SimulationBasedEquivalenceChecker noneq_sim(qc_original, qc_transpiled);
auto results = noneq_sim.check(config);
std::cout << "[" << i << "] ";
results.printCSVEntry();
addToStatistics(i, results.verificationTime + results.preprocessingTime, results.nsims);
if(results.equivalence == ec::Equivalence::NotEquivalent) {
successes++;
}
} catch (std::exception& e) {
std::cout << e.what() << std::endl;
for (const auto& rem: already_removed) {
std::cout << "{ ";
for (const auto r : rem) {
std::cout << r << " ";
}
std::cout << "}" << std::endl;
}
ec::SimulationBasedEquivalenceChecker noneq_sim(qc_original, qc_transpiled);
auto results = noneq_sim.check(config);
std::cout << "[" << i << "] ";
results.printCSVEntry();
addToStatistics(i, results.verificationTime + results.preprocessingTime, results.nsims);
if(results.equivalence == ec::Equivalence::NotEquivalent) {
successes++;
}
}
std::cout << qc_original.getName() << ";" << qc_original.getNqubits() << ";" << qc_original.getNops() << ";" << qc_transpiled.getNops()
Expand Down

0 comments on commit b64d1e4

Please sign in to comment.