Skip to content

Commit

Permalink
Fix IdentityGate.on_each failing when given a qubit that is iterable (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc committed Jun 7, 2020
1 parent 03a7dc8 commit 2b8b085
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
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

0 comments on commit 2b8b085

Please sign in to comment.