# Fidelity of quantum states

https://en.wikipedia.org/wiki/Fidelity_of_quantum_states

## Imports


```python
from IPython.display import display
```


```python
from sympy import init_printing
init_printing(use_latex=True)
```


```python
from sympy import *
from sympy.physics.quantum import *
from sympy.physics.quantum.density import *
from sympy.physics.quantum.spin import (
    Jx, Jy, Jz, Jplus, Jminus, J2,
    JxBra, JyBra, JzBra,
    JxKet, JyKet, JzKet,
)
from IPython.core.display import display_pretty
from sympy.physics.quantum.operator import *
```

## Basic fidelity using spin kets


```python
up = JzKet(S(1)/2,S(1)/2)
down = JzKet(S(1)/2,-S(1)/2)
amp = 1/sqrt(2)
updown = (amp * up ) + (amp * down)
updown
```

Using `represent` turns the kets into matrices:


```python
up_dm = represent(up * Dagger(up))
down_dm = represent(down * Dagger(down)) 
updown_dm = represent(updown * Dagger(updown))
updown_dm
```

Another entangled state:


```python
updown2 = (sqrt(3)/2 )* up + (1/2)*down
updown2
```


```python
fidelity(up_dm, up_dm)
```


```python
fidelity(up_dm, down_dm)
```


```python
fidelity(up_dm, updown_dm).evalf()
```

Alternatively, put kets into density operator and compute fidelity:


```python
d1 = Density( [updown, 0.25], [updown2, 0.75])
d2 = Density( [updown, 0.75], [updown2, 0.25])
fidelity(d1, d2)
```

## Fidelity with qubit states


```python
from sympy.physics.quantum.qubit import Qubit
state1 = Qubit('0')
state2 = Qubit('1')
state3 = (1/sqrt(2))*state1 + (1/sqrt(2))*state2
state4 = (sqrt(S(2)/3))*state1 + (1/sqrt(3))*state2
```


```python
state3
```


```python
state4
```


```python
state1_dm = Density([state1, 1])
state2_dm = Density([state2, 1])
state3_dm = Density([state3, 1])
```


```python
d1 = Density([state3, 0.70], [state4, 0.30])
d2 = Density([state3, 0.20], [state4, 0.80])
fidelity(d1, d2)
```

c1dca584bd7f1188e54fa689bd75708019907aaf
ipynb
Jupyter Notebook
notebooks/fidelity.ipynb
gvvynplaine/quantum_notebooks
58783823596465fe2d6c494c2cc3a53ae69a9752
BSD-3-Clause
2017-10-17T22:44:27.000Z
2022-03-28T06:26:46.000Z
notebooks/fidelity.ipynb
gvvynplaine/quantum_notebooks
58783823596465fe2d6c494c2cc3a53ae69a9752
BSD-3-Clause
2017-10-09T05:16:41.000Z
2018-09-22T03:08:29.000Z
notebooks/fidelity.ipynb
gvvynplaine/quantum_notebooks
58783823596465fe2d6c494c2cc3a53ae69a9752
BSD-3-Clause
2017-10-09T04:22:19.000Z
2022-03-28T06:25:21.000Z
Qwen/Qwen-72B
1. YES
2. YES