-
Notifications
You must be signed in to change notification settings - Fork 0
/
measure_impact.py
179 lines (163 loc) · 5.09 KB
/
measure_impact.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
174
175
176
177
178
179
from __future__ import annotations
from os.path import exists
from os import mkdir, listdir
import argparse
import pickle
from partition_analysis import PartitionAnalyzer
from post_synth import replace_blocks
from bqskit.ir.circuit import Circuit
from bqskit.ir.lang.qasm2.qasm2 import OPENQASM2Language
from bqskit.compiler.machine import MachineModel
from bqskit.passes.partitioning.scan import ScanPartitioner
from bqskit.passes.partitioning.greedy import GreedyPartitioner
from bqskit.passes.partitioning.quick import QuickPartitioner
from bqskit.passes.util.intermediate import SaveIntermediatePass
from mapping import do_layout, do_routing, manual_layout, random_layout
from mapping import dummy_layout, dummy_routing, dummy_synthesis
from topology import get_logical_operations, kernel_type, run_stats, match_kernel
from util import (
load_circuit_structure,
save_block_topology,
setup_options,
get_summary,
)
from old_codebase import synthesize
# Enable logging
import logging
logging.getLogger('bqskit').setLevel(logging.INFO)
if __name__ == '__main__':
# Run setup
#region setup
parser = argparse.ArgumentParser(
description="Run subtopoloy aware synthesis"
" based on the hybrid logical-physical topology scheme"
)
parser.add_argument(
"qasm_file", type=str, help="file to compare",
default="qasm/qft_10.qasm"
)
parser.add_argument(
"--blocksize", dest="blocksize", action="store", nargs='?', default=4,
type=int, help="synthesis block size"
)
parser.add_argument(
"--partitioner", dest="partitioner", action="store", nargs="?",
default="quick", type=str, help="partitioner to use [scan | greedy]"
)
parser.add_argument(
"--dummy_map", action="store_true",
help="skip synthesis and routing"
)
parser.add_argument(
"--partition_only", action="store_true",
help="skip synthesis and routing"
)
parser.add_argument("--topology", dest="map_type", action="store",
default="mesh", type=str,
help="[mesh | linear | falcon]"
)
parser.add_argument("--router", dest="router", action="store",
default="qiskit", type=str,
help="[pytket | qiskit]"
)
parser.add_argument("--alltoall",action="store_true",
help="synthesize to all to all"
)
parser.add_argument("--layout", dest="layout", action="store",
default="none", type=str,
help="[none | random | sabre]"
)
parser.add_argument("--synthesis_impact", action="store_true",
help="Run comparison between non-synthesis and synthesis versions")
args = parser.parse_args()
#endregion
options = setup_options(args.qasm_file, args)
if not exists(options["synthesis_dir"]):
mkdir(options["synthesis_dir"])
## Layout
##region layout
#print("="*80)
#print(f"Doing layout for unsynthesized {options['unsynthesized_layout']}...")
#print("="*80)
if not exists(options["unsynthesized_layout"]):
l2p_mapping = manual_layout(
options["layout_qasm_file"],
options["relayout_remapping_file"],
options["unsynthesized_layout"],
options
)
with open(options["unsynthesized_qubit_remapping"], "wb") as f:
pickle.dump(l2p_mapping, f)
#else:
# print(
# f"Found existing file for {options['unsynthesized_layout']}, "
# "skipping layout"
# )
#endregion
block_files = sorted(listdir(options["partition_dir"]))
block_names = []
block_files.remove("structure.pickle")
for bf in block_files:
if options["checkpoint_as_qasm"]:
block_names.append(bf.split(".qasm")[0])
else:
block_names.append(bf.split(".pickle")[0])
#endregion
## Routing
##region routing
#print("="*80)
#print(f"Doing Routing for {options['unsynthesized_layout']}...")
#print("="*80)
if not exists(options["unsynthesized_mapping"]):
l2p_mapping = do_routing(
options["unsynthesized_layout"],
options["coupling_map"],
options["unsynthesized_mapping"],
options,
)
#else:
# print(
# f"Found existing file for {options['unsynthesized_mapping']}, "
# "skipping routing"
# )
#endregion
## Collect stats
##region collections
#print("="*80)
#print(f"Collecting stats about each partiiton")
#print("="*80)
if args.synthesis_impact:
mapped_analysis = options["mapped_qasm_file"]
block_dir = options["synthesis_dir"]
remapping_file = options["relayout_remapping_file"]
else:
mapped_analysis = options["unsynthesized_mapping"]
#mapped_analysis = options["unsynthesized_layout"]
block_dir = options["partition_dir"]
remapping_file = options["unsynthesized_qubit_remapping"]
#remapping_file = options["relayout_remapping_file"]
# Convert structure using mapping data
with open(f"{options['partition_dir']}/structure.pickle", "rb") as f:
structure = pickle.load(f)
with open(remapping_file, "rb") as f:
l2p_mapping = pickle.load(f)
new_structure = []
for qudit_group in structure:
new_group = []
for qudit in qudit_group:
new_group.append(l2p_mapping[qudit])
new_structure.append(new_group)
# Get list of all block subtopologies
subtopology_list = sorted(listdir(options['subtopology_dir']))
subtopology_list.remove("summary.txt")
analyzer = PartitionAnalyzer(
mapped_analysis,
block_dir,
block_files,
new_structure,
options["num_p"],
options["subtopology_dir"],
subtopology_list,
)
analyzer.run()
#endregion