def list_to_grid(ls):
    """Transforms a list of Commands to a grid representation.

    The grid is a mapping from subsystem indices to lists of :class:`Command` instances touching
    that subsystem, in temporal order. The same Command instance will appear in each list that
    corresponds to one of its subsystems.

    Args:
        ls (Iterable[Command]): quantum circuit
    Returns:
        dict[int, list[Command]]: same circuit in grid form
    """
    grid = {}
    for cmd in ls:
        for r in cmd.get_dependencies():
            grid.setdefault(r.ind, []).append(cmd)
    return grid
