-
Notifications
You must be signed in to change notification settings - Fork 0
/
marp_2d.py
159 lines (109 loc) · 3.61 KB
/
marp_2d.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
# Demonstrates the use of "Schmidt decomposition rounding parameter" ("SDRP")
import math
import random
import sys
import time
from pyqrack import QrackSimulator
def cx(sim, q1, q2):
sim.mcx([q1], q2)
def cy(sim, q1, q2):
sim.mcy([q1], q2)
def cz(sim, q1, q2):
sim.mcz([q1], q2)
def acx(sim, q1, q2):
sim.macx([q1], q2)
def acy(sim, q1, q2):
sim.macy([q1], q2)
def acz(sim, q1, q2):
sim.macz([q1], q2)
def swap(sim, q1, q2):
sim.swap(q1, q2)
def iswap(sim, q1, q2):
sim.iswap(q1, q2)
def iiswap(sim, q1, q2):
sim.adjiswap(q1, q2)
def pswap(sim, q1, q2):
sim.mcz([q1], q2)
sim.swap(q1, q2)
def mswap(sim, q1, q2):
sim.swap(q1, q2)
sim.mcz([q1], q2)
def nswap(sim, q1, q2):
sim.mcz([q1], q2)
sim.swap(q1, q2)
sim.mcz([q1], q2)
def bench_qrack(width, depth, sdrp_samples):
# This is a "nearest-neighbor" coupler random circuit.
start = time.perf_counter()
lcv_range = range(width)
# Nearest-neighbor couplers:
gateSequence = [ 0, 3, 2, 1, 2, 1, 0, 3 ]
two_bit_gates = swap, pswap, mswap, nswap, iswap, iiswap, cx, cy, cz, acx, acy, acz
col_len = math.floor(math.sqrt(width))
while (((width // col_len) * col_len) != width):
col_len -= 1
row_len = width // col_len
sdrp_segments = sdrp_samples - 1
for i in range(0, sdrp_samples):
start = time.perf_counter()
sdrp = 1 if sdrp_samples == 1 else ((sdrp_segments - i) / sdrp_segments)
sim = QrackSimulator(width)
if sdrp > 0:
sim.set_sdrp(sdrp)
is_fail = False
for _ in range(depth):
# Single-qubit gates
for i in lcv_range:
try:
sim.u(i, random.uniform(0, 2 * math.pi), random.uniform(0, 2 * math.pi), random.uniform(0, 2 * math.pi))
except:
is_fail = True
break
# Nearest-neighbor couplers:
############################
gate = gateSequence.pop(0)
gateSequence.append(gate)
for row in range(1, row_len, 2):
for col in range(col_len):
temp_row = row
temp_col = col
temp_row = temp_row + (1 if (gate & 2) else -1);
temp_col = temp_col + (1 if (gate & 1) else 0)
if (temp_row < 0) or (temp_col < 0) or (temp_row >= row_len) or (temp_col >= row_len):
continue
b1 = row * row_len + col
b2 = temp_row * row_len + temp_col
if (b1 >= width) or (b2 >= width):
continue
g = random.choice(two_bit_gates)
try:
g(sim, b1, b2)
sim.try_separate_2qb(b1, b2)
except:
is_fail = True
if is_fail:
break
fidelity = sim.get_unitary_fidelity()
# Terminal measurement
sim.m_all()
print({
'width': width,
'depth': depth,
'sdrp': sdrp,
'time': time.perf_counter() - start,
'fidelity': fidelity
})
def main():
width = 36
depth = 6
sdrp_samples = 11
if len(sys.argv) != 4:
raise RuntimeError('Usage: python3 marp_2d.py [width] [depth] [sdrp_samples]')
width = int(sys.argv[1])
depth = int(sys.argv[2])
sdrp_samples = int(sys.argv[3])
# Run the benchmarks
bench_qrack(width, depth, sdrp_samples)
return 0
if __name__ == '__main__':
sys.exit(main())