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 #5861

Closed
wants to merge 32 commits into from

Conversation

mudit2812
Copy link
Contributor

@mudit2812 mudit2812 commented Jun 17, 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:

  • Add differentiation tests for jax, torch, autograd, tensorflow

Benefits:

Possible Drawbacks:

Related GitHub Issues:
Fixes #5736, #5710

@mudit2812 mudit2812 marked this pull request as draft June 17, 2024 14:18
@mudit2812 mudit2812 linked an issue Jun 17, 2024 that may be closed by this pull request
1 task
@mudit2812
Copy link
Contributor Author

[sc-63943] [sc-63618]

Copy link

codecov bot commented Jun 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (v0.37.0-rc0@5c7ae2b). Learn more about missing BASE report.

Additional details and impacted files
@@              Coverage Diff               @@
##             v0.37.0-rc0    #5861   +/-   ##
==============================================
  Coverage               ?   99.66%           
==============================================
  Files                  ?      425           
  Lines                  ?    40465           
  Branches               ?        0           
==============================================
  Hits                   ?    40331           
  Misses                 ?      134           
  Partials               ?        0           

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

@pytest.mark.parametrize("mcm_method", ["one-shot", "tree-traversal"])
@pytest.mark.parametrize("shots", [100, [100, 101], [100, 100, 101]])
@pytest.mark.parametrize("postselect", [None, 0, 1])
def test_sample_with_prng_key(self, mcm_method, shots, postselect):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test and the one below it have just been moved into the TestJaxIntegration class so no need to be thorough reviewing the aforementioned two tests

@mudit2812 mudit2812 marked this pull request as ready for review June 21, 2024 17:37
@mudit2812 mudit2812 changed the title Add dynamic_one_shot differentiation tests Add dynamic_one_shot tensorflow support + expanded testing Jun 21, 2024
@mudit2812 mudit2812 added this to the v0.37 milestone Jun 21, 2024
@albi3ro albi3ro self-requested a review June 21, 2024 17:57
mcm_samples = qml.math.array(mcm_samples, like=interface)
# Can't use boolean dtype array with tf, hence why conditionally setting items to 0 or 1
has_postselect = qml.math.array(
[[int(op.postselect is not None) for op in all_mcms]], like=interface
[[int(op.postselect is not None) for op in all_mcms]],
like=interface,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we want to convert back to interface data here. If we call dynamic_one_shot after we've converted to numpy, we don't want to convert back into interface data.

Right now it looks like we are still calling convert_to_numpy_parameters after device preprocessing, but I could very easily see that changing in the future.

I was under the assumption that interface was passed to this function for the purpose of determining jit-specific postselection handling.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we could have a "postselection interface" and "result data interface"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was under the assumption that interface was passed to this function for the purpose of determining jit-specific postselection handling.

Yes, for pennylane that is the case, so I can definitely spend time investigating if we really need to convert to interface data, but for Catalyst we need to propagate jax data correctly.

dev = get_device(shots=shots, seed=PRNGKey(678))
params = [np.pi / 4, np.pi / 3]
obs = qml.PauliZ(0) @ qml.PauliZ(1)
def test_finite_diff_in_transform_program(interface, mcm_method):
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not entirely sure this is a mcm test if the diff method is explicitly specified as "finite-diff". What happens if diff_method="best"? Should we automatically switch to finite diff, or give a warning?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then we use parameter shift, but parameter shift is not correct if there is postselection or classical control.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we automatically switch to finite diff, or give a warning?

I think it makes sense to switch to finite diff.

@mudit2812 mudit2812 changed the base branch from master to v0.37.0-rc0 July 2, 2024 19:32
Shiro-Raven and others added 6 commits July 2, 2024 15:34
**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>
dwierichs and others added 13 commits July 2, 2024 15:34
**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:**
**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.
@mudit2812 mudit2812 requested a review from a team as a code owner July 2, 2024 19:34
@astralcai astralcai removed this from the v0.37 milestone Jul 5, 2024
@astralcai astralcai deleted the branch v0.37.0-rc0 July 9, 2024 13:37
@astralcai astralcai closed this Jul 9, 2024
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