qml.shadows.shadow_expval

shadow_expval(tape, H, k=1)[source]

Transform a circuit returning a classical shadow into one that returns the approximate expectation values in a differentiable manner.

See shadow_expval() for more usage details.

Parameters
  • tape (QNode or QuantumTape or Callable) – A quantum circuit.

  • H (Observable or list[Observable]) – Observables for which to compute the expectation values

  • k (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

The transformed circuit as described in qml.transform. Executing this circuit will provide the expectation value estimates for each observable in the form of a tensor.

Return type

qnode (QNode) or quantum function (Callable) or tuple[List[QuantumTape], function]

Example

H = qml.Z(0) @ qml.Z(1)
dev = qml.device("default.qubit", wires=2, shots=10000)

@partial(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)
[array(0.3528)]
>>> qml.grad(circuit)(x)
-0.9323999999999998