Skip to content

Commit

Permalink
Update QNode to not mutate original MCM config (#5850)
Browse files Browse the repository at this point in the history
As name says. Mutating the config in place was causing unexpected
results such as below. Now we create a copy of the original config
before execution begins, which can be mutated as needed.

```python
import pennylane as qml

dev = qml.device("default.qubit")

@qml.qnode(dev, postselect_mode="fill-shots")
def f(x, mp):
    qml.RX(x, 0)
    qml.measure(0, postselect=1)
    return mp(qml.PauliZ(0))
```
```pycon
>>> print(f(1.8, qml.sample, shots=10))
[-1. -1. -1. -1. -1. -1. -1. -1. -1. -1.]
>>> print(f(1.8, qml.expval))
-0.9999999999999998
>>> print(f(1.8, qml.sample, shots=10))
[-1. -1. -1. -1. -1. -1.]
```
  • Loading branch information
mudit2812 authored Jun 13, 2024
1 parent 2450f16 commit cdc36bc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
31 changes: 16 additions & 15 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,6 @@
[1 1 0]
```

* `qml.QNode` and `qml.qnode` now accept two new keyword arguments: `postselect_mode` and `mcm_method`.
These keyword arguments can be used to configure how the device should behave when running circuits with
mid-circuit measurements.
[(#5679)](https://github.com/PennyLaneAI/pennylane/pull/5679)
[(#5833)](https://github.com/PennyLaneAI/pennylane/pull/5833)

* `postselect_mode="hw-like"` will indicate to devices to discard invalid shots when postselecting
mid-circuit measurements. Use `postselect_mode="fill-shots"` to unconditionally sample the postselected
value, thus making all samples valid. This is equivalent to sampling until the number of valid samples
matches the total number of shots.
* `mcm_method` will indicate which strategy to use for running circuits with mid-circuit measurements.
Use `mcm_method="deferred"` to use the deferred measurements principle, or `mcm_method="one-shot"`
to execute once for each shot. If using `qml.jit` with the Catalyst compiler, `mcm_method="single-branch-statistics"`
is also available. Using this method, a single branch of the execution tree will be randomly explored.

* The `default.tensor` device is introduced to perform tensor network simulations of quantum circuits using the `mps` (Matrix Product State) method.
[(#5699)](https://github.com/PennyLaneAI/pennylane/pull/5699)

Expand Down Expand Up @@ -110,6 +95,22 @@

<h4>Mid-circuit measurements and dynamic circuits</h4>

* `qml.QNode` and `qml.qnode` now accept two new keyword arguments: `postselect_mode` and `mcm_method`.
These keyword arguments can be used to configure how the device should behave when running circuits with
mid-circuit measurements.
[(#5679)](https://github.com/PennyLaneAI/pennylane/pull/5679)
[(#5833)](https://github.com/PennyLaneAI/pennylane/pull/5833)
[(#5850)](https://github.com/PennyLaneAI/pennylane/pull/5850)

* `postselect_mode="hw-like"` will indicate to devices to discard invalid shots when postselecting
mid-circuit measurements. Use `postselect_mode="fill-shots"` to unconditionally sample the postselected
value, thus making all samples valid. This is equivalent to sampling until the number of valid samples
matches the total number of shots.
* `mcm_method` will indicate which strategy to use for running circuits with mid-circuit measurements.
Use `mcm_method="deferred"` to use the deferred measurements principle, or `mcm_method="one-shot"`
to execute once for each shot. If using `qml.jit` with the Catalyst compiler, `mcm_method="single-branch-statistics"`
is also available. Using this method, a single branch of the execution tree will be randomly explored.

* The `dynamic_one_shot` transform is made compatible with the Catalyst compiler.
[(#5766)](https://github.com/PennyLaneAI/pennylane/pull/5766)

Expand Down
2 changes: 1 addition & 1 deletion pennylane/workflow/qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ def _execution_component(self, args: tuple, kwargs: dict, override_shots) -> qml
)
self._tape_cached = using_custom_cache and self.tape.hash in cache

mcm_config = self.execute_kwargs["mcm_config"]
mcm_config = copy.copy(self.execute_kwargs["mcm_config"])
finite_shots = _get_device_shots if override_shots is False else override_shots
if not finite_shots:
mcm_config.postselect_mode = None
Expand Down
28 changes: 27 additions & 1 deletion tests/test_qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# pylint: disable=import-outside-toplevel, protected-access, no-member
import warnings
from dataclasses import replace
from dataclasses import asdict, replace
from functools import partial
from typing import Callable, Tuple

Expand Down Expand Up @@ -1839,6 +1839,32 @@ def circuit(x):
with pytest.raises(ValueError, match="Cannot use mcm_method='single-branch-statistics'"):
_ = circuit(param)

@pytest.mark.parametrize("postselect_mode", [None, "fill-shots", "hw-like"])
@pytest.mark.parametrize("mcm_method", [None, "one-shot", "deferred"])
def test_execution_does_not_mutate_config(self, mcm_method, postselect_mode):
"""Test that executing a QNode does not mutate its mid-circuit measurement config options"""
dev = qml.device("default.qubit", wires=2)

original_config = qml.devices.MCMConfig(
postselect_mode=postselect_mode, mcm_method=mcm_method
)

@qml.qnode(dev, **asdict(original_config))
def circuit(x, mp):
qml.RX(x, 0)
qml.measure(0, postselect=1)
return mp(qml.PauliZ(0))

_ = circuit(1.8, qml.expval, shots=10)
assert circuit.execute_kwargs["mcm_config"] == original_config

if mcm_method != "one-shot":
_ = circuit(1.8, qml.expval)
assert circuit.execute_kwargs["mcm_config"] == original_config

_ = circuit(1.8, qml.expval, shots=10)
assert circuit.execute_kwargs["mcm_config"] == original_config


class TestTapeExpansion:
"""Test that tape expansion within the QNode works correctly"""
Expand Down

0 comments on commit cdc36bc

Please sign in to comment.