6
6
#
7
7
# with CQCToFile(file=filename) as cqc:
8
8
9
- from cqc .pythonLib import CQCToFile , qubit
10
9
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
+ )
12
22
13
23
14
24
def test_name (tmpdir ):
@@ -24,12 +34,19 @@ def test_sendSimple(tmpdir):
24
34
filename = os .path .join (str (tmpdir ), 'CQC_File' )
25
35
26
36
with CQCToFile (file = filename , binary = False ) as cqc :
27
- cqc .sendSimple (CQC_TP_HELLO )
37
+ cqc .sendSimple (CQCType . HELLO )
28
38
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
33
50
34
51
35
52
def test_createqubit (tmpdir ):
@@ -38,16 +55,23 @@ def test_createqubit(tmpdir):
38
55
39
56
with CQCToFile (file = filename , binary = False ) as cqc :
40
57
41
- q = qubit (cqc )
42
- q .H ()
58
+ qubit (cqc )
43
59
60
+ # Read the files before cqc goes out of context and flushes
44
61
with open (filename ) as f :
62
+ lines = f .readlines ()
45
63
46
- line = f .readline ()
47
- print (line )
64
+ assert len (lines ) == 1
48
65
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
51
75
52
76
53
77
def test_releasequbit (tmpdir ):
@@ -56,18 +80,32 @@ def test_releasequbit(tmpdir):
56
80
57
81
with CQCToFile (file = filename , binary = False ) as cqc :
58
82
59
- q = qubit (cqc )
60
- q .H ()
83
+ qubit (cqc )
61
84
62
85
with open (filename ) as f :
86
+ lines = f .readlines ()
63
87
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
71
109
72
110
73
111
def test_Hgate (tmpdir ):
@@ -80,13 +118,40 @@ def test_Hgate(tmpdir):
80
118
q .H ()
81
119
82
120
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
90
155
91
156
92
157
def test_some_combinations (tmpdir ):
@@ -167,17 +232,31 @@ def test_flush_on_exit(tmpdir):
167
232
q .X ()
168
233
169
234
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
0 commit comments