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 CancelInversesInterpreter for transforming plxpr #6692

Merged
merged 15 commits into from
Dec 17, 2024

Conversation

mudit2812
Copy link
Contributor

@mudit2812 mudit2812 commented Dec 9, 2024

[sc-72846]

Context:
This PR adds a CancelInversesInterpreter to apply the cancel_inverses transform natively to plxpr.

Description of the Change:

  • Add CancelInversesInterpreter to transform plxpr
  • Note that the interpreter may reorder some primitives with disjoint wires. This does not impact the correctness of the circuit, and is a consequence of the implementation used.
  • Currently, inner jaxpr of transform primitives will not be transformed.

Benefits:
cancel_inverses can be applied natively to plxpr.

Possible Drawbacks:
Potential operator reordering may confuse users.

Related GitHub Issues:

@mudit2812 mudit2812 marked this pull request as ready for review December 12, 2024 23:03
Copy link

codecov bot commented Dec 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.62%. Comparing base (579e70d) to head (4e75f95).
Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #6692    +/-   ##
========================================
  Coverage   99.62%   99.62%            
========================================
  Files         476      478     +2     
  Lines       44733    44973   +240     
========================================
+ Hits        44564    44804   +240     
  Misses        169      169            

☔ 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 very good!

One question from me. Why isn't the operation canceled if I have a nested adjoint operation? Something like:

@CancelInversesInterpreter()
def f():
    qml.adjoint(qml.adjoint(qml.adjoint(qml.PauliX(0))))
    qml.PauliX(0)

>>> jaxpr = jax.make_jaxpr(f)()

{ lambda ; . let
    a:AbstractOperator() = PauliX[n_wires=1] 0
    b:AbstractOperator() = Adjoint a
    c:AbstractOperator() = Adjoint b
    _:AbstractOperator() = Adjoint c
    _:AbstractOperator() = PauliX[n_wires=1] 0
  in () }

If this is too much trouble to implement I am perfectly fine with postponing nested adjoint operators : )

@mudit2812
Copy link
Contributor Author

@PietropaoloFrisoni the same also doesn't work with program capture disabled:

>>> print(qml.transforms.cancel_inverses(qml.tape.QuantumScript([qml.X(0), qml.adjoint(qml.adjoint(qml.adjoint(qml.X(0))))]))[0][0].circuit)
[X(0), Adjoint(Adjoint(Adjoint(X(0))))]

There are many cases in which cancel_inverses misses cancellations, but that is a separate issue 😅 . For now, we're at feature parity with program capture enabled and disabled, so it should be fine.

@PietropaoloFrisoni
Copy link
Contributor

PietropaoloFrisoni commented Dec 13, 2024

@mudit2812 I agree. This can probably be fixed automatically by ensuring that the adjoint of an adjoint operator returns the operator itself

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.

🚀

@mudit2812 mudit2812 requested a review from lillian542 December 16, 2024 16:19
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.

🚀

@mudit2812 mudit2812 enabled auto-merge (squash) December 17, 2024 19:49
@mudit2812 mudit2812 merged commit 2e78d32 into master Dec 17, 2024
45 checks passed
@mudit2812 mudit2812 deleted the cancel-inverses-interpreter branch December 17, 2024 20:05
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