Qualtran
Qualtran copied to clipboard
Should not override `.controlled` for custom controlled implementations
Bloq authors should always override get_ctrl_system and not controlled. I think only the old cirq-ft bloqs ( QubitizationWalkOperator and related) override controlled directly, which must be upgraded.
from qualtran import Bloq
from qualtran.bloqs.for_testing.random_select_and_prepare import RandomPrepareOracle
from qualtran.bloqs.reflection_using_prepare import ReflectionUsingPrepare
from qualtran.drawing import show_bloq
prepare = RandomPrepareOracle.create(3, random_state=42)
R = ReflectionUsingPrepare(prepare)
class Wrapped(Bloq):
@property
def signature(self) -> 'Signature':
return R.signature
def build_composite_bloq(self, bb: 'BloqBuilder', **soqs: 'SoquetT') -> dict[str, 'SoquetT']:
return bb.add_d(R, **soqs)
cb_direct = R.controlled().decompose_bloq()
cb_wrapped = Wrapped().controlled().decompose_bloq().flatten_once(lambda _: True)
show_bloq(cb_direct)
show_bloq(cb_wrapped)
More detailed notebook with above code: https://github.com/anurudhp/Qualtran/blob/2024-04-issue-controlled/controlled_issue.ipynb
We should also remove this: https://github.com/quantumlib/Qualtran/blob/c78dd0b5639b7fb4f471b47d59ff13042ca044c4/qualtran/_infra/gate_with_registers.py#L446-L448
related issue #649