Skip to content

Commit 575ae93

Browse files
authored
Make grid_qubits addable (#2681)
- Fixes #2637
1 parent 58741a1 commit 575ae93

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

cirq/devices/grid_qubit.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ def _json_dict_(self):
163163
return protocols.obj_to_dict_helper(self, ['row', 'col'])
164164

165165
def __add__(self, other: Tuple[int, int]) -> 'GridQubit':
166+
if isinstance(other, GridQubit):
167+
return GridQubit(row=self.row + other.row, col=self.col + other.col)
166168
if not (isinstance(other, tuple) and len(other) == 2 and
167169
all(isinstance(x, int) for x in other)):
168170
raise TypeError(
@@ -171,6 +173,8 @@ def __add__(self, other: Tuple[int, int]) -> 'GridQubit':
171173
return GridQubit(row=self.row + other[0], col=self.col + other[1])
172174

173175
def __sub__(self, other: Tuple[int, int]) -> 'GridQubit':
176+
if isinstance(other, GridQubit):
177+
return GridQubit(row=self.row - other.row, col=self.col - other.col)
174178
if not (isinstance(other, tuple) and len(other) == 2 and
175179
all(isinstance(x, int) for x in other)):
176180
raise TypeError(

cirq/devices/grid_qubit_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ def test_grid_qubit_add_subtract():
154154
assert (2, 5) + cirq.GridQubit(1, 2) == cirq.GridQubit(3, 7)
155155
assert (2, 5) - cirq.GridQubit(1, 2) == cirq.GridQubit(1, 3)
156156

157+
assert cirq.GridQubit(1, 2) + cirq.GridQubit(3, 5) == cirq.GridQubit(4, 7)
158+
assert cirq.GridQubit(3, 5) - cirq.GridQubit(2, 1) == cirq.GridQubit(1, 4)
159+
assert cirq.GridQubit(1, -2) + cirq.GridQubit(3, 5) == cirq.GridQubit(4, 3)
160+
157161

158162
def test_grid_qubit_neg():
159163
assert -cirq.GridQubit(1, 2) == cirq.GridQubit(-1, -2)

0 commit comments

Comments
 (0)