Skip to content

Commit 1bec6b5

Browse files
authored
Improve repr of _BaseAncillaQid classes with prefix (#6555)
* Add failing repr tests for CleanQubit and BorrowableQubit with prefix * Quote prefix string in repr of `_BaseAncillaQid` classes
1 parent 436c937 commit 1bec6b5

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

cirq-core/cirq/ops/qubit_manager.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def with_dimension(self, dimension: int) -> '_BaseAncillaQid':
5353

5454
def __repr__(self) -> str:
5555
dim_str = f', dim={self.dim}' if self.dim != 2 else ''
56-
prefix_str = f', prefix={self.prefix}' if self.prefix != '' else ''
56+
prefix_str = f', prefix={self.prefix!r}' if self.prefix != '' else ''
5757
return f"cirq.ops.{type(self).__name__}({self.id}{dim_str}{prefix_str})"
5858

5959

cirq-core/cirq/ops/qubit_manager_test.py

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def test_clean_qubits():
3131
assert str(q) == '_c(2) (d=3)'
3232
assert repr(q) == 'cirq.ops.CleanQubit(2, dim=3)'
3333

34+
q = cqi.CleanQubit(3, dim=4, prefix="a")
35+
assert str(q) == 'a_c(3) (d=4)'
36+
assert repr(q) == "cirq.ops.CleanQubit(3, dim=4, prefix='a')"
37+
3438
assert cqi.CleanQubit(1) < cqi.CleanQubit(2)
3539

3640

@@ -58,6 +62,10 @@ def test_borrow_qubits():
5862
assert str(q) == '_b(20) (d=4)'
5963
assert repr(q) == 'cirq.ops.BorrowableQubit(20, dim=4)'
6064

65+
q = cqi.BorrowableQubit(30, dim=4, prefix="a")
66+
assert str(q) == 'a_b(30) (d=4)'
67+
assert repr(q) == "cirq.ops.BorrowableQubit(30, dim=4, prefix='a')"
68+
6169
assert cqi.BorrowableQubit(1) < cqi.BorrowableQubit(2)
6270

6371

0 commit comments

Comments
 (0)