-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassemble_best.py
175 lines (154 loc) · 6.85 KB
/
assemble_best.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import argparse
from os import listdir
from os.path import exists
from bqskit import Circuit
from bqskit.ir.gates.constant.cx import CNOTGate
from topology import kernel_type
import pickle
def get_subtopology(circuit):
edge_set = set([])
for op in circuit:
if len(op.location) > 1:
edge_set.add(op.location)
return list(edge_set)
def write_subtopology(edge_set, location):
with open(location, "wb") as f:
pickle.dump(edge_set, f)
def write_block(qasm_file, location):
with open(location, "w") as f_out:
with open(qasm_file, "r") as f_qasm:
f_out.write(f_qasm.read())
if __name__ == '__main__':
"""
NOTE: Only support blocksize 4 right now
"""
parser = argparse.ArgumentParser()
# NOTE: Give block_files/benchmark directory
parser.add_argument("benchmark", type=str)
args = parser.parse_args()
if args.benchmark.endswith('/'):
benchmark = args.benchmark.split('/')[-2]
else:
benchmark = args.benchmark.split('/')[-1]
if '-' in benchmark:
benchmark = benchmark.split('-')[-1]
print(benchmark)
bname = benchmark.split('_preoptimized')[0]
mesh_suffices = (
"mesh_4", "mesh_9", "mesh_16", "mesh_25", "mesh_36",
"mesh_49","mesh_64", "mesh_81", "mesh_100", "mesh_121"
)
for suffix in mesh_suffices:
mesh_name = f'{bname}_preoptimized_{suffix}_blocksize_4_scan'
if exists(f'block_files/{mesh_name}'):
break
#benchmark_base = f'block_files/{benchmark}'
#benchmark_lines = f'synthesis_files/lines-{mesh_name}_kernel'
#benchmark_stars = f'synthesis_files/stars-{mesh_name}_kernel'
#benchmark_rings = f'synthesis_files/rings-{mesh_name}_kernel'
#benchmark_kites = f'synthesis_files/kites-{mesh_name}_kernel'
#benchmark_thetas = f'synthesis_files/thetas-{mesh_name}_kernel'
#benchmark_alls = f'synthesis_files/alls-{mesh_name}_kernel'
# neighbors
benchmark_base = f'block_files/{benchmark}'
benchmark_lines = f'synthesis_files/neighbors_lines-{mesh_name}_kernel'
benchmark_stars = f'synthesis_files/neighbors_stars-{mesh_name}_kernel'
benchmark_rings = f'synthesis_files/neighbors_rings-{mesh_name}_kernel'
benchmark_kites = f'synthesis_files/kites-{mesh_name}_kernel'
benchmark_thetas = f'synthesis_files/thetas-{mesh_name}_kernel'
benchmark_alls = f'synthesis_files/alls-{mesh_name}_kernel'
# WITH NEIGHBOR NAME, OTHER IS ALSO NEIGHBORS JUST WITHOUT THE PREFIX
## biased
#best_blocks = f'synthesis_files/neighbors_biased-{benchmark}_kernel'
#best_topologies = f'subtopology_files/neighbors_biased-{benchmark}_kernel'
# trees
best_blocks = f'synthesis_files/neighbors_trees-{benchmark}_kernel'
best_topologies = f'subtopology_files/neighbors_trees-{benchmark}_kernel'
## embedded
#best_blocks = f'synthesis_files/neighbors_embedded-{benchmark}_kernel'
#best_topologies = f'subtopology_files/neighbors_embedded-{benchmark}_kernel'
## best
#best_blocks = f'synthesis_files/neighbors_best-{benchmark}_kernel'
## biased
#best_blocks = f'synthesis_files/biased-{benchmark}_kernel'
#best_topologies = f'subtopology_files/biased-{benchmark}_kernel'
## trees
#best_blocks = f'synthesis_files/trees-{benchmark}_kernel'
#best_topologies = f'subtopology_files/trees-{benchmark}_kernel'
## embedded
#best_blocks = f'synthesis_files/embedded-{benchmark}_kernel'
#best_topologies = f'subtopology_files/embedded-{benchmark}_kernel'
## best
#best_blocks = f'synthesis_files/best-{benchmark}_kernel'
#best_topologies = f'subtopology_files/best-{benchmark}_kernel'
#best_blocks = f'synthesis_files/{benchmark}_kernel'
#best_topologies = f'subtopology_files/{benchmark}_kernel'
blocks = [b for b in sorted(listdir(benchmark_base)) if b.endswith('.qasm')]
for block in blocks:
best_topology = f'{block.split(".qasm")[0]}_kernel.pickle'
circuit_lines = Circuit.from_file(f'{benchmark_lines}/{block}')
circuit_stars = Circuit.from_file(f'{benchmark_stars}/{block}')
circuit_rings = Circuit.from_file(f'{benchmark_rings}/{block}')
#circuit_kites = Circuit.from_file(f'{benchmark_kites}/{block}')
#circuit_thetas = Circuit.from_file(f'{benchmark_thetas}/{block}')
#circuit_alls = Circuit.from_file(f'{benchmark_alls}/{block}')
#circuit_kites = Circuit(1)
#circuit_thetas = Circuit(1)
circuit_alls = Circuit(1)
## biased
#ring_bias = 3
#kite_bias = 4
#theta_bias = 4
#all_bias = 500
# trees
ring_bias = 10
kite_bias = 20
theta_bias = 20
all_bias = 500
## embedded
#ring_bias = 3
#kite_bias = 10
#theta_bias = 40
#all_bias = 40
## best
#ring_bias = 0
#kite_bias = 0
#theta_bias = 0
#all_bias = 500
cnots_lines = circuit_lines.count(CNOTGate())
cnots_stars = circuit_stars.count(CNOTGate()) + 4
cnots_rings = circuit_rings.count(CNOTGate()) + ring_bias
#cnots_kites = circuit_kites.count(CNOTGate()) + kite_bias
#cnots_thetas= circuit_thetas.count(CNOTGate()) + theta_bias
#cnots_alls = circuit_alls.count(CNOTGate()) + all_bias
#cnots_list = [cnots_lines, cnots_stars, cnots_rings, cnots_kites, cnots_thetas, cnots_alls]
cnots_list = [cnots_lines, cnots_stars, cnots_rings]
min_cnots = min(cnots_list)
print(f'Assembling best blocks for {benchmark}...')
if min_cnots == cnots_lines: # lines
#print(f'{block} - line - (->{cnots_lines}<-, {cnots_stars}, {cnots_rings}) = line selected')
print(f'{block} - line {min_cnots} - {cnots_list}')
write_subtopology(get_subtopology(circuit_lines), f'{best_topologies}/{best_topology}')
write_block(f'{benchmark_lines}/{block}', f'{best_blocks}/{block}')
elif min_cnots == cnots_stars: # stars
#print(f'{block} - ({cnots_lines}, ->{cnots_stars}<-, {cnots_rings}) = star selected')
print(f'{block} - star {min_cnots} - {cnots_list}')
write_subtopology(get_subtopology(circuit_stars), f'{best_topologies}/{best_topology}')
write_block(f'{benchmark_stars}/{block}', f'{best_blocks}/{block}')
elif min_cnots == cnots_rings: # rings
#print(f'{block} - ({cnots_lines}, {cnots_stars}, ->{cnots_rings}<-) = ring selected')
print(f'{block} - ring {min_cnots} - {cnots_list}')
write_subtopology(get_subtopology(circuit_rings), f'{best_topologies}/{best_topology}')
write_block(f'{benchmark_rings}/{block}', f'{best_blocks}/{block}')
# elif min_cnots == cnots_kites: # kites
# print(f'{block} - kite {min_cnots} - {cnots_list}')
# write_subtopology(get_subtopology(circuit_kites), f'{best_topologies}/{best_topology}')
# write_block(f'{benchmark_kites}/{block}', f'{best_blocks}/{block}')
# elif min_cnots == cnots_thetas: # thetas
# print(f'{block} - theta {min_cnots} - {cnots_list}')
# write_subtopology(get_subtopology(circuit_thetas), f'{best_topologies}/{best_topology}')
# write_block(f'{benchmark_thetas}/{block}', f'{best_blocks}/{block}')
# elif min_cnots == cnots_alls: # alls
# print(f'{block} - all {min_cnots} - {cnots_list}')
# write_subtopology(get_subtopology(circuit_alls), f'{best_topologies}/{best_topology}')
# write_block(f'{benchmark_alls}/{block}', f'{best_blocks}/{block}')