#Commutator and expansion based computations with Python & Sympy


```
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)




```




$$1 + t \left(L_{1} + 2 L_{2}\right) + t^{2} \left(L_{1} L_{2} + \frac{\left(L_{1}\right)^{2}}{2} + L_{2} L_{1} + 2 \left(L_{2}\right)^{2}\right) + \mathcal{O}\left(t^{3}\right)$$



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)

```




$$- \left(1 + t L_{1} + \frac{t^{2} \left(L_{1}\right)^{2}}{2} + \mathcal{O}\left(t^{3}\right)\right) \left(1 + t L_{2} + \frac{t^{2} \left(L_{2}\right)^{2}}{2} + \mathcal{O}\left(t^{3}\right)\right) + 1 + t \left(L_{1} + L_{2}\right) + t^{2} \left(\frac{L_{1} L_{2}}{2} + \frac{\left(L_{1}\right)^{2}}{2} + \frac{L_{2} L_{1}}{2} + \frac{\left(L_{2}\right)^{2}}{2}\right) + \mathcal{O}\left(t^{3}\right)$$



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))
```




$$\frac{1}{2} \left(t^{2} L_{2} L_{1} - t^{2} L_{1} L_{2} + \mathcal{O}\left(t^{3}\right)\right)$$



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)

```




$$- \frac{L_{1} L_{2}}{2} + \frac{L_{2} L_{1}}{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))
```




$$\frac{1}{8} \left(- 4 t L_{1} - 4 t^{2} L_{2} L_{1} - 5 t^{2} \left(L_{1}\right)^{2} + \mathcal{O}\left(t^{3}\right)\right)$$



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))
```




$$\frac{1}{24} \left(- 2 t^{3} \left(L_{2}\right)^{2} L_{1} + t^{3} L_{2} \left(L_{1}\right)^{2} + 4 t^{3} L_{2} L_{1} L_{2} + t^{3} \left(L_{1}\right)^{2} L_{2} - 2 t^{3} L_{1} \left(L_{2}\right)^{2} - 2 t^{3} L_{1} L_{2} L_{1} + \mathcal{O}\left(t^{4}\right)\right)$$



Once again, we can collect the important terms by *expand* and *coeff*.


```
expand(Out[222]).coeff(t,3)
```




$$- \frac{L_{1} L_{2}}{12} L_{1} - \frac{L_{1} \left(L_{2}\right)^{2}}{12} + \frac{\left(L_{1}\right)^{2} L_{2}}{24} + \frac{L_{2} L_{1}}{6} L_{2} + \frac{L_{2} \left(L_{1}\right)^{2}}{24} - \frac{\left(L_{2}\right)^{2} L_{1}}{12}$$




```

```

daf0767276fc78e3114e41a809195bc72f160bb7
ipynb
Jupyter Notebook
ipythonNotebooks/commutators_and_sympy.ipynb
kgourgou/blog
c9da56dc87a2b349efe06972a59706bfb181b197
MIT
2015-12-02T06:18:58.000Z
2016-10-07T20:21:04.000Z
ipythonNotebooks/commutators_and_sympy.ipynb
kgourgou/blog
c9da56dc87a2b349efe06972a59706bfb181b197
MIT
ipythonNotebooks/commutators_and_sympy.ipynb
kgourgou/blog
c9da56dc87a2b349efe06972a59706bfb181b197
MIT
Qwen/Qwen-72B
1. YES
2. YES