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

Move device preprocessing to inner execute #5791

Merged
merged 47 commits into from
Jun 12, 2024
Merged

Move device preprocessing to inner execute #5791

merged 47 commits into from
Jun 12, 2024

Conversation

astralcai
Copy link
Contributor

@astralcai astralcai commented Jun 3, 2024

Context:
If there is a gradient transform, we want device preprocessing to happen within the ml boundary, i.e., in inner_execute, to avoid applying the gradient transform on transformed tapes produced by device preprocessing.

Description of the Change:

  1. Adds a inner_transform keyword to qml.execute
  2. Update QNode._execute_component and qml.execute to add device preprocessing to the inner transform program if the gradient_fn is a gradient transform.

Related GitHub Issues:
[sc-59107]

@astralcai astralcai changed the title Add device preprocessing to inner transform Move device preprocessing to inner transform Jun 4, 2024
@astralcai astralcai changed the title Move device preprocessing to inner transform Move device preprocessing to inner execute Jun 4, 2024
@astralcai astralcai changed the base branch from master to template-grads June 5, 2024 19:30
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.

Looks good to me, thanks @astralcai !
Just had a small suggestion for one test, and wanted to follow up on open comments.

pennylane/devices/default_qubit.py Show resolved Hide resolved
pennylane/ops/op_math/condition.py Show resolved Hide resolved
tests/test_qnode.py Show resolved Hide resolved
astralcai and others added 3 commits June 11, 2024 13:04
@astralcai astralcai requested a review from dwierichs June 11, 2024 20:16
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.

Thanks @astralcai

Co-authored-by: Christina Lee <christina@xanadu.ai>
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.

🎉

@astralcai astralcai enabled auto-merge (squash) June 12, 2024 15:33
@astralcai astralcai merged commit 4eb803a into master Jun 12, 2024
40 checks passed
@astralcai astralcai deleted the inner-execute branch June 12, 2024 15:54
mudit2812 added a commit to PennyLaneAI/pennylane-lightning that referenced this pull request Jun 13, 2024
**Context:**
Currently blocked by PennyLaneAI/pennylane#5791.
Device preprocessing is being moved inside the ML boundary, so the
interface information needed for correct `hw-like` postselection is
being lost when using `dynamic_one_shot`.

**Description of the Change:**
`qml.devices.preprocess.mid_circuit_measurements` accepts an additional
`interface` kwarg, which is now being used in
`LightningQubit.preprocess`.

**Benefits:**
Expected behaviour with `mcm_method="one-shot"`,
`postselect_mode="hw-like"`, and `jax.jit`.

**Possible Drawbacks:**

**Related GitHub Issues:**

---------

Co-authored-by: ringo-but-quantum <github-ringo-but-quantum@xanadu.ai>
Co-authored-by: Ali Asadi <10773383+maliasadi@users.noreply.github.com>
mudit2812 added a commit that referenced this pull request Jul 26, 2024
**Context:**
As name says. Gradient workflows no longer raise errors after the merge
of #5791 , but their correctness is yet to be verified.

**Description of the Change:**
* Updated casting rules in `dynamic_one_shot`'s processing function for
tensorflow.
* For the changes to be fully integrated, the way that the interface is
passed around when calling a QNode needed to be changed, so the
following changes were made:
* `QNode` has updated behaviour for how `mcm_config` is used during
execution. In `QNode._execution_component`, a copy of
`self.execute_kwargs["mcm_config"]` is the source of truth, and in
`qml.execute`, `config.mcm_config` is the source of truth.
* Added a private `pad-invalid-samples` `postselect_mode`. The
`postselect_mode` is switched to this automatically in `qml.execute` if
executing with jax and shots and `postselect_mode == "hw-like"`. This
way we standardize how the MCM transforms determine if jax is being
used.
  * Updates to `capture` module to accommodate the above changes.

**Benefits:**
* `dynamic_one_shot` doesn't cast to interfaces inside the ML boundary
* `dynamic_one_shot` works with tensorflow
* Expanded tests

**Possible Drawbacks:**

**Related GitHub Issues:**
Fixes #5736, #5710 

Duplicate of #5861 which was closed due to release branch merge stuff.

---------

Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca>
Co-authored-by: Astral Cai <astral.cai@xanadu.ai>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Yushao Chen (Jerry) <chenys13@outlook.com>
Co-authored-by: Christina Lee <chrissie.c.l@gmail.com>
Co-authored-by: Thomas R. Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: soranjh <40344468+soranjh@users.noreply.github.com>
Co-authored-by: Pietropaolo Frisoni <pietropaolo.frisoni@xanadu.ai>
Co-authored-by: Ahmed Darwish <exclass9.24@gmail.com>
Co-authored-by: Utkarsh <utkarshazad98@gmail.com>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: Christina Lee <christina@xanadu.ai>
Co-authored-by: Mikhail Andrenkov <mikhail@xanadu.ai>
Co-authored-by: Diego <67476785+DSGuala@users.noreply.github.com>
Co-authored-by: Josh Izaac <josh146@gmail.com>
Co-authored-by: Diego <diego_guala@hotmail.com>
Co-authored-by: Vincent Michaud-Rioux <vincentm@nanoacademic.com>
Co-authored-by: lillian542 <38584660+lillian542@users.noreply.github.com>
Co-authored-by: Jack Brown <jack@xanadu.ai>
Co-authored-by: Paul Finlay <50180049+doctorperceptron@users.noreply.github.com>
Co-authored-by: David Ittah <dime10@users.noreply.github.com>
Co-authored-by: Cristian Emiliano Godinez Ramirez <57567043+EmilianoG-byte@users.noreply.github.com>
Co-authored-by: Vincent Michaud-Rioux <vincent.michaud-rioux@xanadu.ai>
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.

5 participants