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

Implement PhasedXZGate._unitary_ #4617

Merged
merged 1 commit into from
Nov 2, 2021
Merged
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
9 changes: 9 additions & 0 deletions cirq-core/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-core/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):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add another test that hits cirq.unitary as well ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is already a test of cirq.unitary: https://github.com/quantumlib/Cirq/blob/master/cirq-core/cirq/ops/phased_x_z_gate_test.py#L196. This worked by decomposition even without a _unitary_ implementation, but other protocols like cirq.kraus didn't.

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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Question: is this tol too high for complex64. I would've expected only around 6-7 decimal digits of reliability.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was copy-pasted from other allclose checks in the same module. I'm inclined to keep it for consistency, but we could certainly relax it throughout the module if desired.



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