Skip to content

Commit 9570b1b

Browse files
author
Axel Dahlberg
committedFeb 19, 2020
Fixed tests
1 parent 8eb1f6f commit 9570b1b

File tree

5 files changed

+129
-50
lines changed

5 files changed

+129
-50
lines changed
 

‎cqc/pythonLib/cqc_mix.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def __init__(self, cqc_connection: CQCMixConnection):
288288
"""
289289
if not isinstance(cqc_connection, CQCMixConnection):
290290
raise TypeError("To use CQCMix the connection needs to be of type CQCMixConnection, "
291-
f"not {type(cqc_connection)}")
291+
"not {}".format(type(cqc_connection)))
292292

293293
self._conn = cqc_connection
294294

‎cqc/util.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
def parse_cqc_message(msg):
4747
# TODO finish parser
4848
hdr_map = {
49+
CQCType.HELLO: None,
4950
CQCType.COMMAND: CQCCmdHeader,
5051
CQCType.MIX: CQCTypeHeader,
5152
CQCType.IF: CQCIfHeader,
@@ -81,7 +82,7 @@ def parse_cqc_message(msg):
8182
elif isinstance(hdr, CQCFactoryHeader):
8283
next_hdr = CQCCmdHeader
8384
else:
84-
raise NotImplementedError(f"for {hdr}")
85+
raise NotImplementedError("for {}".format(hdr))
8586
bits_to_next_first_hdr -= hdr.HDR_LENGTH
8687
if bits_to_next_first_hdr <= 0:
8788
next_hdr = CQCHeader

‎tests/test_CQCToFile.py

+122-43
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@
66
#
77
# with CQCToFile(file=filename) as cqc:
88

9-
from cqc.pythonLib import CQCToFile, qubit
109
import os
11-
from cqc.cqcHeader import CQC_TP_HELLO
10+
11+
from cqc.util import parse_cqc_message
12+
from cqc.pythonLib import CQCToFile, qubit
13+
from cqc.cqcHeader import (
14+
CQCType,
15+
CQC_CMD_NEW,
16+
CQC_CMD_H,
17+
CQC_CMD_X,
18+
CQC_CMD_RELEASE,
19+
CQCHeader,
20+
CQCCmdHeader
21+
)
1222

1323

1424
def test_name(tmpdir):
@@ -24,12 +34,19 @@ def test_sendSimple(tmpdir):
2434
filename = os.path.join(str(tmpdir), 'CQC_File')
2535

2636
with CQCToFile(file=filename, binary=False) as cqc:
27-
cqc.sendSimple(CQC_TP_HELLO)
37+
cqc.sendSimple(CQCType.HELLO)
2838

29-
with open(filename) as f:
30-
contents = f.read()
31-
print(contents)
32-
assert contents[6:10] == "\\x00"
39+
with open(filename) as f:
40+
lines = f.readlines()
41+
42+
assert len(lines) == 1
43+
44+
# Check that the first line is a header of hello type
45+
headers = parse_cqc_message(eval(lines[0][:-1]))
46+
assert len(headers) == 1
47+
hdr = headers[0]
48+
assert isinstance(hdr, CQCHeader)
49+
assert hdr.tp == CQCType.HELLO
3350

3451

3552
def test_createqubit(tmpdir):
@@ -38,16 +55,23 @@ def test_createqubit(tmpdir):
3855

3956
with CQCToFile(file=filename, binary=False) as cqc:
4057

41-
q = qubit(cqc)
42-
q.H()
58+
qubit(cqc)
4359

60+
# Read the files before cqc goes out of context and flushes
4461
with open(filename) as f:
62+
lines = f.readlines()
4563

46-
line = f.readline()
47-
print(line)
64+
assert len(lines) == 1
4865

49-
assert line[6:10] == "\\x01"
50-
assert line[42:46] == "\\x01"
66+
# Check that the first line initialize a qubit
67+
headers = parse_cqc_message(eval(lines[0][:-1]))
68+
assert len(headers) == 2
69+
hdr, cmd = headers
70+
assert isinstance(hdr, CQCHeader)
71+
assert hdr.tp == CQCType.COMMAND
72+
assert isinstance(cmd, CQCCmdHeader)
73+
assert cmd.qubit_id == 0
74+
assert cmd.instr == CQC_CMD_NEW
5175

5276

5377
def test_releasequbit(tmpdir):
@@ -56,18 +80,32 @@ def test_releasequbit(tmpdir):
5680

5781
with CQCToFile(file=filename, binary=False) as cqc:
5882

59-
q = qubit(cqc)
60-
q.H()
83+
qubit(cqc)
6184

6285
with open(filename) as f:
86+
lines = f.readlines()
6387

64-
line = f.readline()
65-
line = f.readline()
66-
line = f.readline()
67-
print(line)
68-
69-
assert line[6:10] == "\\x01"
70-
assert line[42:46] == "\\x17"
88+
assert len(lines) == 2
89+
90+
# Check that the first line initialize a qubit
91+
headers = parse_cqc_message(eval(lines[0][:-1]))
92+
assert len(headers) == 2
93+
hdr, cmd = headers
94+
assert isinstance(hdr, CQCHeader)
95+
assert hdr.tp == CQCType.COMMAND
96+
assert isinstance(cmd, CQCCmdHeader)
97+
assert cmd.qubit_id == 0
98+
assert cmd.instr == CQC_CMD_NEW
99+
100+
# Check that the second line releases the qubit
101+
headers = parse_cqc_message(eval(lines[1][:-1]))
102+
assert len(headers) == 2
103+
hdr, cmd = headers
104+
assert isinstance(hdr, CQCHeader)
105+
assert hdr.tp == CQCType.COMMAND
106+
assert isinstance(cmd, CQCCmdHeader)
107+
assert cmd.qubit_id == 0
108+
assert cmd.instr == CQC_CMD_RELEASE
71109

72110

73111
def test_Hgate(tmpdir):
@@ -80,13 +118,40 @@ def test_Hgate(tmpdir):
80118
q.H()
81119

82120
with open(filename) as f:
83-
84-
line = f.readline()
85-
line = f.readline()
86-
print(line)
87-
88-
assert line[6:10] == "\\x01"
89-
assert line[42:46] == "\\x11"
121+
lines = f.readlines()
122+
123+
# Since pend_messages=False there should be three lines
124+
assert len(lines) == 3
125+
126+
# Check that the first line initialize a qubit
127+
headers = parse_cqc_message(eval(lines[0][:-1]))
128+
assert len(headers) == 2
129+
hdr, cmd = headers
130+
assert isinstance(hdr, CQCHeader)
131+
assert hdr.tp == CQCType.COMMAND
132+
assert isinstance(cmd, CQCCmdHeader)
133+
assert cmd.qubit_id == 0
134+
assert cmd.instr == CQC_CMD_NEW
135+
136+
# Check that the second line does H
137+
headers = parse_cqc_message(eval(lines[1][:-1]))
138+
assert len(headers) == 2
139+
hdr, cmd = headers
140+
assert isinstance(hdr, CQCHeader)
141+
assert hdr.tp == CQCType.COMMAND
142+
assert isinstance(cmd, CQCCmdHeader)
143+
assert cmd.qubit_id == 0
144+
assert cmd.instr == CQC_CMD_H
145+
146+
# Check that the third line releases the qubit
147+
headers = parse_cqc_message(eval(lines[2][:-1]))
148+
assert len(headers) == 2
149+
hdr, cmd = headers
150+
assert isinstance(hdr, CQCHeader)
151+
assert hdr.tp == CQCType.COMMAND
152+
assert isinstance(cmd, CQCCmdHeader)
153+
assert cmd.qubit_id == 0
154+
assert cmd.instr == CQC_CMD_RELEASE
90155

91156

92157
def test_some_combinations(tmpdir):
@@ -167,17 +232,31 @@ def test_flush_on_exit(tmpdir):
167232
q.X()
168233

169234
with open(filename) as f:
170-
171-
line = f.readline()
172-
print(line)
173-
assert line[6:10] == "\\x01"
174-
assert line[42:46] == "\\x01"
175-
line = f.readline()
176-
print(line)
177-
assert line[6:10] == "\\x01"
178-
line = f.readline()
179-
print(line)
180-
assert line[10:14] == "\\x11"
181-
line = f.readline()
182-
print(line)
183-
assert line[10:12] == "\\n"
235+
lines = f.readlines()
236+
237+
# Since pend_messages=True we should only get two lines
238+
assert len(lines) == 2
239+
240+
# Check that the first line initialize a qubit
241+
headers = parse_cqc_message(eval(lines[0][:-1]))
242+
assert len(headers) == 2
243+
hdr, cmd = headers
244+
assert isinstance(hdr, CQCHeader)
245+
assert hdr.tp == CQCType.COMMAND
246+
assert isinstance(cmd, CQCCmdHeader)
247+
assert cmd.qubit_id == 0
248+
assert cmd.instr == CQC_CMD_NEW
249+
250+
# Check that the second line applies H, X and releases
251+
headers = parse_cqc_message(eval(lines[1][:-1]))
252+
assert len(headers) == 4
253+
hdr, *cmds = headers
254+
assert isinstance(hdr, CQCHeader)
255+
assert hdr.tp == CQCType.COMMAND
256+
assert len(cmds) == 3
257+
for cmd in cmds:
258+
assert isinstance(cmd, CQCCmdHeader)
259+
assert cmd.qubit_id == 0
260+
assert cmds[0].instr == CQC_CMD_H
261+
assert cmds[1].instr == CQC_CMD_X
262+
assert cmds[2].instr == CQC_CMD_RELEASE

‎tests/test_cases_cqcconnection/flush.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def get_expected_headers_flush():
3838
version=2,
3939
tp=CQC_TP_COMMAND,
4040
app_id=0,
41-
length=2 * CQC_CMD_HDR_LENGTH,
41+
length=3 * CQC_CMD_HDR_LENGTH,
4242
)
4343
hdr_cmd_h = get_header(
4444
CQCCmdHeader,
@@ -67,8 +67,7 @@ def get_expected_headers_flush():
6767

6868
expected_headers = [
6969
hdr_tp_cmd + hdr_cmd_new,
70-
hdr_tp_cmd_2 + hdr_cmd_h + hdr_cmd_x,
71-
hdr_tp_cmd + hdr_cmd_release
70+
hdr_tp_cmd_2 + hdr_cmd_h + hdr_cmd_x + hdr_cmd_release,
7271
]
7372

7473
return expected_headers

‎tests/test_cqcconnection.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ def test_commands(conn_type, commands_to_apply, get_expected_headers, monkeypatc
103103
full_msg = {}
104104
# Parse and print what we expect and what we got
105105
for name, messages in zip(["EXPECTED", "GOT"], [expected_messages, sent_messages]):
106-
print(f"\n{name}:")
106+
print("\n{}:".format(name))
107107
for msg in messages:
108108
print('[')
109109
for hdr in parse_cqc_message(msg):
110-
print(f" {hdr}")
110+
print(" {}".format(hdr))
111111
print('\n]')
112112
full_msg[name] = b''.join([msg for msg in messages])
113113

0 commit comments

Comments
 (0)
Please sign in to comment.