-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.py
355 lines (308 loc) · 10.7 KB
/
bench.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"""Script for `sim-beta` project to generate qobj json file and analyze reorder algorithms"""
import argparse
import json
import logging
import pickle
from qiskit_aer.noise import NoiseModel
from qutils.misc import *
from reorder import Reorder
from noise import Noise
from qiskit import *
from qiskit.circuit.random import random_circuit
from qiskit.providers import Backend, BackendV1, BackendV2, provider, fake_provider
from qiskit.providers.jobstatus import JobStatus
from qiskit.providers.fake_provider import *
from qiskit.transpiler import CouplingMap
# from qvm.util import *
# from qvm.manager.backend_manager import *
# Logging configuration
logging.basicConfig(
filename="bench.log",
encoding="utf-8",
level=logging.DEBUG,
format="%(asctime)s %(message)s",
)
# IBMQ.save_account("036d2bca315b21dc9525cd05217943de9eab08326f37d652ac23aed075ea3e32ea3a983602b728e1b7c3e5e2a157959dcd0b834eb34ce607b3ec1d6401e9594d", overwrite=True)
DEVICE_LIST = [
"ibm_oslo",
"ibmq_manila",
"ibm_nairobi",
"ibmq_quito",
"ibmq_belem",
"ibmq_lima",
]
# provider = IBMQ.load_account()
provider = None
#### For json format
# sort_keys=True, indent=4, separators=(',', ':')
#### For json format
FAKE_BACKEND = "fake"
USE_BACKEND_NOISE = "backend"
def parse_args():
parser = argparse.ArgumentParser(description="")
parser.add_argument(
"--fusion", type=int, default=0, help="Whether enable fusion (Default: 0)"
)
parser.add_argument(
"--num-qubits", type=int, default=10, help="Number of qubits (Default: 10)"
)
parser.add_argument(
"--backend",
type=str,
default="aer_simulator",
help="Execution environment,"
" if set to IBM device name, [ibm_oslo, ibmq_manila, ibm_nairobi, ibmq_quito, ibmq_belem, ibmq_lima]"
" use real device. Can be a list (',' seperated), will execute the same circuit on each backend, "
" if set to fake, use fake backend (Default: aer_simulator)",
)
parser.add_argument(
"--mode",
type=str,
default="qasm",
help="How to construct a circuit, qasm | random | analysis (Default: qasm)",
)
parser.add_argument(
"--qasm-file", type=str, default="", help="The qasm file path Default: empty"
)
parser.add_argument(
"--depth", type=int, default=10, help="Depth of a circuit (Default: 10)"
)
parser.add_argument(
"--run", type=int, default=1, help="Whether run experiments (Default: 1)"
)
parser.add_argument(
"--analysis",
type=int,
default=0,
help="Whether perform static circuit analysis, (Default: 0)",
)
parser.add_argument(
"--save-qobj",
type=int,
default=0,
help="Whether save qobj file(analysis==1), (Default: 0)",
)
parser.add_argument(
"--qobj-file",
type=str,
default="qobj",
help="qobj file path(analysis==1 && save_qobj==1), (Default: qobj)",
)
parser.add_argument(
"--local-qubits",
type=int,
default=10,
help="Max qubits within a cluster. (analysis==1), (Default: 10)",
)
parser.add_argument(
"--draw-circ",
type=int,
default=0,
help="Whether print circuit diagram. (Default: 0)",
)
parser.add_argument(
"--reorder-method",
type=str,
default="static-new-local",
help="Method for reordering (clustering), (Default: static-new-local)",
)
parser.add_argument(
"--nl",
type=int,
default=2,
help="Number of local qubits (reorder_method=static-new-local), (Default: 2)",
)
parser.add_argument(
"--noise",
type=str,
default="backend",
help="Noise model name: device_name | backend | <customized>, "
"if this is set to IBM device name, use noise model from the deivice, (Default: backend)",
)
parser.add_argument(
"--save-sv",
dest="save_sv",
type=int,
default=0,
help="Whether to save statevector, (Default 0)",
)
return parser.parse_args()
# TODO: Make is_complete_prog optional from command line
def do_analysis(
qobj_dict,
local_qubits=10,
save_qobj=False,
qobj_file=None,
reorder_method="static-new-local",
nl=2,
is_complete_prog=True,
):
if save_qobj:
if qobj_file is None:
raise ValueError("Set qobj file name when save_qobj is True!")
with open(qobj_file, "w") as f:
f.write(
json.dumps(qobj_dict, sort_keys=True, indent=4, separators=(",", ":"))
)
op_lists = get_op_lists(qobj_dict)
reorder = Reorder.get_reorder(reorder_method)
reorder.local_qubits = local_qubits
if reorder_method == "static-new-local":
reorder.custom_local_qubits = nl
for op_list in op_lists:
print_op_list(op_list)
if not is_complete_prog:
op_list = get_op_list_without_measure(op_list)
print("Num ops before: {}".format(len(op_list)))
reorder.run(op_list)
reorder.print_res()
def analysis(
qobj,
local_qubits=10,
save_qobj=False,
qobj_file=None,
reorder_method="static-new-local",
nl=2,
):
qobj_dict = qobj.to_dict()
do_analysis(
qobj_dict,
local_qubits=local_qubits,
save_qobj=save_qobj,
qobj_file=qobj_file,
reorder_method=reorder_method,
nl=2,
)
def construct_circuit(args):
circ = QuantumCircuit()
if args.mode == "qasm":
if not args.qasm_file:
raise Exception("Should set the qasm file path when using qasm mode")
circ = QuantumCircuit().from_qasm_file(args.qasm_file)
elif args.mode == "random":
num_qubits = int(args.num_qubits)
depth = int(args.depth)
circ = random_circuit(num_qubits, depth, measure=True)
return circ
def get_backend_list(args):
backend_list = []
backend_name_list = args.backend.split(",")
for backend_name in backend_name_list:
if backend_name in DEVICE_LIST:
backend = provider.get_backend(backend_name)
elif backend_name == FAKE_BACKEND:
# backend = FakeWashingtonV2()
backend = FakeWashington()
# backend = FakeMelbourne()
# backend = FakeCairo()
# backend = FakeBrooklyn()
else:
backend = Aer.get_backend(backend_name)
backend.set_options(fusion_enable=(False if args.fusion == 0 else True))
backend_list.append(backend)
return backend_list
def run_all_backends():
pass
def run_circ(args, circ):
backend_list = get_backend_list(args)
for backend in backend_list:
coupling_map = None
basis_gates = None
if args.noise == USE_BACKEND_NOISE:
# Use noise model from backend
noise_model = NoiseModel.from_backend(backend)
if isinstance(backend, BackendV1):
coupling_map = backend.configuration().coupling_map
elif isinstance(backend, BackendV2):
coupling_map = backend.coupling_map
else:
raise ValueError("Unsupported backend type")
basis_gates = noise_model.basis_gates
elif args.noise in DEVICE_LIST:
# Use noise model from a specific IBM device
# Note that:
# If use specific device, this will override actual backend's configuration!
noise_device = provider.get_backend(args.noise)
noise_model = NoiseModel.from_backend(noise_device)
coupling_map = noise_device.configuration().coupling_map
basis_gates = noise_model.basis_gates
else:
noise_model = Noise.get_noise_model(args.noise)
# logging.info('zyl-qcs-running::NoiseModel:%s, Backend:%s, CouplingMap:%s', noise_model, backend, coupling_map)
# qobj = compile_circ(circ, backend, coupling_map, basis_gates)
# transpiled = gate_compile(circ, backend, coupling_map, basis_gates)
transpiled = gate_compile(circ, backend)
print("#Inst at gate level: {}".format(len(transpiled)))
scheduled = pulse_compile(transpiled, backend)
print("#Inst at pulse level: {}".format(len(scheduled.instructions)))
qobj = assemble(transpiled)
# qobj = scheduled
# qobj = circ
print(
"dt: {}, duration: {}".format(
backend.configuration().dt, scheduled.duration
)
)
if args.analysis == 1:
analysis(
qobj,
local_qubits=args.local_qubits,
save_qobj=(False if args.save_qobj == 0 else True),
qobj_file=args.qobj_file,
reorder_method=args.reorder_method,
nl=args.nl,
)
if args.run == 1:
run(qobj, backend, noise_model=noise_model, save_sv=args.save_sv)
@profile(t=2)
def compile_circ(circ, backend, coupling_map=None, basis_gates=None):
transpiled = transpile(
circ, backend, coupling_map=coupling_map, basis_gates=basis_gates
)
scheduled = schedule(transpiled, backend)
qobj = assemble(transpiled)
print("dt: {}, duration: {}".format(backend.configuration().dt, scheduled.duration))
return qobj
@profile(t=2)
def gate_compile(circ, backend, coupling_map=None, basis_gates=None):
transpiled = transpile(
circ, backend, coupling_map=coupling_map, basis_gates=basis_gates
)
return transpiled
@profile(t=2)
def pulse_compile(circ, backend):
scheduled = schedule(circ, backend)
return scheduled
def run_analysis_only(args):
if args.qobj_file == "":
raise ValueError("Please provide qobj_file")
qobj_dict = load_qobj_from_path(args.qobj_file)
do_analysis(qobj_dict)
@profile(t=2)
def run(qobj, backend, noise_model=None, save_sv=False):
# TODO: `noise_model` is not an option for IBMQ and will be ignored
# Be careful if version changed
job = backend.run(qobj, noise_model=noise_model)
if backend.name() in DEVICE_LIST:
return
# if job.status() is not JobStatus.DONE:
# print("Job is running")
counts = job.result().get_counts()
# print(counts)
if save_sv:
sv = job.result().get_statevector(0)
with open("temp.txt", "wb") as fw:
pickle.dump(sv.data.tolist()[1:2], fw)
def main():
args = parse_args()
if args.mode in ("qasm", "random"):
circ = construct_circuit(args)
if args.draw_circ == 1:
print(circ.draw(output="text"))
run_circ(args, circ)
elif args.mode == "analysis":
run_analysis_only(args)
else:
raise NotImplementedError("Unsupported mode")
if __name__ == "__main__":
main()