def test_padding_less_than_sched_duration(self):
     """Test that the until arg is respected even for less than the input schedule duration."""
     delay = 10
     sched = (Delay(delay, DriveChannel(0)) +
              Delay(delay, DriveChannel(0)).shift(20))
     ref_sched = (sched | pulse.Delay(5, DriveChannel(0)).shift(10))
     self.assertEqual(transforms.pad(sched, until=15), ref_sched)
Esempio n. 2
0
    def test_replace_schedule(self):
        """Test replacement of schedule."""

        old = Schedule(
            Delay(10, DriveChannel(0)),
            Delay(100, DriveChannel(1)),
        )
        new = Schedule(
            Play(Constant(10, 1.0), DriveChannel(0)),
            Play(Constant(100, 0.1), DriveChannel(1)),
        )
        const = Play(Constant(100, 1.0), DriveChannel(0))

        sched = Schedule()
        sched += const
        sched += old

        new_sched = sched.replace(old, new)

        ref_sched = Schedule()
        ref_sched += const
        ref_sched += new
        self.assertEqual(new_sched, ref_sched)

        # test replace inplace
        sched.replace(old, new, inplace=True)
        self.assertEqual(sched, ref_sched)
Esempio n. 3
0
def pad(schedule: Schedule,
        channels: Optional[Iterable[Channel]] = None,
        until: Optional[int] = None) -> Schedule:
    """Pad the input ``Schedule`` with ``Delay`` s on all unoccupied timeslots until ``until``
    if it is provided, otherwise until ``schedule.duration``.

    Args:
        schedule: Schedule to pad.
        channels: Channels to pad. Defaults to all channels in ``schedule`` if not provided.
                  If the supplied channel is not a member of ``schedule``, it will be added.
        until: Time to pad until. Defaults to ``schedule.duration`` if not provided.

    Returns:
        The padded schedule.
    """
    until = until or schedule.duration

    channels = channels or schedule.channels
    occupied_channels = schedule.channels

    unoccupied_channels = set(channels) - set(occupied_channels)

    empty_timeslot_collection = schedule.timeslots.complement(until)

    for channel in channels:
        for timeslot in empty_timeslot_collection.ch_timeslots(channel):
            schedule |= Delay(timeslot.duration)(timeslot.channel).shift(
                timeslot.start)

    for channel in unoccupied_channels:
        schedule |= Delay(until)(channel)

    return schedule
Esempio n. 4
0
    def test_padding_until_less(self):
        """Test padding until time that is less than schedule duration."""
        delay = 10

        sched = Delay(delay, DriveChannel(0)).shift(10) + Delay(delay, DriveChannel(1))

        ref_sched = sched | Delay(delay, DriveChannel(0)) | Delay(5, DriveChannel(1)).shift(10)

        self.assertEqual(transforms.pad(sched, until=15), ref_sched)
Esempio n. 5
0
 def test_same_commands_on_two_channels_at_same_time_out_of_order(self):
     """Test that schedule with same commands on two channels at the same time equal
     when out of order."""
     sched1 = Schedule()
     sched1 = sched1.append(Delay(100, DriveChannel(1)))
     sched1 = sched1.append(Delay(100, ControlChannel(1)))
     sched2 = Schedule()
     sched2 = sched2.append(Delay(100, ControlChannel(1)))
     sched2 = sched2.append(Delay(100, DriveChannel(1)))
     self.assertEqual(sched1, sched2)
Esempio n. 6
0
    def test_padding_supplied_channels(self):
        """Test padding of only specified channels."""
        delay = 10
        sched = Delay(delay, DriveChannel(0)).shift(10) + Delay(delay, DriveChannel(1))

        ref_sched = sched | Delay(delay, DriveChannel(0)) | Delay(2 * delay, DriveChannel(2))

        channels = [DriveChannel(0), DriveChannel(2)]

        self.assertEqual(transforms.pad(sched, channels=channels), ref_sched)
Esempio n. 7
0
    def test_timeslots(self):
        """Test schedule.timeslots."""
        reference_sched = Schedule()
        reference_sched = reference_sched.insert(10, Delay(10, DriveChannel(0)))
        reference_sched = reference_sched.insert(10, Delay(50, DriveChannel(1)))
        reference_sched = reference_sched.insert(10, ShiftPhase(0.1, DriveChannel(0)))

        reference_sched = reference_sched.insert(100, ShiftPhase(0.1, DriveChannel(1)))

        self.assertEqual(reference_sched.timeslots[DriveChannel(0)], [(10, 10), (10, 20)])
        self.assertEqual(reference_sched.timeslots[DriveChannel(1)], [(10, 60), (100, 100)])
Esempio n. 8
0
    def test_ch_stop_time(self):
        """Test schedule.ch_stop_time."""
        reference_sched = Schedule()
        reference_sched = reference_sched.insert(10, Delay(10, DriveChannel(0)))
        reference_sched = reference_sched.insert(10, Delay(50, DriveChannel(1)))
        reference_sched = reference_sched.insert(10, ShiftPhase(0.1, DriveChannel(0)))

        reference_sched = reference_sched.insert(100, ShiftPhase(0.1, DriveChannel(1)))

        self.assertEqual(reference_sched.ch_stop_time(DriveChannel(0)), 20)
        self.assertEqual(reference_sched.ch_stop_time(DriveChannel(1)), 100)
Esempio n. 9
0
    def test_duration(self):
        """Test schedule.duration."""
        reference_sched = Schedule()
        reference_sched = reference_sched.insert(10, Delay(10, DriveChannel(0)))
        reference_sched = reference_sched.insert(10, Delay(50, DriveChannel(1)))
        reference_sched = reference_sched.insert(10, ShiftPhase(0.1, DriveChannel(0)))

        reference_sched = reference_sched.insert(100, ShiftPhase(0.1, DriveChannel(1)))

        self.assertEqual(reference_sched.duration, 100)
        self.assertEqual(reference_sched.duration, 100)
Esempio n. 10
0
    def test_delay_measure_channel(self):
        """Test Delay on MeasureChannel"""

        measure_ch = self.config.measure(0)
        pulse = Waveform(np.full(10, 0.1))
        # should pass as is an append
        sched = Delay(self.delay_time, measure_ch) + Play(pulse, measure_ch)
        self.assertIsInstance(sched, Schedule)
        # should fail due to overlap
        with self.assertRaises(PulseError):
            sched = Delay(self.delay_time, measure_ch) | Play(pulse, measure_ch)
    def test_padding_prepended_delay(self):
        """Test that there is delay before the first instruction."""
        delay = 10
        sched = (Delay(delay, DriveChannel(0)).shift(10) +
                 Delay(delay, DriveChannel(0)))

        ref_sched = (Delay(delay, DriveChannel(0)) +
                     Delay(delay, DriveChannel(0)) +
                     Delay(delay, DriveChannel(0)))

        self.assertEqual(transforms.pad(sched, until=30, inplace=True), ref_sched)
Esempio n. 12
0
    def test_delay_acquire_channel(self):
        """Test Delay on DriveChannel"""

        acquire_ch = self.config.acquire(0)
        # should pass as is an append
        sched = Delay(self.delay_time, acquire_ch) + Acquire(10, acquire_ch, MemorySlot(0))
        self.assertIsInstance(sched, Schedule)
        # should fail due to overlap
        with self.assertRaises(PulseError):
            sched = Delay(self.delay_time, acquire_ch) | Acquire(10, acquire_ch, MemorySlot(0))
            self.assertIsInstance(sched, Schedule)
    def test_padding_until_greater(self):
        """Test padding until time that is greater than schedule duration."""
        delay = 10

        sched = (Delay(delay, DriveChannel(0)).shift(10) +
                 Delay(delay, DriveChannel(1)))

        ref_sched = (sched | Delay(delay, DriveChannel(0))
                     | Delay(30, DriveChannel(0)).shift(20)
                     | Delay(40, DriveChannel(1)).shift(10))

        self.assertEqual(transforms.pad(sched, until=50), ref_sched)
Esempio n. 14
0
    def test_delay(self):
        """Test delay."""
        delay = Delay(10, DriveChannel(0), name='test_name')

        self.assertIsInstance(delay.id, int)
        self.assertEqual(delay.name, 'test_name')
        self.assertEqual(delay.duration, 10)
        self.assertEqual(delay.operands, (10, DriveChannel(0)))
        self.assertEqual(delay, Delay(10, DriveChannel(0)))
        self.assertNotEqual(delay, Delay(11, DriveChannel(1)))
        self.assertEqual(repr(delay),
                         "Delay(10, DriveChannel(0), name='test_name')")
Esempio n. 15
0
    def test_delay_snapshot_channel(self):
        """Test Delay on DriveChannel"""

        snapshot_ch = SnapshotChannel()
        snapshot = Snapshot(label='test')
        # should pass as is an append
        sched = Delay(self.delay_time, snapshot_ch) + snapshot
        self.assertIsInstance(sched, Schedule)
        # should fail due to overlap
        with self.assertRaises(PulseError):
            sched = Delay(self.delay_time, snapshot_ch) | snapshot << 5
            self.assertIsInstance(sched, Schedule)
Esempio n. 16
0
    def test_delay_control_channel(self):
        """Test Delay on ControlChannel"""

        control_ch = self.config.control([0, 1])[0]
        pulse = Waveform(np.full(10, 0.1))
        # should pass as is an append
        sched = Delay(self.delay_time, control_ch) + Play(pulse, control_ch)
        self.assertIsInstance(sched, Schedule)
        # should fail due to overlap
        with self.assertRaises(PulseError):
            sched = Delay(self.delay_time, control_ch) | Play(pulse, control_ch)
            self.assertIsInstance(sched, Schedule)
Esempio n. 17
0
 def test_delay_drive_channel(self):
     """Test Delay on DriveChannel"""
     drive_ch = self.config.drive(0)
     pulse = SamplePulse(np.full(10, 0.1))
     # should pass as is an append
     sched = Delay(self.delay_time, drive_ch) + Play(pulse, drive_ch)
     self.assertIsInstance(sched, Schedule)
     pulse_instr = sched.instructions[-1]
     # assert last instruction is pulse
     self.assertIsInstance(pulse_instr[1], Play)
     # assert pulse is scheduled at time 10
     self.assertEqual(pulse_instr[0], 10)
     # should fail due to overlap
     with self.assertRaises(PulseError):
         sched = Delay(self.delay_time, drive_ch) | Play(pulse, drive_ch)
    def test_equispaced_with_short_duration(self):
        """Test equispaced context with duration shorter than the schedule duration."""
        d0 = pulse.DriveChannel(0)

        sched = pulse.Schedule()
        for _ in range(3):
            sched.append(Delay(10, d0), inplace=True)

        sched = transforms.align_equispaced(sched, duration=20)

        reference = pulse.Schedule()
        reference.insert(0, Delay(10, d0), inplace=True)
        reference.insert(10, Delay(10, d0), inplace=True)
        reference.insert(20, Delay(10, d0), inplace=True)

        self.assertEqual(sched, reference)
Esempio n. 19
0
    def test_delay(self):
        """Test delay."""
        with self.assertWarns(DeprecationWarning):
            delay_command = Delay(10, name='test_name')

        self.assertEqual(delay_command.name, "test_name")
        self.assertEqual(delay_command.duration, 10)
    def test_numerical_with_short_duration(self):
        """Test numerical alignment context with duration shorter than the schedule duration."""
        d0 = pulse.DriveChannel(0)

        sched = pulse.Schedule()
        for _ in range(3):
            sched.append(Delay(10, d0), inplace=True)

        sched = transforms.align_func(sched, duration=20, func=self._position)

        reference = pulse.Schedule()
        reference.insert(0, Delay(10, d0), inplace=True)
        reference.insert(10, Delay(10, d0), inplace=True)
        reference.insert(20, Delay(10, d0), inplace=True)

        self.assertEqual(sched, reference)
    def test_delay(self):
        """Test delay."""
        delay = Delay(10, DriveChannel(0), name='test_name')

        self.assertEqual(delay.name, "test_name")
        self.assertEqual(delay.duration, 10)
        self.assertEqual(delay.operands, [10, DriveChannel(0)])
Esempio n. 22
0
    def test_numerical_with_longer_duration(self):
        """Test numerical alignment context with duration longer than the schedule duration."""
        context = transforms.AlignFunc(duration=80, func=self._position)

        d0 = pulse.DriveChannel(0)

        schedule = pulse.Schedule()
        for _ in range(3):
            schedule.append(Delay(10, d0), inplace=True)
        schedule = context.align(schedule)

        reference = pulse.Schedule()
        reference.insert(15, Delay(10, d0), inplace=True)
        reference.insert(35, Delay(10, d0), inplace=True)
        reference.insert(55, Delay(10, d0), inplace=True)

        self.assertEqual(schedule, reference)
Esempio n. 23
0
    def test_equispaced_with_longer_duration(self):
        """Test equispaced context with duration longer than the schedule duration."""
        context = transforms.AlignEquispaced(duration=50)

        d0 = pulse.DriveChannel(0)

        schedule = pulse.Schedule()
        for _ in range(3):
            schedule.append(Delay(10, d0), inplace=True)
        schedule = context.align(schedule)

        reference = pulse.Schedule()
        reference.insert(0, Delay(10, d0), inplace=True)
        reference.insert(20, Delay(10, d0), inplace=True)
        reference.insert(40, Delay(10, d0), inplace=True)

        self.assertEqual(schedule, reference)
    def test_equispaced_with_multiple_channels_short_duration(self):
        """Test equispaced context with multiple channels and duration shorter than the total
        duration."""
        d0 = pulse.DriveChannel(0)
        d1 = pulse.DriveChannel(1)

        sched = pulse.Schedule()
        sched.append(Delay(10, d0), inplace=True)
        sched.append(Delay(20, d1), inplace=True)

        sched = transforms.align_equispaced(sched, duration=20)

        reference = pulse.Schedule()
        reference.insert(0, Delay(10, d0), inplace=True)
        reference.insert(0, Delay(20, d1), inplace=True)

        self.assertEqual(sched, reference)
Esempio n. 25
0
    def test_equispaced_with_multiple_channels_longer_duration(self):
        """Test equispaced context with multiple channels and duration longer than the total
        duration."""
        context = transforms.AlignEquispaced(duration=30)

        d0 = pulse.DriveChannel(0)
        d1 = pulse.DriveChannel(1)

        schedule = pulse.Schedule()
        schedule.append(Delay(10, d0), inplace=True)
        schedule.append(Delay(20, d1), inplace=True)
        schedule = context.align(schedule)

        reference = pulse.Schedule()
        reference.insert(0, Delay(10, d0), inplace=True)
        reference.insert(10, Delay(20, d1), inplace=True)

        self.assertEqual(schedule, reference)
Esempio n. 26
0
    def test_default(self):
        """Test default snapshot."""
        snapshot = Snapshot(label='test_name', snapshot_type='state')

        self.assertIsInstance(snapshot.id, int)
        self.assertEqual(snapshot.name, 'test_name')
        self.assertEqual(snapshot.type, 'state')
        self.assertEqual(snapshot.duration, 0)
        self.assertNotEqual(snapshot, Delay(10, DriveChannel(0)))
        self.assertEqual(repr(snapshot), "Snapshot(test_name, state, name='test_name')")
Esempio n. 27
0
    def test_replace_fails_on_overlap(self):
        """Test that replacement fails on overlap."""
        old = Play(Constant(20, 1.0), DriveChannel(0))
        new = Play(Constant(100, 0.1), DriveChannel(0))

        sched = Schedule()
        sched += old
        sched += Delay(100, DriveChannel(0))

        with self.assertRaises(PulseError):
            sched.replace(old, new)
Esempio n. 28
0
def pad(schedule: Schedule,
        channels: Optional[Iterable[Channel]] = None,
        until: Optional[int] = None) -> Schedule:
    """Pad the input ``Schedule`` with ``Delay`` s on all unoccupied timeslots until ``until``
    if it is provided, otherwise until ``schedule.duration``.

    Args:
        schedule: Schedule to pad.
        channels: Channels to pad. Defaults to all channels in ``schedule`` if not provided.
                  If the supplied channel is not a member of ``schedule``, it will be added.
        until: Time to pad until. Defaults to ``schedule.duration`` if not provided.

    Returns:
        The padded schedule.
    """
    until = until or schedule.duration
    channels = channels or schedule.channels

    for channel in channels:
        if channel not in schedule.channels:
            schedule |= Delay(until, channel)
            continue

        curr_time = 0
        # TODO: Replace with method of getting instructions on a channel
        for interval in schedule.timeslots[channel]:
            if curr_time >= until:
                break
            if interval[0] != curr_time:
                end_time = min(interval[0], until)
                schedule = schedule.insert(
                    curr_time, Delay(end_time - curr_time, channel))
            curr_time = interval[1]
        if curr_time < until:
            schedule = schedule.insert(curr_time,
                                       Delay(until - curr_time, channel))

    return schedule
    def test_padding_schedule(self):
        """Test padding schedule."""
        delay = 10
        sched = (Delay(delay, DriveChannel(0)).shift(10) +
                 Delay(delay, DriveChannel(0)).shift(10) +
                 Delay(delay, DriveChannel(1)).shift(10))

        ref_sched = (sched | Delay(delay, DriveChannel(0))
                     | Delay(delay, DriveChannel(0)).shift(20)
                     | Delay(delay, DriveChannel(1))
                     | Delay(2 * delay, DriveChannel(1)).shift(20))

        self.assertEqual(transforms.pad(sched), ref_sched)
    def test_padding_schedule_inverse_order(self):
        """Test padding schedule is insensitive to order in which commands were added.

        This test is the same as `test_adding_schedule` but the order by channel
        in which commands were added to the schedule to be padded has been reversed.
        """
        delay = 10
        sched = (Delay(delay, DriveChannel(1)).shift(10) +
                 Delay(delay, DriveChannel(0)).shift(10) +
                 Delay(delay, DriveChannel(0)).shift(10))

        ref_sched = (sched | Delay(delay, DriveChannel(0))
                     | Delay(delay, DriveChannel(0)).shift(20)
                     | Delay(delay, DriveChannel(1))
                     | Delay(2 * delay, DriveChannel(1)).shift(20))

        self.assertEqual(transforms.pad(sched), ref_sched)