def apply_operators(e):
 e = e.expand()
 muls = e.atoms(Mul)
 subs_list = [(m, _apply_Mul(m)) for m in iter(muls)]
 return e.subs(subs_list)

def apply_operators ( e ) : e = e . expand ( ) muls = e . atoms ( Mul ) subs_list = [ ( m , _apply_Mul ( m ) ) for m in iter ( muls ) ] return e . subs ( subs_list )
take a sympy expression with operators and states and apply the operators .