def test_interval_equality(self): """Test equality and inequality of intervals.""" interval_1 = Interval(3, 10) interval_2 = Interval(3, 10) interval_3 = Interval(2, 10) self.assertTrue(interval_1 == interval_2) self.assertFalse(interval_1 == interval_3)
def test_filter_intervals(self): """Test filtering on intervals.""" device = self.two_qubit_device lp0 = self.linear(duration=3, slope=0.2, intercept=0.1) acquire = Acquire(5) sched = Schedule(name='fake_experiment') sched = sched.insert(0, lp0(device.drives[0])) sched = sched.insert(10, lp0(device.drives[1])) sched = sched.insert(30, FrameChange(phase=-1.57)(device.drives[0])) sched = sched.insert(60, acquire(device.acquires, device.memoryslots)) sched = sched.insert(90, lp0(device.drives[0])) intervals_a = sched.filter(time_ranges=((0, 13), )) for time, inst in intervals_a.instructions: self.assertTrue(0 <= time <= 13) self.assertTrue(inst.timeslots.timeslots[0].interval.stop <= 13) self.assertEqual(len(intervals_a.instructions), 2) intervals_b = sched.filter(time_ranges=[(59, 65)]) self.assertEqual(len(intervals_b.instructions), 1) self.assertEqual(intervals_b.instructions[0][0], 60) self.assertIsInstance(intervals_b.instructions[0][1], AcquireInstruction) non_full_intervals = sched.filter(time_ranges=[(0, 2), (8, 11), (61, 70)]) self.assertEqual(len(non_full_intervals.instructions), 0) multi_interval = sched.filter(time_ranges=[(10, 15), (63, 93)]) self.assertEqual(len(multi_interval.instructions), 2) multi_interval = sched.filter( intervals=[Interval(10, 15), Interval(63, 93)]) self.assertEqual(len(multi_interval.instructions), 2)
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)
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)
def test_invalid_interval(self): """Test invalid instantiation with negative time bounds.""" with self.assertRaises(PulseError): Interval(-1, 5) with self.assertRaises(PulseError): Interval(2, -5) with self.assertRaises(PulseError): Interval(5, 2)
def test_filter_intervals(self): """Test filtering on intervals.""" lp0 = self.linear(duration=3, slope=0.2, intercept=0.1) acquire = Acquire(5) sched = Schedule(name='fake_experiment') sched = sched.insert(0, lp0(self.config.drive(0))) sched = sched.insert(10, lp0(self.config.drive(1))) sched = sched.insert(30, FrameChange(phase=-1.57)(self.config.drive(0))) sched = sched.insert( 60, acquire([self.config.acquire(i) for i in range(2)], [MemorySlot(i) for i in range(2)])) sched = sched.insert(90, lp0(self.config.drive(0))) # split schedule into instructions occuring in (0,13), and those outside filtered, excluded = self._filter_and_test_consistency( sched, time_ranges=((0, 13), )) for start_time, inst in filtered.instructions: self.assertTrue((start_time >= 0) and (start_time + inst.stop_time <= 13)) for start_time, inst in excluded.instructions: self.assertFalse((start_time >= 0) and (start_time + inst.stop_time <= 13)) self.assertEqual(len(filtered.instructions), 2) self.assertEqual(len(excluded.instructions), 3) # split into schedule occuring in and outside of interval (59,65) filtered, excluded = self._filter_and_test_consistency(sched, time_ranges=[ (59, 65) ]) self.assertEqual(len(filtered.instructions), 1) self.assertEqual(filtered.instructions[0][0], 60) self.assertIsInstance(filtered.instructions[0][1], AcquireInstruction) self.assertEqual(len(excluded.instructions), 4) self.assertEqual(excluded.instructions[3][0], 90) self.assertIsInstance(excluded.instructions[3][1], PulseInstruction) # split instructions based on the interval # (none should be, though they have some overlap with some of the instructions) filtered, excluded = \ self._filter_and_test_consistency(sched, time_ranges=[(0, 2), (8, 11), (61, 70)]) self.assertEqual(len(filtered.instructions), 0) self.assertEqual(len(excluded.instructions), 5) # split instructions from multiple non-overlapping intervals, specified # as time ranges filtered, excluded = \ self._filter_and_test_consistency(sched, time_ranges=[(10, 15), (63, 93)]) self.assertEqual(len(filtered.instructions), 2) self.assertEqual(len(excluded.instructions), 3) # split instructions from non-overlapping intervals, specified as Intervals filtered, excluded = \ self._filter_and_test_consistency(sched, intervals=[Interval(10, 15), Interval(63, 93)]) self.assertEqual(len(filtered.instructions), 2) self.assertEqual(len(excluded.instructions), 3)
def test_shift_interval(self): """Test valid interval creation without error. """ interval = Interval(1, 3) shift = interval.shift(10) self.assertEqual(11, shift.start) self.assertEqual(13, shift.stop) self.assertEqual(2, shift.duration) # keep original interval unchanged self.assertEqual(1, interval.start)
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)
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)))
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))
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)
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)
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
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
def test_default(self): """Test valid interval creation without error. """ interval = Interval(1, 3) self.assertEqual(1, interval.start) self.assertEqual(3, interval.stop) self.assertEqual(2, interval.duration)
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))
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)
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
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)
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))')
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)
def test_representation_of_interval_object(self): """Test string representation of intervals.""" interval = Interval(3, 10) self.assertEqual(str(interval), 'Interval(3, 10)')
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)))
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))
def test_check_overlap(self): """Test valid interval creation without error. """ self.assertEqual(True, Interval(1, 3).has_overlap(Interval(2, 4))) self.assertEqual(True, Interval(1, 3).has_overlap(Interval(2, 2))) self.assertEqual(False, Interval(1, 3).has_overlap(Interval(1, 1))) self.assertEqual(False, Interval(1, 3).has_overlap(Interval(3, 3))) self.assertEqual(False, Interval(1, 3).has_overlap(Interval(0, 1))) self.assertEqual(False, Interval(1, 3).has_overlap(Interval(3, 4)))