-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_quartz.cpp
95 lines (86 loc) · 3.49 KB
/
run_quartz.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "quartz/parser/qasm_parser.h"
#include "quartz/tasograph/substitution.h"
#include "quartz/tasograph/tasograph.h"
using namespace quartz;
void parse_args(char **argv, int argc, bool &simulated_annealing,
bool &early_stop, bool &disable_search,
std::string &input_filename, std::string &output_filename,
std::string &eqset_filename) {
assert(argv[1] != nullptr);
input_filename = std::string(argv[1]);
early_stop = true;
for (int i = 2; i < argc; i++) {
if (!std::strcmp(argv[i], "--output")) {
output_filename = std::string(argv[++i]);
continue;
}
if (!std::strcmp(argv[i], "--eqset")) {
eqset_filename = std::string(argv[++i]);
continue;
}
if (!std::strcmp(argv[i], "--disable_search")) {
disable_search = true;
continue;
}
}
}
int main(int argc, char **argv) {
std::string input_fn, output_fn;
std::string eqset_fn = "";
bool simulated_annealing = false;
bool early_stop = false;
bool disable_search = false;
parse_args(argv, argc, simulated_annealing, early_stop, disable_search,
input_fn, output_fn, eqset_fn);
auto fn = input_fn.substr(input_fn.rfind('/') + 1);
// Construct contexts
Context src_ctx({GateType::h, GateType::ccz, GateType::x, GateType::cx, GateType::t, GateType::tdg,
GateType::input_qubit, GateType::input_param});
Context dst_ctx({GateType::h, GateType::x, GateType::t, GateType::tdg, GateType::add,
GateType::cx, GateType::input_qubit, GateType::input_param});
auto union_ctx = union_contexts(&src_ctx, &dst_ctx);
auto xfer_pair = GraphXfer::ccz_cx_t_xfer(&union_ctx);
// Load qasm file
QASMParser qasm_parser(&src_ctx);
CircuitSeq *dag = nullptr;
if (!qasm_parser.load_qasm(input_fn, dag)) {
std::cout << "Parser failed" << std::endl;
}
Graph graph(&src_ctx, dag);
auto start = std::chrono::steady_clock::now();
// Greedy toffoli flip
auto graph_before_search = graph.toffoli_flip_greedy(
GateType::rz, xfer_pair.first, xfer_pair.second);
// graph_before_search->to_qasm(input_fn + ".toffoli_flip", false, false);
auto end = std::chrono::steady_clock::now();
if (disable_search) {
std::cout << "Optimization results of Quartz for " << fn
<< " on Clifford+T gate set."
<< " Gate count after optimization: "
<< graph_before_search->gate_count() << ", "
<< "Circuit depth: " << graph_before_search->circuit_depth()
<< ", "
<< (double)std::chrono::duration_cast<std::chrono::milliseconds>(
end - start)
.count() /
1000.0
<< " seconds." << std::endl;
return 0;
}
// Optimization
auto graph_after_search =
graph_before_search->optimize(&dst_ctx, eqset_fn, fn, /*print_message=*/
true);
end = std::chrono::steady_clock::now();
std::cout << "Optimization results of Quartz for " << fn
<< " on Clifford+T gate set."
<< " Gate count after optimization: "
<< graph_after_search->gate_count() << ", "
<< "Circuit depth: " << graph_after_search->circuit_depth() << ", "
<< (double)std::chrono::duration_cast<std::chrono::milliseconds>(
end - start)
.count() /
1000.0
<< " seconds." << std::endl;
graph_after_search->to_qasm(output_fn, false, false);
}