Ejemplo n.º 1
0
    def align(self, schedule: Schedule) -> Schedule:
        """Reallocate instructions according to the policy.

        Only top-level sub-schedules are aligned. If sub-schedules are nested,
        nested schedules are not recursively aligned.

        Args:
            schedule: Schedule to align.

        Returns:
            Schedule with reallocated instructions.
        """
        instruction_duration_validation(self.duration)

        if self.duration < schedule.duration:
            return schedule

        aligned = Schedule.initialize_from(schedule)
        for ind, (_, child) in enumerate(schedule.children):
            _t_center = self.duration * self._func(ind + 1)
            _t0 = int(_t_center - 0.5 * child.duration)
            if _t0 < 0 or _t0 > self.duration:
                PulseError(
                    "Invalid schedule position t=%d is specified at index=%d" %
                    (_t0, ind))
            aligned.insert(_t0, child, inplace=True)

        return aligned
Ejemplo n.º 2
0
    def align(self, schedule: Schedule) -> Schedule:
        """Reallocate instructions according to the policy.

        Only top-level sub-schedules are aligned. If sub-schedules are nested,
        nested schedules are not recursively aligned.

        Args:
            schedule: Schedule to align.

        Returns:
            Schedule with reallocated instructions.
        """
        duration = self._context_params[0]
        instruction_duration_validation(duration)

        if duration < schedule.duration:
            return schedule

        aligned = Schedule()
        for ind, (_, child) in enumerate(schedule._children):
            _t_center = duration * self._func(ind + 1)
            _t0 = int(_t_center - 0.5 * child.duration)
            if _t0 < 0 or _t0 > duration:
                PulseError('Invalid schedule position t=%d is specified at index=%d' % (_t0, ind))
            aligned.insert(_t0, child, inplace=True)

        return pad(aligned, aligned.channels, until=duration, inplace=True)
Ejemplo n.º 3
0
def _get_timeslots(schedule: ScheduleComponent) -> TimeSlots:
    """Generate timeslots from given schedule component.

    Args:
        schedule: Input schedule component.

    Raises:
        PulseError: When invalid schedule type is specified.
    """
    if isinstance(schedule, Instruction):
        duration = schedule.duration
        instruction_duration_validation(duration)
        timeslots = {channel: [(0, duration)] for channel in schedule.channels}
    elif isinstance(schedule, Schedule):
        timeslots = schedule.timeslots
    else:
        raise PulseError('Invalid schedule type {} is specified.'.format(type(schedule)))

    return timeslots
Ejemplo n.º 4
0
    def align(self, schedule: Schedule) -> Schedule:
        """Reallocate instructions according to the policy.

        Only top-level sub-schedules are aligned. If sub-schedules are nested,
        nested schedules are not recursively aligned.

        Args:
            schedule: Schedule to align.

        Returns:
            Schedule with reallocated instructions.
        """
        duration = self._context_params[0]
        instruction_duration_validation(duration)

        total_duration = sum([child.duration for _, child in schedule._children])
        if duration < total_duration:
            return schedule

        total_delay = duration - total_duration

        if len(schedule._children) > 1:
            # Calculate the interval in between sub-schedules.
            # If the duration cannot be divided by the number of sub-schedules,
            # the modulo is appended and prepended to the input schedule.
            interval, mod = np.divmod(total_delay, len(schedule._children) - 1)
        else:
            interval = 0
            mod = total_delay

        # Calculate pre schedule delay
        delay, mod = np.divmod(mod, 2)

        aligned = Schedule()
        # Insert sub-schedules with interval
        _t0 = int(aligned.stop_time + delay + mod)
        for _, child in schedule._children:
            aligned.insert(_t0, child, inplace=True)
            _t0 = int(aligned.stop_time + interval)

        return pad(aligned, aligned.channels, until=duration, inplace=True)