Skip to content

Commit 0f21e0a

Browse files
codrut3pavoljuhas
andauthored
Implement _value_equality_approximate_values_ for PhasedISwapPowGate. (#6959)
* Implement _value_equality_approximate_values_ for PhasedISwapPowGate. This fixes the problem that phase_exponent is not taken into account for approx_eq. * Assume ISWAP and PhasedISwapPowGate gates are different And also make PhasedISwapPowGate with phase_exponent=0 and phase_exponent=eps approximately equal for small eps. --------- Co-authored-by: Pavol Juhas <[email protected]>
1 parent 48aadd0 commit 0f21e0a

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

cirq-core/cirq/ops/phased_iswap_gate.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,14 @@ def _json_dict_(self) -> Dict[str, Any]:
9393
}
9494

9595
def _value_equality_values_cls_(self):
96-
if self.phase_exponent == 0:
97-
return swap_gates.ISwapPowGate
9896
return PhasedISwapPowGate
9997

10098
def _value_equality_values_(self):
101-
if self.phase_exponent == 0:
102-
return self._iswap._value_equality_values_()
10399
return (self.phase_exponent, *self._iswap._value_equality_values_())
104100

101+
def _value_equality_approximate_values_(self):
102+
return (self.phase_exponent, *self._iswap._value_equality_approximate_values_())
103+
105104
def _is_parameterized_(self) -> bool:
106105
return protocols.is_parameterized(self._iswap) or protocols.is_parameterized(
107106
self._phase_exponent

cirq-core/cirq/ops/phased_iswap_gate_test.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ def test_phased_iswap_init():
3434

3535
def test_phased_iswap_equality():
3636
eq = cirq.testing.EqualsTester()
37-
eq.add_equality_group(cirq.PhasedISwapPowGate(phase_exponent=0, exponent=0.4), cirq.ISWAP**0.4)
38-
eq.add_equality_group(
39-
cirq.PhasedISwapPowGate(phase_exponent=0, exponent=0.4, global_shift=0.3),
40-
cirq.ISwapPowGate(global_shift=0.3) ** 0.4,
41-
)
37+
eq.add_equality_group(cirq.PhasedISwapPowGate(phase_exponent=0, exponent=0.4))
38+
eq.add_equality_group(cirq.ISWAP**0.4)
39+
eq.add_equality_group(cirq.PhasedISwapPowGate(phase_exponent=0, exponent=0.4, global_shift=0.3))
40+
eq.add_equality_group(cirq.ISwapPowGate(global_shift=0.3) ** 0.4)
4241

4342

4443
def test_repr():
@@ -204,3 +203,20 @@ def test_givens_rotation_equivalent_circuit():
204203
@pytest.mark.parametrize('angle_rads', (-np.pi / 5, 0.4, 2, np.pi))
205204
def test_givens_rotation_has_consistent_protocols(angle_rads):
206205
cirq.testing.assert_implements_consistent_protocols(cirq.givens(angle_rads))
206+
207+
208+
def test_approx_eq():
209+
gate0 = cirq.PhasedISwapPowGate(phase_exponent=0)
210+
gate1 = cirq.PhasedISwapPowGate(phase_exponent=1e-12)
211+
gate2 = cirq.PhasedISwapPowGate(phase_exponent=2e-12)
212+
gate3 = cirq.PhasedISwapPowGate(phase_exponent=0.345)
213+
gate4 = cirq.ISwapPowGate()
214+
215+
assert cirq.approx_eq(gate1, gate2)
216+
assert cirq.approx_eq(gate1, gate0)
217+
assert not cirq.approx_eq(gate1, gate3)
218+
assert not cirq.approx_eq(gate0, gate4)
219+
220+
assert cirq.equal_up_to_global_phase(gate1, gate2)
221+
assert cirq.equal_up_to_global_phase(gate1, gate0)
222+
assert not cirq.equal_up_to_global_phase(gate1, gate3)

0 commit comments

Comments
 (0)