Skip to content

Added QASM Import Support for rzz, rxx, ryy, crx, cry, and iswap Gates #7290

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

Merged
merged 10 commits into from
May 20, 2025
139 changes: 86 additions & 53 deletions cirq-core/cirq/contrib/qasm_import/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,41 +238,54 @@ def __init__(self) -> None:
}

qelib_gates = {
'rx': QasmGateStatement(
qasm_gate='rx', cirq_gate=(lambda params: ops.rx(params[0])), num_params=1, num_args=1
),
'sx': QasmGateStatement(
qasm_gate='sx', num_params=0, num_args=1, cirq_gate=ops.XPowGate(exponent=0.5)
'ccx': QasmGateStatement(qasm_gate='ccx', num_params=0, num_args=3, cirq_gate=ops.CCX),
'ch': QasmGateStatement(
qasm_gate='ch', cirq_gate=ops.ControlledGate(ops.H), num_params=0, num_args=2
),
'sxdg': QasmGateStatement(
qasm_gate='sxdg', num_params=0, num_args=1, cirq_gate=ops.XPowGate(exponent=-0.5)
'crx': QasmGateStatement(
qasm_gate='crx',
num_params=1,
num_args=2,
cirq_gate=(lambda params: ops.ControlledGate(ops.rx(params[0]))),
),
'ry': QasmGateStatement(
qasm_gate='ry', cirq_gate=(lambda params: ops.ry(params[0])), num_params=1, num_args=1
'cry': QasmGateStatement(
qasm_gate='cry',
num_params=1,
num_args=2,
cirq_gate=(lambda params: ops.ControlledGate(ops.ry(params[0]))),
),
'rz': QasmGateStatement(
qasm_gate='rz', cirq_gate=(lambda params: ops.rz(params[0])), num_params=1, num_args=1
'crz': QasmGateStatement(
qasm_gate='crz',
num_params=1,
num_args=2,
cirq_gate=(lambda params: ops.ControlledGate(ops.rz(params[0]))),
),
'id': QasmGateStatement(
qasm_gate='id', cirq_gate=ops.IdentityGate(1), num_params=0, num_args=1
'cswap': QasmGateStatement(
qasm_gate='cswap', num_params=0, num_args=3, cirq_gate=ops.CSWAP
),
'u1': QasmGateStatement(
qasm_gate='u1',
cirq_gate=(lambda params: QasmUGate(0, 0, params[0] / np.pi)),
'cu1': QasmGateStatement(
qasm_gate='cu1',
num_params=1,
num_args=1,
),
'u2': QasmGateStatement(
qasm_gate='u2',
cirq_gate=(lambda params: QasmUGate(0.5, params[0] / np.pi, params[1] / np.pi)),
num_params=2,
num_args=1,
num_args=2,
cirq_gate=(lambda params: ops.ControlledGate(QasmUGate(0, 0, params[0] / np.pi))),
),
'u3': QasmGateStatement(
qasm_gate='u3',
'cu3': QasmGateStatement(
qasm_gate='cu3',
num_params=3,
num_args=1,
cirq_gate=(lambda params: QasmUGate(*[p / np.pi for p in params])),
num_args=2,
cirq_gate=(lambda params: ops.ControlledGate(QasmUGate(*[p / np.pi for p in params]))),
),
'cx': QasmGateStatement(qasm_gate='cx', cirq_gate=CX, num_params=0, num_args=2),
'cy': QasmGateStatement(
qasm_gate='cy', cirq_gate=ops.ControlledGate(ops.Y), num_params=0, num_args=2
),
'cz': QasmGateStatement(qasm_gate='cz', cirq_gate=ops.CZ, num_params=0, num_args=2),
'h': QasmGateStatement(qasm_gate='h', num_params=0, num_args=1, cirq_gate=ops.H),
'id': QasmGateStatement(
qasm_gate='id', cirq_gate=ops.IdentityGate(1), num_params=0, num_args=1
),
'iswap': QasmGateStatement(
qasm_gate='iswap', cirq_gate=ops.ISwapPowGate(), num_params=0, num_args=2
),
'r': QasmGateStatement(
qasm_gate='r',
Expand All @@ -284,45 +297,65 @@ def __init__(self) -> None:
)
),
),
'x': QasmGateStatement(qasm_gate='x', num_params=0, num_args=1, cirq_gate=ops.X),
'y': QasmGateStatement(qasm_gate='y', num_params=0, num_args=1, cirq_gate=ops.Y),
'z': QasmGateStatement(qasm_gate='z', num_params=0, num_args=1, cirq_gate=ops.Z),
'h': QasmGateStatement(qasm_gate='h', num_params=0, num_args=1, cirq_gate=ops.H),
's': QasmGateStatement(qasm_gate='s', num_params=0, num_args=1, cirq_gate=ops.S),
't': QasmGateStatement(qasm_gate='t', num_params=0, num_args=1, cirq_gate=ops.T),
'cx': QasmGateStatement(qasm_gate='cx', cirq_gate=CX, num_params=0, num_args=2),
'cy': QasmGateStatement(
qasm_gate='cy', cirq_gate=ops.ControlledGate(ops.Y), num_params=0, num_args=2
'rx': QasmGateStatement(
qasm_gate='rx', cirq_gate=(lambda params: ops.rx(params[0])), num_params=1, num_args=1
),
'cz': QasmGateStatement(qasm_gate='cz', cirq_gate=ops.CZ, num_params=0, num_args=2),
'ch': QasmGateStatement(
qasm_gate='ch', cirq_gate=ops.ControlledGate(ops.H), num_params=0, num_args=2
'ry': QasmGateStatement(
qasm_gate='ry', cirq_gate=(lambda params: ops.ry(params[0])), num_params=1, num_args=1
),
'cu1': QasmGateStatement(
qasm_gate='cu1',
'ryy': QasmGateStatement(
qasm_gate='ryy',
num_params=1,
num_args=2,
cirq_gate=(lambda params: ops.ControlledGate(QasmUGate(0, 0, params[0] / np.pi))),
cirq_gate=(lambda params: ops.YYPowGate(exponent=params[0] / np.pi)),
),
'cu3': QasmGateStatement(
qasm_gate='cu3',
num_params=3,
'rz': QasmGateStatement(
qasm_gate='rz', cirq_gate=(lambda params: ops.rz(params[0])), num_params=1, num_args=1
),
'rxx': QasmGateStatement(
qasm_gate='rxx',
num_params=1,
num_args=2,
cirq_gate=(lambda params: ops.ControlledGate(QasmUGate(*[p / np.pi for p in params]))),
cirq_gate=(lambda params: ops.XXPowGate(exponent=params[0] / np.pi)),
),
'crz': QasmGateStatement(
qasm_gate='crz',
'rzz': QasmGateStatement(
qasm_gate='rzz',
num_params=1,
num_args=2,
cirq_gate=(lambda params: ops.ControlledGate(ops.rz(params[0]))),
cirq_gate=(lambda params: ops.ZZPowGate(exponent=params[0] / np.pi)),
),
's': QasmGateStatement(qasm_gate='s', num_params=0, num_args=1, cirq_gate=ops.S),
'sdg': QasmGateStatement(qasm_gate='sdg', num_params=0, num_args=1, cirq_gate=ops.S**-1),
'swap': QasmGateStatement(qasm_gate='swap', cirq_gate=ops.SWAP, num_params=0, num_args=2),
'cswap': QasmGateStatement(
qasm_gate='cswap', num_params=0, num_args=3, cirq_gate=ops.CSWAP
'sx': QasmGateStatement(
qasm_gate='sx', num_params=0, num_args=1, cirq_gate=ops.XPowGate(exponent=0.5)
),
'ccx': QasmGateStatement(qasm_gate='ccx', num_params=0, num_args=3, cirq_gate=ops.CCX),
'sdg': QasmGateStatement(qasm_gate='sdg', num_params=0, num_args=1, cirq_gate=ops.S**-1),
'sxdg': QasmGateStatement(
qasm_gate='sxdg', num_params=0, num_args=1, cirq_gate=ops.XPowGate(exponent=-0.5)
),
't': QasmGateStatement(qasm_gate='t', num_params=0, num_args=1, cirq_gate=ops.T),
'tdg': QasmGateStatement(qasm_gate='tdg', num_params=0, num_args=1, cirq_gate=ops.T**-1),
'u1': QasmGateStatement(
qasm_gate='u1',
cirq_gate=(lambda params: QasmUGate(0, 0, params[0] / np.pi)),
num_params=1,
num_args=1,
),
'u2': QasmGateStatement(
qasm_gate='u2',
cirq_gate=(lambda params: QasmUGate(0.5, params[0] / np.pi, params[1] / np.pi)),
num_params=2,
num_args=1,
),
'u3': QasmGateStatement(
qasm_gate='u3',
num_params=3,
num_args=1,
cirq_gate=(lambda params: QasmUGate(*[p / np.pi for p in params])),
),
'x': QasmGateStatement(qasm_gate='x', num_params=0, num_args=1, cirq_gate=ops.X),
'y': QasmGateStatement(qasm_gate='y', num_params=0, num_args=1, cirq_gate=ops.Y),
'z': QasmGateStatement(qasm_gate='z', num_params=0, num_args=1, cirq_gate=ops.Z),
}

tokens = QasmLexer.tokens
Expand Down
108 changes: 108 additions & 0 deletions cirq-core/cirq/contrib/qasm_import/_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1512,3 +1512,111 @@ def test_nested_custom_gate_has_keyword_in_name():
parser = QasmParser()
parsed_qasm = parser.parse(qasm)
assert parsed_qasm.circuit == expected


def test_rzz_gate():
qasm = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
rzz(pi/2) q[0],q[1];
"""
parser = QasmParser()
parsed = parser.parse(qasm)
ops = list(parsed.circuit.all_operations())
assert isinstance(ops[0].gate, cirq.ZZPowGate)
assert np.isclose(ops[0].gate.exponent, 0.5)


def test_rxx_gate():
qasm = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
rxx(pi/4) q[0],q[1];
"""
parser = QasmParser()
parsed = parser.parse(qasm)
ops = list(parsed.circuit.all_operations())
assert isinstance(ops[0].gate, cirq.XXPowGate)
assert np.isclose(ops[0].gate.exponent, 0.25)


def test_ryy_gate():
qasm = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
ryy(pi/3) q[0],q[1];
"""
parser = QasmParser()
parsed = parser.parse(qasm)
ops = list(parsed.circuit.all_operations())
assert isinstance(ops[0].gate, cirq.YYPowGate)
assert np.isclose(ops[0].gate.exponent, 1 / 3)


def test_crx_gate():
qasm = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
crx(pi/7) q[0],q[1];
"""
parser = QasmParser()
parsed = parser.parse(qasm)
ops = list(parsed.circuit.all_operations())
gate = ops[0].gate
assert isinstance(gate, cirq.ControlledGate)
assert isinstance(gate.sub_gate, cirq.Rx)
assert np.isclose(gate.sub_gate.exponent, 1 / 7)


def test_cry_gate():
qasm = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
cry(pi/8) q[0],q[1];
"""
parser = QasmParser()
parsed = parser.parse(qasm)
ops = list(parsed.circuit.all_operations())
gate = ops[0].gate
assert isinstance(gate, cirq.ControlledGate)
assert isinstance(gate.sub_gate, cirq.Ry)
assert np.isclose(gate.sub_gate.exponent, 1 / 8)


def test_iswap_gate():
qasm = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
iswap q[0],q[1];
"""
parser = QasmParser()
parsed = parser.parse(qasm)
ops = list(parsed.circuit.all_operations())
assert isinstance(ops[0].gate, cirq.ISwapPowGate)


@pytest.mark.parametrize(
"theta_expr, theta", [("pi/8", np.pi / 8), ("pi/4", np.pi / 4), ("pi/2", np.pi / 2)]
)
def test_rxx_unitary_equivalence(theta_expr, theta):
qasm = f"""
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
rxx({theta_expr}) q[0],q[1];
"""
native = cirq.XXPowGate(exponent=theta / np.pi)
parsed_qasm = QasmParser().parse(qasm)
ops = list(parsed_qasm.circuit.all_operations())
imported = ops[0].gate

U_native = cirq.unitary(native)
U_import = cirq.unitary(imported)

assert np.allclose(U_import, U_native, atol=1e-8)