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

  • 1
    $\begingroup$ for how to implement the commutation rules on bosonic operators see here $\endgroup$
    – glS
    Commented Mar 16, 2017 at 18:01
  • $\begingroup$ 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 $\endgroup$
    – Jason B.
    Commented Mar 16, 2017 at 19:59
  • $\begingroup$ @JasonB. I am trying to get the algebric form, namely not the matrix. $\endgroup$
    – 0x90
    Commented Mar 16, 2017 at 20:00
  • 1
    $\begingroup$ There is the quantum package that has an example of harmonic oscillator. $\endgroup$ Commented Mar 17, 2017 at 4:22
  • 3
    $\begingroup$ @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. $\endgroup$
    – rcollyer
    Commented Mar 17, 2017 at 12:15

1 Answer 1


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[
    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[
    Sequence @@ ConstantArray[op, n],

(* 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[
    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;


  • $\begingroup$ How can I calculate $a \left | m \right >=\sqrt{m} \left | m-1 \right >$? $\endgroup$
    – Errorbar
    Commented May 24 at 12:47
  • $\begingroup$ @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 $\endgroup$
    – glS
    Commented May 24 at 13:12
  • $\begingroup$ 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? $\endgroup$
    – Errorbar
    Commented May 24 at 13:31
  • $\begingroup$ @Errorbar I don't really understand what you're asking. If $m$ is an integer, the code works as is $\endgroup$
    – glS
    Commented May 24 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.