# 007-001-the-harmonic-oscillator.ipynb

In [1]:
from sympy.physics.quantum import *
from sympy.physics.quantum.cartesian import *
from sympy.physics.quantum.operator import *
from sympy.physics.quantum.state import *
from sympy import *
from sympy.core.relational import *
from sympy.physics.units import degree
from sympy.abc import a, b, x, y, z, t, alpha, n, theta, h, f, lamda, i, k
from sympy.abc import w, u, d, beta, r, psi, o, l, c, gamma, phi, j, p, epsilon, omega

In [2]:
F = symbols('F')
springConst = symbols('k', constant =  True)
mass = symbols('m')
vel = symbols('v')
g = symbols('g', constant = True, float = True)
E = symbols('E')
KE = symbols('KE')
PE = symbols('PE')
momentum = symbols('p')
momentumOperator = HermitianOperator('P')
H = HermitianOperator('H')
ketPsi = Ket(psi)
positionOperator = HermitianOperator('X')
blankSpace = Dummy('')
h = symbols('h', constant = True)
f = symbols('f', constant = True)
a_plus = Operator('a_+')
a_minus = Operator('a_-')

In [3]:
forceOnASpring = Eq(F, springConst*x)
display(forceOnASpring)

$\displaystyle F = k x$
In [4]:
forceOnASpring = Eq(mass * g, springConst*x)
display(forceOnASpring)

$\displaystyle g m = k x$
In [5]:
oscillationEq = Eq(omega, sqrt(springConst/mass))
display(oscillationEq)

$\displaystyle \omega = \sqrt{\frac{k}{m}}$
In [6]:
valOfSpringConst = Eq(springConst, omega**2 * mass)
display(valOfSpringConst)

$\displaystyle k = m \omega^{2}$
In [7]:
energyEq = Eq(E, KE + PE)
display(energyEq)

$\displaystyle E = KE + PE$
In [8]:
energyEq = Eq(E, mass * vel**2 / 2 + springConst * x**2 / 2)
display(energyEq)

$\displaystyle E = \frac{k x^{2}}{2} + \frac{m v^{2}}{2}$
In [9]:
energyEq = Eq(E,  momentum**2 / (2 * mass) +  omega**2 * mass * x**2 / 2)
display(energyEq)

$\displaystyle E = \frac{m \omega^{2} x^{2}}{2} + \frac{p^{2}}{2 m}$
In [10]:
# for mass = 1
valOfH = Eq(H,  momentumOperator**2 / 2 +  omega**2 * positionOperator**2 / 2)
display(valOfH)

$\displaystyle H = \frac{\omega^{2} X^{2}}{2} + \frac{P^{2}}{2}$
In [11]:
prodOfX_KetPsi = Eq(positionOperator * ketPsi, x * ketPsi)
display(prodOfX_KetPsi)

$\displaystyle X {\left|\psi\right\rangle } = x {\left|\psi\right\rangle }$
In [12]:
valOfMomentumOperator = Eq(momentumOperator,-I * hbar * Derivative(blankSpace, x))
display(valOfMomentumOperator)

$\displaystyle P = - \hbar i \frac{d}{d x}$
In [13]:
# for mass = 1
# assuming hbar = 1
valOfH = Eq(H, -Derivative(blankSpace, x, 2) / 2 +  omega**2 * positionOperator**2 / 2)
display(valOfH)

$\displaystyle H = \frac{\omega^{2} X^{2}}{2} - \frac{\frac{d^{2}}{d x^{2}} }{2}$
In [14]:
prodOfH_KetPsi = Eq(Eq(H * ketPsi, Add((-Derivative(ketPsi, x, 2) / 2),
omega**2 * positionOperator**2 / 2 * ketPsi)),
E * ketPsi, evaluate = False)
display(prodOfH_KetPsi)

$\displaystyle H {\left|\psi\right\rangle } = \frac{\omega^{2} X^{2} {\left|\psi\right\rangle }}{2} - \frac{\frac{d^{2}}{d x^{2}} {\left|\psi\right\rangle }}{2} = E {\left|\psi\right\rangle }$
In [15]:
valOfKetPsi = Eq(ketPsi, exp(-omega*positionOperator**2 / 2))
display(valOfKetPsi)

$\displaystyle {\left|\psi\right\rangle } = e^{- \frac{\omega X^{2}}{2}}$
In [16]:
diffOfKetPsi = Eq(Derivative(ketPsi, x), - omega * positionOperator * exp(-omega * positionOperator**2 / 2))
display(diffOfKetPsi)

$\displaystyle \frac{d}{d x} {\left|\psi\right\rangle } = - \omega X e^{- \frac{\omega X^{2}}{2}}$
In [17]:
doubleDiffOfKetPsi = Eq(Derivative(ketPsi, x, 2),
-omega * ketPsi + omega**2 * positionOperator**2 * ketPsi)
display(doubleDiffOfKetPsi)

$\displaystyle \frac{d^{2}}{d x^{2}} {\left|\psi\right\rangle } = \omega^{2} X^{2} {\left|\psi\right\rangle } - \omega {\left|\psi\right\rangle }$
In [18]:
prodOfH_KetPsi = Eq(H * ketPsi, Add(Add(omega * ketPsi, - omega**2 * positionOperator**2 * ketPsi) / 2,
omega**2 * positionOperator**2 / 2))
display(prodOfH_KetPsi)

$\displaystyle H {\left|\psi\right\rangle } = \frac{\omega^{2} X^{2}}{2} + \frac{- \omega^{2} X^{2} {\left|\psi\right\rangle } + \omega {\left|\psi\right\rangle }}{2}$
In [19]:
prodOfH_KetPsi = Eq(Eq(H * ketPsi, omega * ketPsi/ 2), E * ketPsi, evaluate = False)
display(prodOfH_KetPsi)

$\displaystyle H {\left|\psi\right\rangle } = \frac{\omega {\left|\psi\right\rangle }}{2} = E {\left|\psi\right\rangle }$
In [20]:
valOfE = Eq(Eq(Eq(E, omega / 2), omega * hbar/ 2, evaluate = False), h * f, evaluate = False)
display(valOfE)

$\displaystyle E = \frac{\omega}{2} = \frac{\hbar \omega}{2} = f h$
In [21]:
valOfH = Eq(H,  (momentumOperator**2  +  omega**2 * positionOperator**2) / 2)
display(valOfH)

$\displaystyle H = \frac{\omega^{2} X^{2} + P^{2}}{2}$
In [22]:
valOfH = Eq(H,  (momentumOperator  + I * omega * positionOperator) * (momentumOperator  - I * omega * positionOperator) / 2)
display(valOfH)

$\displaystyle H = \frac{\left(i \omega X + P\right) \left(- i \omega X + P\right)}{2}$
In [23]:
valOfH = Eq(H,  Add(momentumOperator**2,
omega**2 * positionOperator**2,
I * omega * (positionOperator * momentumOperator - momentumOperator * positionOperator)) / 2)
display(valOfH)

$\displaystyle H = \frac{\omega^{2} X^{2} + i \omega \left(- P X + X P\right) + P^{2}}{2}$
In [24]:
valOfH = Eq(H,  Add(momentumOperator**2,
omega**2 * positionOperator**2,
I * omega * Commutator(positionOperator, momentumOperator)) / 2)
display(valOfH)

$\displaystyle H = \frac{\omega^{2} X^{2} - i \omega \left[P,X\right] + P^{2}}{2}$
In [25]:
commutatorOfPositionOperator_MomentumOperator = Eq(Commutator(positionOperator, momentumOperator), I * hbar)
display(commutatorOfPositionOperator_MomentumOperator)

$\displaystyle - \left[P,X\right] = \hbar i$
In [26]:
# assumming hbar = 1
valOfH = Eq(H, Add((momentumOperator  + I * omega * positionOperator) * (momentumOperator  - I * omega * positionOperator)/ 2,
omega / 2))
display(valOfH)

$\displaystyle H = \frac{\omega}{2} + \frac{\left(i \omega X + P\right) \left(- i \omega X + P\right)}{2}$
In [27]:
commutatorEq = Eq(Commutator((momentumOperator  + I * omega * positionOperator),
(momentumOperator  - I * omega * positionOperator)),
Add(Mul(I, omega, (positionOperator * momentumOperator - momentumOperator * positionOperator)),
-Mul(I, omega, (-positionOperator * momentumOperator + momentumOperator * positionOperator))))
display(commutatorEq)

$\displaystyle \left[i \omega X + P,- i \omega X + P\right] = i \omega \left(- P X + X P\right) - i \omega \left(P X - X P\right)$
In [28]:
commutatorEq = Eq(Eq(Commutator((momentumOperator  + I * omega * positionOperator),
(momentumOperator  - I * omega * positionOperator)),
-Mul(I, omega, Commutator(momentumOperator, positionOperator)))),
2 * omega, evaluate = False)
display(commutatorEq)

$\displaystyle \left[i \omega X + P,- i \omega X + P\right] = - 2 i \omega \left[P,X\right] = 2 \omega$
In [29]:
valOfA_plus = Eq(a_plus, (momentumOperator  + I * omega * positionOperator)/ sqrt(2 * omega))
display(valOfA_plus)

$\displaystyle a_{+} = \frac{\sqrt{2} \left(i \omega X + P\right)}{2 \sqrt{\omega}}$
In [30]:
valOfA_minus = Eq(a_minus, (momentumOperator  - I * omega * positionOperator)/ sqrt(2 * omega))
display(valOfA_minus)

$\displaystyle a_{-} = \frac{\sqrt{2} \left(- i \omega X + P\right)}{2 \sqrt{\omega}}$
In [31]:
commutatorOfA_plus_A_minus = Eq(Commutator(a_plus, a_minus), -1, evaluate = False)
display(commutatorOfA_plus_A_minus)

$\displaystyle \left[a_{+},a_{-}\right] = -1$
In [32]:
valOfA_plus = Eq(a_plus * sqrt(2 * omega), (momentumOperator  + I * omega * positionOperator))
display(valOfA_plus)

$\displaystyle \sqrt{2} \sqrt{\omega} a_{+} = i \omega X + P$
In [33]:
valOfA_minus = Eq(a_minus * sqrt(2 * omega), (momentumOperator  - I * omega * positionOperator))
display(valOfA_minus)

$\displaystyle \sqrt{2} \sqrt{\omega} a_{-} = - i \omega X + P$
In [34]:
# assumming hbar = 1
valOfH = Eq(H, Add(a_plus * a_minus * omega,
omega / 2))
display(valOfH)

$\displaystyle H = \frac{\omega}{2} + \omega a_{+} a_{-}$
In [35]:
prodOfA_minus_KetPsi = Eq(a_minus * ketPsi, Mul((momentumOperator  - I * omega * positionOperator)/ sqrt(2 * omega),
exp(-omega*positionOperator**2 / 2)))
display(prodOfA_minus_KetPsi)

$\displaystyle a_{-} {\left|\psi\right\rangle } = \frac{\sqrt{2} \left(- i \omega X + P\right) e^{- \frac{\omega X^{2}}{2}}}{2 \sqrt{\omega}}$
In [36]:
prodOfA_minus_KetPsi = Eq(a_minus * ketPsi, Mul(Add(-I * Derivative(blankSpace, x),
- I * omega * positionOperator),
1/sqrt(2 * omega),
exp(-omega*positionOperator**2 / 2)))
display(prodOfA_minus_KetPsi)

$\displaystyle a_{-} {\left|\psi\right\rangle } = \frac{\sqrt{2} \left(- i \omega X - i \frac{d}{d x} \right) e^{- \frac{\omega X^{2}}{2}}}{2 \sqrt{\omega}}$
In [37]:
prodOfA_minus_KetPsi = Eq(a_minus * ketPsi, Mul(Add(-omega * positionOperator * ketPsi,
omega * positionOperator * ketPsi,
evaluate = False),
- I/sqrt(2 * omega)))
display(prodOfA_minus_KetPsi)

$\displaystyle a_{-} {\left|\psi\right\rangle } = - \frac{\sqrt{2} i \left(- \omega X {\left|\psi\right\rangle } + \omega X {\left|\psi\right\rangle }\right)}{2 \sqrt{\omega}}$
In [38]:
prodOfA_minus_KetPsi = Eq(a_minus * ketPsi, Mul(Add(-omega * positionOperator * ketPsi,
omega * positionOperator * ketPsi),
- I/sqrt(2 * omega)))
display(prodOfA_minus_KetPsi)

$\displaystyle a_{-} {\left|\psi\right\rangle } = 0$
In [39]:
prodOfH_A_plus_KetPsi = Eq(H * a_plus * ketPsi, E * a_plus * ketPsi)
display(prodOfH_A_plus_KetPsi)

$\displaystyle H a_{+} {\left|\psi\right\rangle } = E a_{+} {\left|\psi\right\rangle }$
In [40]:
prodOfH_A_plus_KetPsi = Eq(Mul(Add(a_plus * omega * a_minus,
omega / 2),
a_plus,
ketPsi),
E * a_plus * ketPsi)
display(prodOfH_A_plus_KetPsi)

$\displaystyle \left(\frac{\omega}{2} + \omega a_{+} a_{-}\right) a_{+} {\left|\psi\right\rangle } = E a_{+} {\left|\psi\right\rangle }$
In [41]:
prodOfH_A_plus_KetPsi = Eq(Add(a_plus * omega * a_minus * a_plus * ketPsi,
omega * a_plus * ketPsi / 2),
E * a_plus * ketPsi)
display(prodOfH_A_plus_KetPsi)

$\displaystyle \frac{\omega a_{+} {\left|\psi\right\rangle }}{2} + \omega a_{+} a_{-} a_{+} {\left|\psi\right\rangle } = E a_{+} {\left|\psi\right\rangle }$
In [42]:
commuatatorA_plus_A_minus = Eq(Commutator(a_plus, a_minus), -1)
display(commuatatorA_plus_A_minus)

$\displaystyle \left[a_{+},a_{-}\right] = -1$
In [43]:
commuatatorA_plus_A_minus = Eq(Commutator(a_plus, a_minus).doit(), -1)
display(commuatatorA_plus_A_minus)

$\displaystyle a_{+} a_{-} - a_{-} a_{+} = -1$
In [44]:
commuatatorA_plus_A_minus = Eq(a_minus * a_plus,
solve(Eq(Commutator(a_plus, a_minus).doit(), -1), a_minus * a_plus)[0])
display(commuatatorA_plus_A_minus)

$\displaystyle a_{-} a_{+} = 1 + a_{+} a_{-}$
In [45]:
prodOfH_A_plus_KetPsi = Eq(Add(a_plus * omega * ketPsi,
omega * a_plus * ketPsi / 2, evaluate = False),
E * a_plus * ketPsi)
display(prodOfH_A_plus_KetPsi)

$\displaystyle \frac{\omega a_{+} {\left|\psi\right\rangle }}{2} + \omega a_{+} {\left|\psi\right\rangle } = E a_{+} {\left|\psi\right\rangle }$
In [46]:
prodOfH_A_plus_KetPsi = Eq(3 * omega * hbar / 2, E)
display(prodOfH_A_plus_KetPsi)

$\displaystyle \frac{3 \hbar \omega}{2} = E$
In [ ]: