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

[Interface] equivalence checker for OpenQASM files #194

Merged
merged 3 commits into from
Dec 21, 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
2 changes: 2 additions & 0 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ file(GLOB_RECURSE TEST_MULT "test_mult.cpp")
file(GLOB_RECURSE TEST_PI "test_pi.cpp")
file(GLOB_RECURSE TEST_CONSTANTS "test_constants.cpp")
file(GLOB_RECURSE TEST_QASM_PARSER "test_qasm_parser.cpp" )
file(GLOB_RECURSE VERIFY_OPENQASM "verify_openqasm.cpp" )
if(USE_ARBLIB)
file(GLOB_RECURSE TEST_ARB "test_arb.cpp")
endif()
Expand Down Expand Up @@ -74,6 +75,7 @@ add_executable(test_mult ${TEST_MULT} )
add_executable(test_pi ${TEST_PI} )
add_executable(test_constants ${TEST_CONSTANTS} )
add_executable(test_qasm_parser ${TEST_QASM_PARSER} )
add_executable(verify_openqasm ${VERIFY_OPENQASM} )
if(USE_ARBLIB)
add_executable(test_arb ${TEST_ARB} )
endif()
82 changes: 82 additions & 0 deletions src/test/verify_openqasm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "quartz/context/context.h"
#include "quartz/dataset/dataset.h"
#include "quartz/gate/gate.h"
#include "quartz/parser/qasm_parser.h"

#include <cassert>

using namespace quartz;

bool add_circ(QASMParser &parser, Context &ctx, Dataset &data, std::string f) {
// Attempts to parse the circuit.
CircuitSeq *circ = nullptr;
if (!parser.load_qasm(f, circ)) {
return false;
}

// Passes ownership of the circuit to the dataset.
data.insert(&ctx, std::unique_ptr<CircuitSeq>(circ));
return true;
}

int main(int argc, char **argv) {
// Sets up an OpenQASM 3 context.
ParamInfo param_info;
Context ctx({GateType::pi, GateType::add, GateType::mult, GateType::neg,
GateType::x, GateType::y, GateType::z, GateType::p,
GateType::h, GateType::s, GateType::t, GateType::sx,
GateType::pdg, GateType::sdg, GateType::tdg, GateType::rx,
GateType::ry, GateType::rz, GateType::cx, GateType::cz,
GateType::cp, GateType::ch, GateType::swap, GateType::ccx,
GateType::ccz, GateType::u1, GateType::u2, GateType::u3,
GateType::cu1},
&param_info);

// Sets up a symbolic OpenQASM 3 parser, for use by both files.
QASMParser parser(&ctx);
parser.use_symbolic_pi(true);

// Processes command-line arguments.
std::string tmpfile = "tmp.json";
std::string outfile = "res.json";
if (argc < 3 || argc > 4) {
std::cerr << "Usage: " << argv[0] << " circ1 circ2 [tmpdir]" << std::endl;
return -1;
} else if (argc == 4) {
std::string dir = argv[3];
tmpfile = dir + "/" + tmpfile;
outfile = dir + "/" + outfile;
}

// Parses and validates the circuits.
Dataset data;
if (!add_circ(parser, ctx, data, argv[1])) {
std::cerr << "Failed to parse the first circuit." << std::endl;
return -1;
}
if (!add_circ(parser, ctx, data, argv[2])) {
std::cerr << "Failed to parse the second circuit." << std::endl;
return -1;
}

// Generates a json file for the Python-based equivalence checker.
if (!data.save_json(&ctx, tmpfile)) {
std::cerr << "Failed to generate the json file." << std::endl;
return -1;
}

// Command-line arguments for the equivalence checker.
std::filesystem::path this_file_path(__FILE__);
auto verifier_path = this_file_path.parent_path()
.parent_path()
.append("python")
.append("verifier")
.append("verify_equivalences.py");
std::string script = verifier_path.string();
std::string arglst = tmpfile + " " + outfile + " " + "True True True True";

// Applies the equivalence checker to the json file.
std::string command = "python " + script + " " + arglst;
system(command.c_str());
return 1;
}
Loading