-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Fix zero-operand gates and instructions #8272
Fix zero-operand gates and instructions #8272
Conversation
A few checks in different places didn't account for zero-operand gates being possible. In most uses, there never would be anything useful here, but it can occasionally be useful to create a "global-phase" instruction, especially if this is later going to have controls applied to it. This is well-formed within the Qiskit data model; spectator qubits implicitly undergo the identity, so this corresponds to the global phase being multiplied by the identity on _all_ qubits.
Thank you for opening a new pull request. Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient. While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone. One or more of the the following people are requested to review this:
|
Pull Request Test Coverage Report for Build 3350717692
💛 - Coveralls |
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 feels a little weird to add a controlled global phase this way but if it's required that's fine by me. So this is to add a global phase on the entire circuit right? If we need a relative phase we could also do something like
phase = QuantumCircuit(1, global_phase=phase)
phase.control(...)
The example you gave is similar to what's being asked for in the linked issue, yeah. It's possible to get around that by other means, but these fixes here are stuff that should work anyway. It's a bit awkward that you need to use a create a full Basically: there's ways to get the desired effect in the linked issue, but this stuff should work anyway, and a very basic "global phase" gate seems like a convenient utility definition we could supply. |
I've fixed the merge conflict and added an extra test for Erick. |
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.
Overall LGTM, but now something like
my_cx = ControlledGate(
name="cx?",
num_qubits=2,
params=[],
num_ctrl_qubits=2, # same as num_qubits!
)
is technically a valid input right? Could we add a test to ensure this breaks as expected?
Also in the class docstring is still says that
CircuitError: If ``num_ctrl_qubits`` >= ``num_qubits``.
maybe
CircuitError: If ``num_ctrl_qubits + base_gate.num_qubits`` >= ``num_qubits``.
would be more accurate? 🙂
Oh, good idea on the last part - that's the constraint that actually should be implemented. We did need the case of |
Done in af1abf0. |
* Fix zero-operand gates and instructions A few checks in different places didn't account for zero-operand gates being possible. In most uses, there never would be anything useful here, but it can occasionally be useful to create a "global-phase" instruction, especially if this is later going to have controls applied to it. This is well-formed within the Qiskit data model; spectator qubits implicitly undergo the identity, so this corresponds to the global phase being multiplied by the identity on _all_ qubits. * Fix controlled-gate qubit-number documentation * Add ScalarOp test * Be stricter about allowed num_ctrl_qubits Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> (cherry picked from commit bb93592)
* Fix zero-operand gates and instructions A few checks in different places didn't account for zero-operand gates being possible. In most uses, there never would be anything useful here, but it can occasionally be useful to create a "global-phase" instruction, especially if this is later going to have controls applied to it. This is well-formed within the Qiskit data model; spectator qubits implicitly undergo the identity, so this corresponds to the global phase being multiplied by the identity on _all_ qubits. * Fix controlled-gate qubit-number documentation * Add ScalarOp test * Be stricter about allowed num_ctrl_qubits Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> (cherry picked from commit bb93592) Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
Summary
A few checks in different places didn't account for zero-operand gates
being possible. In most uses, there never would be anything useful
here, but it can occasionally be useful to create a "global-phase"
instruction, especially if this is later going to have controls applied
to it. This is well-formed within the Qiskit data model; spectator
qubits implicitly undergo the identity, so this corresponds to the
global phase being multiplied by the identity on all qubits.
Details and comments
This came up in #8236. This PR doesn't add the requested gate, it just removes the roadblocks from a user-defined definition - such a gate should already be representable in the
Gate.definition
form, but due to the bugs fixed here, would fail in weird ways.There's probably more places where this edge case will slip through and make a mess that I didn't catch here, but this makes everything I could immediately think of work - transpilation at pre-defined levels, gate control, conversion to
Operator
, etc.