Skip to content

Commit

Permalink
Fix some test incompatibilities with latest numpy (quantumlib#3107)
Browse files Browse the repository at this point in the history
- Also fix ZPowGate and PhasedXPowGate not supporting approximate equality
- Also fix some tests using deprecated methods
  • Loading branch information
Strilanc authored Jul 8, 2020
1 parent b21294b commit 62ef7f7
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 21 deletions.
21 changes: 7 additions & 14 deletions cirq/ion/convert_to_ion_gates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def test_convert_to_ion_gates():

rx = cirq.ion.ConvertToIonGates().convert_one(OtherX().on(q0))
rop = cirq.ion.ConvertToIonGates().convert_one(op)
rcnot = cirq.ion.ConvertToIonGates().convert_one(OtherCNOT().on(q0, q1))
assert rx == [
cirq.PhasedXPowGate(phase_exponent=1).on(cirq.GridQubit(0, 0))
]
Expand All @@ -65,19 +64,13 @@ def test_convert_to_ion_gates():
cirq.rx(-1 * np.pi / 2).on(op.qubits[1]),
cirq.ry(-1 * np.pi / 2).on(op.qubits[0])
]
assert rcnot == [
cirq.PhasedXPowGate(phase_exponent=-0.75,
exponent=0.5).on(cirq.GridQubit(0, 0)),
cirq.PhasedXPowGate(phase_exponent=1,
exponent=0.25).on(cirq.GridQubit(0, 1)),
cirq.T.on(cirq.GridQubit(0, 0)),
cirq.ms(-0.5 * np.pi / 2).on(cirq.GridQubit(0, 0), cirq.GridQubit(0,
1)),
(cirq.Y**0.5).on(cirq.GridQubit(0, 0)),
cirq.PhasedXPowGate(phase_exponent=1,
exponent=0.25).on(cirq.GridQubit(0, 1)),
(cirq.Z**-0.75).on(cirq.GridQubit(0, 0))
]

rcnot = cirq.ion.ConvertToIonGates().convert_one(OtherCNOT().on(q0, q1))
assert cirq.approx_eq([op for op in rcnot if len(op.qubits) > 1],
[cirq.ms(-0.5 * np.pi / 2).on(q0, q1)],
atol=1e-4)
assert cirq.allclose_up_to_global_phase(
cirq.unitary(cirq.Circuit(rcnot)), cirq.unitary(OtherCNOT().on(q0, q1)))


def test_convert_to_ion_circuit():
Expand Down
9 changes: 9 additions & 0 deletions cirq/ops/common_gates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,3 +759,12 @@ def test_commutes():
assert cirq.commutes(cirq.Z, cirq.Z(cirq.LineQubit(0)),
default=None) is None
assert cirq.commutes(cirq.Z**0.1, cirq.XPowGate(exponent=0))


def test_approx_eq():
assert cirq.approx_eq(cirq.Z**0.1, cirq.Z**0.2, atol=0.3)
assert not cirq.approx_eq(cirq.Z**0.1, cirq.Z**0.2, atol=0.05)
assert cirq.approx_eq(cirq.Y**0.1, cirq.Y**0.2, atol=0.3)
assert not cirq.approx_eq(cirq.Y**0.1, cirq.Y**0.2, atol=0.05)
assert cirq.approx_eq(cirq.X**0.1, cirq.X**0.2, atol=0.3)
assert not cirq.approx_eq(cirq.X**0.1, cirq.X**0.2, atol=0.05)
2 changes: 1 addition & 1 deletion cirq/ops/phased_x_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from cirq.type_workarounds import NotImplementedType


@value.value_equality(manual_cls=True)
@value.value_equality(manual_cls=True, approximate=True)
class PhasedXPowGate(gate_features.SingleQubitGate):
"""A gate equivalent to the circuit ───Z^-p───X^t───Z^p───."""

Expand Down
18 changes: 18 additions & 0 deletions cirq/ops/phased_x_gate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,24 @@ def test_eq():
global_shift=-0.5))


def test_approx_eq():
assert cirq.approx_eq(
cirq.PhasedXPowGate(phase_exponent=0.1, exponent=0.2, global_shift=0.3),
cirq.PhasedXPowGate(phase_exponent=0.1, exponent=0.2, global_shift=0.3),
atol=1e-4,
)
assert not cirq.approx_eq(
cirq.PhasedXPowGate(phase_exponent=0.1, exponent=0.2, global_shift=0.4),
cirq.PhasedXPowGate(phase_exponent=0.1, exponent=0.2, global_shift=0.3),
atol=1e-4,
)
assert cirq.approx_eq(
cirq.PhasedXPowGate(phase_exponent=0.1, exponent=0.2, global_shift=0.4),
cirq.PhasedXPowGate(phase_exponent=0.1, exponent=0.2, global_shift=0.3),
atol=0.2,
)


def test_str_repr():
assert str(cirq.PhasedXPowGate(phase_exponent=0.25)) == 'PhX(0.25)'
assert str(cirq.PhasedXPowGate(phase_exponent=0.25,
Expand Down
2 changes: 1 addition & 1 deletion cirq/ops/random_gate_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _mixture_(self):
if self._is_parameterized_():
return NotImplemented

mixture = protocols.mixture_channel(self.sub_gate, None)
mixture = protocols.mixture(self.sub_gate, None)
if mixture is None:
return None

Expand Down
5 changes: 4 additions & 1 deletion cirq/optimizers/decompositions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ def test_single_qubit_matrix_to_phased_x_z_known():

actual = cirq.single_qubit_matrix_to_phased_x_z(
np.array([[0, -1j], [1j, 0]]), atol=0.01)
assert cirq.approx_eq(actual, [cirq.Y], atol=1e-9)
print(repr(actual))
assert cirq.approx_eq(
actual, [cirq.PhasedXPowGate(phase_exponent=0.5, exponent=-1)],
atol=1e-9)

actual = cirq.single_qubit_matrix_to_phased_x_z(
np.array([[1, 0], [0, -1]]), atol=0.01)
Expand Down
6 changes: 4 additions & 2 deletions cirq/protocols/mixture_protocol_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ class Yes1:
def _decompose_(self):
return [cirq.X(cirq.LineQubit(0))]

assert not cirq.has_mixture_channel(No1())
assert cirq.has_mixture_channel(Yes1())
with cirq.testing.assert_logs('cirq.has_mixture', ' has_mixture_channel '):
assert not cirq.has_mixture_channel(No1())
with cirq.testing.assert_logs('cirq.has_mixture', ' has_mixture_channel '):
assert cirq.has_mixture_channel(Yes1())


def test_valid_mixture():
Expand Down
4 changes: 2 additions & 2 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ the magic methods that can be implemented.
cirq.equal_up_to_global_phase
cirq.has_channel
cirq.has_mixture
cirq.has_mixture_channel
cirq.has_stabilizer_effect
cirq.has_unitary
cirq.inverse
Expand All @@ -337,7 +336,6 @@ the magic methods that can be implemented.
cirq.measurement_key
cirq.measurement_keys
cirq.mixture
cirq.mixture_channel
cirq.mul
cirq.num_qubits
cirq.pauli_expansion
Expand Down Expand Up @@ -711,6 +709,8 @@ These objects and methods will be removed in a future version of the library.

cirq.QFT
cirq.final_wavefunction
cirq.has_mixture_channel
cirq.mixture_channel
cirq.subwavefunction
cirq.validate_normalized_state
cirq.wavefunction_partial_trace_as_mixture
Expand Down

0 comments on commit 62ef7f7

Please sign in to comment.