I am new to Mathematica and am trying to apply it to quantum mechanics problems.

The practice project I am dealing with now is considering quantum harmonic oscillator and ladder operator algebra used to solve it. I had to define replacements, which would take into account all of the algebraic properties when dealing with ladder operators (linearity, commutation relation, non-commutative product, etc.), and then apply these replacements to the Hamiltonian in standard p-x form to convert it into ladder operator form.

I've encountered a bit of a snag in the final replacement, which should expand:

(a+b)^2 = a**a + a**b + b**a + b**b

where a, b are operators. Conventional // Expand does this commutatively. Is there a way to do it for a general power?

All the help is much appreciated!

  • 1
    $\begingroup$ Try Block[{Times = NonCommutativeMultiply}, Replace[Expand[(a + b)^2], c_^k_. :> (Times @@ ConstantArray[c, k]), 1]] $\endgroup$ Oct 1, 2018 at 19:26
  • $\begingroup$ Hm, no, it doesn't seem to work. Naturally, the first and easiest thing I tried is just straight out define noncomexpand := {(a_+b_)^2 -> a**a + a**b + b**a + b**b} and applying this as a replacement works, but eventually I will be dealing with higher powers and this will not be sufficient.. $\endgroup$ Oct 1, 2018 at 19:49
  • $\begingroup$ "it doesn't seem to work" - a concrete example of what does not work would be very helpful in seeing what's going on, as opposed to just saying "it doesn't work". $\endgroup$ Oct 1, 2018 at 19:53
  • $\begingroup$ Apologies, you are right. Certainly, your code works when evaluated simply as it is. But when I try to apply it to the the end result of a series of replacements with @@, it goes haywire. The main error message is ReplaceRepeated but I see no associated problems with that. Perhaps I may be using the syntax wrong. The main idea is to define the Hamiltonian, then just apply a set of replacement rules to it, so that we transform it into ladder operator form. Could you perhaps define your code as a replacement rule (such that it can be used with /. syntax)? $\endgroup$ Oct 1, 2018 at 20:06
  • $\begingroup$ Can you come up with at least a small/simple example of where it fails? Also, you will note I used Replace[] instead of ReplaceAll[] (/.), as indiscriminate replacement can lead to hard-to-diagnose behavior. $\endgroup$ Oct 1, 2018 at 20:07

3 Answers 3


Take a look at the NCAlgebra package.

NCExpand[(a + b)^2]
(* a ** a + a ** b + b ** a + b ** b *)
Block[{Power = (NonCommutativeMultiply @@ ConstantArray[##]&)}, Distribute[(a + b)^2]]

a ** a + a ** b + b ** a + b ** b

Also: a variation on J.M.'s suggestion in the comments:

Block[{Times = NonCommutativeMultiply}, Expand[(a + b)^2] /. 
  Power -> (Times @@ ConstantArray[##] &)]

a ** a + a ** b + b ** a + b ** b


With the teaching assistants help, we were able to procure the following form for noncommutative expand, which works (when applied as a replacement):

noncomexpand:={x_^{n_?IntegerQ} /; \[Not] ConstQ[x] && n > 0 :> CenterDot @@ ConstantArray[x, n]}

Hope this helps!


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.