qml.shadows.shadow_expval¶
-
shadow_expval
(H, k=1)[source]¶ Transform a QNode returning a classical shadow into one that returns the approximate expectation values in a differentiable manner.
See
shadow_expval()
for more usage details.- Parameters
H (
Observable
or list[Observable
]) – Observables for which to compute the expectation valuesk (int) – k (int): Number of equal parts to split the shadow’s measurements to compute the median of means.
k=1
corresponds to simply taking the mean over all measurements.
- Returns
1-D tensor containing the expectation value estimates for each observable
- Return type
tensor-like[float]
Example
H = qml.PauliZ(0) @ qml.PauliZ(1) dev = qml.device("default.qubit", wires=2, shots=10000) @qml.shadows.shadow_expval(H, k=1) @qml.qnode(dev) def circuit(x): qml.Hadamard(wires=0) qml.CNOT(wires=[0, 1]) qml.RX(x, wires=0) return qml.classical_shadow(wires=[0, 1])
>>> x = np.array(1.2) >>> circuit(x) tensor(0.3069, requires_grad=True) >>> qml.grad(circuit)(x) -0.9323999999999998
code/api/pennylane.shadows.shadow_expval
Download Python script
Download Notebook
View on GitHub