##// END OF EJS Templates
Merge pull request #2124 from bfroehle/use_alias_magic...
Merge pull request #2124 from bfroehle/use_alias_magic Add an API for registering magic aliases. Add a method `register_alias` to `MagicsManager` which can be used to register new magic aliases. Each magic alias is an instance of `MagicAlias`, a helper class whose `__call__` looks up the target of the alias (at call time) and dispatches the magic call. As a future benefit, this could be easily extended to allow for new aliases which contain some flags to pass to the function. For example, it would be easy to change the behavior to allow the creation of an `%ex` alias for `%edit -x`.

File last commit:

r7739:dff285da
r8023:a5beb59f merge
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}$$