Skip to content

Commit

Permalink
Implement PhasedXZGate._unitary_ (quantumlib#4617)
Browse files Browse the repository at this point in the history
  • Loading branch information
maffoo authored Nov 2, 2021
1 parent 5711353 commit f245eb2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cirq/ops/phased_x_z_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ def _qasm_(self, args: 'cirq.QasmArgs', qubits: Tuple['cirq.Qid', ...]) -> Optio
def _has_unitary_(self) -> bool:
return not self._is_parameterized_()

def _unitary_(self) -> Optional[np.ndarray]:
"""See `cirq.SupportsUnitary`."""
if self._is_parameterized_():
return None
z_pre = protocols.unitary(ops.Z ** -self._axis_phase_exponent)
x = protocols.unitary(ops.X ** self._x_exponent)
z_post = protocols.unitary(ops.Z ** (self._axis_phase_exponent + self._z_exponent))
return z_post @ x @ z_pre

def _decompose_(self, qubits: Sequence['cirq.Qid']) -> 'cirq.OP_TREE':
q = qubits[0]
yield ops.Z(q) ** -self._axis_phase_exponent
Expand Down
16 changes: 16 additions & 0 deletions cirq/ops/phased_x_z_gate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,22 @@ def test_from_matrix_close_unitary(unitary: np.ndarray):
)


@pytest.mark.parametrize(
'unitary',
[
cirq.testing.random_unitary(2),
cirq.testing.random_unitary(2),
cirq.testing.random_unitary(2),
np.array([[0, 1], [1j, 0]]),
],
)
def test_from_matrix_close_kraus(unitary: np.ndarray):
gate = cirq.PhasedXZGate.from_matrix(unitary)
kraus = cirq.kraus(gate)
assert len(kraus) == 1
cirq.testing.assert_allclose_up_to_global_phase(kraus[0], unitary, atol=1e-8)


def test_protocols():
a = random.random()
b = random.random()
Expand Down

0 comments on commit f245eb2

Please sign in to comment.