Skip to content

Support numpy integer arrays as grid offsets #3130

@viathor

Description

@viathor

It turns out that computations of grid coordinates are done more conveniently using numpy integer arrays than 2-tuples of python ints. However, when offsets thus computed are used with GridQubit exception is raised. For example:

In [2]: q = cirq.GridQubit(10, 20)

In [3]: d = np.array([2, 3], dtype=int)

In [4]: q + d
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-2e9dafea4fc1> in <module>
----> 1 q + d

~/src/Cirq/cirq/devices/grid_qubit.py in __add__(self, other)
     81         if not (isinstance(other, tuple) and len(other) == 2 and
     82                 all(isinstance(x, int) for x in other)):
---> 83             raise TypeError('Can only add integer tuples of length 2 to '
     84                             f'{type(self).__name__}. Instead was {other}')
     85         return self._with_row_col(row=self.row + other[0],

TypeError: Can only add integer tuples of length 2 to GridQubit. Instead was [2 3]

In [5]: q + tuple(int(v) for v in d)
Out[5]: cirq.GridQubit(12, 23)

It'd be nice to be able to say q + d instead of having to write q + tuple(int(v) for v in d) or similar.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions