##// END OF EJS Templates
avoid references to fiel out of directory...
avoid references to fiel out of directory request for packaging, it would be nice for example not to reference files outside of exampel directory copy ../../_static/logo.png in logo/logo.png use subfolder for demo purpose of targetting subfolder in demo notebook

File last commit:

r7739:dff285da
r9992:713f1db0
Show More
sympy_quantum_computing.ipynb
442 lines | 27.6 KiB | text/plain | TextLexer
/ docs / examples / notebooks / sympy_quantum_computing.ipynb

Basic Symbolic Quantum Mechanics with SymPy

We first load the IPython extensions that enable LaTeX-based mathematical printing of SymPy objects, and then import the quantum computing libraries from SymPy.

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

Bras and Kets

Create symbolic states

In [3]:
phi, psi = Ket('phi'), Ket('psi')
alpha, beta = symbols('alpha beta', complex=True)

Create a superposition

In [4]:
state = alpha*psi + beta*phi; state
Out[4]:
$$\alpha {\left|\psi\right\rangle } + \beta {\left|\phi\right\rangle }$$

Dagger the superposition and multiply the original

In [5]:
ip = Dagger(state)*state; ip
Out[5]:
$$\left(\overline{\alpha} {\left\langle \psi\right|} + \overline{\beta} {\left\langle \phi\right|}\right) \left(\alpha {\left|\psi\right\rangle } + \beta {\left|\phi\right\rangle }\right)$$

Distribute

In [6]:
qapply(expand(ip))
Out[6]:
$$\alpha \overline{\alpha} \left\langle \psi \right. {\left|\psi\right\rangle } + \alpha \overline{\beta} \left\langle \phi \right. {\left|\psi\right\rangle } + \beta \overline{\alpha} \left\langle \psi \right. {\left|\phi\right\rangle } + \beta \overline{\beta} \left\langle \phi \right. {\left|\phi\right\rangle }$$

Operators

Create symbolic operators

In [7]:
A = Operator('A')
B = Operator('B')
C = Operator('C')

Test commutativity

In [8]:
A*B == B*A
Out[8]:
False

Distribute A+B squared

In [9]:
expand((A+B)**2)
Out[9]:
$$A B + \left(A\right)^{2} + B A + \left(B\right)^{2}$$

Create a commutator

In [10]:
comm = Commutator(A,B); comm
Out[10]:
$$\left[A,B\right]$$

Carry out the commutator

In [11]:
comm.doit()
Out[11]:
$$A B - B A$$

Create a more fancy commutator

In [12]:
comm = Commutator(A*B,B+C); comm
Out[12]:
$$\left[A B,B + C\right]$$

Expand the commutator

In [13]:
comm.expand(commutator=True)
Out[13]:
$$\left[A,B\right] B + \left[A,C\right] B + A \left[B,C\right]$$

Carry out and expand the commutators

In [14]:
_.doit().expand()
Out[14]:
$$A B C + A \left(B\right)^{2} - B A B - C A B$$

Take the dagger

In [15]:
Dagger(_)
Out[15]:
$$- B^{\dagger} A^{\dagger} B^{\dagger} - B^{\dagger} A^{\dagger} C^{\dagger} + \left(B^{\dagger}\right)^{2} A^{\dagger} + C^{\dagger} B^{\dagger} A^{\dagger}$$