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

Fix leftover quantum operations in classical functions during the gradient passes #439

Merged
merged 7 commits into from
Jan 12, 2024

Conversation

dime10
Copy link
Collaborator

@dime10 dime10 commented Jan 8, 2024

Resolves an issue in the gradient dialect brought to light by #397. The error raised is as follows:

error: 'func.func' op cloned during the gradient pass is not free of quantum ops

The error is thrown when when a quantum function is cloned but with all of its quantum operations removed, in order to retain only the classical code. This is done in several places in the gradient dialect.

In this case, certain input programs (see linked issue) would still cause the gradient pass to be unable to remove all quantum operations from a function. A snippet of the offending IR would look like:

%i0 = tensor.extract %idx : tensor<i64>
%q = quantum.extract %r[%i0]
%i1 = tensor.extract %idx : tensor<i64>
%q = quantum.insert %r[%i1], %q

Even though the qubit extraction and insertion are performed at the same index, the SSA value used in both operations is not the same, and so the operations are not automatically folded way. This folding is usually relied upon to remove all quantum operations from a function.

Applying CSE before the gradient pass resolves the issue.

[sc-53218]
fixes #437

- `0_` to avoid it appearing after the HLO pass in intermediate files
- more descriptive name than "pipeline"
MLIR generated from the JAX frontend typically contains a lot of
duplicate instructions. For instance, the extraction of a scalar value
from the same scalar tensor could be repeated several times.

Besides making the program larger than it needs to be, it also
makes it more difficult to perform insert/extract elimination
when dynamic wires are used. This was causing the gradient pass to fail
in some instances, specifically when duplicating a hybrid function
while removing all quantum code from it.
@dime10 dime10 added bug Something isn't working compiler Pull requests that update the compiler labels Jan 8, 2024
@dime10 dime10 requested a review from josh146 January 8, 2024 14:13
@dime10 dime10 added this to the v0.4.0 milestone Jan 8, 2024
Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

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

That was quick 🚤 thanks @dime10!

@dime10 dime10 removed this from the v0.4.0 milestone Jan 8, 2024
@dime10
Copy link
Collaborator Author

dime10 commented Jan 12, 2024

@erick-xanadu I implemented the single register & qubit reset in between transform tapes (626df4d), if you want to give it a look.

Copy link
Contributor

@erick-xanadu erick-xanadu left a comment

Choose a reason for hiding this comment

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

Very smart solution!

Copy link

codecov bot commented Jan 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (cc58054) 99.56% compared to head (6607298) 99.56%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #439   +/-   ##
=======================================
  Coverage   99.56%   99.56%           
=======================================
  Files          43       43           
  Lines        7643     7662   +19     
  Branches      512      514    +2     
=======================================
+ Hits         7610     7629   +19     
  Misses         17       17           
  Partials       16       16           

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

@dime10 dime10 merged commit 8f66c16 into main Jan 12, 2024
19 checks passed
@dime10 dime10 deleted the fix-leftover-quantum branch January 12, 2024 15:57
@josh146 josh146 mentioned this pull request Jan 26, 2024
josh146 added a commit that referenced this pull request Jan 29, 2024
**Context:** v0.4.1 bugfix PR

**Description of the Change:**

Incorporates the following PRs: #439 #457 #437 #465 #471

---------

Co-authored-by: David Ittah <dime10@users.noreply.github.com>
Co-authored-by: Ali Asadi <10773383+maliasadi@users.noreply.github.com>
Co-authored-by: Romain Moyard <rmoyard@gmail.com>
Co-authored-by: Rashid N H M <95639609+rashidnhm@users.noreply.github.com>
Co-authored-by: erick-xanadu <110487834+erick-xanadu@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler Pull requests that update the compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Gradient compilation failing when classical processing used with wires within a for loop
3 participants