(m)
Take a Mul instance with operators and apply them to states.

    This method applies all operators with integer state labels
    to the actual states.  For symbolic state labels, nothing is done.
    When inner products of FockStates are encountered (like <a|b>),
    they are converted to instances of InnerProduct.

    This does not currently work on double inner products like,
    <a|b><c|d>.

    If the argument is not a Mul, it is simply returned as is.
Take a Mul instance with operators and apply them to states.
Take a Mul instance with operators and apply them to states .
def _apply_Mul(m):
    """
    Take a Mul instance with operators and apply them to states.

    This method applies all operators with integer state labels
    to the actual states.  For symbolic state labels, nothing is done.
    When inner products of FockStates are encountered (like <a|b>),
    they are converted to instances of InnerProduct.

    This does not currently work on double inner products like,
    <a|b><c|d>.

    If the argument is not a Mul, it is simply returned as is.
    """
    if not isinstance(m, Mul):
        return m
    c_part, nc_part = m.args_cnc()
    n_nc = len(nc_part)
    if n_nc == 0 or n_nc == 1:
        return m
    else:
        last = nc_part[-1]
        next_to_last = nc_part[-2]
        if isinstance(last, FockStateKet):
            if isinstance(next_to_last, SqOperator):
                if next_to_last.is_symbolic:
                    return m
                else:
                    result = next_to_last.apply_operator(last)
                    if result == 0:
                        return S.Zero
                    else:
                        return _apply_Mul(Mul(*(c_part + nc_part[:-2] + [result])))
            elif isinstance(next_to_last, Pow):
                if isinstance(next_to_last.base, SqOperator) and \
                        next_to_last.exp.is_Integer:
                    if next_to_last.base.is_symbolic:
                        return m
                    else:
                        result = last
                        for i in range(next_to_last.exp):
                            result = next_to_last.base.apply_operator(result)
                            if result == 0:
                                break
                        if result == 0:
                            return S.Zero
                        else:
                            return _apply_Mul(Mul(*(c_part + nc_part[:-2] + [result])))
                else:
                    return m
            elif isinstance(next_to_last, FockStateBra):
                result = InnerProduct(next_to_last, last)
                if result == 0:
                    return S.Zero
                else:
                    return _apply_Mul(Mul(*(c_part + nc_part[:-2] + [result])))
            else:
                return m
        else:
            return m
def _apply_Mul ( m ) : if not isinstance ( m , Mul ) : return m c_part , nc_part = m . args_cnc ( ) n_nc = len ( nc_part ) if n_nc == 0 or n_nc == 1 : return m else : last = nc_part [ - 1 ] next_to_last = nc_part [ - 2 ] if isinstance ( last , FockStateKet ) : if isinstance ( next_to_last , SqOperator ) : if next_to_last . is_symbolic : return m else : result = next_to_last . apply_operator ( last ) if result == 0 : return S . Zero else : return _apply_Mul ( Mul ( * ( c_part + nc_part [ : - 2 ] + [ result ] ) ) ) elif isinstance ( next_to_last , Pow ) : if isinstance ( next_to_last . base , SqOperator ) and next_to_last . exp . is_Integer : if next_to_last . base . is_symbolic : return m else : result = last for i in range ( next_to_last . exp ) : result = next_to_last . base . apply_operator ( result ) if result == 0 : break if result == 0 : return S . Zero else : return _apply_Mul ( Mul ( * ( c_part + nc_part [ : - 2 ] + [ result ] ) ) ) else : return m elif isinstance ( next_to_last , FockStateBra ) : result = InnerProduct ( next_to_last , last ) if result == 0 : return S . Zero else : return _apply_Mul ( Mul ( * ( c_part + nc_part [ : - 2 ] + [ result ] ) ) ) else : return m else : return m