Simplification method for cperm << rhs.
    Decompose a series product of a channel permutation and a reducible circuit
    with appropriate block structure by decomposing the permutation into a
    permutation within each block of rhs and a block permutation and a residual
    part.  This allows for achieving something close to a normal form for
    circuit expression.

def _factor_permutation_for_blocks(cperm, rhs):
    """Simplification method for cperm << rhs.
    Decompose a series product of a channel permutation and a reducible circuit
    with appropriate block structure by decomposing the permutation into a
    permutation within each block of rhs and a block permutation and a residual
    part.  This allows for achieving something close to a normal form for
    circuit expression.
    """
    rbs = rhs.block_structure
    if rhs == cid(rhs.cdim):
        return cperm
    if len(rbs) > 1:
        residual_lhs, transformed_rhs, carried_through_lhs \
                = cperm._factorize_for_rhs(rhs)
        if residual_lhs == cperm:
            raise CannotSimplify()
        return SeriesProduct.create(residual_lhs, transformed_rhs,
                                    carried_through_lhs)
    raise CannotSimplify()