from sympy.physics.quantum import Commutator, Dagger, Operator
from sympy import simplify, exp, series
init_printing()
t = Symbol("t")
Here's a quick demo on how to do computations with commutators and expansions involving operators with Python and Sympy. Let's define a couple of operators first.
L1= Operator("L1")
L2= Operator("L2")
L = L1+L2;
Note that those operators by definition do not commute.
Commutator(L1,L2)==0
False
Now let's move on to an operator defined by an expansion, the exponential operator. This is a well-defined operator as long as L is bounded. Most often it's denoted by $e^{Lt}$ and is really nothing more than
$$e^{Lt}=\sum_{k=0}^{\infty}\frac{(Lt)^k}{k!}$$Let's define a function that approximates this operator, up to a certain order.
def expseries(L,t,k):
return series(exp(L*t),x=t,n=k);
Now we can compute the expansions of those operators and combinations of them. For example, the first terms of $e^{(L_1+2L_2)t}$ are
expseries(L1+2*L2,t,3)
We can also compute the difference between different exponential operators. For example, it is a fact that we can approximate the operator $e^{Lt}=e^{(L_1+L_2)t}$ with $e^{L_1t}e^{L_2t}$
This is called the Lie splitting and with Python & Sympy we can figure out the difference between those two, which is a new operator.
expseries(L1+L2,t,3)-expseries(L1,t,3)*expseries(L2,t,3)
Those are the error terms, but clearly we can have python do more simplifications for us. We use sympy's simplify for that.
simplify(expseries(L1+L2,t,3)-expseries(L1,t,3)*expseries(L2,t,3))
Now things look a lot better! Note that the term in there is actually the commutator of $L_1$ and $L_2$. It's easy to isolate that term in python with an expand and coeff.
expand(simplify(expseries(L1+L2,t,3)-expseries(L1,t,3)*expseries(L2,t,3))).coeff(t,2)
We can use the same idea to compute the errors between arbitrarily complex splittings.
For example, we can also approximate $e^{Lt}$ with $e^{L_1t/2}e^{L_2t}e^{L_1t/2}$. This is called the Strang splitting. Again we try to compute the error term.
simplify(expseries(L1+L2,t,3)-expseries(L1/2,t,3)*expseries(L2,t,3)*expseries(L1,t,3))
In this case, the error term contains first order terms. Since we know that Strang is more accurate than Lie, it must be the case that we don't have enough terms in the expansion. A lot of multiplications will be hidden under the following simplify.
simplify(expseries(L1+L2,t,4)-expseries(L1/2,t,4)*expseries(L2,t,4)*expseries(L1/2,t,4))
Once again, we can collect the important terms by expand and coeff.
expand(Out[222]).coeff(t,3)