quantumlib / Qualtran

Qᴜᴀʟᴛʀᴀɴ is a Python library for expressing and analyzing Fault Tolerant Quantum algorithms.
https://qualtran.readthedocs.io/en/latest/
Apache License 2.0
147 stars 39 forks source link

Appropriate way to add on/off single controlled bloq? #883

Closed fdmalone closed 1 month ago

fdmalone commented 4 months ago

Is there a simpler way to add a singly-controlled bloq:

ctrl_spec = CtrlSpec(cvs=[0])
(ctrl,), q = bb.add(ZGate(), ctrl=(ctrl,), q=q)

seems a little awkward.

Is

ctrl_spec = CtrlSpec(QBit(), cvs=0b0)
ctrl, q = bb.add(ZGate(), ctrl=ctrl, q=q)

preferred?

mpharrigan commented 4 months ago

sorry, I don't fully understand your code snippets. Where are you using the ctrl_spec in either of them?

ctrl, q = bb.add(ZGate().controlled(), ctrl=ctrl, q=q)

would be how I would do it

fdmalone commented 4 months ago

I want an off-control.

fdmalone commented 4 months ago

Whoops, should have been:

ctrl_spec = CtrlSpec(cvs=[0])
(ctrl,), q = bb.add(ZGate().controlled(ctrl_spec), ctrl=(ctrl,), q=q)

and

ctrl_spec = CtrlSpec(QBit(), cvs=0b0)
ctrl, q = bb.add(ZGate().controlled(ctrl_spec), ctrl=ctrl, q=q)
mpharrigan commented 4 months ago

So you want a one-bit off-control. In the first one, you have to wrap your ctrl soquet in a lengh-1-list, which seems wrong and annoying. In the second one, you have to specify QBit(). You'd prefer something where you can just put cvs=0? Is this a correct summary of the issue?

fdmalone commented 4 months ago

Right the second way seems a bit verbose. Very low priority though, just ran into it in #882

mpharrigan commented 4 months ago

Does it not work if you do CtrlSpec(cvs=0)? the default data type should be QBit(), no?