From dc8a43bb2ca2570f3465a465e636f42007b05056 Mon Sep 17 00:00:00 2001 From: richrines1 <85512171+richrines1@users.noreply.github.com> Date: Thu, 14 Nov 2024 13:28:18 -0600 Subject: [PATCH] fix `phase_by` protocol for tagged operations (#6792) `cirq.phase_by` can return a default value for operation that cannot be phased, but that was broken for tagged operations. Here is a fix for that. Fixes #6791 --- cirq-core/cirq/ops/raw_types.py | 4 +++- cirq-core/cirq/ops/raw_types_test.py | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cirq-core/cirq/ops/raw_types.py b/cirq-core/cirq/ops/raw_types.py index 696868fe780..168c3cd1765 100644 --- a/cirq-core/cirq/ops/raw_types.py +++ b/cirq-core/cirq/ops/raw_types.py @@ -939,7 +939,9 @@ def _trace_distance_bound_(self) -> float: return protocols.trace_distance_bound(self.sub_operation) def _phase_by_(self, phase_turns: float, qubit_index: int) -> 'cirq.Operation': - return protocols.phase_by(self.sub_operation, phase_turns, qubit_index) + return protocols.phase_by( + self.sub_operation, phase_turns, qubit_index, default=NotImplemented + ) def __pow__(self, exponent: Any) -> 'cirq.Operation': return self.sub_operation**exponent diff --git a/cirq-core/cirq/ops/raw_types_test.py b/cirq-core/cirq/ops/raw_types_test.py index 34804c95fdf..97cfb3e5a13 100644 --- a/cirq-core/cirq/ops/raw_types_test.py +++ b/cirq-core/cirq/ops/raw_types_test.py @@ -662,6 +662,8 @@ def test_tagged_operation_forwards_protocols(): assert cirq.commutes(tagged_x, clifford_x) assert cirq.commutes(clifford_x, tagged_x) assert cirq.commutes(tagged_x, tagged_x) + assert cirq.phase_by(clifford_x, 0.125, 0, default=None) is None + assert cirq.phase_by(tagged_x, 0.125, 0, default=None) is None assert cirq.trace_distance_bound(y**0.001) == cirq.trace_distance_bound( (y**0.001).with_tags(tag)