<h1>Symbolic Quantum Computing</h1>


```
%load_ext sympyprinting
```


```
from sympy import sqrt, symbols, Rational
from sympy import expand, Eq, Symbol, simplify, exp, sin
from sympy.physics.quantum import *
from sympy.physics.quantum.qubit import *
from sympy.physics.quantum.gate import *
from sympy.physics.quantum.grover import *
from sympy.physics.quantum.qft import QFT, IQFT, Fourier
from sympy.physics.quantum.circuitplot import circuit_plot
```

<h2>Qubits</h2>

The state of a set of qubits (Two state systems) is the quantum state that is of interest in Quantum Computing.


```
alpha, beta = symbols('alpha beta',real=True)
```


```
psi = alpha*Qubit('00') + beta*Qubit('11'); psi

```


```
Dagger(psi)

```


```
qapply(Dagger(Qubit('00'))*psi)

```

SymPy supports many different types of measurements.


```
for state, prob in measure_all(psi):
    display(state)
    display(prob)

```

Qubits can be represented in the computational basis.


```
represent(psi)

```




$$\left(\begin{smallmatrix}\alpha\\0\\0\\\beta\end{smallmatrix}\right)$$



<h2>Gates</h2>

Gate objects are the operators which act on a quantum state.


```
g = X(0); g

```


```
represent(g, nqubits=2)

```




$$\left(\begin{smallmatrix}0 & 1 & 0 & 0\\1 & 0 & 0 & 0\\0 & 0 & 0 & 1\\0 & 0 & 1 & 0\end{smallmatrix}\right)$$




```
c = H(0)*Qubit('00'); c

```


```
qapply(c)

```


```
for gate in [H,X,Y,Z,S,T]:
    for state in [Qubit('0'),Qubit('1')]:
        lhs = gate(0)*state
        rhs = qapply(lhs)
        display(Eq(lhs,rhs))
```

<h2>Symbolic gate rules and circuit simplification</h2>


```
for g1 in (Y,Z,H):
    for g2 in (Y,Z,H):
        e = Commutator(g1(0),g2(0))
        if g1 != g2:
            display(Eq(e,e.doit()))

```


```
c = H(0)*X(1)*H(0)**2*CNOT(0,1)*X(1)**3*X(0)*Z(1)**2; c

```


```
circuit_plot(c, nqubits=2)
```

This performs a commutator/anticommutator aware bubble sort algorithm to simplify a circuit.


```
gate_simp(c)

```


```
circuit_plot(gate_simp(c),nqubits=2)
```

175f2eb01bc01973bdc56889da8a6aea71348e25
ipynb
Jupyter Notebook
docs/examples/notebooks/quantum_computing.ipynb
tinyclues/ipython
71e32606b0242772b81c9be0d40751ba47d95f2c
BSD-3-Clause-Clear
2016-05-26T10:57:18.000Z
2016-05-26T10:57:18.000Z
docs/examples/notebooks/quantum_computing.ipynb
adgaudio/ipython
a924f50c0f7b84127391f1c396326258c2b303e2
BSD-3-Clause-Clear
docs/examples/notebooks/quantum_computing.ipynb
adgaudio/ipython
a924f50c0f7b84127391f1c396326258c2b303e2
BSD-3-Clause-Clear
Qwen/Qwen-72B
1. YES
2. YES