def qsimplify_pauli(e):
 if isinstance(e, Operator):
  return e
 if isinstance(e, (Add, Pow, exp)):
  t = type(e)
  return t(*(qsimplify_pauli(arg) for arg in e.args))
 if isinstance(e, Mul):
  (c, nc) = e.args_cnc()
  nc_s = []
  while nc:
   curr = nc.pop(0)
   while (len(nc) and isinstance(curr, SigmaOpBase) and isinstance(nc[0], SigmaOpBase) and (curr.name == nc[0].name)):
    x = nc.pop(0)
    y = _qsimplify_pauli_product(curr, x)
    (c1, nc1) = y.args_cnc()
    curr = Mul(*nc1)
    c = (c + c1)
   nc_s.append(curr)
  return (Mul(*c) * Mul(*nc_s))
 return e

def qsimplify_pauli ( e ) : if isinstance ( e , Operator ) : return e if isinstance ( e , ( Add , Pow , exp ) ) : t = type ( e ) return t ( * ( qsimplify_pauli ( arg ) for arg in e . args ) ) if isinstance ( e , Mul ) : ( c , nc ) = e . args_cnc ( ) nc_s = [ ] while nc : curr = nc . pop ( 0 ) while ( len ( nc ) and isinstance ( curr , SigmaOpBase ) and isinstance ( nc [ 0 ] , SigmaOpBase ) and ( curr . name == nc [ 0 ] . name ) ) : x = nc . pop ( 0 ) y = _qsimplify_pauli_product ( curr , x ) ( c1 , nc1 ) = y . args_cnc ( ) curr = Mul ( * nc1 ) c = ( c + c1 ) nc_s . append ( curr ) return ( Mul ( * c ) * Mul ( * nc_s ) ) return e
simplify an expression that includes products of pauli operators .