Skip to content

Commit 857e202

Browse files
authored
Bugfix in comparison_key and nicer with_dimension of _BaseAncillaQid (#6554)
* Bugfix in comparison_key and nicer with_dimension of _BaseAncillaQid * Fix pylint
1 parent 9ab6601 commit 857e202

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

cirq-core/cirq/ops/qubit_manager.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import abc
1616
import dataclasses
17-
from typing import Iterable, List, TYPE_CHECKING
17+
from typing import Iterable, List, TYPE_CHECKING, Tuple
1818
from cirq.ops import raw_types
1919

2020
if TYPE_CHECKING:
@@ -41,13 +41,16 @@ class _BaseAncillaQid(raw_types.Qid):
4141
dim: int = 2
4242
prefix: str = ''
4343

44-
def _comparison_key(self) -> int:
45-
return self.id
44+
def _comparison_key(self) -> Tuple[str, int]:
45+
return self.prefix, self.id
4646

4747
@property
4848
def dimension(self) -> int:
4949
return self.dim
5050

51+
def with_dimension(self, dimension: int) -> '_BaseAncillaQid':
52+
return dataclasses.replace(self, dim=dimension)
53+
5154
def __repr__(self) -> str:
5255
dim_str = f', dim={self.dim}' if self.dim != 2 else ''
5356
prefix_str = f', prefix={self.prefix}' if self.prefix != '' else ''

cirq-core/cirq/ops/qubit_manager_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,23 @@ def test_clean_qubits():
2727
q = cqi.CleanQubit(2, dim=3)
2828
assert q.id == 2
2929
assert q.dimension == 3
30+
assert q.with_dimension(4) == cqi.CleanQubit(2, dim=4)
3031
assert str(q) == '_c(2) (d=3)'
3132
assert repr(q) == 'cirq.ops.CleanQubit(2, dim=3)'
3233

3334
assert cqi.CleanQubit(1) < cqi.CleanQubit(2)
3435

3536

37+
def test_ancilla_qubits_prefix():
38+
assert cqi.CleanQubit(1, prefix="1") != cqi.CleanQubit(1, prefix="2")
39+
assert cqi.CleanQubit(1, prefix="1") < cqi.CleanQubit(1, prefix="2")
40+
assert cqi.CleanQubit(1, prefix="1") < cqi.CleanQubit(2, prefix="1")
41+
assert cqi.BorrowableQubit(1, prefix="1") != cqi.BorrowableQubit(1, prefix="2")
42+
assert cqi.BorrowableQubit(1, prefix="1") < cqi.BorrowableQubit(1, prefix="2")
43+
assert cqi.BorrowableQubit(1, prefix="1") < cqi.BorrowableQubit(2, prefix="1")
44+
assert cqi.CleanQubit(1, prefix="1") != cqi.BorrowableQubit(1, prefix="1")
45+
46+
3647
def test_borrow_qubits():
3748
q = cqi.BorrowableQubit(10)
3849
assert q.id == 10
@@ -43,6 +54,7 @@ def test_borrow_qubits():
4354
q = cqi.BorrowableQubit(20, dim=4)
4455
assert q.id == 20
4556
assert q.dimension == 4
57+
assert q.with_dimension(10) == cqi.BorrowableQubit(20, dim=10)
4658
assert str(q) == '_b(20) (d=4)'
4759
assert repr(q) == 'cirq.ops.BorrowableQubit(20, dim=4)'
4860

0 commit comments

Comments
 (0)