Skip to content

Commit 0f4deee

Browse files
Remove pretty_name part 3 (#1402)
* Remove pretty_name part 3 Change the default implementation of Bloq.pretty_name to go via str. Removing pretty_name implementations and moving them to str or wire_symbols as necessary. This change removes all references to pretty_name * Remove pretty_name from notebooks. * Change adjoint string --------- Co-authored-by: Matthew Harrigan <[email protected]>
1 parent 462ec9f commit 0f4deee

28 files changed

+145
-104
lines changed

qualtran/_infra/adjoint.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,9 @@ def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT':
167167
counts[bloq.adjoint()] += n
168168
return counts
169169

170-
def pretty_name(self) -> str:
171-
"""The subbloq's pretty_name with a dagger."""
172-
return self.subbloq.pretty_name() + '†'
173-
174170
def __str__(self) -> str:
175171
"""Delegate to subbloq's `__str__` method."""
176-
return f'Adjoint(subbloq={str(self.subbloq)})'
172+
return f'{str(self.subbloq)}'
177173

178174
def wire_symbol(
179175
self, reg: Optional['Register'], idx: Tuple[int, ...] = tuple()

qualtran/_infra/adjoint_test.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ def test_serial_combo_adjoint():
4848
assert (
4949
TestSerialCombo().adjoint().decompose_bloq().debug_text()
5050
== """\
51-
Adjoint(subbloq=TestAtom('atom2'))<0>
51+
TestAtom('atom2')<0>
5252
LeftDangle.reg -> q
53-
q -> Adjoint(subbloq=TestAtom('atom1'))<1>.q
53+
q -> TestAtom('atom1')<1>.q
5454
--------------------
55-
Adjoint(subbloq=TestAtom('atom1'))<1>
56-
Adjoint(subbloq=TestAtom('atom2'))<0>.q -> q
57-
q -> Adjoint(subbloq=TestAtom('atom0'))<2>.q
55+
TestAtom('atom1')<1>
56+
TestAtom('atom2')<0>.q -> q
57+
q -> TestAtom('atom0')<2>.q
5858
--------------------
59-
Adjoint(subbloq=TestAtom('atom0'))<2>
60-
Adjoint(subbloq=TestAtom('atom1'))<1>.q -> q
59+
TestAtom('atom0')<2>
60+
TestAtom('atom1')<1>.q -> q
6161
q -> RightDangle.reg"""
6262
)
6363

@@ -72,16 +72,16 @@ def test_cbloq_adjoint_function():
7272
assert (
7373
adj_cbloq.debug_text()
7474
== """\
75-
Adjoint(subbloq=TestAtom('atom2'))<0>
75+
TestAtom('atom2')<0>
7676
LeftDangle.reg -> q
77-
q -> Adjoint(subbloq=TestAtom('atom1'))<1>.q
77+
q -> TestAtom('atom1')<1>.q
7878
--------------------
79-
Adjoint(subbloq=TestAtom('atom1'))<1>
80-
Adjoint(subbloq=TestAtom('atom2'))<0>.q -> q
81-
q -> Adjoint(subbloq=TestAtom('atom0'))<2>.q
79+
TestAtom('atom1')<1>
80+
TestAtom('atom2')<0>.q -> q
81+
q -> TestAtom('atom0')<2>.q
8282
--------------------
83-
Adjoint(subbloq=TestAtom('atom0'))<2>
84-
Adjoint(subbloq=TestAtom('atom1'))<1>.q -> q
83+
TestAtom('atom0')<2>
84+
TestAtom('atom1')<1>.q -> q
8585
q -> RightDangle.reg"""
8686
)
8787

@@ -139,7 +139,7 @@ def test_names():
139139
assert cast(Text, atom.wire_symbol(reg=None)).text == "TestAtom"
140140

141141
adj_atom = Adjoint(atom)
142-
assert str(adj_atom) == "Adjoint(subbloq=TestAtom)"
142+
assert str(adj_atom) == "TestAtom"
143143
assert cast(Text, adj_atom.wire_symbol(reg=None)).text == "TestAtom†"
144144

145145

qualtran/_infra/bloq.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@ def signature(self) -> 'Signature':
110110
about this bloq.
111111
"""
112112

113-
def pretty_name(self) -> str:
114-
return str(self)
115-
116113
def build_composite_bloq(self, bb: 'BloqBuilder', **soqs: 'SoquetT') -> Dict[str, 'SoquetT']:
117114
"""Override this method to define a Bloq in terms of its constituent parts.
118115
@@ -537,7 +534,7 @@ def wire_symbol(
537534
from qualtran.drawing import directional_text_box, Text
538535

539536
if reg is None:
540-
name = self.pretty_name()
537+
name = str(self)
541538
if len(name) <= 10:
542539
return Text(name)
543540
return Text(name[:8] + '..')

qualtran/_infra/bloq_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_bloq():
2828
ctrl, trg = tb.signature
2929
assert ctrl.bitsize == 1
3030
assert ctrl.side == Side.THRU
31-
assert tb.pretty_name() == 'TestTwoBitOp'
31+
assert str(tb) == 'TestTwoBitOp'
3232

3333
with pytest.raises(DecomposeTypeError):
3434
tb.decompose_bloq()

qualtran/bloqs/basic_gates/rotation_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def test_as_cirq_op():
103103
)
104104

105105

106-
def test_pretty_name():
106+
def test_str():
107107
assert str(ZPowGate()) == "Z**1.0"
108108
assert str(XPowGate()) == "X**1.0"
109109
assert str(YPowGate()) == "Y**1.0"

qualtran/bloqs/basic_gates/z_basis_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def test_zero_state_effect(bit):
163163

164164
def test_int_state_manual():
165165
k = IntState(255, bitsize=8)
166-
assert k.pretty_name() == '|255>'
166+
assert str(k) == '|255>'
167167
(val,) = k.call_classically()
168168
assert val == 255
169169

@@ -177,7 +177,7 @@ def test_int_state_manual():
177177

178178
def test_int_effect_manual():
179179
k = IntEffect(255, bitsize=8)
180-
assert k.pretty_name() == '<255|'
180+
assert str(k) == '<255|'
181181
ret = k.call_classically(val=255)
182182
assert ret == ()
183183

qualtran/bloqs/block_encoding/chebyshev_polynomial.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def signature(self) -> Signature:
9393
resource=QAny(self.resource_bitsize), # if resource_bitsize is 0, not present
9494
)
9595

96-
def pretty_name(self) -> str:
96+
def __str__(self) -> str:
9797
return f"B[T_{self.order}({self.block_encoding})]"
9898

9999
@property

qualtran/bloqs/chemistry/pbc/first_quantization/projectile/prepare_uv.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
r"""PREPARE the potential energy terms of the first quantized chemistry Hamiltonian with projectile.
1515
"""
1616
from functools import cached_property
17-
from typing import Dict, TYPE_CHECKING
17+
from typing import Dict, Optional, Tuple, TYPE_CHECKING
1818

1919
from attrs import frozen
2020

@@ -23,6 +23,7 @@
2323
from qualtran.bloqs.chemistry.pbc.first_quantization.projectile.prepare_nu import (
2424
PrepareNuStateWithProj,
2525
)
26+
from qualtran.drawing import Text, WireSymbol
2627

2728
if TYPE_CHECKING:
2829
from qualtran.resource_counting import BloqCountDictT, SympySymbolAllocator
@@ -75,8 +76,10 @@ def signature(self) -> Signature:
7576
]
7677
)
7778

78-
def pretty_name(self) -> str:
79-
return r'PREP UV'
79+
def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
80+
if reg is None:
81+
return Text("PREP UV")
82+
return super().wire_symbol(reg, idx)
8083

8184
def build_composite_bloq(
8285
self, bb: BloqBuilder, mu: SoquetT, nu: SoquetT, m: SoquetT, l: SoquetT, flag_nu: SoquetT

qualtran/bloqs/chemistry/pbc/first_quantization/projectile/select_and_prepare.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ def signature(self) -> Signature:
103103
def adjoint(self) -> 'Bloq':
104104
return evolve(self, is_adjoint=not self.is_adjoint)
105105

106-
def pretty_name(self) -> str:
107-
return 'PREP TUV'
106+
def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
107+
if reg is None:
108+
return Text("PREP TUV")
109+
return super().wire_symbol(reg, idx)
108110

109111
def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT':
110112
if self.is_adjoint:
@@ -140,7 +142,7 @@ def signature(self) -> Signature:
140142

141143
def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
142144
if reg is None:
143-
return Text(self.pretty_name())
145+
return Text('MultiSwap')
144146
if reg.name == 'sel':
145147
return TextBox('In')
146148
elif reg.name == 'targets':
@@ -282,8 +284,10 @@ def junk_registers(self) -> Tuple[Register, ...]:
282284
Register('flags', QBit(), shape=(4,)),
283285
)
284286

285-
def pretty_name(self) -> str:
286-
return r'PREP'
287+
def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
288+
if reg is None:
289+
return Text("PREP")
290+
return super().wire_symbol(reg, idx)
287291

288292
def build_composite_bloq(
289293
self,
@@ -461,8 +465,10 @@ def signature(self) -> Signature:
461465
[*self.control_registers, *self.selection_registers, *self.target_registers]
462466
)
463467

464-
def pretty_name(self) -> str:
465-
return r'SELECT'
468+
def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
469+
if reg is None:
470+
return Text("SELECT")
471+
return super().wire_symbol(reg, idx)
466472

467473
def build_composite_bloq(
468474
self,

qualtran/bloqs/chemistry/pbc/first_quantization/projectile/select_t.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
# limitations under the License.
1414
r"""Bloqs for SELECT T for the first quantized chemistry Hamiltonian with a quantum projectile."""
1515
from functools import cached_property
16-
from typing import TYPE_CHECKING
16+
from typing import Optional, Tuple, TYPE_CHECKING
1717

1818
from attrs import frozen
1919

2020
from qualtran import Bloq, bloq_example, QAny, QBit, Register, Signature
2121
from qualtran.bloqs.basic_gates import Toffoli
22+
from qualtran.drawing import Text, WireSymbol
2223

2324
if TYPE_CHECKING:
2425
from qualtran.resource_counting import BloqCountDictT, SympySymbolAllocator
@@ -69,8 +70,10 @@ def signature(self) -> Signature:
6970
]
7071
)
7172

72-
def pretty_name(self) -> str:
73-
return r'SEL T'
73+
def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
74+
if reg is None:
75+
return Text("SEL T")
76+
return super().wire_symbol(reg, idx)
7477

7578
def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT':
7679
# Modification of the SEL T costs from the first quantized bloq with n_p replace with n_n.

qualtran/bloqs/chemistry/pbc/first_quantization/projectile/select_uv.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
r"""Bloqs for SELECT for the U and V parts of the first quantized chemistry Hamiltonian."""
1515
from collections import Counter
1616
from functools import cached_property
17-
from typing import TYPE_CHECKING
17+
from typing import Optional, Tuple, TYPE_CHECKING
1818

1919
from attrs import frozen
2020

2121
from qualtran import Bloq, bloq_example, QAny, QBit, QInt, Register, Signature
2222
from qualtran.bloqs.arithmetic import Add, SignedIntegerToTwosComplement
2323
from qualtran.bloqs.basic_gates import Toffoli
2424
from qualtran.bloqs.chemistry.pbc.first_quantization.select_uv import ApplyNuclearPhase
25+
from qualtran.drawing import Text, WireSymbol
2526

2627
if TYPE_CHECKING:
2728
from qualtran.resource_counting import BloqCountDictT, BloqCountT, SympySymbolAllocator
@@ -75,8 +76,10 @@ def signature(self) -> Signature:
7576
]
7677
)
7778

78-
def pretty_name(self) -> str:
79-
return r'SEL UV'
79+
def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
80+
if reg is None:
81+
return Text('SEL UV')
82+
return super().wire_symbol(reg, idx)
8083

8184
def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT':
8285
cost = Counter['Bloq']()

qualtran/bloqs/chemistry/pbc/first_quantization/select_and_prepare.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ class PrepareTUVSuperpositions(Bloq):
7373
def signature(self) -> Signature:
7474
return Signature.build(tuv=1, uv=1)
7575

76-
def pretty_name(self) -> str:
77-
return 'PREP TUV'
76+
def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
77+
if reg is None:
78+
return Text("PREP TUV")
79+
return super().wire_symbol(reg, idx)
7880

7981
def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT':
8082
n_eta_zeta = (self.eta + 2 * self.lambda_zeta - 1).bit_length()
@@ -161,7 +163,7 @@ def _reshape_reg(
161163

162164
def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
163165
if reg is None:
164-
return Text(self.pretty_name())
166+
return Text('MultiSwap')
165167
if reg.name == 'sel':
166168
return TextBox('In')
167169
elif reg.name == 'targets':
@@ -170,9 +172,6 @@ def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -
170172
return TextBox('×(y)')
171173
raise ValueError(f'Unknown name: {reg.name}')
172174

173-
def pretty_name(self) -> str:
174-
return 'MultiSwap'
175-
176175
def build_composite_bloq(
177176
self, bb: BloqBuilder, sel: SoquetT, targets: SoquetT, junk: SoquetT
178177
) -> Dict[str, 'SoquetT']:
@@ -282,8 +281,10 @@ def l1_norm_coeffs(self) -> SymbolicFloat:
282281
)
283282
return self.sum_of_l1_coeffs
284283

285-
def pretty_name(self) -> str:
286-
return r'PREP'
284+
def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
285+
if reg is None:
286+
return Text("PREP")
287+
return super().wire_symbol(reg, idx)
287288

288289
def build_composite_bloq(
289290
self,
@@ -447,8 +448,10 @@ def signature(self) -> Signature:
447448
[*self.control_registers, *self.selection_registers, *self.target_registers]
448449
)
449450

450-
def pretty_name(self) -> str:
451-
return r'SELECT'
451+
def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
452+
if reg is None:
453+
return Text("SELECT")
454+
return super().wire_symbol(reg, idx)
452455

453456
def build_composite_bloq(
454457
self,

qualtran/bloqs/chemistry/pbc/first_quantization/select_t.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
# limitations under the License.
1414
r"""Bloqs for SELECT T for the first quantized chemistry Hamiltonian."""
1515
from functools import cached_property
16-
from typing import TYPE_CHECKING
16+
from typing import Optional, Tuple, TYPE_CHECKING
1717

1818
from attrs import frozen
1919

2020
from qualtran import Bloq, bloq_example, BloqDocSpec, QAny, QBit, Register, Signature
2121
from qualtran.bloqs.basic_gates import Toffoli
22+
from qualtran.drawing import Text, WireSymbol
2223

2324
if TYPE_CHECKING:
2425
from qualtran.resource_counting import BloqCountDictT, SympySymbolAllocator
@@ -57,8 +58,10 @@ def signature(self) -> Signature:
5758
]
5859
)
5960

60-
def pretty_name(self) -> str:
61-
return r'SEL T'
61+
def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
62+
if reg is None:
63+
return Text("SEL T")
64+
return super().wire_symbol(reg, idx)
6265

6366
def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT':
6467
# Cost is $5(n_{p} - 1) + 2$ which comes from copying each $w$ component of $p$

qualtran/bloqs/chemistry/pbc/first_quantization/select_uv.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
# limitations under the License.
1414
r"""Bloqs for SELECT for the U and V parts of the first quantized chemistry Hamiltonian."""
1515
from functools import cached_property
16-
from typing import TYPE_CHECKING
16+
from typing import Optional, Tuple, TYPE_CHECKING
1717

1818
from attrs import frozen
1919

2020
from qualtran import Bloq, bloq_example, BloqDocSpec, QAny, QBit, QInt, Register, Signature
2121
from qualtran.bloqs.arithmetic import Add, SignedIntegerToTwosComplement
2222
from qualtran.bloqs.basic_gates import Toffoli
23+
from qualtran.drawing import Text, WireSymbol
2324

2425
if TYPE_CHECKING:
2526
from qualtran.resource_counting import BloqCountDictT, SympySymbolAllocator
@@ -56,8 +57,10 @@ def signature(self) -> Signature:
5657
]
5758
)
5859

59-
def pretty_name(self) -> str:
60-
return r'-e^(-k_ν⋅R_l)'
60+
def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
61+
if reg is None:
62+
return Text(r'-e^(-k_ν⋅R_l)')
63+
return super().wire_symbol(reg, idx)
6164

6265
def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT':
6366
n_p = self.num_bits_p
@@ -111,8 +114,10 @@ def signature(self) -> Signature:
111114
]
112115
)
113116

114-
def pretty_name(self) -> str:
115-
return r'SEL UV'
117+
def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
118+
if reg is None:
119+
return Text("SEL UV")
120+
return super().wire_symbol(reg, idx)
116121

117122
def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT':
118123
cost_tc = (SignedIntegerToTwosComplement(self.num_bits_p), 6)

0 commit comments

Comments
 (0)