From 577d0334dad6d09b39f36f1be7b27684a2f92e42 Mon Sep 17 00:00:00 2001 From: Revanth Gundala Date: Sat, 19 Apr 2025 12:56:13 -0700 Subject: [PATCH 1/8] Updated linting --- cirq-core/cirq/contrib/qasm_import/_parser.py | 139 +++++++++++------- .../cirq/contrib/qasm_import/_parser_test.py | 87 +++++++++++ 2 files changed, 173 insertions(+), 53 deletions(-) diff --git a/cirq-core/cirq/contrib/qasm_import/_parser.py b/cirq-core/cirq/contrib/qasm_import/_parser.py index 29cb0d34726..80a7771ba07 100644 --- a/cirq-core/cirq/contrib/qasm_import/_parser.py +++ b/cirq-core/cirq/contrib/qasm_import/_parser.py @@ -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', @@ -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 diff --git a/cirq-core/cirq/contrib/qasm_import/_parser_test.py b/cirq-core/cirq/contrib/qasm_import/_parser_test.py index f0239b468c0..16879fd15d4 100644 --- a/cirq-core/cirq/contrib/qasm_import/_parser_test.py +++ b/cirq-core/cirq/contrib/qasm_import/_parser_test.py @@ -1512,3 +1512,90 @@ 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) From 4e769a48d28732d03679079e0c283a2cdd3f596c Mon Sep 17 00:00:00 2001 From: Revanth Gundala Date: Sun, 20 Apr 2025 14:17:34 -0700 Subject: [PATCH 2/8] Unitary equivalence test for rxx --- .../cirq/contrib/qasm_import/_parser_test.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/cirq-core/cirq/contrib/qasm_import/_parser_test.py b/cirq-core/cirq/contrib/qasm_import/_parser_test.py index 16879fd15d4..fbab86fdc2d 100644 --- a/cirq-core/cirq/contrib/qasm_import/_parser_test.py +++ b/cirq-core/cirq/contrib/qasm_import/_parser_test.py @@ -1599,3 +1599,29 @@ def test_iswap_gate(): 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=0) From 8f1f84618620ec1484fb198844185d572c264fea Mon Sep 17 00:00:00 2001 From: Revanth Gundala Date: Sun, 20 Apr 2025 14:19:06 -0700 Subject: [PATCH 3/8] Added tolerance --- cirq-core/cirq/contrib/qasm_import/_parser_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cirq-core/cirq/contrib/qasm_import/_parser_test.py b/cirq-core/cirq/contrib/qasm_import/_parser_test.py index fbab86fdc2d..be3c4b10714 100644 --- a/cirq-core/cirq/contrib/qasm_import/_parser_test.py +++ b/cirq-core/cirq/contrib/qasm_import/_parser_test.py @@ -1624,4 +1624,4 @@ def test_rxx_unitary_equivalence(theta_expr, theta): U_native = cirq.unitary(native) U_import = cirq.unitary(imported) - assert np.allclose(U_import, U_native, atol=0) + assert np.allclose(U_import, U_native, atol=1e-8) From 2eef09de0c7eb7ce76b44295ef023c6f8fb9c884 Mon Sep 17 00:00:00 2001 From: Revanth Gundala Date: Mon, 21 Apr 2025 15:51:48 -0700 Subject: [PATCH 4/8] Fixed linting --- cirq-core/cirq/contrib/qasm_import/_parser_test.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cirq-core/cirq/contrib/qasm_import/_parser_test.py b/cirq-core/cirq/contrib/qasm_import/_parser_test.py index be3c4b10714..a4ff1f0536d 100644 --- a/cirq-core/cirq/contrib/qasm_import/_parser_test.py +++ b/cirq-core/cirq/contrib/qasm_import/_parser_test.py @@ -1602,12 +1602,7 @@ def test_iswap_gate(): @pytest.mark.parametrize( - "theta_expr, theta", - [ - ("pi/8", np.pi / 8), - ("pi/4", np.pi / 4), - ("pi/2", np.pi / 2), - ], + "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""" From 312f14f84004f66761abfde7c5bd264a105a5be0 Mon Sep 17 00:00:00 2001 From: Revanth Gundala Date: Tue, 22 Apr 2025 14:32:55 -0700 Subject: [PATCH 5/8] Reformatted Unit Tests --- .../cirq/contrib/qasm_import/_parser_test.py | 140 ++++++++++++------ 1 file changed, 92 insertions(+), 48 deletions(-) diff --git a/cirq-core/cirq/contrib/qasm_import/_parser_test.py b/cirq-core/cirq/contrib/qasm_import/_parser_test.py index a4ff1f0536d..eca6b4dc060 100644 --- a/cirq-core/cirq/contrib/qasm_import/_parser_test.py +++ b/cirq-core/cirq/contrib/qasm_import/_parser_test.py @@ -1051,7 +1051,7 @@ def test_two_qubit_gates_not_enough_args(qasm_gate: str): include "qelib1.inc"; qreg q[2]; {qasm_gate} q[0]; -""" + """ parser = QasmParser() @@ -1522,10 +1522,19 @@ def test_rzz_gate(): 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) + + q0, q1 = cirq.NamedQubit('q_0'), cirq.NamedQubit('q_1') + + expected_circuit = Circuit() + expected_circuit.append(cirq.ZZPowGate(exponent=0.5).on(q0, q1)) + + 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': 2} def test_rxx_gate(): @@ -1536,10 +1545,19 @@ def test_rxx_gate(): 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) + + q0, q1 = cirq.NamedQubit('q_0'), cirq.NamedQubit('q_1') + + expected_circuit = Circuit() + expected_circuit.append(cirq.XXPowGate(exponent=0.25).on(q0, q1)) + + 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': 2} def test_ryy_gate(): @@ -1550,10 +1568,19 @@ def test_ryy_gate(): 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) + + q0, q1 = cirq.NamedQubit('q_0'), cirq.NamedQubit('q_1') + + expected_circuit = Circuit() + expected_circuit.append(cirq.YYPowGate(exponent=1 / 3).on(q0, q1)) + + 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': 2} def test_crx_gate(): @@ -1564,28 +1591,19 @@ def test_crx_gate(): 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) + q0, q1 = cirq.NamedQubit('q_0'), cirq.NamedQubit('q_1') -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) + expected_circuit = Circuit() + expected_circuit.append(cirq.ControlledGate(cirq.rx(np.pi / 7)).on(q0, q1)) + + 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': 2} def test_iswap_gate(): @@ -1596,27 +1614,53 @@ def test_iswap_gate(): iswap q[0],q[1]; """ parser = QasmParser() - parsed = parser.parse(qasm) - ops = list(parsed.circuit.all_operations()) - assert isinstance(ops[0].gate, cirq.ISwapPowGate) + + q0, q1 = cirq.NamedQubit('q_0'), cirq.NamedQubit('q_1') + + expected_circuit = Circuit() + expected_circuit.append(cirq.ISwapPowGate().on(q0, q1)) + + 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': 2} @pytest.mark.parametrize( - "theta_expr, theta", [("pi/8", np.pi / 8), ("pi/4", np.pi / 4), ("pi/2", np.pi / 2)] + "qasm_gate,cirq_gate,num_params,num_args", + [ + (name, stmt.cirq_gate, stmt.num_params, stmt.num_args) + for name, stmt in QasmParser.qelib_gates.items() + ], ) -def test_rxx_unitary_equivalence(theta_expr, theta): +def test_all_qelib_gates_unitary_equivalence(qasm_gate, cirq_gate, num_params, num_args): + theta = np.pi / 4 + thetas = [theta] * num_params + params_str = f"({','.join('pi/4' for _ in range(num_params))})" if num_params else "" + qubit_names, qubits = [], [] + for i in range(num_args): + qubit_names.append(f"q[{i}]") + qubits.append(cirq.NamedQubit(f"q_{i}")) qasm = f""" - OPENQASM 2.0; - include "qelib1.inc"; - qreg q[2]; - rxx({theta_expr}) q[0],q[1]; + OPENQASM 2.0; + include "qelib1.inc"; + qreg q[{num_args}]; + {qasm_gate}{params_str} {','.join(qubit_names)}; """ - 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) + parser = QasmParser() + parsed_qasm = parser.parse(qasm) + if num_params: + gate = cirq_gate(thetas) + else: + gate = cirq_gate + expected = Circuit() + expected.append(gate.on(*qubits)) + imported = list(parsed_qasm.circuit.all_operations())[0].gate + U_native = cirq.unitary(gate) U_import = cirq.unitary(imported) - assert np.allclose(U_import, U_native, atol=1e-8) + assert parsed_qasm.qregs == {'q': num_args} From 30287909e26002933c2fbcbdb6feab783e1362a7 Mon Sep 17 00:00:00 2001 From: Revanth Gundala Date: Tue, 22 Apr 2025 14:56:24 -0700 Subject: [PATCH 6/8] Paramaterized Theta --- cirq-core/cirq/contrib/qasm_import/_parser_test.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cirq-core/cirq/contrib/qasm_import/_parser_test.py b/cirq-core/cirq/contrib/qasm_import/_parser_test.py index eca6b4dc060..1f381ca4779 100644 --- a/cirq-core/cirq/contrib/qasm_import/_parser_test.py +++ b/cirq-core/cirq/contrib/qasm_import/_parser_test.py @@ -1636,10 +1636,12 @@ def test_iswap_gate(): for name, stmt in QasmParser.qelib_gates.items() ], ) -def test_all_qelib_gates_unitary_equivalence(qasm_gate, cirq_gate, num_params, num_args): - theta = np.pi / 4 +@pytest.mark.parametrize("theta,theta_str", [(np.pi / 4, "pi/4"), (np.pi / 2, "pi/2")]) +def test_all_qelib_gates_unitary_equivalence( + qasm_gate, cirq_gate, num_params, num_args, theta, theta_str +): thetas = [theta] * num_params - params_str = f"({','.join('pi/4' for _ in range(num_params))})" if num_params else "" + params_str = f"({','.join(theta_str for _ in range(num_params))})" if num_params else "" qubit_names, qubits = [], [] for i in range(num_args): qubit_names.append(f"q[{i}]") From efb20da0327c878fc2daecb2a7eb3407adbeeaf7 Mon Sep 17 00:00:00 2001 From: Revanth Gundala Date: Tue, 22 Apr 2025 14:57:39 -0700 Subject: [PATCH 7/8] 3 params --- cirq-core/cirq/contrib/qasm_import/_parser_test.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cirq-core/cirq/contrib/qasm_import/_parser_test.py b/cirq-core/cirq/contrib/qasm_import/_parser_test.py index 1f381ca4779..74bffb82241 100644 --- a/cirq-core/cirq/contrib/qasm_import/_parser_test.py +++ b/cirq-core/cirq/contrib/qasm_import/_parser_test.py @@ -1636,10 +1636,8 @@ def test_iswap_gate(): for name, stmt in QasmParser.qelib_gates.items() ], ) -@pytest.mark.parametrize("theta,theta_str", [(np.pi / 4, "pi/4"), (np.pi / 2, "pi/2")]) -def test_all_qelib_gates_unitary_equivalence( - qasm_gate, cirq_gate, num_params, num_args, theta, theta_str -): +@pytest.mark.parametrize("theta,theta_str", [(np.pi/4, "pi/4"), (np.pi/2, "pi/2"), (np.pi, "pi")]) +def test_all_qelib_gates_unitary_equivalence(qasm_gate, cirq_gate, num_params, num_args, theta, theta_str): thetas = [theta] * num_params params_str = f"({','.join(theta_str for _ in range(num_params))})" if num_params else "" qubit_names, qubits = [], [] From c186e8c6b6285d1dab041d1f42e851111943741e Mon Sep 17 00:00:00 2001 From: Revanth Gundala Date: Fri, 25 Apr 2025 13:36:28 -0700 Subject: [PATCH 8/8] Fixed linting --- cirq-core/cirq/contrib/qasm_import/_parser_test.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cirq-core/cirq/contrib/qasm_import/_parser_test.py b/cirq-core/cirq/contrib/qasm_import/_parser_test.py index 74bffb82241..2f7c1467864 100644 --- a/cirq-core/cirq/contrib/qasm_import/_parser_test.py +++ b/cirq-core/cirq/contrib/qasm_import/_parser_test.py @@ -1636,8 +1636,12 @@ def test_iswap_gate(): for name, stmt in QasmParser.qelib_gates.items() ], ) -@pytest.mark.parametrize("theta,theta_str", [(np.pi/4, "pi/4"), (np.pi/2, "pi/2"), (np.pi, "pi")]) -def test_all_qelib_gates_unitary_equivalence(qasm_gate, cirq_gate, num_params, num_args, theta, theta_str): +@pytest.mark.parametrize( + "theta,theta_str", [(np.pi / 4, "pi/4"), (np.pi / 2, "pi/2"), (np.pi, "pi")] +) +def test_all_qelib_gates_unitary_equivalence( + qasm_gate, cirq_gate, num_params, num_args, theta, theta_str +): thetas = [theta] * num_params params_str = f"({','.join(theta_str for _ in range(num_params))})" if num_params else "" qubit_names, qubits = [], []