Warn/raise in case shot noise and caching are enabled at the same time?
See original GitHub issueFeature details
When default.qubit with shots != None but also cache=1 is used then the same (noisy) result is returned again and again when a QNode is called. It is hard to imagine a case where this is what a user wants.
Either they want a QNode to return a noisy result that is different each time it is called (they can get this by setting cache=0), or they want only a single noisy result, in which case caching doesnāt provide any benefit.
I think it would be nice to raise an exception or at least a warning when a user makes a device with shot noise and caching enabled.
Alternatively the the device could go back to state caching, as it used to be in ancient times⦠This would give a speed benefit because the circuit would not need to be re-evaluated when the same shot-noisy QNode is called several times.
Implementation
To demonstrate the āproblemā consider this sample program:
import pennylane as qml
from pennylane import numpy as np
shots=76
dev = qml.device(
'default.qubit',
wires=2,
shots=shots,
cache=1,
)
np.random.seed(87)
params = np.random.randn(2, 2, 3)
observable_as_array = np.array([[ 1. +0.j, 0. +0.j, 0. +0.j, 0. +0.j],
[ 0. +0.j, 0.4+0.j, 21.6+0.j, 0. +0.j],
[ 0. +0.j, 21.6+0.j, -0.4+0.j, 0. +0.j],
[ 0. +0.j, 0. +0.j, 0. +0.j, -1. +0.j]])
@qml.qnode(dev, diff_method='parameter-shift')
def circuit(params):
qml.templates.layers.StronglyEntanglingLayers(params, wires=range(dev.num_wires))
return qml.expval(qml.Hermitian(observable_as_array, wires=range(dev.num_wires)))
for _ in range(10):
print(circuit(params))
How important would you say this feature is?
1: Not important. Would be nice to have.
Additional information
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (12 by maintainers)

Top Related StackOverflow Question
Yes, that is entirely correct.
Good points @trbromley, this is a good reason to support the same tape multiple times š¤
It doesnāt atm, but this is something I would like to add - allow
shotsto be metadata on tape objects.