Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reset to QASM parser #6710

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cirq-core/cirq/contrib/qasm_import/_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(self):
'bit': 'BIT',
'creg': 'CREG',
'measure': 'MEASURE',
'reset': 'RESET',
'if': 'IF',
'->': 'ARROW',
'==': 'EQ',
Expand Down Expand Up @@ -115,6 +116,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
Expand Down
11 changes: 11 additions & 0 deletions cirq-core/cirq/contrib/qasm_import/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ def p_format(self, p):
# circuit : new_reg circuit
# | gate_op circuit
# | measurement circuit
# | reset circuit
# | if circuit
# | empty

Expand All @@ -310,6 +311,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
Expand Down Expand Up @@ -526,6 +528,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))]
pavoljuhas marked this conversation as resolved.
Show resolved Hide resolved

# if operations
# if : IF '(' carg EQ NATURAL_NUMBER ')' ID qargs

Expand Down
36 changes: 34 additions & 2 deletions cirq-core/cirq/contrib/qasm_import/_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import textwrap
from typing import Callable

import numpy as np
Expand Down Expand Up @@ -706,6 +707,35 @@ def test_measurement_bounds():
parser.parse(qasm)


def test_reset():
qasm = textwrap.dedent(
"""\
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([cirq.X(q_0), cirq.reset(q_0), cirq.measure(q_0, key='c_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;
Expand Down Expand Up @@ -1068,12 +1098,13 @@ def test_openqasm_3_0_qubits():
x q[0];

b[0] = measure q[0];
reset q[0];
"""
parser = QasmParser()

q0 = cirq.NamedQubit('q_0')

expected_circuit = Circuit([cirq.X.on(q0), cirq.measure(q0, key='b_0')])
expected_circuit = Circuit([cirq.X.on(q0), cirq.measure(q0, key='b_0'), cirq.reset(q0)])

parsed_qasm = parser.parse(qasm)

Expand All @@ -1092,12 +1123,13 @@ def test_openqasm_3_0_scalar_qubit():
x q;

b = measure q;
reset q;
"""
parser = QasmParser()

q0 = cirq.NamedQubit('q_0')

expected_circuit = Circuit([cirq.X.on(q0), cirq.measure(q0, key='b_0')])
expected_circuit = Circuit([cirq.X.on(q0), cirq.measure(q0, key='b_0'), cirq.reset(q0)])

parsed_qasm = parser.parse(qasm)

Expand Down