I’m learning classical shadows and I’ve successfully reconstructed 10 qubits GHZ state.Now I have a shadow state,and I wanna calculate the fidelity between shadow state and standard 10 qubits GHZ state.I wanna know the exact python code for the fidelity computing.

Hello @DonaldTrump,

.

For calculating fidelities, you have two options: `qml.qinfo.fidelity`

and `qml.math.fidelity`

. Let’s look at two examples.

`qml.info.fidelity`

acts on circuits that return a quantum state using `qml.state()`

. First, let’s create two QNodes:

```
dev = qml.device('default.qubit', wires = 2) #create a two wire device
@qml.qnode(dev)
def circuit_1():
# Creates a two qubit GHZ state
qml.Hadamard(wires = 0)
qml.CNOT(wires = [0,1])
return qml.state()
@qml.qnode(dev)
def circuit_2(alpha):
# Creates an entangled state dependent on alpha
qml.RY(alpha, wires = 0)
qml.CNOT(wires = [0,1])
return qml.state()
```

We are going to find the fidelity between the output states for both circuits, for alpha = \pi/6.

To do this, we run

```
qml.qinfo.fidelity(circuit_1, circuit_2, wires0=[0,1], wires1=[0,1])(None,np.pi/6)
```

Note that we specified the wires for the state in both circuits using `wires0`

and `wires1`

. In this case, we want the whole state for both circuits and not only a reduced state. If we wanted a reduced state, we could specify only some of the wires.

Also note the `(None, np.pi/6)`

argument that is wrapped by the `qml.qinfo.fidelity`

. These are the arguments of `circuit_1`

and `circuit_2`

respectively. Since `circuit_1`

doesn’t depend on any parameters, we pass `None`

.

In the back end, `qml.qinfo.fidelity`

works using `qml.math.fidelity`

. This one is useful if you already have the states as **density matrices** and don’t care too much about the circuits that prepared the states. It works *only* on density matrices though, so you have to be careful to convert your state vectors into density matrices using `qml.math.dm_from_state_vector`

.

For example, if I want to find the fidelity between the states \vert 0 \rangle and \vert + \rangle = \frac{1}{\sqrt{2}}(\vert 0 \rangle + \vert 1 \rangle), we can express our states as arrays:

```
state_0 = [1,0]
state_1 = [1/np.sqrt(2), 1/np.sqrt(2)]
```

But running `qml.math.fidelity`

in the above will not work, so we transform to density matrices:

```
state_0 = qml.math.dm_from_state_vector(state_0)
state_1 = qml.math.dm_from_state_vector(state_1)
```

Finally we can run `qml.math.fidelity(state_0, state_1)`

and get the answer.

Hope one of these options will work for your use case! Let us know if you have additional questions!

Alvaro