-
Notifications
You must be signed in to change notification settings - Fork 19
/
run_test.py
231 lines (187 loc) · 6.73 KB
/
run_test.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
import run
import numpy as np
import pytest
F = 1/np.sqrt(2)
def test_one_project():
np.testing.assert_almost_equal(
np.array([0, 1]),
run.one_project(1, 0))
np.testing.assert_almost_equal(
np.array([0, 1, 0, 1]),
run.one_project(2, 0))
np.testing.assert_almost_equal(
np.array([0, 0, 1, 1]),
run.one_project(2, 1))
np.testing.assert_almost_equal(
np.array([0, 1, 0, 1, 0, 1, 0, 1]),
run.one_project(3, 0))
np.testing.assert_almost_equal(
np.array([0, 0, 1, 1, 0, 0, 1, 1]),
run.one_project(3, 1))
np.testing.assert_almost_equal(
np.array([0, 0, 0, 0, 1, 1, 1, 1]),
run.one_project(3, 2))
def matrix(n, update_fn):
results = []
for x in range(2 ** n):
state = np.zeros(2 ** n)
state[x] = 1.0
results.append(update_fn(state))
return np.transpose(np.array(results))
def test_h_one_qubit():
np.testing.assert_almost_equal(
np.array([
[F, F],
[F, -F]]),
matrix(1, lambda x: run.simulate_h(x, [0], 1)))
def test_h_two_qubits():
np.testing.assert_almost_equal(
np.array([
[F, F, 0, 0],
[F, -F, 0, 0],
[0, 0, F, F],
[0, 0, F, -F]]),
matrix(2, lambda x: run.simulate_h(x, [0], 2)))
np.testing.assert_almost_equal(
np.array([
[F, 0, F, 0],
[0, F, 0, F],
[F, 0, -F, 0],
[0, F, 0, -F]]),
matrix(2, lambda x: run.simulate_h(x, [1], 2)))
def test_cp_two_qubits():
np.testing.assert_almost_equal(
np.diag([1, 1, 1, 1j]),
matrix(2, lambda x: run.simulate_cp(x, [0, 1], 2)))
def test_cp_three_qubits():
np.testing.assert_almost_equal(
np.diag([1, 1, 1, 1j, 1, 1, 1, 1j]),
matrix(3, lambda x: run.simulate_cp(x, [0, 1], 3)))
np.testing.assert_almost_equal(
np.diag([1, 1, 1, 1j, 1, 1, 1, 1j]),
matrix(3, lambda x: run.simulate_cp(x, [1, 0], 3)))
np.testing.assert_almost_equal(
np.diag([1, 1, 1, 1, 1, 1j, 1, 1j]),
matrix(3, lambda x: run.simulate_cp(x, [0, 2], 3)))
np.testing.assert_almost_equal(
np.diag([1, 1, 1, 1, 1, 1j, 1, 1j]),
matrix(3, lambda x: run.simulate_cp(x, [2, 0], 3)))
np.testing.assert_almost_equal(
np.diag([1, 1, 1, 1, 1, 1, 1j, 1j]),
matrix(3, lambda x: run.simulate_cp(x, [1, 2], 3)))
np.testing.assert_almost_equal(
np.diag([1, 1, 1, 1, 1, 1, 1j, 1j]),
matrix(3, lambda x: run.simulate_cp(x, [2, 1], 3)))
def measurement_idempotent(n, index):
for x in range(2 ** n):
state = np.zeros(2 ** n)
state[x] = 1.0
np.testing.assert_almost_equal(state, run.simulate_m(state, [index], n))
def test_measurement_idempotent():
for n in range(3):
for index in range(n):
measurement_idempotent(n, index)
def test_measurement_result_one_qubit(capfd):
np.random.seed(0)
state = np.array([F, F])
state = run.simulate_m(state, [0], 1)
out, _ = capfd.readouterr()
assert out.strip() == 'Measured 1 on qubit 0.'
np.testing.assert_almost_equal(np.array([0, 1]), state)
np.random.seed(1)
state = np.array([F, F])
state = run.simulate_m(state, [0], 1)
out, _ = capfd.readouterr()
assert out.strip() == 'Measured 0 on qubit 0.'
np.testing.assert_almost_equal(np.array([1, 0]), state)
def test_measurement_result_two_qubits(capfd):
np.random.seed(0)
state = 0.5 * np.array([1, 1, 1, 1])
state = run.simulate_m(state, [0], 2)
out, _ = capfd.readouterr()
assert out.strip() == 'Measured 1 on qubit 0.'
np.testing.assert_almost_equal(
np.array([0, F, 0, F]), state)
np.random.seed(1)
state = 0.5 * np.array([1, 1, 1, 1])
state = run.simulate_m(state, [0], 2)
out, _ = capfd.readouterr()
assert out.strip() == 'Measured 0 on qubit 0.'
np.testing.assert_almost_equal(
np.array([F, 0, F, 0]), state)
np.random.seed(0)
state = 0.5 * np.array([1, 1, 1, 1])
state = run.simulate_m(state, [1], 2)
out, _ = capfd.readouterr()
assert out.strip() == 'Measured 1 on qubit 1.'
np.testing.assert_almost_equal(
np.array([0, 0, F, F]), state)
np.random.seed(1)
state = 0.5 * np.array([1, 1, 1, 1])
state = run.simulate_m(state, [1], 2)
out, _ = capfd.readouterr()
assert out.strip() == 'Measured 0 on qubit 1.'
np.testing.assert_almost_equal(
np.array([F, F, 0, 0]), state)
def test_parse_qubits():
assert [0] == run.parse_qubit([run.S, run.S], run.S, run.E)
assert [1] == run.parse_qubit([run.E, run.E], run.S, run.E)
assert [0] == run.parse_qubit([run.S, run.S, run.S, run.S], run.S, run.E)
assert [1] == run.parse_qubit([run.S, run.S, run.E, run.E], run.S, run.E)
assert [2] == run.parse_qubit([run.E, run.E, run.S, run.S], run.S, run.E)
assert [3] == run.parse_qubit([run.E, run.E, run.E, run.E], run.S, run.E)
assert [0, 0] == run.parse_qubit(
[run.S, run.S, run.S, run.E, run.S, run.S], run.S, run.E)
assert [1, 2] == run.parse_qubit(
[run.E, run.E, run.E, run.S, run.E, run.E, run.S, run.S], run.S, run.E)
assert [0] == run.parse_qubit(['a', 'a'], 'a', run.E)
assert [1] == run.parse_qubit(['b', 'b'], run.S, 'b')
def test_parse_qubits_invalid_token_number():
with pytest.raises(SyntaxError, matches='number'):
run.parse_qubit([run.S], run.S, run.E)
with pytest.raises(SyntaxError, matches='number'):
run.parse_qubit([run.S, run.S, run.E], run.S, run.E)
def test_parse_one_qubit(tmpdir):
p = tmpdir.join('parse.qsel')
p.write('superposition superposition superposition superposition\n'
'superposition entanglement superposition superposition')
all_qubits, program = run.parse(str(p))
assert all_qubits == set([0])
assert program == [
{'gate': 'H', 'qubits': [0]},
{'gate': 'M', 'qubits': [0]}]
def test_parse_two_qubit(tmpdir):
p = tmpdir.join('parse.qsel')
p.write('entanglement entanglement superposition superposition '
'superposition entanglement entanglement entanglement\n'
'superposition entanglement superposition superposition')
all_qubits, program = run.parse(str(p))
assert all_qubits == set([0, 1])
assert program == [
{'gate': 'CP', 'qubits': [0, 1]},
{'gate': 'M', 'qubits': [0]}]
def test_parse_too_few_qubits(tmpdir):
p = tmpdir.join('parse.qsel')
p.write('entanglement entanglement superposition')
with pytest.raises(SyntaxError, match='Not enough'):
run.parse(str(p))
def test_parse_bad_tokens(tmpdir):
p = tmpdir.join('parse.qsel')
p.write('entanglement x')
with pytest.raises(SyntaxError, match='Only'):
run.parse(str(p))
def test_parse_h_wrong_qubit_number(tmpdir):
p = tmpdir.join('parse.qsel')
p.write(' '.join([run.S, run.S, run.S, run.S, run.S, run.E, run.E, run.E]))
with pytest.raises(SyntaxError, match='H gate'):
run.parse(str(p))
def test_parse_cp_wrong_qubit_number(tmpdir):
p = tmpdir.join('parse.qsel')
p.write(' '.join([run.E, run.E, run.S, run.S]))
with pytest.raises(SyntaxError, match='CP gate'):
run.parse(str(p))
def test_parse_m_wrong_qubit_number(tmpdir):
p = tmpdir.join('parse.qsel')
p.write(' '.join([run.S, run.E, run.S, run.S, run.S, run.E, run.E, run.E]))
with pytest.raises(SyntaxError, match='M gate'):
run.parse(str(p))