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!
Block[{Times = NonCommutativeMultiply}, Replace[Expand[(a + b)^2], c_^k_. :> (Times @@ ConstantArray[c, k]), 1]]
$\endgroup$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$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$Replace[]
instead ofReplaceAll[]
(/.
), as indiscriminate replacement can lead to hard-to-diagnose behavior. $\endgroup$