-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_scale.py
More file actions
98 lines (76 loc) · 3.15 KB
/
Copy pathmain_scale.py
File metadata and controls
98 lines (76 loc) · 3.15 KB
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
96
97
98
import sys
import time
import networkx as nx
import argparse
from qasm_parser import importQasm, exportQasm
from graph_builder import MeasureF, Commute, BuildCanon, DeleteOne, PotentialPair, reuseGraph
from smt_solver import smt
from ilp_solver import ilp
from rebuild import rebuild, rebuild_dep
from divide import divide_graph, conquer_graph, save_graph, gate_time
import os
import config
def main():
start_time = time.time()
config.f = open(config.log_file, "w")
config.qc = importQasm(config.input_file)
config.totalNum = len(config.qc.data)
if config.mode == 'eager' or config.mode == 'all':
MeasureF()
Commute()
canonical, paral_pair = BuildCanon()
if config.depth is not None:
D = config.depth
else:
D = len(config.qc.data)
round = 0
config.used = set()
config.gateT = []
config.reuseNum = 0
config.a = {}
config.remain = [i for i in range(config.qc.num_qubits)]
while True:
config.f.write("round: " + str(round) + "\n")
reuse_pair = PotentialPair(canonical)
if all(len(v) == 0 for v in reuse_pair.values()): # no more reuse pairs
break
select, filtered_reuse, filtered_paral = reuseGraph(reuse_pair, config.value_k, paral_pair)
if not select: # no more k-densest subgraph
break
subgraph = divide_graph(canonical.copy(), select)
if config.solver == 'SMT':
sol = smt(subgraph, filtered_reuse, D, filtered_paral, select)
else:
sol = ilp(subgraph, filtered_reuse, D, filtered_paral, select)
if sol is False:
break
config.used.update(config.a.keys())
# Update global graph: add edge Mi → vj for all reuse pairs
canonical = conquer_graph(canonical)
glocal_gateT = gate_time(canonical)
re_qc = rebuild_dep(glocal_gateT)
end_time = time.time()
print("total time: ", end_time - start_time, "s", flush=True)
exportQasm(re_qc, config.output_file)
out = importQasm(config.output_file)
print("depth:", out.depth())
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--input', type=str, required=True, help='Input file path for QASM')
parser.add_argument('--output', type=str, required=True, help='Output file path for QASM')
parser.add_argument('--log', type=str, required=True, help='Log file path')
parser.add_argument('--mode', type=str, choices=['none', 'reorder', 'eager', 'all'], default='all',
help='Mode selection:(none, reorder, eager, all)')
parser.add_argument('--depth', type=int, default = None, help='depth constraint')
parser.add_argument('--solver', type=str, choices=['SMT', 'ILP'], default='ILP',
help='SMT or ILP solver')
parser.add_argument("--k", type=int, default=8, help="k value for k-densest subgraph")
args = parser.parse_args()
config.input_file = args.input
config.output_file = args.output
config.log_file = args.log
config.mode = args.mode
config.depth = args.depth
config.solver = args.solver
config.value_k = args.k
main()