-
Notifications
You must be signed in to change notification settings - Fork 603
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
Make parametric controlled operators inherit from ControlledOp
#5069
Conversation
ControlledOp
ControlledOp
Given the size of the diff, could it make sense to split this up into smaller PRs? |
The size of the diff is actually smaller than it looks. We're moving these controlled operators from |
Move test cases exclusively for controlled parametric ops to tests/ops/op_math/test_parametric_controlled_ops.py
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #5069 +/- ##
==========================================
- Coverage 99.68% 99.67% -0.02%
==========================================
Files 394 392 -2
Lines 35720 35575 -145
==========================================
- Hits 35609 35460 -149
- Misses 111 115 +4 ☔ View full report in Codecov by Sentry. |
ControlledOp
ControlledOp
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.
a few little comments, but otherwise this is looking good!
Co-authored-by: Matthew Silverman <matthews@xanadu.ai>
Linking to corresponding Shortcut story: [sc-53529] |
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.
Looks great! 🎉 Thanks for handling all 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.
🕺 🕺
**Context:** All controlled operations should inherit from the general Controlled class, and the decomposition of controlled operations is not consistent for custom and non-custom controlled operations. This is a continuation of #5069 This is the first PR out of two for this rework. The second PR will focus on making sure that all custom controlled operations inherit from Controlled for more consistent inheritance structure. **Description of the Change:** - Make `MultiControlledX` inherit from ControlledOp. - `qml.ctrl` called on operators with custom controlled versions will return instances of the custom class. - Special handling of `PauliX` based controlled operations (`PauliX`, `CNOT`, `Toffoli`, `MultiControlledX`) - Calling `qml.ctrl` on one of these operators will always resolve to the best option in `CNOT`, `Toffoli`, or `MultiControlledX` depending on the number of control wires and control values. - `qml.ctrl` will flatten nested controlled operators to a single multi-controlled operation. - Controlled operators with a custom controlled version decomposes like how their controlled counterpart decomposes, as opposed to decomposing into their controlled version. - Special handling of `PauliX` based controlled operations: e.g., `Controlled(CNOT([0, 1]), [2, 3])` will have the same decomposition behaviour as a `MultiControlledX([2, 3, 0, 1])` **Benefits:** Cleaner code and more consistent behaviour **Possible Drawbacks:** Change of decomposition behaviour may cause issues. ~For `MultiControlledX`, the `wires` attribute now refers to all wires, as in `control_wires + target_wire + work_wires`, to access only the `control_wires + target_wires`, use the `active_wires` attribute.~ **Related GitHub Issues:** #5069 #1447 **Related Shortcut Stories** [sc-55949] [sc-55131] [sc-55358] --------- Co-authored-by: Christina Lee <christina@xanadu.ai> Co-authored-by: Matthew Silverman <matthews@xanadu.ai>
Context:
Many controlled operations have been added to PennyLane over the years, such as CNOT, CRY, CZ, etc. For code tidiness, we would like to make them all inherit from the ControlledOp base.
Description of the Change:
CRX
,CRY
,CRZ
,CRot
,ControlledPhaseShift
are now:pennylane.ops.qubit
topennylane.ops.op_math
ControlledOp
Calling
qml.ctrl
onRX
,RY
,RZ
,Rot
, andPhaseShift
with a single control wire will return gates of typesCRX
,CRY
, etc. as opposed to a generalControlled
operator.The decomposition of an operator created with calling
qml.ctrl
onRX
,RY
,RZ
,Rot
,PhaseShift
with a single control wire will now be the full decomposition instead of a single controlled gate. For example:Will become:
Benefits:
Code cleanliness.
Related GitHub Issues:
#1447