-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqasm_analyze.py
118 lines (101 loc) · 3.15 KB
/
qasm_analyze.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
print("Starting analysis script")
print("Importing libraries")
import sys
import os
import timeout_decorator
from qiskit import qasm3, transpile
from qiskit_aer import Aer
from qiskit.visualization import plot_histogram
import matplotlib
matplotlib.use("svg")
import matplotlib.pyplot as plt
RED = "\033[0;31m"
GREEN = "\033[0;32m"
YELLOW = "\033[0;33m"
NC = "\033[0m"
LOAD_TIMEOUT = 40
DRAW_TIMEOUT = 40
SIMULATE_TIMEOUT = 300
SIM_SHOTS = 10000
def format_label(x):
x = x[::-1]
parts = x.split(" ")
assert len(parts) == 2
if parts[1] == "0" * len(parts[1]):
if parts[0] == "":
return "null"
else:
return parts[0]
else:
return "error"
@timeout_decorator.timeout(DRAW_TIMEOUT, use_signals=False)
def draw_circuit(circuit, basename):
print("Drawing circuit")
out_filename = "diagrams/circuits/" + basename + ".svg"
try:
circuit.draw(
"mpl",
filename=out_filename,
fold=100,
)
except Exception:
circuit.draw("mpl", scale=0.2, filename=out_filename)
print(f"Diagram in {out_filename}")
@timeout_decorator.timeout(SIMULATE_TIMEOUT, use_signals=False)
def simulate_circuit(circuit, basename):
reg_counts = {reg.name: len(reg) for reg in circuit.cregs}
if len(reg_counts) == 0 or max(reg_counts.values()) == 0:
counts = {"null": SIM_SHOTS}
else:
backend = Aer.get_backend("qasm_simulator")
print("Transpiling circuit")
circuit = transpile(
circuit,
basis_gates=(backend.operation_names + ["if_else"]),
optimization_level=3,
seed_transpiler=0,
)
print("Depth:", circuit.depth())
print("Gates:", sum(circuit.count_ops().values()))
job = backend.run(
circuit,
seed_simulator=0,
shots=SIM_SHOTS,
)
print("Simulating circuit")
result = job.result()
counts_raw = result.get_counts()
counts_list = [(format_label(x), y) for x, y in counts_raw.items()]
counts = dict()
for x, y in counts_list:
if x not in counts:
counts[x] = 0
counts[x] += y
out_filename = "diagrams/sim_results/" + basename + "_sim_results.svg"
fig, ax = plt.subplots()
plot_histogram(counts, ax=ax)
fig.savefig(out_filename)
plt.close(fig)
print(f"Results in {out_filename}")
def analyze_file(qasm_filename):
print(f"Analyzing file {qasm_filename}")
basename = os.path.splitext(os.path.basename(qasm_filename))[0]
print("Loading circuit")
circuit = timeout_decorator.timeout(LOAD_TIMEOUT)(qasm3.load)(qasm_filename)
draw_circuit(circuit, basename)
simulate_circuit(circuit, basename)
path = sys.argv[1]
if os.path.isdir(path):
print()
for filename in os.listdir(path):
if filename.endswith(".qasm"):
try:
analyze_file(os.path.join(path, filename))
except Exception as e:
print(f"{RED}Error: {e}{NC}")
print()
else:
try:
analyze_file(path)
except Exception as e:
print(f"{RED}Error: {e}{NC}")