##// 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
basic_quantum.ipynb
281 lines | 30.8 KiB | text/plain | TextLexer

Basic Symbolic Quantum Mechanics

In [18]:
%load_ext sympyprinting
In [19]:
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 [20]:
phi, psi = Ket('phi'), Ket('psi')
alpha, beta = symbols('alpha beta', complex=True)

Create a superposition

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

Dagger the superposition and multiply the original

In [22]:
ip = Dagger(state)*state; ip
Out[22]:
$$\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 [23]:
qapply(expand(ip))
Out[23]:
$$\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 [24]:
A = Operator('A')
B = Operator('B')
C = Operator('C')

Test commutativity

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

Distribute A+B squared

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

Create a commutator

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

Carry out the commutator

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

Create a more fancy commutator

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

Expand the commutator

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

Carry out and expand the commutators

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

Take the dagger

In [32]:
Dagger(_)
Out[32]:
$$- 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}$$