File tree 3 files changed +46
-2
lines changed
3 files changed +46
-2
lines changed Original file line number Diff line number Diff line change @@ -30,7 +30,9 @@ def __init__(self):
30
30
'qreg' : 'QREG' ,
31
31
'creg' : 'CREG' ,
32
32
'measure' : 'MEASURE' ,
33
+ 'if' : 'IF' ,
33
34
'->' : 'ARROW' ,
35
+ '!=' : 'NE' ,
34
36
}
35
37
36
38
tokens = [
@@ -98,10 +100,18 @@ def t_MEASURE(self, t):
98
100
r"""measure"""
99
101
return t
100
102
103
+ def t_IF (self , t ):
104
+ r"""if"""
105
+ return t
106
+
101
107
def t_ARROW (self , t ):
102
108
"""->"""
103
109
return t
104
110
111
+ def t_NE (self , t ):
112
+ """!="""
113
+ return t
114
+
105
115
def t_ID (self , t ):
106
116
r"""[a-zA-Z][a-zA-Z\d_]*"""
107
117
return t
Original file line number Diff line number Diff line change @@ -290,15 +290,17 @@ def p_format(self, p):
290
290
# circuit : new_reg circuit
291
291
# | gate_op circuit
292
292
# | measurement circuit
293
+ # | if circuit
293
294
# | empty
294
295
295
296
def p_circuit_reg (self , p ):
296
297
"""circuit : new_reg circuit"""
297
298
p [0 ] = self .circuit
298
299
299
- def p_circuit_gate_or_measurement (self , p ):
300
+ def p_circuit_gate_or_measurement_or_if (self , p ):
300
301
"""circuit : circuit gate_op
301
- | circuit measurement"""
302
+ | circuit measurement
303
+ | circuit if"""
302
304
self .circuit .append (p [2 ])
303
305
p [0 ] = self .circuit
304
306
@@ -497,6 +499,13 @@ def p_measurement(self, p):
497
499
ops .MeasurementGate (num_qubits = 1 , key = creg [i ]).on (qreg [i ]) for i in range (len (qreg ))
498
500
]
499
501
502
+ # if operations
503
+ # if : IF '(' carg NE NATURAL_NUMBER ')' ID qargs
504
+
505
+ def p_if (self , p ):
506
+ """if : IF '(' carg NE NATURAL_NUMBER ')' gate_op"""
507
+ p [0 ] = [ops .ClassicallyControlledOperation (conditions = p [3 ], sub_operation = tuple (p [7 ])[0 ])]
508
+
500
509
def p_error (self , p ):
501
510
if p is None :
502
511
raise QasmException ('Unexpected end of file' )
Original file line number Diff line number Diff line change @@ -217,6 +217,31 @@ def test_CX_gate():
217
217
assert parsed_qasm .qregs == {'q1' : 2 , 'q2' : 2 }
218
218
219
219
220
+ def test_classical_control ():
221
+ qasm = """OPENQASM 2.0;
222
+ qreg q[2];
223
+ creg m_a[1];
224
+ measure q[0] -> m_a[0];
225
+ if (m_a!=0) CX q[0], q[1];
226
+ """
227
+ parser = QasmParser ()
228
+
229
+ q_0 = cirq .NamedQubit ('q_0' )
230
+ q_1 = cirq .NamedQubit ('q_1' )
231
+ expected_circuit = cirq .Circuit (
232
+ cirq .measure (q_0 , key = 'm_a_0' ),
233
+ cirq .CNOT (q_0 , q_1 ).with_classical_controls ('m_a_0' ),
234
+ )
235
+
236
+ parsed_qasm = parser .parse (qasm )
237
+
238
+ assert parsed_qasm .supportedFormat
239
+ assert not parsed_qasm .qelib1Include
240
+
241
+ ct .assert_same_circuits (parsed_qasm .circuit , expected_circuit )
242
+ assert parsed_qasm .qregs == {'q' : 2 }
243
+
244
+
220
245
def test_CX_gate_not_enough_args ():
221
246
qasm = """OPENQASM 2.0;
222
247
qreg q[2];
You can’t perform that action at this time.
0 commit comments