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

  • 2
    $\begingroup$ 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. $\endgroup$
    – m_goldberg
    Mar 16, 2017 at 15:23
  • 10
    $\begingroup$ Leave it open. I might have something that can be useful for this. I'll try to dig it out tonight. $\endgroup$
    – rcollyer
    Mar 16, 2017 at 17:52
  • 1
    $\begingroup$ for how to implement the commutation rules on bosonic operators see here $\endgroup$
    – glS
    Mar 16, 2017 at 18:01
  • 2
    $\begingroup$ @rco here, have another reopen vote $\endgroup$
    – LLlAMnYP
    Mar 17, 2017 at 6:04
  • 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
    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;



Your Answer

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

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