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

Fix IdentityGate.on_each failing when given a qubit that is iterable #3036

Merged
merged 3 commits into from
Jun 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions cirq/ops/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ def on_each(self, *targets: Union['cirq.Qid', Iterable[Any]]
'gate.')
operations: List['cirq.Operation'] = []
for target in targets:
if isinstance(target, Iterable) and not isinstance(target, str):
operations.extend(self.on_each(*target))
elif isinstance(target, raw_types.Qid):
if isinstance(target, raw_types.Qid):
operations.append(self.on(target))
elif isinstance(target, Iterable) and not isinstance(target, str):
operations.extend(self.on_each(*target))
else:
raise ValueError(
'Gate was called with type different than Qid. Type: {}'.
Expand Down
20 changes: 20 additions & 0 deletions cirq/ops/identity_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import itertools
from typing import Any

import numpy as np
import pytest
Expand Down Expand Up @@ -46,6 +47,25 @@ def test_identity_on_each():
cirq.I.on_each('abc')


def test_identity_on_each_iter_second():

class Q(cirq.Qid):

@property
def dimension(self) -> int:
return 2

def _comparison_key(self) -> Any:
return 1

def __iter__(self):
# Having this method makes `isinstance(x, Iterable)` return True.
raise NotImplementedError()

q = Q()
assert cirq.I.on_each(q) == [cirq.I(q)]


def test_identity_on_each_only_single_qubit():
q0, q1 = cirq.LineQubit.range(2)
q0_3, q1_3 = q0.with_dimension(3), q1.with_dimension(3)
Expand Down