# 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$$.

• I'm voting to close this question as off-topic because there is no well-posed question in this post; it is simply a rant about missing features that the OP would like to have. Mar 16, 2017 at 15:23
• Leave it open. I might have something that can be useful for this. I'll try to dig it out tonight. Mar 16, 2017 at 17:52
• for how to implement the commutation rules on bosonic operators see here
– glS
Mar 16, 2017 at 18:01
• @rco here, have another reopen vote Mar 17, 2017 at 6:04
• @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. 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]