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 dynamic_one_shot tensorflow support + expanded testing #5973

Merged
merged 63 commits into from
Jul 26, 2024

Conversation

mudit2812
Copy link
Contributor

@mudit2812 mudit2812 commented Jul 9, 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.

mudit2812 and others added 30 commits June 17, 2024 10:13
**Context:**
This is a follow up to this
[PR](#5789) which added
detailed documentation for the new PennyLane Debugger feature.

---------

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: Mudit Pandey <mudit.pandey@xanadu.ai>
Co-authored-by: Thomas R. Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: soranjh <40344468+soranjh@users.noreply.github.com>
#5904)

**Context:**
#5882 led to deprecation
warnings raised in non-applicable scenarios

**Description of the Change:**
Only raise deprecation warning if the circuit contains measurements

**Benefits:**
Less confusing deprecation warnings
**Context:** When creating an initial MPS from a dense state vector, the
order of wires was not preserved since we were not providing the
`wire_order` parameter to `state_vector`.

**Description of the Change:** As above.

**Benefits:** If the MPS is created from a dense state vector, now the
order of wires is preserved.

**Possible Drawbacks:** None that I can think of.

**Related GitHub Issues:** None.

**Related ShortCut Stories**: [sc-66787]
The objective of this PR is to reorganize the changelog into sections.

Please don't worry about the content in each bullet point, we can polish
that in follow up PRs.
**Context:**
Currently, `draw()` and `draw_mpl()` can only be requested after
applying the full transform program, with the exception of the stages
provided through expansion_strategy.

**Description of the Change:**
Using the new `level` argument from construct_batch, the functions are
adapted to make use of the argument as well, which allows for more
flexible requests and ability to pinpoint where exactly in the transform
program to draw a circuit. As done before, the new functionality works
with transforms that split the tape (only in the case for `draw`. For
`draw_mpl`, a warning is raised and only the first tape is plotted).

**Benefits:**
Better plotting UX.

**Note for Reviewers:**
Minor bugs in `construct_batch` have been discovered during work on this
PR, and so expect minor fixes to tests relating to `specs`.

[[sc-53735](https://app.shortcut.com/xanaduai/story/53735)] Supersedes
#5139

---------

Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
Co-authored-by: Thomas R. Bromley <49409390+trbromley@users.noreply.github.com>
**Context:** Adds a transform for adding `NoiseModels`.

**Description of the Change:** Adds `add_noise.py` under
`pennylane/transforms` that gives the said method for inserting
operations according to a provided noise model.

**Benefits:** We support noise models. 

**Possible Drawbacks:** None.

**Related GitHub Issues:** [sc-64843]

---------

Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca>
**Context:**

**Description of the Change:**
Takes the case into account where `qml.probs` does not have wires
specified, requiring `hadamard_grad` to consider tape wires.

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**
Fixes #5840 

[sc-65711]
Automatic update of stable requirement files to snapshot valid python
environments.

Because bots are not able to trigger CI on their own, please do so by
pushing an empty commit to this branch using the following command:

```
git commit --allow-empty -m 'trigger ci'
```

Alternatively, wait for this branch to be out-of-date with master, then
just use the "Update branch" button!

Note that it is expected that the PennyLane-Lightning repo is a version
ahead of the release, because the
version number is taken from the dev branch. Trying to `pip install`
from the files will fail until that
major version of Lightning is released.

If pip install fails with a not found error when installing because of
this, it can be fixed by manually
downgrading the PennyLane-Lightning version number in the file by 1
version and trying again.

---------

Co-authored-by: GitHub Actions Bot <>
Co-authored-by: Christina Lee <christina@xanadu.ai>
**Context:**
Adding documentation for the new debugger feature.

---------

Co-authored-by: Mikhail Andrenkov <mikhail@xanadu.ai>
Co-authored-by: Utkarsh <utkarshazad98@gmail.com>
Co-authored-by: Diego <67476785+DSGuala@users.noreply.github.com>
Co-authored-by: Josh Izaac <josh146@gmail.com>
Co-authored-by: soranjh <40344468+soranjh@users.noreply.github.com>
Co-authored-by: Diego <diego_guala@hotmail.com>
…asets (#5484)

**Context:**
Users have trouble finding all `bondlength` and `basis` combinations for
qchem molecules. They end up using the slider on the website to check
which bond lengths are available one-by-one.

![image](https://github.com/PennyLaneAI/pennylane/assets/67476785/fe2686b9-8f37-4e4e-854f-bf4708e0900a)


**Description of the Change:**
- Updated the quickstart guide to say "viewing available datasets"
instead of "dataset structure"
- Updated the `qml.data` docs to encourage users to first read the
quickstart guide

**Benefits:**
Users should be more likely to reach the `list_datasets` description in
the quickstart guide and know that it can be used to view the available
datasets.

**Possible Drawbacks:**
Users who use pennylane.ai/datasets but don't go to the documentation
still won't see this.

---------

Co-authored-by: Utkarsh <utkarshazad98@gmail.com>
**Context:**
The PennyLane documentation has a quickstart page on measurements, which
includes a large section about mid-circuit measurements.
With the focus on dynamic circuits and mid-circuit measurements more
generally, we would like to make the content on MCMs more visible.

**Description of the Change:**
Extracts the MCM part of the measurements quickstart and adds it as its
own quickstart page.
Adds some details on simulation techniques and polishes the content.

**Benefits:**
Visibility, documentation extent for MCMs

**Possible Drawbacks:**

**Related GitHub Issues:**

[sc-65318]

---------

Co-authored-by: Vincent Michaud-Rioux <vincentm@nanoacademic.com>
**Context:**
Previously, the QASM circuit converted to PL had no measurements (even
if the circuit ended with measurements, the assumption was we remove
those and add PL measurements once the circuit is converted).

In the UnitaryHack, a contributor added the option to specify
measurements when converting a circuit `from_qasm`, like the
`from_qiskit` function. In doing so, the `from_qasm` circuit took on the
current `from_qiskit` convention. Calling `from_qasm(qasm_str)`
previously returned only the operators, and now returns operators +
measurements. This is a breaking change with no deprecation cycle.

PennyLaneAI/pennylane-qiskit#469

**Description of the Change:**
Adds a deprecation warning to `from_qasm`

[sc-66318]

---------

Co-authored-by: lillian542 <38584660+lillian542@users.noreply.github.com>
Co-authored-by: Christina Lee <christina@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
**Context:**
The `from_openfermion` function is updated to supports the conversion of
qubit operators in addition to fermionic operators.

**Description of the Change:**

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**
**Context:**
PyTrees are an ideal format for dataset attributes, since they provide a
compact representation of deeply nested structures, and Pennylane
Operators can easily be converted to and from pytrees.

Shortcut story:
https://app.shortcut.com/xanaduai/story/63174/datasets-serialization-using-pytrees

**Description of the Change:**
- Adds a `pytree.serialization` module for converting `PyTreeStructure`
objects to and from a JSON representation
- Adds a `DatasetPyTree` data attribute type that can store any
pytree-compatible type
- `DatasetPyTree` is now the default attribute type for `Operator`
- The `pytree.unflatten` method disables recording, to prevent
deserialized operators from being queued

**Benefits:**
- Datasets support any Pennylane object with a Pytree representation and
serializable metadata
- Complex serialization logic in ``DatasetOperator`` can be deprecated
- Reduces file size of datasets

**Possible Drawbacks:**
- Pytree datasets will not be compatible with versions < 0.37
- Type information for metadata is not preserved - `tuple`, `Wires`, and
`Shots` objects will all be converted
to `list` on a round-trip. `__init__` methods are responsible for
ensuring that metadata is converted to the correct type

---------

Co-authored-by: albi3ro <chrissie.c.l@gmail.com>
Co-authored-by: Christina Lee <christina@xanadu.ai>
Co-authored-by: Thomas R. Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
Co-authored-by: Paul Finlay <50180049+doctorperceptron@users.noreply.github.com>
Co-authored-by: Diego <67476785+DSGuala@users.noreply.github.com>
Co-authored-by: Utkarsh <utkarshazad98@gmail.com>
Co-authored-by: Diego <diego_guala@hotmail.com>
**Context:**

Catalyst PR #837 (PennyLaneAI/catalyst#837)
needs a couple minor updates to the capture module.

**Description of the Change:**

1) makes it possible to do `from pennylane.capture import
AbstractOperator, AbstractMeasurement, qnode_prim` so we don't have to
touch private functions

2) Adds `qnode` as a keyword argument that gets bound to the qnode
primitive

3) Makes it so we can capture a sample measurement specified like
`qml.sample(wires=1)`

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**
[sc-66703]

---------

Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
…convention changes. (#5853)

**Context:**
Allow for `qml.qsvt()` to accept phase angles as jax arrays and perform
convention conversions.

---------

Co-authored-by: David Ittah <dime10@users.noreply.github.com>
**Context:**

Currently the decomposition of a Pauli Rotation with an identity as
Pauli word returns an empty list. I.e,

```python
qml.PauliRot(theta, Pauli_word="I").decomposition() == []
```

**Description of the Change:**

Implement the correct decomposition for this case using
`qml.GlobalPhase`

**Benefits:**

The `TrotterProduct` for Hamiltonians with an identity using
`default.qubit` and `default.lightning` now return the same correct
result.

```python
import pennylane as qml
import numpy as np

symbols = ["H", "H"]
coordinates = np.array([[0.0, 0.0, -0.6614], [0.0, 0.0, 0.6614]])
H, num_qubits = qml.qchem.molecular_hamiltonian(symbols, coordinates, basis="sto-3g")

results = []
for dev_name in ["default.qubit", "lightning.qubit"]:
    dev = qml.device(dev_name, wires=num_qubits)
    @qml.qnode(dev)
    def circuit():
        qml.TrotterProduct(H, 1.0)
        return qml.state()
    results.append(circuit())
qml.math.allclose(*results)

>> True
```

**Related GitHub Issues:**
[#5859]

[sc-66115]
**Context:**
Some updates are being made to Catalyst for mid-circuit measurements
that are dependent on the changes in this PR.

**Description of the Change:**
* Change reference in `dynamic_one_shot` to catalyst's
`MidCircuitMeasure.out_classical_tracers` to
`MidCircuitMeasure.mcm_tracer`.
* Change `MidMeasureMP.name` to be more general. This is because
`MidCircuitMeasure` will soon inherit `MidMeasureMP`, and this change
makes more sense than adding a `name` property to `MidCircuitMeasure`.

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**
@vincentmr vincentmr self-requested a review July 19, 2024 21:20
Copy link
Contributor

@vincentmr vincentmr left a comment

Choose a reason for hiding this comment

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

@mudit2812 Nice PR @mudit2812! I think not relying on implicit rules and introducing "pad-invalid-samples" will make the code more robust. Kudos for disentangling the mcm_config spaghetti. The PR description says: "Add differentiation tests for jax, torch, autograd, tensorflow", am I correct that tests are limited to test_finite_diff_in_transform_program and we do not validate correctness yet? Will this PR's changes impact old-API devices and has it been sorted out?

doc/releases/changelog-dev.md Show resolved Hide resolved
pennylane/devices/qubit/apply_operation.py Outdated Show resolved Hide resolved
pennylane/transforms/dynamic_one_shot.py Show resolved Hide resolved
pennylane/transforms/dynamic_one_shot.py Outdated Show resolved Hide resolved
pennylane/transforms/dynamic_one_shot.py Outdated Show resolved Hide resolved
pennylane/workflow/execution.py Show resolved Hide resolved
pennylane/workflow/execution.py Show resolved Hide resolved
pennylane/workflow/qnode.py Outdated Show resolved Hide resolved
pennylane/workflow/qnode.py Show resolved Hide resolved
Copy link

codecov bot commented Jul 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.65%. Comparing base (b7675ca) to head (0afaa2c).
Report is 5 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5973      +/-   ##
==========================================
- Coverage   99.66%   99.65%   -0.01%     
==========================================
  Files         428      430       +2     
  Lines       41182    41158      -24     
==========================================
- Hits        41042    41017      -25     
- Misses        140      141       +1     

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

@mudit2812
Copy link
Contributor Author

The PR description says: "Add differentiation tests for jax, torch, autograd, tensorflow", am I correct that tests are limited to test_finite_diff_in_transform_program and we do not validate correctness yet?

@vincentmr the PR description is a bit outdated. Because finite diff behaves weirdly with shots, the variance in the gradient value is so high that it's not really possible to test for gradient correctness. I'll remove that line from the PR description.

@mudit2812 mudit2812 requested a review from vincentmr July 22, 2024 18:15
Copy link
Contributor

@vincentmr vincentmr left a comment

Choose a reason for hiding this comment

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

Just a couple questions from my side, but then I'm ready to approve. Nice PR @mudit2812 .

pennylane/devices/qubit/apply_operation.py Outdated Show resolved Hide resolved
pennylane/measurements/mid_measure.py Show resolved Hide resolved
pennylane/transforms/dynamic_one_shot.py Outdated Show resolved Hide resolved
mudit2812 and others added 2 commits July 24, 2024 11:25
Co-authored-by: Vincent Michaud-Rioux <vincent.michaud-rioux@xanadu.ai>
@mudit2812 mudit2812 requested review from albi3ro and removed request for albi3ro July 24, 2024 15:44
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.

🎉 Definitely like the "pad-invalid-samples" postselect mode of doing things. Thanks for your patience with this PR :)

@mudit2812 mudit2812 enabled auto-merge (squash) July 26, 2024 18:16
@mudit2812 mudit2812 merged commit eb7c349 into master Jul 26, 2024
40 checks passed
@mudit2812 mudit2812 deleted the dos-grad-tests branch July 26, 2024 19:02
mudit2812 added a commit to PennyLaneAI/pennylane-lightning that referenced this pull request Jul 26, 2024
**Context:**
Changes needed after PennyLaneAI/pennylane#5973
gets merged for correctness.

**Description of the Change:**

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**

---------

Co-authored-by: ringo-but-quantum <github-ringo-but-quantum@xanadu.ai>
mudit2812 added a commit to PennyLaneAI/catalyst that referenced this pull request Jul 26, 2024
**Context:**
As name says. Change needed for correctness after
PennyLaneAI/pennylane#5973 gets merged.

**Description of the Change:**
Use `postselect_mode` instead of `interface` when calling postprocessing
function in `dynamic_one_shot`.

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**
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.

[BUG] Differentiation does not work with dynamic_one_shot