qp.shadows.shadow_state

shadow_state(tape, wires, diffable=False)[source]

Transform a circuit returning a classical shadow into one that returns the reconstructed state in a differentiable manner.

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

  • 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 transformed circuit as described in qp.transform. Executing this circuit will provide the reconstructed state in the form of a tensor.

Return type:

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

Example

import numpy as onp
from pennylane import numpy as np

dev = qp.device("default.qubit", wires=2, seed=42)

@qp.set_shots(shots=10000)
@qp.shadows.shadow_state(wires=[0, 1], diffable=True)
@qp.qnode(dev)
def circuit(x):
    qp.Hadamard(wires=0)
    qp.CNOT(wires=[0, 1])
    qp.RX(x, wires=0)
    return qp.classical_shadow(wires=[0, 1], seed=99)
>>> x = np.array(1.2, requires_grad=True)
>>> onp.random.seed(123)
>>> circuit(x)
array([[ 3.2582501e-01+0.j      , -8.2500000e-03+0.238425j,
         2.9999996e-04+0.230925j,  3.3142501e-01+0.009j   ],
       [-8.2500000e-03-0.238425j,  1.6337499e-01+0.j      ,
         1.5997499e-01-0.0099j  , -5.5499999e-03-0.251475j],
       [ 2.9999996e-04-0.230925j,  1.5997499e-01+0.0099j  ,
         1.5797499e-01+0.j      ,  2.9999996e-04-0.241275j],
       [ 3.3142501e-01-0.009j   , -5.5499999e-03+0.251475j,
         2.9999996e-04+0.241275j,  3.5282502e-01+0.j      ]],
      dtype=complex64)
>>> onp.random.seed(123)
>>> qp.jacobian(lambda x: np.real(circuit(x)))(x)
array([[-2.30550e-01,  5.62500e-03,  2.25000e-04, -2.29725e-01],
       [ 5.62500e-03,  2.30550e-01,  2.29725e-01, -1.57500e-03],
       [ 2.25000e-04,  2.29725e-01,  2.37900e-01,  2.47500e-03],
       [-2.29725e-01, -1.57500e-03,  2.47500e-03, -2.37900e-01]])