-
Notifications
You must be signed in to change notification settings - Fork 26
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
[Frontend] Add support for native Python if statements via AutoGraph #235
Conversation
a011853
to
f9d7b51
Compare
The verifier function would always exclude the last return type (typically the quantum register), regardless of wether tracing took place in a QNode or not.
FileCheck tests are updated to check for code snippets in the autograph style, rather than the more direct style originally expected from a custom implementation.
Codecov Report
@@ Coverage Diff @@
## main #235 +/- ##
==========================================
+ Coverage 99.26% 99.31% +0.04%
==========================================
Files 38 41 +3
Lines 6923 7115 +192
Branches 343 370 +27
==========================================
+ Hits 6872 7066 +194
+ Misses 28 27 -1
+ Partials 23 22 -1
|
Particular emphasis is placed on ensuring that QJIT/QNode decorators work with autograph in various configurations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incredible work! 🥳 I'm very excited at how nice the UI is. Just a few questions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, David! Please see a few technical questions and suggestions from my side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work @dime10! I mostly just reviewed the docstring
.. when the user activates the feature.
Still needs to resolve top-level import issue.
Public API functions for autograph can always be imported, in particular the documentation builder can extract their docstrings without a tensorflow installation.
614bbf5
to
124a00a
Compare
Just checking is it possible to remove a change request without explicit approval
@dime10 please see my comment in the remaining discussion and I'm OK with closing it! The QNode copying problem worries me a little, but more in generic rather than in relation to this particular PR. While checking the sources I found that we already use a similar pattern here. I think I'll ask the PL team what do they think about the copying policies. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work @dime10!
Introduces support for Python `for .. in ...:` statement capture as part of the compiled program. Similar to PR #235, AutoGraph is used to convert such statements into the equivalent Catalyst version before tracing occurs. Specifically, the following constructs are supported via AutoGraph: - `for elem in iterable:` - These get converted into a `for_loop(0, len(iterable), 1)` with `elem = iterable[i]` automatically assigned using the iteration index, assuming `iterable` is convertible to a JAX array. If this is not the case, the loop is executed as is in Python. - `for i in range(start, stop, step):` These are converted directly into their equivalent `for_loop(start, stop, step)`. Contrary to the default Python `range`, when AutoGraph is enabled `range` can also accept dynamic tracers as `start`, `stop`, `step` values. If _any_ exception is raised during the tracing of the `for_loop` body, Catalyst will fall back to Python with a warning. - `for i, elem in enemurate(iterable):` - These get converted into `for_loop(0, len(iterable), 1)` with the iteration index assigned to the variable chosen by the user (in this case `i`), and `elem = iterable[i]`. This also assumes that `iterable` is convertible to an array, and that the loop body traces without exception, otherwise the loop is executed in Python. Note that a warning is raised when when a Python fallback is triggered due to a tracing exception. Python fallbacks caused by the iterable not being convertible to array are silent. [sc-41287]
Adds imperative style if-statement support to Catalyst.
The feature is based on the autograph module from tensorflow. Besides the if-statement transformation itself, this PR contains all necessary changes to integrate autograph into the Catalyst pipeline.
Tensorflow is assumed to be installed for tests (not added to
requirements.txt
due to the large package size!), but is otherwise an optional dependency. The user will receive a warning if they try to use it without Tensorflow being installed.A bug in the
cond
function is also fixed that didn't raise an error for mismatched return types in a classical tracing context.[sc-41286]