Examples of using BosonOperator and FermionOperator

Note: This notebook requires and experimental branch of sympy available at: https://github.com/jrjohansson/sympy

In [1]:
from sympy import *
init_printing()
In [2]:
from sympy.physics.quantum import *
from sympy.physics.quantum.boson import *
from sympy.physics.quantum.fermion import *
from sympy.physics.quantum.operatorordering import *

Docstrings

In [3]:
#help(BosonOp)
In [4]:
#help(FermionOp)
In [5]:
help(normal_order)
Help on function normal_order in module sympy.physics.quantum.operatorordering:

normal_order(expr, recursive_limit=10, _recursive_depth=0)
    Normal order an expression with bosonic or fermionic operators. Note
    that this normal order is not equivalent to the original expression, but
    the creation and annihilation operators in each term in expr is reordered
    so that the expression becomes normal ordered.
    
    Parameters
    ==========
    
    expr : expression
        The expression to normal order.
    
    recursive_limit : int (default 10)
        The number of allowed recursive applications of the function.
    
    Examples
    ========
    
    >>> from sympy.physics.quantum import Dagger
    >>> from sympy.physics.quantum.boson import BosonOp
    >>> from sympy.physics.quantum.operatorordering import normal_order
    >>> a = BosonOp("a")
    >>> normal_order(a * Dagger(a))
    Dagger(a)*a

In [6]:
help(normal_ordered_form)
Help on function normal_ordered_form in module sympy.physics.quantum.operatorordering:

normal_ordered_form(expr, independent=False, recursive_limit=10, _recursive_depth=0)
    Write an expression with bosonic or fermionic operators on normal
    ordered form, where each term is normally ordered. Note that this
    normal ordered form is equivalent to the original expression.
    
    Parameters
    ==========
    
    expr : expression
        The expression write on normal ordered form.
    
    recursive_limit : int (default 10)
        The number of allowed recursive applications of the function.
    
    Examples
    ========
    
    >>> from sympy.physics.quantum import Dagger
    >>> from sympy.physics.quantum.boson import BosonOp
    >>> from sympy.physics.quantum.operatorordering import normal_ordered_form
    >>> a = BosonOp("a")
    >>> normal_ordered_form(a * Dagger(a))
    1 + Dagger(a)*a

Test Boson operators

In [7]:
a = BosonOp('a')
b = BosonOp('b')
In [8]:
a.is_annihilation, Dagger(a).is_annihilation
Out[8]:
(True, False)
In [9]:
Commutator(a, Dagger(a)).doit()
Out[9]:
$$1$$
In [10]:
Commutator(Dagger(a), a).doit()
Out[10]:
$$-1$$
In [11]:
Commutator(a, a).doit()
Out[11]:
$$0$$
In [12]:
Commutator(Dagger(a), Dagger(a)).doit()
Out[12]:
$$0$$
In [13]:
Dagger(Dagger(Dagger(a)))
Out[13]:
$${{a}^\dagger}$$
In [14]:
Commutator(a, Dagger(Dagger(a))) #.doit()
Out[14]:
$$0$$
In [15]:
Commutator(a, b).doit()
Out[15]:
$${a} {b} - {b} {a}$$
In [16]:
Commutator(a, b).doit(independent=True)
Out[16]:
$$0$$

Test Fermion operators

In [17]:
c = FermionOp('c')
d = FermionOp('d')
In [18]:
Commutator(c,Dagger(c))
Out[18]:
$$- \left[{{c}^\dagger},{c}\right]$$
In [19]:
AntiCommutator(c,Dagger(c))
Out[19]:
$$\left\{{{c}^\dagger},{c}\right\}$$
In [20]:
AntiCommutator(c, Dagger(c)).doit()
Out[20]:
$$1$$
In [21]:
AntiCommutator(Dagger(c), c).doit()
Out[21]:
$$1$$
In [22]:
AntiCommutator(c,d).doit()
Out[22]:
$${c} {d} + {d} {c}$$

Commutators between fermions and bosons

In [23]:
Commutator(a, c).doit()
Out[23]:
$$0$$

Simplifying operator expressions

Test operator ordering of bosons

In [24]:
normal_ordered_form(Dagger(a) * a)
Out[24]:
$${{a}^\dagger} {a}$$
In [25]:
normal_ordered_form(a * Dagger(a))
Out[25]:
$$1 + {{a}^\dagger} {a}$$
In [26]:
normal_ordered_form(a * Dagger(a) * a)
Out[26]:
$${{a}^\dagger} \left({a}\right)^{2} + {a}$$
In [27]:
normal_ordered_form(a * a * Dagger(a))
Out[27]:
$${{a}^\dagger} \left({a}\right)^{2} + 2 {a}$$
In [28]:
normal_ordered_form(a * a * a * Dagger(a))
Out[28]:
$${{a}^\dagger} \left({a}\right)^{3} + 3 \left({a}\right)^{2}$$
In [29]:
normal_ordered_form(a * Dagger(a) * a * Dagger(a))
Out[29]:
$$1 + 3 {{a}^\dagger} {a} + \left({{a}^\dagger}\right)^{2} \left({a}\right)^{2}$$
In [30]:
normal_ordered_form(a * Dagger(a) * Dagger(a) * Dagger(a))
Out[30]:
$$3 \left({{a}^\dagger}\right)^{2} + \left({{a}^\dagger}\right)^{3} {a}$$
In [31]:
normal_ordered_form(a * Dagger(b))
Out[31]:
$${{b}^\dagger} {a} + \left[{a},{{b}^\dagger}\right]$$
In [32]:
normal_ordered_form(a * Dagger(b)).doit()
Out[32]:
$${a} {{b}^\dagger}$$
In [33]:
normal_ordered_form(a * Dagger(b), independent=True)
Out[33]:
$${{b}^\dagger} {a}$$
In [34]:
normal_ordered_form(a * Dagger(b) * Dagger(a))
Out[34]:
$${{b}^\dagger} + {{b}^\dagger} {{a}^\dagger} {a} + \left[{a},{{b}^\dagger}\right] {{a}^\dagger}$$
In [35]:
normal_ordered_form(a * Dagger(b) * Dagger(a), independent=True)
Out[35]:
$${{a}^\dagger} {{b}^\dagger} {a} + {{b}^\dagger}$$

Normal ordering

In [36]:
normal_order(Dagger(a) * a)
Out[36]:
$${{a}^\dagger} {a}$$
In [37]:
normal_order(a * Dagger(a))
Out[37]:
$${{a}^\dagger} {a}$$
In [38]:
normal_order(a * a * a * Dagger(a))
Out[38]:
$${{a}^\dagger} \left({a}\right)^{3}$$
In [39]:
normal_order(a * Dagger(a) * a * Dagger(a) * a)
Out[39]:
$$\left({{a}^\dagger}\right)^{2} \left({a}\right)^{3}$$

Wick's contraction:

In [40]:
a * Dagger(a) - normal_order(a * Dagger(a))
Out[40]:
$$- {{a}^\dagger} {a} + {a} {{a}^\dagger}$$
In [41]:
normal_ordered_form(a * Dagger(a) - normal_order(a * Dagger(a)))
Out[41]:
$$1$$

Test operator ordering of fermions

In [42]:
normal_ordered_form(Dagger(c) * c)
Out[42]:
$${{c}^\dagger} {c}$$
In [43]:
normal_ordered_form(c * Dagger(c))
Out[43]:
$$1 - {{c}^\dagger} {c}$$
In [44]:
normal_ordered_form(c * c * Dagger(c))
Out[44]:
$${{c}^\dagger} \left({c}\right)^{2}$$
In [45]:
normal_ordered_form(c * c * c * Dagger(c))
Out[45]:
$$- {{c}^\dagger} \left({c}\right)^{3} + \left({c}\right)^{2}$$
In [46]:
normal_ordered_form(c * Dagger(c) * c * Dagger(c))
Out[46]:
$$1 - {{c}^\dagger} {c} - \left({{c}^\dagger}\right)^{2} \left({c}\right)^{2}$$
In [47]:
normal_ordered_form(c * Dagger(d))
Out[47]:
$$\left\{{c},{{d}^\dagger}\right\} - {{d}^\dagger} {c}$$
In [48]:
normal_ordered_form(c * Dagger(d), independent=True)
Out[48]:
$$- {{d}^\dagger} {c}$$

Normal ordering

In [49]:
normal_order(Dagger(c) * c)
Out[49]:
$${{c}^\dagger} {c}$$
In [50]:
normal_order(c * Dagger(c))
Out[50]:
$$- {{c}^\dagger} {c}$$
In [51]:
normal_order(c * Dagger(c) * c * Dagger(c) * c)
Out[51]:
$$- \left({{c}^\dagger}\right)^{2} \left({c}\right)^{3}$$

Wick's contraction

In [52]:
c * Dagger(c) - normal_order(c * Dagger(c))
Out[52]:
$${{c}^\dagger} {c} + {c} {{c}^\dagger}$$
In [53]:
normal_ordered_form(c * Dagger(c) - normal_order(c * Dagger(c)))
Out[53]:
$$1$$

States

Boson Fock states

In [54]:
ket = BosonFockKet(2)

ket
Out[54]:
$${\left|2\right\rangle }$$
In [55]:
bra = BosonFockBra(2)

bra
Out[55]:
$${\left\langle 2\right|}$$
In [56]:
type(bra), type(ket)
Out[56]:
(sympy.physics.quantum.boson.BosonFockBra,
 sympy.physics.quantum.boson.BosonFockKet)
In [57]:
a * ket, qapply(a * ket)
Out[57]:
$$\begin{pmatrix}{a} {\left|2\right\rangle }, & \sqrt{2} {\left|1\right\rangle }\end{pmatrix}$$
In [58]:
bra * a, qapply(bra * a, dagger=True)
Out[58]:
$$\begin{pmatrix}{\left\langle 2\right|} {a}, & \sqrt{3} {\left\langle 3\right|}\end{pmatrix}$$
In [59]:
qapply(Dagger(ket) * ket)
Out[59]:
$$\left\langle 2 \right. {\left|2\right\rangle }$$
In [60]:
(Dagger(ket) * ket).doit()
Out[60]:
$$1$$
In [61]:
(bra * Dagger(bra)).doit()
Out[61]:
$$1$$
In [62]:
Dagger(ket) * Dagger(a) * ket, qapply(Dagger(ket) * Dagger(a) * ket).doit()
Out[62]:
$$\begin{pmatrix}{\left\langle 2\right|} {{a}^\dagger} {\left|2\right\rangle }, & 0\end{pmatrix}$$
In [63]:
(bra * Dagger(a) * Dagger(a) * a * a * ket).doit()
Out[63]:
$${\left\langle 2\right|} \left({{a}^\dagger}\right)^{2} \left({a}\right)^{2} {\left|2\right\rangle }$$
In [64]:
qapply(Dagger(ket) * Dagger(a) * a * ket).doit()
Out[64]:
$$2$$
In [65]:
qapply(bra * Dagger(a) * a * Dagger(bra)).doit()
Out[65]:
$$2$$
In [66]:
ket = BosonFockKet(4)
bra = BosonFockBra(2)

bra * a * a * ket, qapply(bra * a * a * ket).doit()
Out[66]:
$$\begin{pmatrix}{\left\langle 2\right|} \left({a}\right)^{2} {\left|4\right\rangle }, & 2 \sqrt{3}\end{pmatrix}$$

Coherent states

In [67]:
alpha, beta = symbols("alpha, beta")
In [68]:
ket = BosonCoherentKet(alpha)

ket
Out[68]:
$${\left|\alpha\right\rangle }$$
In [69]:
bra = BosonCoherentBra(beta)

bra
Out[69]:
$${\left\langle \beta\right|}$$
In [70]:
bra * ket
Out[70]:
$$\left\langle \beta \right. {\left|\alpha\right\rangle }$$
In [71]:
(bra * ket).doit()
Out[71]:
$$e^{\alpha \overline{\beta} - \frac{\left\lvert{\alpha}\right\rvert^{2}}{2} - \frac{\left\lvert{\beta}\right\rvert^{2}}{2}}$$
In [72]:
qapply((bra * qapply(a * ket))).doit()
Out[72]:
$$\alpha e^{\alpha \overline{\beta}} e^{- \frac{\left\lvert{\alpha}\right\rvert^{2}}{2}} e^{- \frac{\left\lvert{\beta}\right\rvert^{2}}{2}}$$
In [73]:
qapply(Dagger(ket) * Dagger(a) * a * ket, dagger=True)
Out[73]:
$$\alpha \alpha^{\dagger}$$
In [74]:
normal_ordered_form(a * Dagger(a))
Out[74]:
$$1 + {{a}^\dagger} {a}$$
In [75]:
qapply(bra * a * Dagger(a) * ket, dagger=True)
Out[75]:
$${\left\langle \beta\right|} {a} {{a}^\dagger} {\left|\alpha\right\rangle }$$
In [76]:
qapply(bra * normal_ordered_form(a * Dagger(a)) * ket, dagger=True)
Out[76]:
$$\alpha e^{- \frac{\left\lvert{\alpha}\right\rvert^{2}}{2}} e^{- \frac{\left\lvert{\beta}\right\rvert^{2}}{2}} \beta^{\dagger} \operatorname{transpose}{\left (e^{\alpha \overline{\beta}} \right )} + e^{\alpha \overline{\beta}} e^{- \frac{\left\lvert{\alpha}\right\rvert^{2}}{2}} e^{- \frac{\left\lvert{\beta}\right\rvert^{2}}{2}}$$

Fermion Fock states

In [77]:
ket = FermionFockKet(1)

ket
Out[77]:
$${\left|1\right\rangle }$$
In [78]:
bra = FermionFockBra(0)

bra
Out[78]:
$${\left\langle 0\right|}$$
In [79]:
(bra * ket).doit()
Out[79]:
$$0$$
In [80]:
c * ket
Out[80]:
$${c} {\left|1\right\rangle }$$
In [81]:
qapply(c * ket)
Out[81]:
$${\left|0\right\rangle }$$
In [82]:
qapply(c * c * ket)
Out[82]:
$$0$$
In [83]:
qapply(Dagger(c) * ket)
Out[83]:
$$0$$
In [84]:
qapply(Dagger(c) * c * ket)
Out[84]:
$${\left|1\right\rangle }$$
In [85]:
Dagger(c) * c
Out[85]:
$${{c}^\dagger} {c}$$
In [86]:
qapply(Dagger(ket) * Dagger(c) * c * ket).doit()
Out[86]:
$$1$$
In [87]:
qapply(bra * Dagger(c) * c * Dagger(bra)).doit()
Out[87]:
$$0$$

TensorProduct spaces

In [88]:
I2 = TensorProduct(IdentityOperator(), IdentityOperator())

I2
Out[88]:
$${{\mathcal{I}}}\otimes {{\mathcal{I}}}$$
In [89]:
psi = TensorProduct(BosonFockKet(1),BosonFockKet(2))

psi
Out[89]:
$${{\left|1\right\rangle }}\otimes {{\left|2\right\rangle }}$$
In [90]:
I2 * psi
Out[90]:
$${{\mathcal{I}}}\otimes {{\mathcal{I}}} {{\left|1\right\rangle }}\otimes {{\left|2\right\rangle }}$$
In [91]:
qapply(I2 * psi)
Out[91]:
$${{\left|1\right\rangle }}\otimes {{\left|2\right\rangle }}$$
In [93]:
a1 = TensorProduct(BosonOp("a"), IdentityOperator())
a2 = TensorProduct(IdentityOperator(), BosonOp("a"))

a1, a2
Out[93]:
$$\begin{pmatrix}{{a}}\otimes {{\mathcal{I}}}, & {{\mathcal{I}}}\otimes {{a}}\end{pmatrix}$$
In [94]:
a1 * psi
Out[94]:
$${{a}}\otimes {{\mathcal{I}}} {{\left|1\right\rangle }}\otimes {{\left|2\right\rangle }}$$
In [95]:
psi1 = qapply(a1 * psi)

psi1
Out[95]:
$${{\left|0\right\rangle }}\otimes {{\left|2\right\rangle }}$$
In [96]:
psi2 = qapply(a2 * psi)

psi2
Out[96]:
$$\sqrt{2} {{\left|1\right\rangle }}\otimes {{\left|1\right\rangle }}$$
In [97]:
qapply(Dagger(psi1) * psi2).doit()
Out[97]:
$$0$$
In [98]:
qapply(Dagger(psi2) * psi2).doit()
Out[98]:
$$2 \cdot {1}\otimes {1}$$
In [99]:
e = TensorProduct(IdentityOperator(), BosonFockKet(1))

e
Out[99]:
$${{\mathcal{I}}}\otimes {{\left|1\right\rangle }}$$
In [100]:
qapply(Dagger(psi2) * e).doit()
Out[100]:
$$\sqrt{2} {{\left\langle 1\right|}}\otimes {1}$$
In [101]:
qapply(qapply(Dagger(psi2) * e) * TensorProduct(BosonFockKet(1), IdentityOperator())).doit()
Out[101]:
$$\sqrt{2} \cdot {1}\otimes {{\mathcal{I}}}$$
In [102]:
bra = TensorProduct(BosonFockBra(1),BosonFockBra(2))
ket = TensorProduct(BosonFockKet(1),BosonFockKet(2))
In [103]:
qapply(bra * ket).doit()
Out[103]:
$${1}\otimes {1}$$
In [104]:
e = qapply(qapply(TensorProduct(IdentityOperator(), BosonCoherentBra(2)) * 
       TensorProduct(a, normal_ordered_form(Dagger(b)*b*Dagger(b)*b)) * 
       TensorProduct(IdentityOperator(), BosonCoherentKet(2))))

e
Out[104]:
$$2 {{a}}\otimes \left({{\left\langle 2\right|} {{b}^\dagger} {\left|2\right\rangle }}\right) + 4 {{a}}\otimes \left({{\left\langle 2\right|} \left({{b}^\dagger}\right)^{2} {\left|2\right\rangle }}\right)$$
In [105]:
ket = TensorProduct(IdentityOperator(), BosonFockKet(2))
bra = Dagger(ket)

qapply(qapply(bra * TensorProduct(a, normal_ordered_form(Dagger(b)*b*Dagger(b)*b)) * ket)).doit()
Out[105]:
$$4 {{a}}\otimes {1}$$

Versions

In [106]:
#%install_ext http://raw.github.com/jrjohansson/version_information/master/version_information.py
%reload_ext version_information

%version_information sympy
Out[106]:
SoftwareVersion
Python3.4.1 (default, May 21 2014, 01:39:38) [GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)]
IPython2.1.0
OSposix [darwin]
sympy0.7.4.1-git
Thu Jun 05 15:32:12 2014 JST