# Symbolic Quantum Computing

## Imports


```python
%matplotlib inline
```


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


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


```python
from sympy import sqrt, symbols, Rational
from sympy import expand, Eq, Symbol, simplify, exp, sin, srepr
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
```

## Qubits


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


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


```python
Dagger(psi)
```


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


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

Qubits can be represented in the computational basis.


```python
represent(psi)
```

## Gates

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


```python
g = X(0)
g
```


```python
represent(g, nqubits=2)
```


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


```python
qapply(c)
```


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


```python
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()))
```


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


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

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


```python
gate_simp(c)
```


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

901e91ae3128b6cb221175afccc12e6845ea716b
ipynb
Jupyter Notebook
notebooks/qubits.ipynb
gvvynplaine/quantum_notebooks
58783823596465fe2d6c494c2cc3a53ae69a9752
BSD-3-Clause
2017-10-17T22:44:27.000Z
2022-03-28T06:26:46.000Z
notebooks/qubits.ipynb
gvvynplaine/quantum_notebooks
58783823596465fe2d6c494c2cc3a53ae69a9752
BSD-3-Clause
2017-10-09T05:16:41.000Z
2018-09-22T03:08:29.000Z
notebooks/qubits.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