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

Plxpr can capture operations #5511

Merged
merged 63 commits into from
May 10, 2024
Merged

Plxpr can capture operations #5511

merged 63 commits into from
May 10, 2024

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Apr 12, 2024

HIGHLY EXPERIMENTAL FEATURE

Context:

To improve integration with catalyst, we want a way to be able to natively capture the creation of quantum operations into an intermediate representation. This is one of the early PR's for this experimental push.

Description of the Change:

This PR adds a PLXPR metaclass that Operator uses. This allows for the capture of all Operator classes into jaxpr.

qml.capture.enable_plxpr()

def qfunc(a):
    qml.X(0)
    qml.IsingXX(a, wires=range(2))
    qml.ctrl(qml.adjoint(qml.X(0)), 1)

    0.5 * qml.X(0) @ qml.Y(1) + qml.Z(2)
    
jaxpr = jax.make_jaxpr(qfunc)(0)
jaxpr
{ lambda ; a:i32[]. let
    _:AbstractOperator() = PauliX[n_wires=1] 0
    _:AbstractOperator() = IsingXX[n_wires=2] a 0 1
    b:AbstractOperator() = PauliX[n_wires=1] 0
    c:AbstractOperator() = Adjoint b
    _:AbstractOperator() = Controlled[control_values=None work_wires=None] c 1
    d:AbstractOperator() = PauliX[n_wires=1] 0
    e:AbstractOperator() = SProd[id=None] 0.5 d
    f:AbstractOperator() = PauliY[n_wires=1] 1
    g:AbstractOperator() = Prod[id=None] e f
    h:AbstractOperator() = PauliZ[n_wires=1] 2
    _:AbstractOperator() = Sum[grouping_type=None id=None method=rlf] g h
  in () }

We can also return the jaxpr to normal pennylane qfunc behaviour via jax.core.eval_jaxpr:

with qml.queuing.AnnotatedQueue() as q:
    jax.core.eval_jaxpr(jaxpr.jaxpr, jaxpr.consts, 0.1)
q.queue
[X(0),
 IsingXX(0.1, wires=[0, 1]),
 Controlled(Adjoint(X(0)), control_wires=[1]),
 (0.5 * X(0)) @ Y(1) + Z(2)]

Benefits:

Possible Drawbacks:

  • Metaprogramming in python is an edge skill, and often not the best way to solve a problem. Messing around with things like this can often have unintended consequences down the line.

  • With PLXPR, wires will be restricted to be jax-tracable friendly labels.

Related GitHub Issues:

[sc-61199]

@albi3ro albi3ro changed the title Plxpr capture operations Plxpr can capture operations Apr 12, 2024
Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

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

Some comments :)

pennylane/capture/meta_type.py Outdated Show resolved Hide resolved
pennylane/capture/explanations.md Outdated Show resolved Hide resolved
pennylane/capture/meta_type.py Outdated Show resolved Hide resolved
pennylane/capture/meta_type.py Outdated Show resolved Hide resolved
pennylane/capture/meta_type.py Outdated Show resolved Hide resolved
pennylane/capture/meta_type.py Outdated Show resolved Hide resolved
pennylane/capture/meta_type.py Outdated Show resolved Hide resolved
pennylane/capture/meta_type.py Outdated Show resolved Hide resolved
pennylane/operation.py Outdated Show resolved Hide resolved
@albi3ro albi3ro requested review from timmysilv, dime10 and rmoyard and removed request for timmysilv April 15, 2024 22:52
Copy link

codecov bot commented Apr 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.67%. Comparing base (9c9b6ba) to head (34133fe).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5511      +/-   ##
==========================================
- Coverage   99.68%   99.67%   -0.01%     
==========================================
  Files         412      414       +2     
  Lines       38666    38522     -144     
==========================================
- Hits        38545    38398     -147     
- Misses        121      124       +3     

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

albi3ro and others added 8 commits May 7, 2024 14:51
@albi3ro albi3ro requested a review from dwierichs May 9, 2024 17:54
Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

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

This is in really good shape now :) Thanks for the continued effort!

pennylane/operation.py Show resolved Hide resolved
pennylane/ops/op_math/controlled.py Show resolved Hide resolved
tests/capture/test_operators.py Outdated Show resolved Hide resolved
tests/capture/test_operators.py Show resolved Hide resolved
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Copy link
Contributor

@dime10 dime10 left a comment

Choose a reason for hiding this comment

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

Love it 😍 The simplicity of it is really nice especially. I just have a few questions that came up while reading through it:

doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
pennylane/capture/__init__.py Outdated Show resolved Hide resolved
pennylane/capture/primitives.py Show resolved Hide resolved
pennylane/capture/primitives.py Show resolved Hide resolved
pennylane/ops/op_math/controlled.py Show resolved Hide resolved
Copy link
Contributor

@dime10 dime10 left a comment

Choose a reason for hiding this comment

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

🙌

albi3ro and others added 2 commits May 10, 2024 17:29
@albi3ro albi3ro merged commit 64de143 into master May 10, 2024
38 checks passed
@albi3ro albi3ro deleted the plxpr-capture-operations branch May 10, 2024 22:14
albi3ro added a commit that referenced this pull request May 22, 2024
**Context:**

In PR #5511 , we introduced the meta type `qml.capture.MetaOperator`
that hijacked the class creation processes. The `__call__` signature for
these classes got altered to the completely general `(cls, *args,
**kwargs)` instead of the more specific version used by `__init__`.

**Description of the Change:**

Adds a `__signature__` property to `MetaOperator` that ensures that the
signature will always be the `__init__` signature.

**Benefits:**

Signatures continue to match the `__init__` definition.

**Possible Drawbacks:**

The metaprogramming black magic may still have other consequences we
still aren't aware of. This fixes the particular signature issue, but it
still shows the consequences of metaprogramming.

**Related GitHub Issues:**

Fixes #5724 [sc-63734]

<img width="487" alt="Screenshot 2024-05-22 at 9 42 10 AM"
src="https://github.com/PennyLaneAI/pennylane/assets/6364575/1abf41ce-b72a-4734-a069-3dd37e7f8c28">
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.

None yet

6 participants