qml.shadows.shadow_state¶
-
shadow_state
(wires, diffable=False)[source]¶ Transform a QNode returning a classical shadow into one that returns the reconstructed state in a differentiable manner.
- Parameters
wires (list[int] or list[list[int]]) – If a list of ints, this represents the wires over which to reconstruct the state. If a list of list of ints, a state is reconstructed for every element of the outer list, saving qfunc evaluations.
diffable (bool) – If True, reconstruct the state in a differentiable fashion, where the gradient of the reconstructed state approaches the gradient of the true state in expectation. This comes at a performance cost.
- Returns
The reconstructed states
- Return type
list[tensor-like[complex]]
Example
dev = qml.device("default.qubit", wires=2, shots=10000) @qml.shadows.shadow_state(wires=[0, 1], diffable=True) @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.33714998+0.j , 0.007875 +0.228825j , -0.010575 +0.22642499j, 0.33705002+0.01125j ], [ 0.007875 -0.228825j , 0.16104999+0.j , 0.17055 -0.0126j , 0.011025 -0.232575j ], [-0.010575 -0.22642499j, 0.17055 +0.0126j , 0.16704999+0.j , -0.006075 -0.225225j ], [ 0.33705002-0.01125j , 0.011025 +0.232575j , -0.006075 +0.225225j , 0.33475 +0.j ]], dtype=complex64, requires_grad=True) >>> qml.jacobian(lambda x: np.real(circuit(x)))(x) array([[-0.245025, -0.005325, 0.004275, -0.2358 ], [-0.005325, 0.235275, 0.2358 , -0.004275], [ 0.004275, 0.2358 , 0.244875, -0.002175], [-0.2358 , -0.004275, -0.002175, -0.235125]])
code/api/pennylane.shadows.shadow_state
Download Python script
Download Notebook
View on GitHub