Provide some guidelines for how to write QNode passes #2483
Conversation
|
Hello. You may have forgotten to update the changelog!
|
1 similar comment
|
Hello. You may have forgotten to update the changelog!
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2483 +/- ##
==========================================
+ Coverage 96.61% 96.76% +0.14%
==========================================
Files 156 156
Lines 16984 17016 +32
Branches 1657 1658 +1
==========================================
+ Hits 16409 16465 +56
+ Misses 421 403 -18
+ Partials 154 148 -6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
joeycarter
left a comment
There was a problem hiding this comment.
Looks good! 💯
I have no notes for now, but may want to revisit next time I write a qnode pass. Thanks @dime10!
mudit2812
left a comment
There was a problem hiding this comment.
Let me know if this is clear enough. We could add IR example if additional clarity is needed :)
We already have a google doc with an IR example, so it would be great to move it here if this is going to be a centralized guidance doc. Let me see if I can find the doc and send you a link on Slack.
…#2497) _Note I don't think this PR is a must, but while working on the [transform post-processing epic](https://app.shortcut.com/xanaduai/epic/107240) one of the ideas was to better formalize some of the attributes we use and might have implicit assumptions for. The verification introduced here is not very strong, but it is something. There are several older passes in Catalyst which currently don't follow the proposed notion of qnode functions (e.g. they might [not propagate the attribute](https://github.com/PennyLaneAI/catalyst/blob/main/mlir/lib/Gradient/Transforms/GradMethods/ParameterShift.cpp) in new functions they create, or they might also have [unusual structures](https://github.com/PennyLaneAI/catalyst/blob/main/mlir/lib/Mitigation/Transforms/MitigationMethods/Zne.cpp) like having MPs in a function but device init/quantum allocs in a different one)._ In this PR, the existing `qnode` attribute is renamed to `quantum.node` to indicate the dialect origin. Additionally, verification logic is introduced to formalize the notion of a "qnode" function (see also #2483), which is characterized by the presence of measurement process operations. These operations represent an implicit transition from quantum state manipulation to execution logic and classical post-processing. While quantum gate operations can appear in functions that are not qnodes, measurement processes should not as they have this "non-local" effect. The attribute helps identify functions with quantum execution semantics during transformations. The new attribute and verification logic is currently implemented during the "quantum kernel" phase only, i.e. before `inline-nested-modules` is called. All functions carrying the attribute ~must have at least one measurement process~ (I have removed this verification for now since Catalyst still uses MP-free QNodes as a feature), and all measurement processes appearing within a nested module (quantum kernel) must be contained in a func op with that attribute. To limit the scope of this PR the new attribute is replaced with the previous `qnode` attribute after lowering the quantum kernel module. A bug in the xDSL split-non-commuting pass is also fixed (required to pass the new verification). 9f75c0e [sc-107492]
…#2497) _Note I don't think this PR is a must, but while working on the [transform post-processing epic](https://app.shortcut.com/xanaduai/epic/107240) one of the ideas was to better formalize some of the attributes we use and might have implicit assumptions for. The verification introduced here is not very strong, but it is something. There are several older passes in Catalyst which currently don't follow the proposed notion of qnode functions (e.g. they might [not propagate the attribute](https://github.com/PennyLaneAI/catalyst/blob/main/mlir/lib/Gradient/Transforms/GradMethods/ParameterShift.cpp) in new functions they create, or they might also have [unusual structures](https://github.com/PennyLaneAI/catalyst/blob/main/mlir/lib/Mitigation/Transforms/MitigationMethods/Zne.cpp) like having MPs in a function but device init/quantum allocs in a different one)._ In this PR, the existing `qnode` attribute is renamed to `quantum.node` to indicate the dialect origin. Additionally, verification logic is introduced to formalize the notion of a "qnode" function (see also #2483), which is characterized by the presence of measurement process operations. These operations represent an implicit transition from quantum state manipulation to execution logic and classical post-processing. While quantum gate operations can appear in functions that are not qnodes, measurement processes should not as they have this "non-local" effect. The attribute helps identify functions with quantum execution semantics during transformations. The new attribute and verification logic is currently implemented during the "quantum kernel" phase only, i.e. before `inline-nested-modules` is called. All functions carrying the attribute ~must have at least one measurement process~ (I have removed this verification for now since Catalyst still uses MP-free QNodes as a feature), and all measurement processes appearing within a nested module (quantum kernel) must be contained in a func op with that attribute. To limit the scope of this PR the new attribute is replaced with the previous `qnode` attribute after lowering the quantum kernel module. A bug in the xDSL split-non-commuting pass is also fixed (required to pass the new verification). 9f75c0e [sc-107492]
…#2497) _Note I don't think this PR is a must, but while working on the [transform post-processing epic](https://app.shortcut.com/xanaduai/epic/107240) one of the ideas was to better formalize some of the attributes we use and might have implicit assumptions for. The verification introduced here is not very strong, but it is something. There are several older passes in Catalyst which currently don't follow the proposed notion of qnode functions (e.g. they might [not propagate the attribute](https://github.com/PennyLaneAI/catalyst/blob/main/mlir/lib/Gradient/Transforms/GradMethods/ParameterShift.cpp) in new functions they create, or they might also have [unusual structures](https://github.com/PennyLaneAI/catalyst/blob/main/mlir/lib/Mitigation/Transforms/MitigationMethods/Zne.cpp) like having MPs in a function but device init/quantum allocs in a different one)._ In this PR, the existing `qnode` attribute is renamed to `quantum.node` to indicate the dialect origin. Additionally, verification logic is introduced to formalize the notion of a "qnode" function (see also #2483), which is characterized by the presence of measurement process operations. These operations represent an implicit transition from quantum state manipulation to execution logic and classical post-processing. While quantum gate operations can appear in functions that are not qnodes, measurement processes should not as they have this "non-local" effect. The attribute helps identify functions with quantum execution semantics during transformations. The new attribute and verification logic is currently implemented during the "quantum kernel" phase only, i.e. before `inline-nested-modules` is called. All functions carrying the attribute ~must have at least one measurement process~ (I have removed this verification for now since Catalyst still uses MP-free QNodes as a feature), and all measurement processes appearing within a nested module (quantum kernel) must be contained in a func op with that attribute. To limit the scope of this PR the new attribute is replaced with the previous `qnode` attribute after lowering the quantum kernel module. A bug in the xDSL split-non-commuting pass is also fixed (required to pass the new verification). 9f75c0e [sc-107492]
[sc-107497]