Skip to content

Bloq namespacing and display (__str__) updates.#1825

Open
mpharrigan wants to merge 4 commits intoquantumlib:mainfrom
mpharrigan:2026-03/sundry-bloq-strs
Open

Bloq namespacing and display (__str__) updates.#1825
mpharrigan wants to merge 4 commits intoquantumlib:mainfrom
mpharrigan:2026-03/sundry-bloq-strs

Conversation

@mpharrigan
Copy link
Collaborator

@mpharrigan mpharrigan commented Mar 9, 2026

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 Qualtran-L1: Objectstrings #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)

@mpharrigan mpharrigan marked this pull request as ready for review March 9, 2026 22:49
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a nice improvement for the library, enhancing bloq namespacing and making __str__ representations more consistent and Python-like. The changes are well-structured and include relevant tests. I have two suggestions for improvement. First, to maintain consistency across the basic rotation gates, I recommend adding a CRx bloq and a specialized get_ctrl_system for Rx, similar to what has been done for Ry in this PR. Second, the new wire_symbol implementation in the Power bloq could be made more robust to handle different types of wire symbols for bloq titles. I've provided a code suggestion for a cleaner and more resilient implementation.

Comment on lines +697 to +709
def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
if ctrl_spec != CtrlSpec():
return super().get_ctrl_system(ctrl_spec)

from qualtran.bloqs.mcmt.specialized_ctrl import get_ctrl_system_1bit_cv_from_bloqs

return get_ctrl_system_1bit_cv_from_bloqs(
bloq=self,
ctrl_spec=ctrl_spec,
current_ctrl_bit=None,
bloq_with_ctrl=CRy(self.angle, eps=self.eps),
ctrl_reg_name='ctrl',
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This is a great addition! For consistency with Ry and Rz, it would be good to also add a specialized get_ctrl_system for Rx. This would involve creating a CRx bloq, similar to how CRy was added in this PR. The _controlled_rp_circuit helper is already generic enough to support this.

You could add the following CRx class:

@frozen
class CRx(Bloq):
    r"""A controlled Rx rotation."""
    angle: SymbolicFloat
    eps: SymbolicFloat = 1e-11

    @cached_property
    def signature(self) -> 'Signature':
        return Signature.build(ctrl=1, q=1)

    def build_composite_bloq(
        self, bb: 'BloqBuilder', ctrl: 'Soquet', q: 'Soquet'
    ) -> Dict[str, 'SoquetT']:
        return _controlled_rp_circuit(
            bb, single_q_pow_cls=XPowGate, angle=self.angle, eps=self.eps, ctrl=ctrl, q=q
        )

    def __str__(self):
        return f'CRx({self.angle})'

And then add this method to the Rx class:

    def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
        if ctrl_spec != CtrlSpec():
            return super().get_ctrl_system(ctrl_spec)

        from qualtran.bloqs.mcmt.specialized_ctrl import get_ctrl_system_1bit_cv_from_bloqs

        return get_ctrl_system_1bit_cv_from_bloqs(
            bloq=self,
            ctrl_spec=ctrl_spec,
            current_ctrl_bit=None,
            bloq_with_ctrl=CRx(self.angle, eps=self.eps),
            ctrl_reg_name='ctrl',
        )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant