Skip to content

Commit

Permalink
Added r(theta, phi) gate (#4455)
Browse files Browse the repository at this point in the history
As requested in #3415.

Made changes in `_parser.py` and added `r` gate under. I have replicated the format of `u3` and set params to `QasmUGate` according to `r(theta, phi) = u3(theta, phi - pi/2, -phi + pi/2)`. 

Also wrote a test for the same.
  • Loading branch information
Zshan0 committed Aug 24, 2021
1 parent 886fc51 commit 932f065
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
10 changes: 10 additions & 0 deletions cirq-core/cirq/contrib/qasm_import/_parser.py
Expand Up @@ -207,6 +207,16 @@ def __init__(self):
num_args=1,
cirq_gate=(lambda params: QasmUGate(*[p / np.pi for p in params])),
),
'r': QasmGateStatement(
qasm_gate='r',
num_params=2,
num_args=1,
cirq_gate=(
lambda params: QasmUGate(
params[0] / np.pi, (params[1] / np.pi) - 0.5, (-params[1] / np.pi) + 0.5
)
),
),
'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),
Expand Down
33 changes: 25 additions & 8 deletions cirq-core/cirq/contrib/qasm_import/_parser_test.py
Expand Up @@ -740,12 +740,36 @@ def test_u3_gate():
assert parsed_qasm.qregs == {'q': 2}


def test_r_gate():
qasm = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[1];
r(pi, pi / 2.0) q[0];
"""
parser = QasmParser()

q0 = cirq.NamedQubit('q_0')

expected_circuit = Circuit()
expected_circuit.append(QasmUGate(1.0, 0.0, 0.0)(q0))

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}


@pytest.mark.parametrize(
'qasm_gate',
[
'id',
'u2',
'u3',
'r',
]
+ [g[0] for g in rotation_gates]
+ [g[0] for g in single_qubit_gates],
Expand All @@ -766,14 +790,7 @@ def test_standard_single_qubit_gates_wrong_number_of_args(qasm_gate):

@pytest.mark.parametrize(
['qasm_gate', 'num_params'],
[
['id', 0],
['u2', 2],
['u3', 3],
['rx', 1],
['ry', 1],
['rz', 1],
]
[['id', 0], ['u2', 2], ['u3', 3], ['rx', 1], ['ry', 1], ['rz', 1], ['r', 2]]
+ [[g[0], 0] for g in single_qubit_gates],
)
def test_standard_gates_wrong_params_error(qasm_gate: str, num_params: int):
Expand Down

0 comments on commit 932f065

Please sign in to comment.