Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@
(np.float64(1.0), np.float64(0.58))
```

* A new device preprocess transform, `~.devices.preprocess.no_analytic`, is available for hardware devices and hardware-like simulators.
It validates that all executions are shot-based.
[(#8037)](https://github.com/PennyLaneAI/pennylane/pull/8037)

<h4>OpenQASM-PennyLane interoperability</h4>

* The :func:`qml.from_qasm3` function can now convert OpenQASM 3.0 circuits that contain
Expand Down Expand Up @@ -551,6 +555,7 @@ Joey Carter,
Yushao Chen,
Diksha Dhawan,
Marcus Edwards,
Lillian Frederiksen,
Pietropaolo Frisoni,
Simone Gasperini,
David Ittah,
Expand Down
21 changes: 21 additions & 0 deletions pennylane/devices/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,27 @@ def no_sampling(
return (tape,), null_postprocessing


@transform
def no_analytic(
tape: QuantumScript, name: str = "device"
) -> tuple[QuantumScriptBatch, PostprocessingFn]:
"""Raises an error if the tape does not have finite shots.
Args:
tape (QuantumTape or .QNode or Callable): a quantum circuit
name (str): name to use in error message.
Returns:
qnode (QNode) or quantum function (Callable) or tuple[List[.QuantumTape], function]:
The unaltered input circuit. The output type is explained in :func:`qml.transform <pennylane.transform>`.


This transform can be added to forbid analytic results. This is relevant for devices
that can only return samples and/or counts based results.
"""
if not tape.shots:
raise DeviceError(f"Analytic execution is not supported with {name}")
return (tape,), null_postprocessing


@transform
def validate_device_wires(
tape: QuantumScript, wires: qml.wires.Wires | None = None, name: str = "device"
Expand Down
13 changes: 13 additions & 0 deletions tests/devices/test_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
measurements_from_counts,
measurements_from_samples,
mid_circuit_measurements,
no_analytic,
no_sampling,
null_postprocessing,
validate_adjoint_trainable_params,
Expand Down Expand Up @@ -149,6 +150,18 @@ def test_no_sampling():
no_sampling(tape2, name="abc")


def test_no_analytic():
"""Test for the no_anayltic transform"""

tape1 = qml.tape.QuantumScript(shots=2)
batch, _ = no_analytic(tape1)
assert batch[0] is tape1

tape2 = qml.tape.QuantumScript(shots=None)
with pytest.raises(DeviceError, match="Analytic execution is not supported with abc"):
no_analytic(tape2, name="abc")


def test_validate_adjoint_trainable_params_obs_warning():
"""Tests warning raised for validate_adjoint_trainable_params with trainable observables."""

Expand Down