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 2 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 @@ -29,6 +29,7 @@ def __init__(self):
reserved = {
'qreg': 'QREG',
'creg': 'CREG',
'reset': 'RESET',
'measure': 'MEASURE',
'if': 'IF',
'->': 'ARROW',
Expand Down Expand Up @@ -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
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 @@ -293,6 +293,7 @@ def p_format(self, p):
# circuit : new_reg circuit
# | gate_op circuit
# | measurement circuit
# | reset circuit
# | if circuit
# | empty

Expand All @@ -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
Expand Down Expand Up @@ -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 qreg ';'"""
qreg = p[2]

p[0] = [ops.ResetChannel().on(qreg[i]) for i in range(len(qreg))]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be some sort of check here to ensure the reset is applied on a qubit that is actually defined in the quantum register? E.g. should some error be raised when trying to parse the following QASM program?

OPENQASM 2.0;
include "qelib1.inc";
qreg q[1];
reset q[2];


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

Expand Down
30 changes: 30 additions & 0 deletions cirq-core/cirq/contrib/qasm_import/_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading