Skip to content
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

Allow qml.cond to fallback to the Python interpreter if a compiler is not available and there are no MCMs #6016

Merged
merged 29 commits into from
Aug 2, 2024

Conversation

josh146
Copy link
Member

@josh146 josh146 commented Jul 21, 2024

Context:

In Catalyst, catalyst.cond will seamlessly fallback to Python interpretation when @qjit is not present. However, qml.cond will instead raise an error when @qjit is not present and the condition is not a mid-circuit measurement, which is not ideal.

Description of the Change:

  • When @qjit is not present and the conditional does not contain a mid-circuit measurement, qml.cond will now simply execute the conditional in standard Python.

  • qml.cond can be optionally used in decorator mode (matching the behaviour of catalyst.cond).

Benefits:

  • This will allow the same code to work with and without @qjit, without needing to modify the workflow.

  • This will also allow templates and operation decompositions to use qml.cond internally, with no changes for standard Python execution, but with conditionals captured automatically when using @qjit.

Possible Drawbacks:

  • Not a drawback per se, more of a note, but the CondCallable class introduced in this PR might be needed (and extended) to support capturing for conditional statements as JAXPR primitives(?). It is based on the equivalent classes from Catalyst.

  • Introduces an additional decorator UI. This is not documented for now, but it will easily allow existing Catalyst code to port from using catalyst.cond to qml.cond.

Related GitHub Issues: n/a

@josh146 josh146 marked this pull request as ready for review July 21, 2024 03:58
Copy link

codecov bot commented Jul 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.65%. Comparing base (0b0e212) to head (a659340).
Report is 292 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6016      +/-   ##
==========================================
+ Coverage   99.52%   99.65%   +0.13%     
==========================================
  Files         430      430              
  Lines       41835    41436     -399     
==========================================
- Hits        41637    41295     -342     
+ Misses        198      141      -57     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@josh146
Copy link
Member Author

josh146 commented Jul 22, 2024

[sc-69157]

Copy link
Contributor

@dime10 dime10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! I don't have good visibility into whether and how this will interact with existing PennyLane cond though.

pennylane/ops/op_math/condition.py Outdated Show resolved Hide resolved
doc/releases/changelog-dev.md Show resolved Hide resolved
Base automatically changed from josh146-patch-3 to master July 24, 2024 15:48
Copy link
Contributor

@PietropaoloFrisoni PietropaoloFrisoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @josh146!

It seems to me that right now the changes are overlapping with #6014, which has been merged today. I am approving the PR since I guess these will be resolved soon.

@josh146
Copy link
Member Author

josh146 commented Jul 24, 2024

Thanks @PietropaoloFrisoni, I've just resolved the conflicts!

Copy link
Contributor

@mudit2812 mudit2812 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Very exciting to expand qml.cond 😄

pennylane/ops/op_math/condition.py Show resolved Hide resolved
pennylane/ops/op_math/condition.py Outdated Show resolved Hide resolved
pennylane/ops/op_math/condition.py Outdated Show resolved Hide resolved
PietropaoloFrisoni and others added 4 commits August 1, 2024 16:54
…into `CondCallable` (#6063)

**Context:** For 'historical reasons', the captured version (see
sc-66774) and the non-captured version (see sc-69157) of `qml.cond` have
a different style, which is rather uneven.

The purpose of this story is to incorporate them in such a way that the
implementation is more similar to the one in Catalyst. Or, to make
another example, the implementation of `qml.for_loop` in PL (see
sc-66736 and sc-69432). The purpose is not to change/add
functionalities, but rather to unify the code structure so that it is
more elegant.

**Description of the Change:** As above.

**Benefits:** Better and cleaner structure, more similar to Catalyst.

**Possible Drawbacks:** None that I can think of right now directly due
to this PR (we just moved some code).

**Related GitHub Issues:** None

**Related Shortcut Stories:** [sc-70342]
@josh146 josh146 merged commit 4d574a9 into master Aug 2, 2024
38 checks passed
@josh146 josh146 deleted the cond-python-fallback branch August 2, 2024 02:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants