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

run_autograph is now idempotent #7001

Merged
merged 10 commits into from
Feb 27, 2025
Merged

run_autograph is now idempotent #7001

merged 10 commits into from
Feb 27, 2025

Conversation

andrijapau
Copy link
Contributor

@andrijapau andrijapau commented Feb 24, 2025

Context:

When support for control flow with autograph (through the QNode execution pipeline) was introduced in #6837 a minor bug was discovered where if you try to apply autograph to an already autograph'd function, an ambiguous error would show up,

qml.capture.enable()
from pennylane.capture import run_autograph

dev = qml.device("default.qubit", wires=1)

@qml.qnode(qml.device("default.qubit", wires=1), autograph=True)
def circ(x: float):
    qml.RY(x, wires=0)
    return qml.expval(qml.PauliZ(0))

>>> ag_circ = run_autograph(circ)
>>> ag_ag_circ = run_autograph(ag_circ)
ValueError: closure mismatch, requested ('ag__',), but source function had ()

There are a few routes to solve this issue,

  1. Raise a more helpful error
  2. Throw a warning and proceed with converted function (Catalyst does this)
  3. Silently proceed with converted function

Description of the Change:

Option (2) was preferred and so if an already converted function is detected (by the presence of the ag_uncoverted attribute), we throw an AutoGraphWarning and just return early. Now we have the behaviour,

qml.capture.enable()
from pennylane.capture import run_autograph

dev = qml.device("default.qubit", wires=1)

@qml.qnode(qml.device("default.qubit", wires=1), autograph=True)
def circ(x: float):
    qml.RY(x, wires=0)
    return qml.expval(qml.PauliZ(0))

ag_circ = run_autograph(circ)
>>> ag_ag_circ = run_autograph(ag_circ)
AutoGraphWarning: AutoGraph will not transform the function <function ...> as it has already been transformed.
>>> ag_ag_circ.func is ag_circ.func
True
>>> ag_ag_circ(-1)
Array(0.5403023, dtype=float32)

Benefits: Enables autograph specific tests to run with the default autograph=True argument through the QNode.

Possible Drawbacks:

Suppressed the warnings in the autograph test folder. This means that a future test that emits this warning could be missed if we don't pay attention to the WAE action that is run weekly.

Fixes [sc-83366]

This comment was marked as resolved.

@andrijapau andrijapau changed the title [Bug Fix] run_autograph is now idempotent run_autograph is now idempotent Feb 24, 2025
Copy link

codecov bot commented Feb 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.60%. Comparing base (64e226f) to head (55d488d).
Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #7001    +/-   ##
========================================
  Coverage   99.60%   99.60%            
========================================
  Files         484      484            
  Lines       46141    46249   +108     
========================================
+ Hits        45957    46065   +108     
  Misses        184      184            

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

@andrijapau andrijapau requested a review from albi3ro February 25, 2025 17:20
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

@lillian542 lillian542 left a comment

Choose a reason for hiding this comment

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

LGTM! 🚀

@andrijapau andrijapau enabled auto-merge (squash) February 27, 2025 16:22
@andrijapau andrijapau merged commit 2c3a11f into master Feb 27, 2025
45 checks passed
@andrijapau andrijapau deleted the fix-run-ag-bug branch February 27, 2025 16:48
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