Skip to content

Commit 6ff3d66

Browse files
authored
[Bugfix] Re-allow non-integral line/grid qubits (#7110)
* Allow non-integral line/grid qubits * tests * fix numpy overflow * fix tests * fix tests on windows * Address PR comments
1 parent 2ad4136 commit 6ff3d66

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

cirq-core/cirq/devices/grid_qubit.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,6 @@ def __new__(cls, row: int, col: int, *, dimension: int) -> 'cirq.GridQid':
213213
dimension: The dimension of the qid's Hilbert space, i.e.
214214
the number of quantum levels.
215215
"""
216-
row = int(row)
217-
col = int(col)
218216
dimension = int(dimension)
219217
key = (row, col, dimension)
220218
inst = cls._cache.get(key)
@@ -224,7 +222,7 @@ def __new__(cls, row: int, col: int, *, dimension: int) -> 'cirq.GridQid':
224222
inst._row = row
225223
inst._col = col
226224
inst._dimension = dimension
227-
inst._hash = ((dimension - 2) * 1_000_003 + col) * 1_000_003 + row
225+
inst._hash = ((dimension - 2) * 1_000_003 + hash(col)) * 1_000_003 + hash(row)
228226
cls._cache[key] = inst
229227
return inst
230228

@@ -380,15 +378,13 @@ def __new__(cls, row: int, col: int) -> 'cirq.GridQubit':
380378
row: the row coordinate
381379
col: the column coordinate
382380
"""
383-
row = int(row)
384-
col = int(col)
385381
key = (row, col)
386382
inst = cls._cache.get(key)
387383
if inst is None:
388384
inst = super().__new__(cls)
389385
inst._row = row
390386
inst._col = col
391-
inst._hash = col * 1_000_003 + row
387+
inst._hash = hash(col) * 1_000_003 + hash(row)
392388
cls._cache[key] = inst
393389
return inst
394390

cirq-core/cirq/devices/grid_qubit_test.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -393,22 +393,30 @@ def test_complex():
393393
assert isinstance(complex(cirq.GridQubit(row=1, col=2)), complex)
394394

395395

396-
def test_numpy_index():
397-
np5, np6, np3 = [np.int64(i) for i in [5, 6, 3]]
396+
@pytest.mark.parametrize('dtype', (np.int8, np.int64, float, np.float64))
397+
def test_numpy_index(dtype):
398+
np5, np6, np3 = [dtype(i) for i in [5, 6, 3]]
398399
q = cirq.GridQubit(np5, np6)
399-
hash(q) # doesn't throw
400+
assert hash(q) == hash(cirq.GridQubit(5, 6))
400401
assert q.row == 5
401402
assert q.col == 6
402403
assert q.dimension == 2
403-
assert isinstance(q.row, int)
404-
assert isinstance(q.col, int)
405404
assert isinstance(q.dimension, int)
406405

407406
q = cirq.GridQid(np5, np6, dimension=np3)
408-
hash(q) # doesn't throw
407+
assert hash(q) == hash(cirq.GridQid(5, 6, dimension=3))
409408
assert q.row == 5
410409
assert q.col == 6
411410
assert q.dimension == 3
412-
assert isinstance(q.row, int)
413-
assert isinstance(q.col, int)
414411
assert isinstance(q.dimension, int)
412+
413+
414+
@pytest.mark.parametrize('dtype', (float, np.float64))
415+
def test_non_integer_index(dtype):
416+
# Not supported type-wise, but is used in practice, so behavior needs to be preserved.
417+
q = cirq.GridQubit(dtype(5.5), dtype(6.5))
418+
assert hash(q) == hash(cirq.GridQubit(5.5, 6.5))
419+
assert q.row == 5.5
420+
assert q.col == 6.5
421+
assert isinstance(q.row, dtype)
422+
assert isinstance(q.col, dtype)

cirq-core/cirq/devices/line_qubit.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ def __new__(cls, x: int, dimension: int) -> 'cirq.LineQid':
191191
dimension: The dimension of the qid's Hilbert space, i.e.
192192
the number of quantum levels.
193193
"""
194-
x = int(x)
195194
dimension = int(dimension)
196195
key = (x, dimension)
197196
inst = cls._cache.get(key)
@@ -200,7 +199,7 @@ def __new__(cls, x: int, dimension: int) -> 'cirq.LineQid':
200199
inst = super().__new__(cls)
201200
inst._x = x
202201
inst._dimension = dimension
203-
inst._hash = (dimension - 2) * 1_000_003 + x
202+
inst._hash = (dimension - 2) * 1_000_003 + hash(x)
204203
cls._cache[key] = inst
205204
return inst
206205

@@ -302,12 +301,11 @@ def __new__(cls, x: int) -> 'cirq.LineQubit':
302301
Args:
303302
x: The x coordinate.
304303
"""
305-
x = int(x)
306304
inst = cls._cache.get(x)
307305
if inst is None:
308306
inst = super().__new__(cls)
309307
inst._x = x
310-
inst._hash = x
308+
inst._hash = hash(x)
311309
cls._cache[x] = inst
312310
return inst
313311

cirq-core/cirq/devices/line_qubit_test.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,18 +287,26 @@ def test_numeric():
287287
assert isinstance(complex(cirq.LineQubit(x=5)), complex)
288288

289289

290-
def test_numpy_index():
291-
np5 = np.int64(5)
290+
@pytest.mark.parametrize('dtype', (np.int8, np.int64, float, np.float64))
291+
def test_numpy_index(dtype):
292+
np5 = dtype(5)
292293
q = cirq.LineQubit(np5)
293294
assert hash(q) == 5
294295
assert q.x == 5
295296
assert q.dimension == 2
296-
assert isinstance(q.x, int)
297297
assert isinstance(q.dimension, int)
298298

299-
q = cirq.LineQid(np5, np.int64(3))
299+
q = cirq.LineQid(np5, dtype(3))
300300
hash(q) # doesn't throw
301301
assert q.x == 5
302302
assert q.dimension == 3
303-
assert isinstance(q.x, int)
304303
assert isinstance(q.dimension, int)
304+
305+
306+
@pytest.mark.parametrize('dtype', (float, np.float64))
307+
def test_non_integer_index(dtype):
308+
# Not supported type-wise, but is used in practice, so behavior needs to be preserved.
309+
q = cirq.LineQubit(dtype(5.5))
310+
assert q.x == 5.5
311+
assert q.x == dtype(5.5)
312+
assert isinstance(q.x, dtype)

0 commit comments

Comments
 (0)