-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_CQCToFile.py
262 lines (187 loc) · 6.37 KB
/
test_CQCToFile.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
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
261
262
# Tests for the CQCToFile class
# To get a temporary directory start the function with
#
# filename = os.path.join(str(tmpdir), 'CQC_File')
#
# with CQCToFile(file=filename) as cqc:
import os
from cqc.util import parse_cqc_message
from cqc.pythonLib import CQCToFile, qubit
from cqc.cqcHeader import (
CQCType,
CQC_CMD_NEW,
CQC_CMD_H,
CQC_CMD_X,
CQC_CMD_RELEASE,
CQCHeader,
CQCCmdHeader
)
def test_name(tmpdir):
filename = os.path.join(str(tmpdir), 'CQC_File')
with CQCToFile(file=filename) as cqc:
assert cqc.name == filename
def test_sendSimple(tmpdir):
filename = os.path.join(str(tmpdir), 'CQC_File')
with CQCToFile(file=filename, binary=False) as cqc:
cqc.sendSimple(CQCType.HELLO)
with open(filename) as f:
lines = f.readlines()
assert len(lines) == 1
# Check that the first line is a header of hello type
headers = parse_cqc_message(eval(lines[0][:-1]))
assert len(headers) == 1
hdr = headers[0]
assert isinstance(hdr, CQCHeader)
assert hdr.tp == CQCType.HELLO
def test_createqubit(tmpdir):
filename = os.path.join(str(tmpdir), 'CQC_File')
with CQCToFile(file=filename, binary=False) as cqc:
qubit(cqc)
# Read the files before cqc goes out of context and flushes
with open(filename) as f:
lines = f.readlines()
assert len(lines) == 1
# Check that the first line initialize a qubit
headers = parse_cqc_message(eval(lines[0][:-1]))
assert len(headers) == 2
hdr, cmd = headers
assert isinstance(hdr, CQCHeader)
assert hdr.tp == CQCType.COMMAND
assert isinstance(cmd, CQCCmdHeader)
assert cmd.qubit_id == 0
assert cmd.instr == CQC_CMD_NEW
def test_releasequbit(tmpdir):
filename = os.path.join(str(tmpdir), 'CQC_File')
with CQCToFile(file=filename, binary=False) as cqc:
qubit(cqc)
with open(filename) as f:
lines = f.readlines()
assert len(lines) == 2
# Check that the first line initialize a qubit
headers = parse_cqc_message(eval(lines[0][:-1]))
assert len(headers) == 2
hdr, cmd = headers
assert isinstance(hdr, CQCHeader)
assert hdr.tp == CQCType.COMMAND
assert isinstance(cmd, CQCCmdHeader)
assert cmd.qubit_id == 0
assert cmd.instr == CQC_CMD_NEW
# Check that the second line releases the qubit
headers = parse_cqc_message(eval(lines[1][:-1]))
assert len(headers) == 2
hdr, cmd = headers
assert isinstance(hdr, CQCHeader)
assert hdr.tp == CQCType.COMMAND
assert isinstance(cmd, CQCCmdHeader)
assert cmd.qubit_id == 0
assert cmd.instr == CQC_CMD_RELEASE
def test_Hgate(tmpdir):
filename = os.path.join(str(tmpdir), 'CQC_File')
with CQCToFile(file=filename, binary=False) as cqc:
q = qubit(cqc)
q.H()
with open(filename) as f:
lines = f.readlines()
# Since pend_messages=False there should be three lines
assert len(lines) == 3
# Check that the first line initialize a qubit
headers = parse_cqc_message(eval(lines[0][:-1]))
assert len(headers) == 2
hdr, cmd = headers
assert isinstance(hdr, CQCHeader)
assert hdr.tp == CQCType.COMMAND
assert isinstance(cmd, CQCCmdHeader)
assert cmd.qubit_id == 0
assert cmd.instr == CQC_CMD_NEW
# Check that the second line does H
headers = parse_cqc_message(eval(lines[1][:-1]))
assert len(headers) == 2
hdr, cmd = headers
assert isinstance(hdr, CQCHeader)
assert hdr.tp == CQCType.COMMAND
assert isinstance(cmd, CQCCmdHeader)
assert cmd.qubit_id == 0
assert cmd.instr == CQC_CMD_H
# Check that the third line releases the qubit
headers = parse_cqc_message(eval(lines[2][:-1]))
assert len(headers) == 2
hdr, cmd = headers
assert isinstance(hdr, CQCHeader)
assert hdr.tp == CQCType.COMMAND
assert isinstance(cmd, CQCCmdHeader)
assert cmd.qubit_id == 0
assert cmd.instr == CQC_CMD_RELEASE
def test_some_combinations(tmpdir):
filename = os.path.join(str(tmpdir), 'CQC_File')
with CQCToFile(file=filename) as cqc:
q = cqc.createEPR("Alice")
q.H()
a = qubit(cqc)
a.cnot(q)
cqc.sendQubit(a, "Alice")
c = cqc.recvQubit()
d = cqc.recvEPR()
c.H()
d.H()
c.cnot(d)
def test_flushing(tmpdir):
filename = os.path.join(str(tmpdir), 'CQC_File')
with CQCToFile(file=filename, pend_messages=True) as cqc:
assert not cqc._pending_headers
q = qubit(cqc)
q.H()
q.X()
q.Z()
assert cqc._pending_headers
cqc.flush()
assert not cqc._pending_headers
def test_qubitIDs(tmpdir):
filename = os.path.join(str(tmpdir), 'CQC_File')
with CQCToFile(file=filename) as cqc:
a = qubit(cqc)
a.X()
b = qubit(cqc)
b.Z()
c = qubit(cqc)
c.H()
assert a._qID == 0
assert b._qID == 1
assert c._qID == 2
def test_measurement(tmpdir):
filename = os.path.join(str(tmpdir), 'CQC_File')
with CQCToFile(file=filename) as cqc:
q = qubit(cqc)
a = q.measure()
assert a == 0
def test_flush_on_exit(tmpdir):
filename = os.path.join(str(tmpdir), 'CQC_File')
with CQCToFile(file=filename, pend_messages=True, binary=False) as cqc:
q = qubit(cqc)
q.H()
q.X()
with open(filename) as f:
lines = f.readlines()
# Since pend_messages=True we should only get two lines
assert len(lines) == 2
# Check that the first line initialize a qubit
headers = parse_cqc_message(eval(lines[0][:-1]))
assert len(headers) == 2
hdr, cmd = headers
assert isinstance(hdr, CQCHeader)
assert hdr.tp == CQCType.COMMAND
assert isinstance(cmd, CQCCmdHeader)
assert cmd.qubit_id == 0
assert cmd.instr == CQC_CMD_NEW
# Check that the second line applies H, X and releases
headers = parse_cqc_message(eval(lines[1][:-1]))
assert len(headers) == 4
hdr, *cmds = headers
assert isinstance(hdr, CQCHeader)
assert hdr.tp == CQCType.COMMAND
assert len(cmds) == 3
for cmd in cmds:
assert isinstance(cmd, CQCCmdHeader)
assert cmd.qubit_id == 0
assert cmds[0].instr == CQC_CMD_H
assert cmds[1].instr == CQC_CMD_X
assert cmds[2].instr == CQC_CMD_RELEASE