Skip to content

Commit 2a57894

Browse files
authored
Access attributes directly in GridQubit instead of using properties (#6075)
Review: @viathor
1 parent 03405d1 commit 2a57894

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

cirq-core/cirq/devices/grid_qubit.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ def col(self) -> int:
4545
return self._col
4646

4747
def with_dimension(self, dimension: int) -> 'GridQid':
48-
return GridQid(self.row, self.col, dimension=dimension)
48+
return GridQid(self._row, self._col, dimension=dimension)
4949

5050
def is_adjacent(self, other: 'cirq.Qid') -> bool:
5151
"""Determines if two qubits are adjacent qubits."""
5252
return (
5353
isinstance(other, GridQubit)
54-
and abs(self.row - other.row) + abs(self.col - other.col) == 1
54+
and abs(self._row - other._row) + abs(self._col - other._col) == 1
5555
)
5656

5757
def neighbors(self, qids: Optional[Iterable[ops.Qid]] = None) -> Set['_BaseGridQid']:
@@ -71,7 +71,7 @@ def _with_row_col(self, row: int, col: int) -> Self:
7171
"""Returns a qid with the same type but a different coordinate."""
7272

7373
def __complex__(self) -> complex:
74-
return self.col + 1j * self.row
74+
return self._col + 1j * self._row
7575

7676
def __add__(self, other: Union[Tuple[int, int], Self]) -> Self:
7777
if isinstance(other, _BaseGridQid):
@@ -80,7 +80,7 @@ def __add__(self, other: Union[Tuple[int, int], Self]) -> Self:
8080
"Can only add GridQids with identical dimension. "
8181
f"Got {self.dimension} and {other.dimension}"
8282
)
83-
return self._with_row_col(row=self.row + other.row, col=self.col + other.col)
83+
return self._with_row_col(row=self._row + other._row, col=self._col + other._col)
8484
if not (
8585
isinstance(other, (tuple, np.ndarray))
8686
and len(other) == 2
@@ -90,7 +90,7 @@ def __add__(self, other: Union[Tuple[int, int], Self]) -> Self:
9090
'Can only add integer tuples of length 2 to '
9191
f'{type(self).__name__}. Instead was {other}'
9292
)
93-
return self._with_row_col(row=self.row + other[0], col=self.col + other[1])
93+
return self._with_row_col(row=self._row + other[0], col=self._col + other[1])
9494

9595
def __sub__(self, other: Union[Tuple[int, int], Self]) -> Self:
9696
if isinstance(other, _BaseGridQid):
@@ -99,7 +99,7 @@ def __sub__(self, other: Union[Tuple[int, int], Self]) -> Self:
9999
"Can only subtract GridQids with identical dimension. "
100100
f"Got {self.dimension} and {other.dimension}"
101101
)
102-
return self._with_row_col(row=self.row - other.row, col=self.col - other.col)
102+
return self._with_row_col(row=self._row - other._row, col=self._col - other._col)
103103
if not (
104104
isinstance(other, (tuple, np.ndarray))
105105
and len(other) == 2
@@ -109,7 +109,7 @@ def __sub__(self, other: Union[Tuple[int, int], Self]) -> Self:
109109
"Can only subtract integer tuples of length 2 to "
110110
f"{type(self).__name__}. Instead was {other}"
111111
)
112-
return self._with_row_col(row=self.row - other[0], col=self.col - other[1])
112+
return self._with_row_col(row=self._row - other[0], col=self._col - other[1])
113113

114114
def __radd__(self, other: Tuple[int, int]) -> Self:
115115
return self + other
@@ -118,7 +118,7 @@ def __rsub__(self, other: Tuple[int, int]) -> Self:
118118
return -self + other
119119

120120
def __neg__(self) -> Self:
121-
return self._with_row_col(row=-self.row, col=-self.col)
121+
return self._with_row_col(row=-self._row, col=-self._col)
122122

123123

124124
class GridQid(_BaseGridQid):
@@ -255,16 +255,16 @@ def from_diagram(diagram: str, dimension: int) -> List['GridQid']:
255255
return [GridQid(*c, dimension=dimension) for c in coords]
256256

257257
def __repr__(self) -> str:
258-
return f"cirq.GridQid({self.row}, {self.col}, dimension={self.dimension})"
258+
return f"cirq.GridQid({self._row}, {self._col}, dimension={self.dimension})"
259259

260260
def __str__(self) -> str:
261-
return f"q({self.row}, {self.col}) (d={self.dimension})"
261+
return f"q({self._row}, {self._col}) (d={self.dimension})"
262262

263263
def _circuit_diagram_info_(
264264
self, args: 'cirq.CircuitDiagramInfoArgs'
265265
) -> 'cirq.CircuitDiagramInfo':
266266
return protocols.CircuitDiagramInfo(
267-
wire_symbols=(f"({self.row}, {self.col}) (d={self.dimension})",)
267+
wire_symbols=(f"({self._row}, {self._col}) (d={self.dimension})",)
268268
)
269269

270270
def _json_dict_(self) -> Dict[str, Any]:
@@ -305,13 +305,13 @@ def __hash__(self) -> int:
305305
def __eq__(self, other):
306306
# Explicitly implemented for performance (vs delegating to Qid).
307307
if isinstance(other, GridQubit):
308-
return self.row == other.row and self.col == other.col
308+
return self._row == other._row and self._col == other._col
309309
return NotImplemented
310310

311311
def __ne__(self, other):
312312
# Explicitly implemented for performance (vs delegating to Qid).
313313
if isinstance(other, GridQubit):
314-
return self.row != other.row or self.col != other.col
314+
return self._row != other._row or self._col != other._col
315315
return NotImplemented
316316

317317
@property
@@ -412,15 +412,15 @@ def from_diagram(diagram: str) -> List['GridQubit']:
412412
return [GridQubit(*c) for c in coords]
413413

414414
def __repr__(self) -> str:
415-
return f"cirq.GridQubit({self.row}, {self.col})"
415+
return f"cirq.GridQubit({self._row}, {self._col})"
416416

417417
def __str__(self) -> str:
418-
return f"q({self.row}, {self.col})"
418+
return f"q({self._row}, {self._col})"
419419

420420
def _circuit_diagram_info_(
421421
self, args: 'cirq.CircuitDiagramInfoArgs'
422422
) -> 'cirq.CircuitDiagramInfo':
423-
return protocols.CircuitDiagramInfo(wire_symbols=(f"({self.row}, {self.col})",))
423+
return protocols.CircuitDiagramInfo(wire_symbols=(f"({self._row}, {self._col})",))
424424

425425
def _json_dict_(self) -> Dict[str, Any]:
426426
return protocols.obj_to_dict_helper(self, ['row', 'col'])

0 commit comments

Comments
 (0)