1

I am solving a commutator algebra in SymPy with the Hamiltonian


    from sympy import*
    a=Operator("a")
    ad=Dagger(a)
    b=Operator("b")
    bd=Dagger(b)

    H= ad*a + bd*b

Is there any way I can define commutation relations such as $[a,a^\dagger]=1$, $[b,b^\dagger]=1$ and $[a,b]=0$ ?

I want it such that if I calculate $[a,ad*b]$ I get $b$. There is a code in answer to one of the questions but it does not work in this case.

1 Answers1

0

The function apply_ccr given by @m93a in here should work. You might want to try something like the following,

from sympy import *
from sympy.physics.quantum import *

com1 = Eq(Commutator(a, ad), 1)
com2 = Eq(Commutator(b, bd), 1)
com3 = Eq(Commutator(a, b), 0)

expr = (a*ad - ad*a) + (b*a + a*b) + (b*bd + bd*b) # example
print(expr) # −†+†+†++†+

for com in [com1, com2, com3]:
    expr = apply_ccr(expr, com)

print(expr) # 2+2†+2

Note it doesn't seem to work if your expression contains commutators.

Jason Yu
  • 101
  • 3
  • It does not work for my particular case `com1 = sympy.Eq( Commutator(a, ad), 1 )` `com2=sympy.Eq( Commutator(a, b), 0 )` `com3=sympy.Eq( Commutator(ad, b), b )` `expr=(Commutator(a,b*ad)).doit()` `print(expr)` `#-(-a*b*Dagger(a) + b*Dagger(a)*a)` `for com in [com1,com2,com3]:` `expr = apply_ccr(expr, com)` `print(expr)` `#-(-a*b*Dagger(a) + b*Dagger(a)*a)` – harsh vardhan upadhyay Feb 19 '23 at 10:59
  • As mentioned, it doesn't seem to work when your expression contains commutators. You can perhaps just do `expr = a*b*ad - b*ad*a` instead. – Jason Yu Feb 19 '23 at 18:38
  • When I apply `.doit()`, it expands the commutator, so it becomes the same thing as you have written. – harsh vardhan upadhyay Feb 20 '23 at 18:29