-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinitialize_micromoth.py
More file actions
260 lines (226 loc) · 7.42 KB
/
initialize_micromoth.py
File metadata and controls
260 lines (226 loc) · 7.42 KB
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
# First we strip comments and blank lines from the top-level version of MicroMoth.py
f = open("../../micromoth.py", "r")
file = f.read().split('\n')
f.close()
for j,line in enumerate(file):
file[j] = line.split("#")[0]
if line.count("'''")==2:
file[j] = line.split("'''")[0]
new_file = []
for j,line in enumerate(file):
if (line!='') and (not line.isspace()):
new_file.append(line)
new_file = '\n'.join(new_file)
f = open("micromoth.py", "w")
f.write( new_file )
f.close()
print('Comments removed from top-level version of MicroMoth.py.')
# Then we test to see if it works!
from micromoth import *
shots = int(1e6)
def test_trig():
assert( sin(pi/2)==1.0 )
assert( cos(2*pi)==1.0 )
def test_x():
qc = QuantumCircuit(1)
qc.x(0)
assert( simulate(qc,shots=shots,get='statevector')==[[0.0,0.0],[1.0,0.0]] )
qc = QuantumCircuit(2)
qc.x(1)
assert( simulate(qc,shots=shots,get='statevector')==[[0.0,0.0],[0.0,0.0],[1.0,0.0],[0.0,0.0]] )
qc = QuantumCircuit(2)
qc.x(0)
qc.x(1)
assert( simulate(qc,shots=shots,get='statevector')==[[0.0,0.0],[0.0,0.0],[0.0,0.0],[1.0,0.0]] )
def test_h():
qc = QuantumCircuit(2)
qc.h(0)
assert( simulate(qc,shots=shots,get='statevector')==[[0.70710678118, 0.0], [0.70710678118, 0.0], [0.0, 0.0], [0.0, 0.0]] )
qc = QuantumCircuit(2)
qc.h(1)
assert( simulate(qc,shots=shots,get='statevector')==[[0.70710678118, 0.0], [0.0, 0.0], [0.70710678118, 0.0], [0.0, 0.0]] )
qc = QuantumCircuit(2)
qc.h(0)
qc.h(1)
assert( simulate(qc,shots=shots,get='statevector')==[[0.49999999999074046, 0.0], [0.49999999999074046, 0.0], [0.49999999999074046, 0.0], [0.49999999999074046, 0.0]] )
def test_rx():
qc = QuantumCircuit(1)
qc.rx(pi/4,0)
assert(simulate(qc,get='statevector')==[[0.9238795325112867, 0.0], [0.0, -0.3826834323650898]])
qc = QuantumCircuit(2)
qc.rx(pi/4,0)
qc.rx(pi/8,1)
assert(simulate(qc,get='statevector')==[[0.9061274463528878, 0.0], [0.0, -0.37533027751786524], [0.0, -0.18023995550173696], [-0.0746578340503426, 0.0]])
qc.h(0)
qc.h(1)
assert(simulate(qc,get='statevector')==[[0.4157348061435736, -0.27778511650465676], [0.4903926401925336, 0.0975451610062577], [0.4903926401925336, -0.0975451610062577], [0.4157348061435736, 0.27778511650465676]])
def test_rz():
# arbitrary angles
tx = 2.8777603974458796
tz = 0.5589019778800038
# an rz rotatation using h*rx*h
qcx = QuantumCircuit(1)
qcx.rx(tx,0)
qcx.h(0)
qcx.rx(tz,0)
qcx.h(0)
ketx = simulate(qcx,get='statevector')
# a plain rz rotation
qcz = QuantumCircuit(1)
qcz.rx(tx,0)
qcz.rz(tz,0)
simulate(qcz,get='statevector')
ketz = simulate(qcz,get='statevector')
# check they are the same
for j in range(2):
for k in range(2):
assert round(ketx[j][k],3)==round(ketz[j][k],3)
def test_ry():
qc = QuantumCircuit(1)
qc.ry(pi/8,0)
assert(simulate(qc,get='statevector')==[[0.9807852803850672, -6.938893903907228e-17], [0.19509032201251536, 0.0]])
def test_cx():
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0,1)
assert( simulate(qc,shots=shots,get='statevector')==[[0.70710678118, 0.0], [0.0, 0.0], [0.0, 0.0], [0.70710678118, 0.0]] )
qc = QuantumCircuit(2)
qc.x(0)
qc.cx(0,1)
qc.cx(1,0)
qc.cx(0,1)
assert( simulate(qc,shots=shots,get='statevector')==[[0.0, 0.0], [0.0, 0.0], [1.0, 0.0], [0.0, 0.0]] )
def test_swap00():
qc = QuantumCircuit(2)
assert( simulate(qc,shots=shots,get='statevector')==[[1.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]] )
qc.swap(0,1)
assert( simulate(qc,shots=shots,get='statevector')==[[1.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]] )
def test_swap01():
qc = QuantumCircuit(2)
qc.x(0)
assert( simulate(qc,shots=shots,get='statevector')==[[0.0, 0.0], [1.0, 0.0], [0.0, 0.0], [0.0, 0.0]] )
qc.swap(0,1)
assert( simulate(qc,shots=shots,get='statevector')==[[0.0, 0.0], [0.0, 0.0], [1.0, 0.0], [0.0, 0.0]] )
def test_swap10():
qc = QuantumCircuit(2)
qc.x(1)
assert( simulate(qc,shots=shots,get='statevector')==[[0.0, 0.0], [0.0, 0.0], [1.0, 0.0], [0.0, 0.0]] )
qc.swap(0,1)
assert( simulate(qc,shots=shots,get='statevector')==[[0.0, 0.0], [1.0, 0.0], [0.0, 0.0], [0.0, 0.0]] )
def test_swap11():
qc = QuantumCircuit(2)
qc.x(0)
qc.x(1)
assert( simulate(qc,shots=shots,get='statevector')==[[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [1.0, 0.0]] )
qc.swap(0,1)
assert( simulate(qc,shots=shots,get='statevector')==[[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [1.0, 0.0]] )
def test_memory():
qc = QuantumCircuit(2,2)
qc.h(0)
qc.h(1)
qc.measure_all()
m = simulate(qc,shots=shots,get='memory')
assert( len(m)==shots )
p00 = 0
for out in m:
p00 += round(out=='00')/shots
assert( round(p00,2)==0.25 )
qc = QuantumCircuit(1,1)
qc.h(0)
qc.measure(0,0)
m = simulate(qc,shots=shots,get='memory')
assert( len(m)==shots )
p0 = 0
for out in m:
p0 += round(out=='0')/shots
assert( round(p0,1)==0.5 )
def test_counts():
qc = QuantumCircuit(2,2)
qc.h(0)
qc.h(1)
qc.measure(0,0)
qc.measure(1,1)
c = simulate(qc,shots=shots,get='counts')
for out in c:
p = float(c[out])/shots
assert( round(p,2)==0.25 )
def test_probs():
qc = QuantumCircuit(2,2)
qc.h(0)
qc.h(1)
p = simulate(qc,shots=shots,get='probabilities_dict')
for out in p:
assert( round(p[out],2)==0.25 )
def test_add():
for n in [1,2]:
qc = QuantumCircuit(n,n)
meas = QuantumCircuit(n,n)
for j in range(n):
qc.h(j)
meas.measure(j,j)
c = simulate(qc+meas,shots=shots,get='counts')
for out in c:
p = float(c[out])/shots
assert( round(p,2)==round(1.0/2**n,2) )
def test_multiqubit():
qc = QuantumCircuit(7,7)
qc.h(0)
qc.cx(0,2)
qc.cx(2,1)
qc.h(5)
qc.cx(5,3)
qc.cx(3,4)
qc.cx(3,6)
ket = simulate(qc,get='statevector')
check = True
for string in ['0000000','0000111','1111000','1111111']:
check = check and round(ket[int(string,2)][0],2)==0.50
assert( check )
for j in range(7):
qc.measure(j,j)
counts = simulate(qc,shots=shots,get='counts')
check = True
for string in ['0000000','0000111','1111000','1111111']:
p = float(counts[string])/shots
check = check and round(p,2)==0.25
assert( check )
def test_reorder ():
qc = QuantumCircuit(2,2)
qc.x(0)
qc.measure(0,1)
qc.measure(1,0)
counts = simulate(qc,shots=shots,get='counts')
assert counts['01']==shots
qc = QuantumCircuit(5,4)
qc.x(1)
qc.x(3)
qc.x(4)
qc.measure(1,0)
qc.measure(3,1)
qc.measure(4,2)
qc.measure(0,3)
counts = simulate(qc,shots=shots,get='counts')
assert counts['0111']==shots
def test_noise ():
qc = QuantumCircuit(2,2)
p = simulate(qc,get='probabilities_dict',noise_model=[0.1,0.2])
correct_p = {'00': 0.7200000000000001,'01': 0.08000000000000002,'10': 0.18000000000000002,'11': 0.020000000000000004}
for out in correct_p:
assert p[out]==correct_p[out]
test_trig()
test_x()
test_h()
test_rx()
test_rz()
test_ry
test_cx()
test_memory()
test_counts()
test_add()
test_multiqubit()
test_noise ()
test_swap00()
test_swap01()
test_swap10()
test_swap11()
print('Tests passed!')