# Buggy code
def compress_pulses(schedules: List[Schedule]) -> List[Schedule]:
    """Optimization pass to replace identical pulses.

    Args:
        schedules: Schedules to compress.

    Returns:
        Compressed schedules.
    """

    existing_pulses = []
    new_schedules = []

    for schedule in schedules:
        new_schedule = Schedule(name=schedule.name)

        for time, inst in schedule.instructions:
            if isinstance(inst, instructions.Play):
                if inst.pulse in existing_pulses:
                    idx = existing_pulses.index(inst.pulse)
                    identical_pulse = existing_pulses[idx]
                    new_schedule |= (
                        instructions.Play(identical_pulse, inst.channel, inst.name)
                        << time
                    )
                else:
                    existing_pulses.append(inst.pulse)
                    new_schedule |= inst << time
            else:
                new_schedule |= inst << time

        new_schedules.append(new_schedule)

    return new_schedules


# 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 compress_pulses(schedules: List[Schedule]) -> List[Schedule]:
    """Optimization pass to replace identical pulses.

    Args:
        schedules: Schedules to compress.

    Returns:
        Compressed schedules.
    """

    existing_pulses = []
    new_schedules = []

    for schedule in schedules:
        new_schedule = Schedule(name=schedule.name)

        for time, inst in schedule.instructions:
            if isinstance(inst, instructions.Play):
                if inst.pulse in existing_pulses:
                    idx = existing_pulses.index(inst.pulse)
                    identical_pulse = existing_pulses[idx]
                    new_schedule |= (
                        instructions.Play(identical_pulse, inst.channel, inst.name)
                        << time
                    )
                else:
                    existing_pulses.append(inst.pulse)
                    new_schedule.insert(time, inst, inplace=True)
            else:
                new_schedule.insert(time, inst, inplace=True)

        new_schedules.append(new_schedule)

    return new_schedules