Skip to content

Commit 857d6b5

Browse files
authored
Bloq namespacing and display (__str__) updates. (#1825)
As part of #1824 , I want to be better about our convention for importing bloqs and `__str__` representations in the qualtran standard library (i.e. `qualtran.bloqs`). There's no additional restriction on custom, user-authored bloqs. These changes make sure: - bloqs are generally imported "one level up" from their file. e.g. `ControlledAddOrSubtract` is defined in `qualtran/bloqs/arithmetic/controlled_add_or_subtract.py` but it prefers to be imported from `qualtran.bloqs.arithmetic`. This is codified in the (existing) default `_pkg_(cls) -> str` classmethod. You can override it if you want something different. - The `__str__` representation looks like an object string (cc #1823). This PR removes some special symbols in favor of more Python-looking strings. note that this causes more bloq examples to be loadable (visible in the re-generated manifest)
1 parent 1c6e5e0 commit 857d6b5

21 files changed

Lines changed: 232 additions & 34 deletions

qualtran/bloqs/arithmetic/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
LinearDepthHalfLessThanEqual,
3030
SingleQubitCompare,
3131
)
32+
from qualtran.bloqs.arithmetic.controlled_add_or_subtract import ControlledAddOrSubtract
3233
from qualtran.bloqs.arithmetic.controlled_addition import CAdd
3334
from qualtran.bloqs.arithmetic.conversions import (
3435
SignedIntegerToTwosComplement,

qualtran/bloqs/arithmetic/addition.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,9 @@ def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT':
480480

481481
return counts
482482

483+
def __str__(self):
484+
return f'AddK(k={self.k})'
485+
483486

484487
@bloq_example(generalizer=ignore_split_join)
485488
def _add_k() -> AddK:

qualtran/bloqs/arithmetic/addition_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,8 @@ def test_controlled_add_from_add():
449449
assert ctrl == 0
450450
assert a == 5
451451
assert b == 15
452+
453+
454+
def test_add_k_str():
455+
add_k = AddK(QUInt(5), k=3)
456+
assert str(add_k) == "AddK(k=3)"

qualtran/bloqs/arithmetic/bitwise.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ def on_classical_vals(self, x: 'ClassicalValT') -> Dict[str, 'ClassicalValT']:
231231
x %= 2**self.dtype.bitsize
232232
return {'x': x}
233233

234+
def __str__(self):
235+
return f'BitwiseNot({self.dtype.num_bits})'
236+
234237

235238
@bloq_example
236239
def _bitwise_not() -> BitwiseNot:

qualtran/bloqs/arithmetic/bitwise_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,8 @@ def test_bitwisenot_classical_action(dtype, bitsize):
184184
else:
185185
valid_range = range(2**bitsize)
186186
qlt_testing.assert_consistent_classical_action(b, x=valid_range)
187+
188+
189+
def test_bitwise_not_str():
190+
bloq = BitwiseNot(QUInt(5))
191+
assert str(bloq) == "BitwiseNot(5)"

qualtran/bloqs/basic_gates/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from .identity import Identity
2929
from .on_each import OnEach
3030
from .power import Power
31-
from .rotation import CRz, CZPowGate, Rx, Ry, Rz, XPowGate, YPowGate, ZPowGate
31+
from .rotation import CRy, CRz, CZPowGate, Rx, Ry, Rz, XPowGate, YPowGate, ZPowGate
3232
from .s_gate import SGate
3333
from .su2_rotation import SU2RotationGate
3434
from .swap import CSwap, Swap, TwoBitCSwap, TwoBitSwap

qualtran/bloqs/basic_gates/global_phase.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def _add_ctrled(
118118
return bloq, _add_ctrled
119119

120120
def __str__(self) -> str:
121-
return f'GPhase({self.coefficient})'
121+
return f'GPhase({self.exponent})'
122122

123123

124124
@bloq_example

qualtran/bloqs/basic_gates/global_phase_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,8 @@ def test_t_complexity():
7777

7878
def test_global_phase(bloq_autotester):
7979
bloq_autotester(_global_phase)
80+
81+
82+
def test_global_phase_str():
83+
bloq = GlobalPhase(exponent=0.5)
84+
assert str(bloq) == "GPhase(0.5)"

qualtran/bloqs/basic_gates/on_each.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,4 @@ def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -
9696
return super().wire_symbol(reg, idx)
9797

9898
def __str__(self):
99-
return f'{self.gate}{self.n}'
99+
return f'{self.gate}(oneach={self.n})'

qualtran/bloqs/basic_gates/on_each_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,8 @@ def test_call_graph():
5959
c1 = x_on_each.bloq_counts(generalizer=ignore_split_join)
6060
c2 = x_on_each.decompose_bloq().bloq_counts(generalizer=ignore_split_join)
6161
assert c1 == c2
62+
63+
64+
def test_on_each_str():
65+
on_each = OnEach(10, XGate())
66+
assert str(on_each) == "X(oneach=10)"

0 commit comments

Comments
 (0)