Support adjoint of operators who decompose with control flow#2667
Open
Support adjoint of operators who decompose with control flow#2667
Conversation
Catalyst doesn't accept control operations that are wrapped directly in a symbolic Adjoint operator. However, adjoint regions with control flow operations inside are supported. So we update the decomposition mechanism to automatically turn operator adjoints into qfunc adjoints when applied to control flow.
6139b06 to
2c59f3a
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2667 +/- ##
=======================================
Coverage 97.12% 97.12%
=======================================
Files 156 156
Lines 17733 17743 +10
Branches 1696 1695 -1
=======================================
+ Hits 17223 17233 +10
Misses 373 373
Partials 137 137 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently, something like the following fails with an error:
The reason is that the adjoint is applied directly to an operator, generating a symbolic
Adjoint()op instance, whereas applying adjoint to functions generatesHybridAdjoint()instances with a separate body region. While the latter has support for computing the adjoint of control flow operations in the compiler, the former (in Python PennyLane) does not.This hasn't been a problem so far because
ForLoopinstances are not manipulated directly in user workflows, and so adjoint with control flow would have always generated the function version. With control flow appearing as part of decompositions however it's possible forAdjoint(ForLoop)instances to be generated now, for example when an adjointed operator like a template needs to be decomposed.The fix consists of automatically "decomposing"
Adjoint(ForLoop)intoHybridAdjoint([ForLoop]). The same is done for while loops and conditionals. However, switch statements are not yet included because the adjoint algorithm in catalyst does not have support for it (since it was added later).closes #2669
[sc-116053]