# Buggy code
def add_implicit_acquires(
    schedule: interfaces.ScheduleComponent, meas_map: List[List[int]]
) -> Schedule:
    """Return a new schedule with implicit acquires from the measurement mapping replaced by
    explicit ones.

    .. warning:: Since new acquires are being added, Memory Slots will be set to match the
                 qubit index. This may overwrite your specification.

    Args:
        schedule: Schedule to be aligned.
        meas_map: List of lists of qubits that are measured together.

    Returns:
        A ``Schedule`` with the additional acquisition instructions.
    """
    new_schedule = Schedule(name=schedule.name)
    acquire_map = dict()

    for time, inst in schedule.instructions:
        if isinstance(inst, instructions.Acquire):
            if inst.mem_slot and inst.mem_slot.index != inst.channel.index:
                warnings.warn(
                    "One of your acquires was mapped to a memory slot which didn't match"
                    " the qubit index. I'm relabeling them to match."
                )

            # Get the label of all qubits that are measured with the qubit(s) in this instruction
            all_qubits = []
            for sublist in meas_map:
                if inst.channel.index in sublist:
                    all_qubits.extend(sublist)
            # Replace the old acquire instruction by a new one explicitly acquiring all qubits in
            # the measurement group.
            for i in all_qubits:
                explicit_inst = (
                    instructions.Acquire(
                        inst.duration,
                        chans.AcquireChannel(i),
                        mem_slot=chans.MemorySlot(i),
                        kernel=inst.kernel,
                        discriminator=inst.discriminator,
                    )
                    << time
                )
                if time not in acquire_map:
                    new_schedule |= explicit_inst
                    acquire_map = {time: {i}}
                elif i not in acquire_map[time]:
                    new_schedule |= explicit_inst
                    acquire_map[time].add(i)
        else:
            new_schedule |= inst << time

    return new_schedule


# Traceback (RecursionError)
---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
<ipython-input-99-d345b17804b7> in <module>
11
12 transformed_sched = transforms.compress_pulses([rb_scheds])
---> 13 transformed_sched[0].instructions

~/qiskit/qiskit-terra/qiskit/pulse/schedule.py in instructions(self)
126                     sorted(chan.name for chan in inst.channels))
127
--> 128         return tuple(sorted(self._instructions(), key=key))
129
130     def ch_duration(self, *channels: List[Channel]) -> int:

~/qiskit/qiskit-terra/qiskit/pulse/schedule.py in _instructions(self, time)
174         """
175         for insert_time, child_sched in self._children:
--> 176             yield from child_sched._instructions(time + insert_time)
177
178     def union(self,

... last 1 frames repeated, from the frame below ...

~/qiskit/qiskit-terra/qiskit/pulse/schedule.py in _instructions(self, time)
174         """
175         for insert_time, child_sched in self._children:
--> 176             yield from child_sched._instructions(time + insert_time)
177
178     def union(self,

RecursionError: maximum recursion depth exceeded while calling a Python object

# Fixed code
def add_implicit_acquires(
    schedule: interfaces.ScheduleComponent, meas_map: List[List[int]]
) -> Schedule:
    """Return a new schedule with implicit acquires from the measurement mapping replaced by
    explicit ones.

    .. warning:: Since new acquires are being added, Memory Slots will be set to match the
                 qubit index. This may overwrite your specification.

    Args:
        schedule: Schedule to be aligned.
        meas_map: List of lists of qubits that are measured together.

    Returns:
        A ``Schedule`` with the additional acquisition instructions.
    """
    new_schedule = Schedule(name=schedule.name)
    acquire_map = dict()

    for time, inst in schedule.instructions:
        if isinstance(inst, instructions.Acquire):
            if inst.mem_slot and inst.mem_slot.index != inst.channel.index:
                warnings.warn(
                    "One of your acquires was mapped to a memory slot which didn't match"
                    " the qubit index. I'm relabeling them to match."
                )

            # Get the label of all qubits that are measured with the qubit(s) in this instruction
            all_qubits = []
            for sublist in meas_map:
                if inst.channel.index in sublist:
                    all_qubits.extend(sublist)
            # Replace the old acquire instruction by a new one explicitly acquiring all qubits in
            # the measurement group.
            for i in all_qubits:
                explicit_inst = (
                    instructions.Acquire(
                        inst.duration,
                        chans.AcquireChannel(i),
                        mem_slot=chans.MemorySlot(i),
                        kernel=inst.kernel,
                        discriminator=inst.discriminator,
                    )
                    << time
                )
                if time not in acquire_map:
                    new_schedule |= explicit_inst
                    acquire_map = {time: {i}}
                elif i not in acquire_map[time]:
                    new_schedule |= explicit_inst
                    acquire_map[time].add(i)
        else:
            new_schedule.insert(time, inst, inplace=True)

    return new_schedule