예제 #1
0
 def test_can_create_with_valid_timeslots(self):
     """Test valid time-slot collection creation without error.
     """
     slots = [
         Timeslot(Interval(1, 3), AcquireChannel(0)),
         Timeslot(Interval(3, 5), AcquireChannel(0))
     ]
     TimeslotCollection(*slots)
예제 #2
0
 def test_shift(self):
     """Test if `shift` shifts the collection for specified time.
     """
     actual = TimeslotCollection(Timeslot(Interval(1, 3),
                                          AcquireChannel(0))).shift(10)
     expected = TimeslotCollection(
         Timeslot(Interval(11, 13), AcquireChannel(0)))
     self.assertEqual(expected, actual)
예제 #3
0
    def test_zero_duration_timeslot(self):
        """Test that TimeslotCollection works properly for zero duration timeslots."""
        # test that inserting zero duration pulses at start and stop of pulse works.
        TimeslotCollection(Timeslot(Interval(0, 10), AcquireChannel(0)),
                           Timeslot(Interval(0, 0), AcquireChannel(0)),
                           Timeslot(Interval(10, 10), AcquireChannel(0)))

        with self.assertRaises(PulseError):
            TimeslotCollection(Timeslot(Interval(0, 10), AcquireChannel(0)),
                               Timeslot(Interval(5, 5), AcquireChannel(0)))
예제 #4
0
    def test_empty_collection(self):
        """Test empty collection creation and its operations.
        """
        empty = TimeslotCollection()
        self.assertEqual(True, empty.is_mergeable_with(empty))

        # can merge with normal collection
        normal = TimeslotCollection(Timeslot(Interval(1, 3), AcquireChannel(0)),
                                    Timeslot(Interval(3, 5), AcquireChannel(0)))
        self.assertEqual(True, empty.is_mergeable_with(normal))
        self.assertEqual(normal, empty.merged(normal))
예제 #5
0
    def test_merged_timeslots_do_not_overlap_internally(self):
        """Test to make sure the timeslots do not overlap internally."""
        # same interval but different channel
        int1 = Timeslot(Interval(1, 3), AcquireChannel(0))
        col1 = TimeslotCollection(Timeslot(Interval(5, 7), AcquireChannel(0)))
        col2 = TimeslotCollection(Timeslot(Interval(5, 7), AcquireChannel(1)))

        merged = TimeslotCollection(int1, col1, col2)

        from_internal = TimeslotCollection(*merged.timeslots)

        self.assertEqual(merged, from_internal)
예제 #6
0
    def __init__(self,
                 command,
                 *channels: List[Channel],
                 timeslots: TimeslotCollection = None,
                 name=None):
        """
        command (Command): Pulse command to schedule
        *channels: List of pulse channels to schedule with command
        timeslots: Optional list of timeslots. If channels are supplied timeslots
            cannot also be given
        name: Name of Instruction
        """
        self._command = command
        self._name = name if name else self._command.name

        if timeslots and channels:
            raise PulseError(
                'Channels and timeslots may not both be supplied.')

        if not timeslots:
            duration = command.duration
            self._timeslots = TimeslotCollection(
                *(Timeslot(Interval(0, duration), channel)
                  for channel in channels))
        else:
            self._timeslots = timeslots
    def __init__(self,
                 command,
                 *channels: List[Channel],
                 timeslots: Optional[TimeslotCollection] = None,
                 name: Optional[str] = None):
        """
        Args:
            command: Pulse command to schedule
            *channels: List of pulse channels to schedule with command
            timeslots: Optional list of timeslots. If channels are supplied timeslots
                cannot also be given
            name: Name of Instruction

        Raises:
            PulseError: If both channels and timeslots are supplied
        """
        self._command = command
        self._name = name if name else self._command.name

        if timeslots and channels:
            raise PulseError(
                'Channels and timeslots may not both be supplied.')

        if not timeslots:
            duration = command.duration
            self._timeslots = TimeslotCollection(
                *(Timeslot(Interval(0, duration), channel)
                  for channel in channels))
        else:
            self._timeslots = timeslots

        channels = self.channels

        self._buffer = max(chan.buffer for chan in channels) if channels else 0
예제 #8
0
    def __init__(self, duration: Union['commands.Command', int],
                 *channels: Channel,
                 name: Optional[str] = None):
        """Instruction initializer.

        Args:
            duration: Length of time taken by the instruction in terms of dt.
            *channels: List of pulse channels that this instruction operates on.
            name: Display name for this instruction.
        """
        self._command = None
        if not isinstance(duration, int):
            # TODO: Add deprecation warning once all instructions are migrated
            self._command = duration
            if name is None:
                name = self.command.name
            duration = self.command.duration
        self._duration = duration

        self._timeslots = TimeslotCollection(*(Timeslot(Interval(0, duration), channel)
                                               for channel in channels if channel is not None))

        if name is None:
            name = "{}{}".format(self.__class__.__name__.lower(), str(hex(self.__hash__()))[3:8])
        self._name = name
예제 #9
0
    def test_is_mergeable_does_not_mutate_TimeslotCollection(self):
        """Test that is_mergeable_with does not mutate the given TimeslotCollection"""

        # Different channel but different interval
        col1 = TimeslotCollection(Timeslot(Interval(1, 2), AcquireChannel(0)))
        expected_channels = col1.channels
        col2 = TimeslotCollection(Timeslot(Interval(3, 4), AcquireChannel(1)))
        col1.is_mergeable_with(col2)
        self.assertEqual(col1.channels, expected_channels)

        # Different channel but same interval
        col1 = TimeslotCollection(Timeslot(Interval(1, 2), AcquireChannel(0)))
        expected_channels = col1.channels
        col2 = TimeslotCollection(Timeslot(Interval(1, 2), AcquireChannel(1)))
        col1.is_mergeable_with(col2)
        self.assertEqual(col1.channels, expected_channels)
예제 #10
0
    def test_default(self):
        """Test valid time-slot creation without error.
        """
        slot = Timeslot(Interval(1, 3), AcquireChannel(0))
        self.assertEqual(Interval(1, 3), slot.interval)
        self.assertEqual(AcquireChannel(0), slot.channel)

        self.assertEqual(1, slot.start)
        self.assertEqual(3, slot.stop)
        self.assertEqual(2, slot.duration)
예제 #11
0
    def test_can_merge_two_mergeable_collections(self):
        """Test if merge two mergeable time-slot collections.
        """
        # same interval but different channel
        col1 = TimeslotCollection(Timeslot(Interval(1, 3), AcquireChannel(0)))
        col2 = TimeslotCollection(Timeslot(Interval(1, 3), AcquireChannel(1)))
        self.assertEqual(True, col1.is_mergeable_with(col2))
        expected = TimeslotCollection(Timeslot(Interval(1, 3), AcquireChannel(0)),
                                      Timeslot(Interval(1, 3), AcquireChannel(1)))
        self.assertEqual(expected, col1.merged(col2))

        # same channel but different interval
        col1 = TimeslotCollection(Timeslot(Interval(1, 3), AcquireChannel(0)))
        col2 = TimeslotCollection(Timeslot(Interval(3, 5), AcquireChannel(0)))
        self.assertEqual(True, col1.is_mergeable_with(col2))
        expected = TimeslotCollection(Timeslot(Interval(1, 3), AcquireChannel(0)),
                                      Timeslot(Interval(3, 5), AcquireChannel(0)))
        self.assertEqual(expected, col1.merged(col2))
예제 #12
0
    def test_complement_set_stop_time(self):
        """Test complement of TimeslotCollection obeys stop_time."""
        stop_time = 20

        test_collection = TimeslotCollection(
            Timeslot(Interval(5, 7), DriveChannel(0)),
            Timeslot(Interval(10, 12), DriveChannel(0)),
            Timeslot(Interval(0, 3), DriveChannel(1)))
        ref_complement = TimeslotCollection(
            Timeslot(Interval(0, 5), DriveChannel(0)),
            Timeslot(Interval(7, 10), DriveChannel(0)),
            Timeslot(Interval(12, stop_time), DriveChannel(0)),
            Timeslot(Interval(3, stop_time), DriveChannel(1)))
        complement_collection = test_collection.complement(stop_time=stop_time)

        self.assertEqual(ref_complement, complement_collection)
예제 #13
0
    def __init__(self, command, *channels: List[Channel],
                 name: Optional[str] = None):
        """Instruction initializer.

        Args:
            command: Pulse command to schedule
            *channels: List of pulse channels to schedule with command
            name: Name of Instruction
        """
        self._command = command
        self._name = name if name else self._command.name

        duration = command.duration

        self._timeslots = TimeslotCollection(*(Timeslot(Interval(0, duration), channel)
                                               for channel in channels))

        channels = self.channels
예제 #14
0
    def test_complement(self):
        """Test complement of TimeslotCollection is properly created."""
        test_collection = TimeslotCollection(
            Timeslot(Interval(5, 7), DriveChannel(0)),
            Timeslot(Interval(10, 12), DriveChannel(0)),
            Timeslot(Interval(0, 3), DriveChannel(1)))
        ref_complement = TimeslotCollection(
            Timeslot(Interval(0, 5), DriveChannel(0)),
            Timeslot(Interval(7, 10), DriveChannel(0)),
            Timeslot(Interval(3, 12), DriveChannel(1)))
        complement_collection = test_collection.complement()

        self.assertEqual(ref_complement, complement_collection)
예제 #15
0
 def test_representation_of_timeslot_object(self):
     """Test representation of Timeslot object."""
     slot = Timeslot(Interval(1, 5), AcquireChannel(0))
     self.assertEqual(repr(slot), 'Timeslot(AcquireChannel(0), (1, 5))')
예제 #16
0
 def test_shift(self):
     """Test shifting of Timeslot."""
     slot_1 = Timeslot(Interval(1, 3), AcquireChannel(0))
     slot_2 = Timeslot(Interval(1 + 5, 3 + 5), AcquireChannel(0))
     shifted_slot = slot_1.shift(+5)
     self.assertTrue(slot_2 == shifted_slot)
예제 #17
0
    def test_check_overlap(self):
        """Test valid interval creation without error.
        """
        dc0 = DriveChannel(0)
        dc1 = DriveChannel(1)

        self.assertEqual(
            True,
            Timeslot(Interval(1, 3),
                     dc0).has_overlap(Timeslot(Interval(2, 4), dc0)))

        self.assertEqual(
            True,
            Timeslot(Interval(1, 3),
                     dc0).has_overlap(Timeslot(Interval(2, 2), dc0)))

        self.assertEqual(
            False,
            Timeslot(Interval(1, 3),
                     dc0).has_overlap(Timeslot(Interval(1, 1), dc0)))
        self.assertEqual(
            False,
            Timeslot(Interval(1, 3),
                     dc0).has_overlap(Timeslot(Interval(3, 3), dc0)))
        self.assertEqual(
            False,
            Timeslot(Interval(1, 3),
                     dc0).has_overlap(Timeslot(Interval(0, 1), dc0)))
        self.assertEqual(
            False,
            Timeslot(Interval(1, 3),
                     dc0).has_overlap(Timeslot(Interval(3, 4), dc0)))

        # check no overlap if different channels
        self.assertEqual(
            False,
            Timeslot(Interval(1, 3),
                     dc0).has_overlap(Timeslot(Interval(1, 3), dc1)))
예제 #18
0
 def test_unmergeable_collections(self):
     """Test if return false for unmergeable collections.
     """
     col1 = TimeslotCollection(Timeslot(Interval(1, 3), AcquireChannel(0)))
     col2 = TimeslotCollection(Timeslot(Interval(2, 4), AcquireChannel(0)))
     self.assertEqual(False, col1.is_mergeable_with(col2))