Pad a circuit by adding a `n`-channel identity circuit at index `k`

    That is, a circuit of channel dimension $N$ is extended to one of channel
    dimension $N+n$, where the channels $k$, $k+1$, ...$k+n-1$, just pass
    through the system unaffected.  E.g. let ``A``, ``B`` be two single channel
    systems::

        >>> A = CircuitSymbol('A', cdim=1)
        >>> B = CircuitSymbol('B', cdim=1)
        >>> print(ascii(pad_with_identity(A+B, 1, 2)))
        A + cid(2) + B

    This method can also be applied to irreducible systems, but in that case
    the result can not be decomposed as nicely.

    Args:
        circuit (Circuit): circuit to pad
        k (int): The index at which to insert the circuit
        n (int): The number of channels to pass through

    Returns:
        Circuit: An extended circuit that passes through the channels
            $k$, $k+1$, ..., $k+n-1$

def pad_with_identity(circuit, k, n):
    """Pad a circuit by adding a `n`-channel identity circuit at index `k`

    That is, a circuit of channel dimension $N$ is extended to one of channel
    dimension $N+n$, where the channels $k$, $k+1$, ...$k+n-1$, just pass
    through the system unaffected.  E.g. let ``A``, ``B`` be two single channel
    systems::

        >>> A = CircuitSymbol('A', cdim=1)
        >>> B = CircuitSymbol('B', cdim=1)
        >>> print(ascii(pad_with_identity(A+B, 1, 2)))
        A + cid(2) + B

    This method can also be applied to irreducible systems, but in that case
    the result can not be decomposed as nicely.

    Args:
        circuit (Circuit): circuit to pad
        k (int): The index at which to insert the circuit
        n (int): The number of channels to pass through

    Returns:
        Circuit: An extended circuit that passes through the channels
            $k$, $k+1$, ..., $k+n-1$

    """
    circuit_n = circuit.cdim
    combined_circuit = circuit + circuit_identity(n)
    permutation = (list(range(k)) + list(range(circuit_n, circuit_n + n)) +
                   list(range(k, circuit_n)))
    return (CPermutation.create(invert_permutation(permutation)) <<
            combined_circuit << CPermutation.create(permutation))