##// END OF EJS Templates
Merge pull request #802 from pv/enh/extension-docs...
Merge pull request #802 from pv/enh/extension-docs Lists the extensions that are bundled with IPython in our docs.

File last commit:

r4637:d919e2ec
r4908:a28aa56c merge
Show More
quantum_computing.ipynb
424 lines | 76.1 KiB | text/plain | TextLexer

Symbolic Quantum Computing

In [1]:
%load_ext sympyprinting
In [2]:
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

Qubits

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

In [3]:
alpha, beta = symbols('alpha beta',real=True)
In [4]:
psi = alpha*Qubit('00') + beta*Qubit('11'); psi
Out[4]:
$$\alpha {\left|00\right\rangle } + \beta {\left|11\right\rangle }$$
In [5]:
Dagger(psi)
Out[5]:
$$\alpha {\left\langle 00\right|} + \beta {\left\langle 11\right|}$$
In [6]:
qapply(Dagger(Qubit('00'))*psi)
Out[6]:
$$\alpha$$

SymPy supports many different types of measurements.

In [7]:
for state, prob in measure_all(psi):
    display(state)
    display(prob)
$${\left|00\right\rangle }$$
$$\frac{\alpha^{2}}{\alpha^{2} + \beta^{2}}$$
$${\left|11\right\rangle }$$
$$\frac{\beta^{2}}{\alpha^{2} + \beta^{2}}$$

Qubits can be represented in the computational basis.

In [8]:
represent(psi)
Out[8]:
$$\left(\begin{smallmatrix}\alpha\\0\\0\\\beta\end{smallmatrix}\right)$$

Gates

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

In [9]:
g = X(0); g
Out[9]:
$$X_{0}$$
In [10]:
represent(g, nqubits=2)
Out[10]:
$$\left(\begin{smallmatrix}0 & 1 & 0 & 0\\1 & 0 & 0 & 0\\0 & 0 & 0 & 1\\0 & 0 & 1 & 0\end{smallmatrix}\right)$$
In [11]:
c = H(0)*Qubit('00'); c
Out[11]:
$$H_{0} {\left|00\right\rangle }$$
In [12]:
qapply(c)
Out[12]:
$$\frac{1}{2} \sqrt{2} {\left|00\right\rangle } + \frac{1}{2} \sqrt{2} {\left|01\right\rangle }$$
In [13]:
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))
$$H_{0} {\left|0\right\rangle } = \frac{1}{2} \sqrt{2} {\left|0\right\rangle } + \frac{1}{2} \sqrt{2} {\left|1\right\rangle }$$
$$H_{0} {\left|1\right\rangle } = \frac{1}{2} \sqrt{2} {\left|0\right\rangle } - \frac{1}{2} \sqrt{2} {\left|1\right\rangle }$$
$$X_{0} {\left|0\right\rangle } = {\left|1\right\rangle }$$
$$X_{0} {\left|1\right\rangle } = {\left|0\right\rangle }$$
$$Y_{0} {\left|0\right\rangle } = \mathbf{\imath} {\left|1\right\rangle }$$
$$Y_{0} {\left|1\right\rangle } = - \mathbf{\imath} {\left|0\right\rangle }$$
$$Z_{0} {\left|0\right\rangle } = {\left|0\right\rangle }$$
$$Z_{0} {\left|1\right\rangle } = - {\left|1\right\rangle }$$
$$S_{0} {\left|0\right\rangle } = {\left|0\right\rangle }$$
$$S_{0} {\left|1\right\rangle } = \mathbf{\imath} {\left|1\right\rangle }$$
$$T_{0} {\left|0\right\rangle } = {\left|0\right\rangle }$$
$$T_{0} {\left|1\right\rangle } = e^{\frac{1}{4} \mathbf{\imath} \pi} {\left|1\right\rangle }$$

Symbolic gate rules and circuit simplification

In [14]:
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()))
$$\left[Y_{0},Z_{0}\right] = 2 \mathbf{\imath} X_{0}$$
$$- \left[H_{0},Y_{0}\right] = - \sqrt{2} \mathbf{\imath} \left(- X_{0} + Z_{0}\right)$$
$$- \left[Y_{0},Z_{0}\right] = - 2 \mathbf{\imath} X_{0}$$
$$- \left[H_{0},Z_{0}\right] = \sqrt{2} \mathbf{\imath} Y_{0}$$
$$\left[H_{0},Y_{0}\right] = \sqrt{2} \mathbf{\imath} \left(- X_{0} + Z_{0}\right)$$
$$\left[H_{0},Z_{0}\right] = - \sqrt{2} \mathbf{\imath} Y_{0}$$
In [15]:
c = H(0)*X(1)*H(0)**2*CNOT(0,1)*X(1)**3*X(0)*Z(1)**2; c
Out[15]:
$$H_{0} X_{1} \left(H_{0}\right)^{2} CNOT_{0,1} X_{1} X_{0}$$
In [16]:
circuit_plot(c, nqubits=2)
Out[16]:
<sympy.physics.quantum.circuitplot.CircuitPlot object at 0x48cd350>
No description has been provided for this image

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

In [17]:
gate_simp(c)
Out[17]:
$$H_{0} CNOT_{0,1} X_{0}$$
In [18]:
circuit_plot(gate_simp(c),nqubits=2)
Out[18]:
<sympy.physics.quantum.circuitplot.CircuitPlot object at 0x3da1990>
No description has been provided for this image