-
Notifications
You must be signed in to change notification settings - Fork 76
Strict BloqAsCirqGate
and CirqGateAsBloq
#1603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
cbloq = bb.finalize(ctrl=ctrl, target=target) | ||
target = bb.add(ZeroState()) | ||
q = [*ctrl, target] | ||
c0, c1, target = bb.add(CirqGateAsBloq(and_gate), q=q) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a backwards incompatible change? The CirqGateAsBloq doesn't preserve the signature anymore if the underlying gate is a GateWithRegisters
?
In theory, this should be fine since GateWithRegisters
are bloqs so we should use them without wrapping in CirqGateAsBloq
, but I'm nervous that this might break stuff in the interop. The tests are passing so that's a good indication, but have you looked into any potential consequence of this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One way to be full proof could be to raise an error if a user tries to wrap a GateWithRegisters
into a CirqGateAsBloq
(or any other object that satisfies isinstance(gate, Bloq)
).
I don't like the current behavior where it silently works, but does the wrong thing. It's better to explicitly raise an error if we don't want / expect users to wrap bloq objects in this wrapper
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I can add some error checking since this changes the behavior of using CirqGateAsBloq
with a GateWithRegisters
.
It was on purpose to make CirqGateAsBloq
only use the cirq.Gate
API, which doesn't have a notion of signatures. If we wanted to preserve the "special casing", I would (theoretically) argue for a different adapter: GateWithRegistersAsBloq
... but this is redundant :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually, I don't know if an error is appropriate: since GateWithRegisters
is a cirq.Gate
, you should be able to wrap it -- but it will treat it as a cirq gate. Maybe I'll emit a warning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It won't silently fail anyways: if you were wrapping a sided GateWithRegisters with CirqGateAsBloq, the change will change the signature; which will raise an error when you're trying to wire it up.
And of course the fix is simple: remove the adapter! I've added a warning
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Main feedback is to explicitly raise an error if CirqGateAsBloq
is passed a Bloq
object, instead of silently ignoring the LEFT / RIGHT registers and making everything a THRU register.
This PR triggers #1336 |
I'm going to revert the part of |
pytest time back to normal (lower than normal?) |
* strict BloqAsCirqGate and CirqGateAsBloq * fixy * Warn when wrapping a bloq * let cirq handle _unitary_ still
We use the adapter pattern for adapting cirq and qualtran (bloq) obejcts, namely
BloqAsCirqGate
andCirqGateAsBloq
. This PR does not modifyqualtran.GateWithRegisters
, which acts simultaneously as a cirq gate and qualtran bloq.BloqAsCirqGate
is now only acirq.Gate
.This PR removes the
signature
andon_registers
methods fromBloqAsCirqGate(cirq.Gate)
. These methods are bloq-y and one shouldn't rely on them being available on something that purports to be a cirq gate.BloqAsCirqGate
also learns a new_has_unitary_
implementation, which fixes #1570. This fix was revealed when I encountered a related bug while writing this PR: one of the unit tests started taking a long (indefinite) time; which was again traced tocirq.Gate.controlled_by
trying to validate that the gate was unitary.CirqGateAsBloq
is now only aBloq
.This PR removes the
GateWithRegisters(Bloq, cirq.Gate)
functionality from the adapter. Only the bloq API is available onCirqGateAsBloq
(so things like_unitary_
, diagram info, ... are removed).The functionality for
CirqGateAsBloq
is (still) contained inCirqGateAsBloqBase
which can be used to bootstrap bloq definitions.