Skip to content

Propagate _channel_ from GateOperation to its Gate #4255

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

Merged
merged 1 commit into from
Jun 23, 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
12 changes: 12 additions & 0 deletions cirq-core/cirq/ops/gate_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ def _mixture_(self) -> Sequence[Tuple[float, Any]]:
return getter()
return NotImplemented

def _has_channel_(self) -> bool:
getter = getattr(self.gate, '_has_channel_', None)
if getter is not None:
return getter()
return NotImplemented

def _channel_(self) -> Union[Tuple[np.ndarray], NotImplementedType]:
getter = getattr(self.gate, '_channel_', None)
if getter is not None:
return getter()
return NotImplemented

def _has_kraus_(self) -> bool:
getter = getattr(self.gate, '_has_kraus_', None)
if getter is not None:
Expand Down
25 changes: 25 additions & 0 deletions cirq-core/cirq/ops/gate_operation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Tuple

import numpy as np
import pytest
Expand Down Expand Up @@ -433,3 +434,27 @@ def _is_parameterized_(self):
assert not cirq.is_parameterized(No1().on(q))
assert not cirq.is_parameterized(No2().on(q))
assert cirq.is_parameterized(Yes().on(q))


def test_channel_propagates_to_gate():
class TestGate(cirq.SingleQubitGate):
def _channel_(self) -> np.ndarray:
return (np.eye(2),)

def _has_channel_(self) -> bool:
return True

def assert_kraus_eq(ks1: Tuple[np.ndarray, ...], ks2: Tuple[np.ndarray, ...]) -> None:
assert len(ks1) == len(ks2)
for k1, k2 in zip(ks1, ks2):
assert np.all(k1 == k2)

identity_kraus = (np.eye(2),)
q = cirq.LineQubit(0)
gate = TestGate()
gate_op = TestGate().on(q)
with cirq.testing.assert_deprecated(deadline='v0.13', count=None):
assert cirq.has_channel(gate)
assert cirq.has_channel(gate_op)
assert_kraus_eq(cirq.channel(gate), identity_kraus)
assert_kraus_eq(cirq.channel(gate_op), identity_kraus)