forked from zxcalc/pyzx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.py
134 lines (124 loc) · 4.24 KB
/
benchmark.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
import sys
import pyzx as zx
import os
import time
import multiprocessing as mp
class CircuitComparer:
def __init__(self, dirname, before, after):
self.fname_before = os.path.join(dirname, before)
if after:
self.fname_after = os.path.join(dirname, after)
else:
self.fname_after = ""
self.fname_tpar = ""
if before.find('before') != -1:
self.name = before[:-7]
else:
self.name = before
self.has_run = False
def __str__(self):
return "CircuitComparer({}, {})".format(self.name, str(self.has_run))
def __repr__(self):
return str(self)
def run(self):
if self.has_run: return True
#try:
if self.fname_after:
c = zx.Circuit.from_quipper_file(self.fname_after).to_basic_gates()
self.t_opt = c.tcount()
else:
self.t_opt = '-'
c = zx.Circuit.load(self.fname_before).to_basic_gates()
self.qubits = c.qubits
#except TypeError: return False
if self.fname_tpar:
c2 = zx.Circuit.load(self.fname_tpar)
self.tpar = c2.tcount()
else: self.tpar = "-"
self.gatecount = len(c.gates)
self.t_before = c.tcount()
g = c.to_graph()
t = time.time()
while True:
zx.simplify.full_reduce(g)
break
m = zx.rules.match_gadgets_phasepoly(g)
if not m: break
zx.rules.apply_gadget_phasepoly(g, m)
self.t_after = zx.tcount(g)
self.time_simpl = time.time() - t
t = time.time()
self.extracts = True
try:
c2 = zx.extract.streaming_extract(g,quiet=True)
self.time_extr = time.time() - t
except Exception:
self.extracts = False
self.time_extr = "-"
self.has_run = True
del c, g
return True
def pretty(self):
if not self.has_run:
success = self.run()
else: success = True
if not success:
return self.name + " -"
s = self.name.ljust(20) + str(self.qubits).rjust(7)
s += str(self.gatecount).rjust(8) + str(self.t_before).rjust(9) + str(self.t_opt).rjust(10)
s += str(self.tpar).rjust(6) + str(self.t_after).rjust(7)
s += "{:.2f}".format(self.time_simpl).rjust(12)
s += "{:.2f}".format(self.time_extr).rjust(14)
#s += ("y" if self.extracts else "n").rjust(7)
return s
def runner(arg):
c, printlock = arg
s = c.pretty()
with printlock:
print(s)
sys.stdout.flush()
return s
if __name__ == '__main__':
dirs = [r'circuits\Arithmetic_and_Toffoli', r'circuits\QFT_and_Adders', r'circuits\Other']
beforefiles = []
afterfiles = []
tparfiles = []
for d in dirs:
for f in os.listdir(d):
if not os.path.isfile(os.path.join(d,f)): continue
if f.find('before') != -1:
beforefiles.append((f,d))
elif f.find('tpar') != -1:
tparfiles.append((f,d))
elif f.find('.qc') != -1 or f.find('.tfc') != -1:
beforefiles.append((f,d))
else: afterfiles.append((f,d))
circuits = []
for f, d in beforefiles:
n = f[:-7]
for f2,d2 in afterfiles:
if d!=d2: continue
if f2.startswith(n):
c = CircuitComparer(d, f, f2)
circuits.append(c)
break
else:
c = CircuitComparer(d, f, '')
circuits.append(c)
for f2,d2 in tparfiles:
if d!=d2: continue
if f2.startswith(n):
circuits[-1].fname_tpar = os.path.join(d2,f2)
nprocesses = 4
m = mp.Manager()
printlock = m.Lock()
pool = mp.Pool(processes=nprocesses)
print("Circuit".ljust(20), "qubits", "G-count", "T-before", "T-kitchen", "T-par", " T-us", " Time-Simp", "Time-Extract")
try:
strings = pool.map(runner, [(c,printlock) for c in circuits])
finally:
pool.terminate()
strings.sort()
print("\n\n")
print("Circuit".ljust(20), "qubits", "G-count", "T-before", "T-kitchen", "T-par", " T-us", " Time-Simp", "Time-Extract")
print("\n".join(strings))