From e1e1730e8ab41cd6bcfc655b1a78993442797d7b Mon Sep 17 00:00:00 2001 From: Roland Siegbert Date: Sat, 14 Sep 2024 15:04:56 +0200 Subject: [PATCH 1/2] Add `reset` to QasmParser Add a test which can serve as example including the modifications to support the `reset` keyword in the import of QASM files in the PLY based lexer/parser. --- cirq-core/cirq/contrib/qasm_import/_lexer.py | 5 ++++ cirq-core/cirq/contrib/qasm_import/_parser.py | 11 +++++++ .../cirq/contrib/qasm_import/_parser_test.py | 30 +++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/cirq-core/cirq/contrib/qasm_import/_lexer.py b/cirq-core/cirq/contrib/qasm_import/_lexer.py index 206d9e88d74..d13ebf349f7 100644 --- a/cirq-core/cirq/contrib/qasm_import/_lexer.py +++ b/cirq-core/cirq/contrib/qasm_import/_lexer.py @@ -29,6 +29,7 @@ def __init__(self): reserved = { 'qreg': 'QREG', 'creg': 'CREG', + 'reset': 'RESET', 'measure': 'MEASURE', 'if': 'IF', '->': 'ARROW', @@ -95,6 +96,10 @@ def t_MEASURE(self, t): r"""measure""" return t + def t_RESET(self, t): + r"""reset""" + return t + def t_IF(self, t): r"""if""" return t diff --git a/cirq-core/cirq/contrib/qasm_import/_parser.py b/cirq-core/cirq/contrib/qasm_import/_parser.py index e7bcdae06db..1d9e8606047 100644 --- a/cirq-core/cirq/contrib/qasm_import/_parser.py +++ b/cirq-core/cirq/contrib/qasm_import/_parser.py @@ -293,6 +293,7 @@ def p_format(self, p): # circuit : new_reg circuit # | gate_op circuit # | measurement circuit + # | reset # | if circuit # | empty @@ -303,6 +304,7 @@ def p_circuit_reg(self, p): def p_circuit_gate_or_measurement_or_if(self, p): """circuit : circuit gate_op | circuit measurement + | circuit reset | circuit if""" self.circuit.append(p[2]) p[0] = self.circuit @@ -499,6 +501,15 @@ def p_measurement(self, p): ops.MeasurementGate(num_qubits=1, key=creg[i]).on(qreg[i]) for i in range(len(qreg)) ] + # reset operations + # reset : RESET qarg + + def p_reset(self, p): + """reset : RESET qarg ';'""" + qreg = p[2] + + p[0] = [ops.ResetChannel().on(qreg[i]) for i in range(len(qreg))] + # if operations # if : IF '(' carg EQ NATURAL_NUMBER ')' ID qargs diff --git a/cirq-core/cirq/contrib/qasm_import/_parser_test.py b/cirq-core/cirq/contrib/qasm_import/_parser_test.py index 4b0ca8e50f1..23c31bdf928 100644 --- a/cirq-core/cirq/contrib/qasm_import/_parser_test.py +++ b/cirq-core/cirq/contrib/qasm_import/_parser_test.py @@ -706,6 +706,36 @@ def test_measurement_bounds(): parser.parse(qasm) +def test_reset(): + qasm =""" + OPENQASM 2.0; + include "qelib1.inc"; + qreg q[1]; + creg c[1]; + x q[0]; + reset q[0]; + measure q[0] -> c[0]; + """ + + parser = QasmParser() + + q_0 = cirq.NamedQubit('q_0') + + expected_circuit = Circuit() + expected_circuit.append(cirq.X(q_0)) + expected_circuit.append(cirq.ResetChannel().on(q_0)) + expected_circuit.append(cirq.MeasurementGate(num_qubits=1, key='c_0').on(q_0)) + + parsed_qasm = parser.parse(qasm) + + assert parsed_qasm.supportedFormat + assert parsed_qasm.qelib1Include + + ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit) + assert parsed_qasm.qregs == {'q': 1} + assert parsed_qasm.cregs == {'c': 1} + + def test_u1_gate(): qasm = """ OPENQASM 2.0; From 9d2be2daf0dcd9ebfd6674dcf66a03fa554f290c Mon Sep 17 00:00:00 2001 From: Roland Siegbert Date: Sat, 14 Sep 2024 15:09:22 +0200 Subject: [PATCH 2/2] Fix comments & typos --- cirq-core/cirq/contrib/qasm_import/_parser.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cirq-core/cirq/contrib/qasm_import/_parser.py b/cirq-core/cirq/contrib/qasm_import/_parser.py index 1d9e8606047..f2b30423c2d 100644 --- a/cirq-core/cirq/contrib/qasm_import/_parser.py +++ b/cirq-core/cirq/contrib/qasm_import/_parser.py @@ -293,7 +293,7 @@ def p_format(self, p): # circuit : new_reg circuit # | gate_op circuit # | measurement circuit - # | reset + # | reset circuit # | if circuit # | empty @@ -505,7 +505,7 @@ def p_measurement(self, p): # reset : RESET qarg def p_reset(self, p): - """reset : RESET qarg ';'""" + """reset : RESET qreg ';'""" qreg = p[2] p[0] = [ops.ResetChannel().on(qreg[i]) for i in range(len(qreg))]