# Classical shadows in calculating fidelity

**URL:** <https://discuss.pennylane.ai/t/classical-shadows-in-calculating-fidelity/3727>\
**Category:** Demos\
**Created:** [November 25, 2023, 8:19am UTC](https://discuss.pennylane.ai/t/classical-shadows-in-calculating-fidelity/3727 "2023-11-25T08:19:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DonaldTrump](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/donaldtrump/32/2096_2.png) [@DonaldTrump](https://discuss.pennylane.ai/u/DonaldTrump)\
**Post date:** [November 25, 2023, 8:19am UTC](https://discuss.pennylane.ai/t/classical-shadows-in-calculating-fidelity/3727/1 "2023-11-25T08:19:56Z")

</div>

I’m learning [classical shadows](https://pennylane.ai/qml/demos/tutorial_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.

---

<div class="post-metadata">

**Author:** ![Alvaro\_Ballon](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/alvaro_ballon/32/874_2.png) [@Alvaro\_Ballon](https://discuss.pennylane.ai/u/Alvaro_Ballon)\
**Post date:** [November 27, 2023, 7:30pm UTC](https://discuss.pennylane.ai/t/classical-shadows-in-calculating-fidelity/3727/2 "2023-11-27T19:30:38Z")

</div>

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:

```auto
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

```auto
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:

```auto
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:

```auto
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
