# How to implement ladder operators for the quantum harmonic oscillator?

I would like to write the annihilation and creation operators for the harmonic oscillator, and see how they act on basis states of the form $$\lvert n\rangle$$.

What's the best approach to implement this in Mathematica?

In particular, this would require to implement the commutation relations $$[a,a^\dagger] = 1$$, and to see how $$|n\rangle$$ changes when we apply the creation and destruction operators $$a^\dagger$$ and $$a$$.

• for how to implement the commutation rules on bosonic operators see here
– glS
Commented Mar 16, 2017 at 18:01
• Do you need the operators for general non-commutative algebra, or do you just need matrix representations in some finite basis for calculating expectation values? If the latter, it's fairly straightforward Commented Mar 16, 2017 at 19:59
• @JasonB. I am trying to get the algebric form, namely not the matrix.
– 0x90
Commented Mar 16, 2017 at 20:00
• There is the quantum package that has an example of harmonic oscillator. Commented Mar 17, 2017 at 4:22
• @JasonB. you're right, if it is a finite basis of dimension, k, it is simple: a[k_] := DiagonalMatrix[ConstantArray[1, k-1], k, -1] and $a^\dagger$ is then automatically defined. Commented Mar 17, 2017 at 12:15

Here is a simple implementation:

Protect[qCO, qDO];

qOperatorQ[expr_] := MatchQ[expr, qCO | qDO | Ket[n_Integer]];

(* take scalars out *)
CenterDot[left___, Times[scalar_?NumericQ, op_?qOperatorQ], right___] := Times[
scalar,
CenterDot[left, op, right]
];

(* Implement commutation relations *)
CenterDot[left___, qDO, qCO, right___] := Plus[
CenterDot[left, qCO, qDO, right],
CenterDot[left, right]
];

(* Allow to use powers of operators *)
CenterDot[left___, Power[op : (qCO | qDO), n_Integer], right___] := CenterDot[
left,
Sequence @@ ConstantArray[op, n],
right
];

(* effective OneIdentity attribute *)
CenterDot[op_?qOperatorQ] := op;

(* implement action on Fock states *)
CenterDot[left___, qDO, Ket[0]] := 0;
CenterDot[left___, qCO, Ket[n_Integer]] := Times[
Sqrt[n + 1],
CenterDot[left, Ket[n + 1]]
];
CenterDot[left___, qDO, Ket[n_Integer]] := Times[
Sqrt[n],
CenterDot[left, Ket[n - 1]]
];


I used CenterDot to denote the operator product (note that you can write a CenterDot product with the shortcut [esc].[esc]), qCO to denote the creation operator $$a^\dagger$$, qDO for the destruction operator $$a$$, and the built-in symbol Ket[n] to denote the Fock basis state $$\lvert n \rangle$$ (which can be inserted with [esc]ket[esc]).

You can of course change this notation as you like. Here are a couple of examples of how to use the above (I here keep the full and ugly description of the operators to more clearly show what symbols are being used. Note that you can insert the SuperDagger with a[ctrl+^][esc]dg[esc]):

a = qDO;
SuperDagger[a] = qCO;

a\[CenterDot]Ket[2]
a\[CenterDot]a\[CenterDot]Ket[2]
a^2\[CenterDot]Ket[2]
a\[CenterDot]a\[CenterDot]a\[CenterDot]Ket[2]

a\[CenterDot]SuperDagger[a]\[CenterDot]a\[CenterDot]Ket[2]
a\[CenterDot](SuperDagger[a])^4\[CenterDot]a\[CenterDot]Ket[2]

• How can I calculate $a \left | m \right >=\sqrt{m} \left | m-1 \right >$? Commented May 24 at 12:47
• @Errorbar the functions assume the arguments of Ket to be integers, if that's what you're asking. I guess you can change this simply removing the parts of the code matching for Integer though
– glS
Commented May 24 at 13:12
• Yes that works. But do we have a method that could recoginze the variable m as an integer in an specific expression without changing your code? Commented May 24 at 13:31
• @Errorbar I don't really understand what you're asking. If $m$ is an integer, the code works as is
– glS
Commented May 24 at 13:40