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

Add autograph support for for loops #6426

Open
wants to merge 9 commits into
base: autograph_while_loop
Choose a base branch
from

Conversation

lillian542
Copy link
Contributor

@lillian542 lillian542 commented Oct 21, 2024

Context:
We added initial autograph support with custom PennyLane conversion for if/else statements in #6406, and while loops in #6413. This PR adds support for for loops as well.

Description of the Change:
We add a custom PennyLane implementation for for_stmt for autograph, based on the existing Catalyst implementation.

Benefits:

We can have logic like this in a captured function:

@qml.qnode(qml.device("default.qubit", wires=1))
def f(params):
    for x in params:
        qml.RY(x, wires=0)
    return qml.expval(qml.PauliZ(0))

ag_circuit = run_autograph(f)
jaxpr = jax.make_jaxpr(ag_circuit)(jnp.array([1.0, 2.0, 3.0]))

and the resulting JAXPR will contain a for-loop instead of unrolling the entire loop.

Possible Drawbacks:
Not a drawback, per se, but this is the part of the implementation that is most "behind" catalyst in terms of feature parity. Catalyst has an option to fall back on a Python implementation that is based on queueing instead of raising an error in some circumstances. A few test are xfailed with a note that they are implemented in Catalyst.

Relevant Shortcut Stories:
[sc-71821]

@lillian542 lillian542 changed the base branch from master to autograph_while_loop October 21, 2024 22:31

This comment was marked as resolved.

@lillian542 lillian542 marked this pull request as ready for review November 26, 2024 22:01
Copy link

codecov bot commented Nov 26, 2024

Codecov Report

Attention: Patch coverage is 89.89899% with 10 lines in your changes missing coverage. Please review.

Please upload report for BASE (autograph_while_loop@84ca81b). Learn more about missing BASE report.
Report is 4 commits behind head on autograph_while_loop.

Files with missing lines Patch % Lines
pennylane/capture/autograph/ag_primitives.py 89.89% 10 Missing ⚠️
Additional details and impacted files
@@                   Coverage Diff                   @@
##             autograph_while_loop    #6426   +/-   ##
=======================================================
  Coverage                        ?   99.43%           
=======================================================
  Files                           ?      458           
  Lines                           ?    42976           
  Branches                        ?        0           
=======================================================
  Hits                            ?    42734           
  Misses                          ?      242           
  Partials                        ?        0           

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

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.

Looks pretty good to me!

pennylane/capture/autograph/ag_primitives.py Outdated Show resolved Hide resolved
pennylane/capture/autograph/ag_primitives.py Show resolved Hide resolved
tests/capture/autograph/test_for_loop.py Show resolved Hide resolved
tests/capture/autograph/test_for_loop.py Show resolved Hide resolved
Copy link
Contributor

@astralcai astralcai left a comment

Choose a reason for hiding this comment

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

LGTM! 🎸

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.

3 participants