def test_create_reservation_add_at_end(): time_slot = qtypes.QuantumTimeSlot( processor_name='test', start_time=Timestamp(seconds=1000000), end_time=Timestamp(seconds=2000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.UNALLOCATED, ) p = NothingProcessor(processor_id='test', schedule=[time_slot]) p.create_reservation( start_time=_time(2500000), end_time=_time(3500000), ) assert p.get_schedule(from_time=_time(500000), to_time=_time(2500000)) == [ qtypes.QuantumTimeSlot( processor_name='test', start_time=Timestamp(seconds=1000000), end_time=Timestamp(seconds=2000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.UNALLOCATED, ), qtypes.QuantumTimeSlot( processor_name='test', start_time=Timestamp(seconds=2500000), end_time=Timestamp(seconds=3500000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.RESERVATION, ), ]
def test_get_schedule(): time_slot = qtypes.QuantumTimeSlot( processor_name='test', start_time=Timestamp(seconds=1000000), end_time=Timestamp(seconds=2000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.OPEN_SWIM, ) p = NothingProcessor(processor_id='test', schedule=[time_slot]) assert p.get_schedule(from_time=_time(500000), to_time=_time(2500000)) == [time_slot] assert p.get_schedule(from_time=_time(1500000), to_time=_time(2500000)) == [time_slot] assert p.get_schedule(from_time=_time(500000), to_time=_time(1500000)) == [time_slot] assert p.get_schedule(from_time=_time(500000), to_time=_time(750000)) == [] assert p.get_schedule(from_time=_time(2500000), to_time=_time(300000)) == [] # check unbounded cases unbounded_start = qtypes.QuantumTimeSlot( processor_name='test', end_time=Timestamp(seconds=1000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.OPEN_SWIM, ) unbounded_end = qtypes.QuantumTimeSlot( processor_name='test', start_time=Timestamp(seconds=2000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.OPEN_SWIM, ) p = NothingProcessor(processor_id='test', schedule=[unbounded_start, unbounded_end]) assert ( p.get_schedule( from_time=_time(500000), to_time=_time(2500000), ) == [unbounded_start, unbounded_end] ) assert ( p.get_schedule( from_time=_time(1500000), to_time=_time(2500000), ) == [unbounded_end] ) assert ( p.get_schedule( from_time=_time(500000), to_time=_time(1500000), ) == [unbounded_start] ) assert ( p.get_schedule( from_time=_time(1200000), to_time=_time(1500000), ) == [] )
def test_bad_schedule(): time_slot1 = qtypes.QuantumTimeSlot( processor_name='test', start_time=Timestamp(seconds=1000000), end_time=Timestamp(seconds=3000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.OPEN_SWIM, ) time_slot2 = qtypes.QuantumTimeSlot( processor_name='test', start_time=Timestamp(seconds=2000000), end_time=Timestamp(seconds=4000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.OPEN_SWIM, ) with pytest.raises(ValueError, match='cannot overlap'): _ = NothingProcessor(processor_id='test', schedule=[time_slot1, time_slot2])
def __init__( self, *, processor_id: str, engine: Optional['AbstractEngine'] = None, expected_down_time: Optional[datetime.datetime] = None, expected_recovery_time: Optional[datetime.datetime] = None, schedule: Optional[List[qtypes.QuantumTimeSlot]] = None, project_name: str = 'fake_project', ): self._engine = engine self._expected_recovery_time = expected_recovery_time self._expected_down_time = expected_down_time self._reservations: Dict[str, qtypes.QuantumReservation] = {} self._resource_id_counter = 0 self._processor_id = processor_id self._project_name = project_name if schedule is None: self._schedule = [ qtypes.QuantumTimeSlot( processor_name=self._processor_id, slot_type=qenums.QuantumTimeSlot.TimeSlotType.UNALLOCATED, ) ] else: self._schedule = copy.copy(schedule) self._schedule.sort(key=lambda t: t.start_time.seconds or -1) for idx in range(len(self._schedule) - 1): if self._schedule[idx].end_time.seconds > self._schedule[ idx + 1].start_time.seconds: raise ValueError('Time slots cannot overlap!')
def test_create_reservation_splitunbounded(): time_slot_begin = qtypes.QuantumTimeSlot( processor_name='test', end_time=Timestamp(seconds=3000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.UNALLOCATED, ) time_slot_end = qtypes.QuantumTimeSlot( processor_name='test', start_time=Timestamp(seconds=5000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.UNALLOCATED, ) p = NothingProcessor(processor_id='test', schedule=[time_slot_begin, time_slot_end]) p.create_reservation( start_time=_time(1000000), end_time=_time(2000000), ) p.create_reservation( start_time=_time(6000000), end_time=_time(7000000), ) assert p.get_schedule(from_time=_time(200000), to_time=_time(10000000)) == [ qtypes.QuantumTimeSlot( processor_name='test', end_time=Timestamp(seconds=1000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.UNALLOCATED, ), qtypes.QuantumTimeSlot( processor_name='test', start_time=Timestamp(seconds=1000000), end_time=Timestamp(seconds=2000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.RESERVATION, ), qtypes.QuantumTimeSlot( processor_name='test', start_time=Timestamp(seconds=2000000), end_time=Timestamp(seconds=3000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.UNALLOCATED, ), qtypes.QuantumTimeSlot( processor_name='test', start_time=Timestamp(seconds=5000000), end_time=Timestamp(seconds=6000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.UNALLOCATED, ), qtypes.QuantumTimeSlot( processor_name='test', start_time=Timestamp(seconds=6000000), end_time=Timestamp(seconds=7000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.RESERVATION, ), qtypes.QuantumTimeSlot( processor_name='test', start_time=Timestamp(seconds=7000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.UNALLOCATED, ), ]
def _reservation_to_time_slot( self, reservation: qtypes.QuantumReservation) -> qtypes.QuantumTimeSlot: """Changes a reservation object into a time slot object.""" return qtypes.QuantumTimeSlot( processor_name=self._processor_id, start_time=Timestamp(seconds=reservation.start_time.seconds), end_time=Timestamp(seconds=reservation.end_time.seconds), slot_type=qenums.QuantumTimeSlot.TimeSlotType.RESERVATION, )
def to_proto(self): time_slot = qtypes.QuantumTimeSlot( processor_name=self.processor_id, start_time=_to_timestamp(self.start_time), end_time=_to_timestamp(self.end_time), slot_type=self.slot_type, ) if self.project_id: time_slot.reservation_config.project_id = self.project_id config = time_slot.maintenance_config if self.maintenance_title: config.title = self.maintenance_title if self.maintenance_description: config.description = self.maintenance_description return time_slot
def test_from_to_proto_plain(): slot = enums.QuantumTimeSlot.TimeSlotType.RESERVATION proto = qtypes.QuantumTimeSlot( processor_name='potofgold', start_time=Timestamp(seconds=1500000000), end_time=Timestamp(seconds=1500010000), slot_type=slot, ) time_slot = cg.EngineTimeSlot( processor_id='potofgold', start_time=datetime.datetime.fromtimestamp(1500000000), end_time=datetime.datetime.fromtimestamp(1500010000), slot_type=slot, ) actual_from_proto = cg.EngineTimeSlot.from_proto(proto) assert actual_from_proto == time_slot actual_to_proto = cg.EngineTimeSlot.to_proto(time_slot) assert actual_to_proto == proto
def test_from_to_proto_reservation(): slot = enums.QuantumTimeSlot.TimeSlotType.RESERVATION proto = qtypes.QuantumTimeSlot( processor_name='potofgold', start_time=Timestamp(seconds=1500000000), end_time=Timestamp(seconds=1500010000), slot_type=slot, reservation_config=qtypes.QuantumTimeSlot.ReservationConfig( project_id='super_secret_quantum' ), ) time_slot = cg.EngineTimeSlot( processor_id='potofgold', start_time=datetime.datetime.fromtimestamp(1500000000), end_time=datetime.datetime.fromtimestamp(1500010000), slot_type=slot, project_id='super_secret_quantum', ) actual_from_proto = cg.EngineTimeSlot.from_proto(proto) assert actual_from_proto == time_slot actual_to_proto = cg.EngineTimeSlot.to_proto(time_slot) assert actual_to_proto == proto
def test_from_to_proto_no_start_time(): slot = enums.QuantumTimeSlot.TimeSlotType.MAINTENANCE proto = qtypes.QuantumTimeSlot( processor_name='potofgold', start_time=Timestamp(seconds=1500040000), slot_type=slot, maintenance_config=qtypes.QuantumTimeSlot.MaintenanceConfig( title='Testing', description='Testing some new configuration.', ), ) time_slot = cg.EngineTimeSlot( processor_id='potofgold', start_time=datetime.datetime.fromtimestamp(1500040000), slot_type=slot, maintenance_title='Testing', maintenance_description='Testing some new configuration.', ) actual_from_proto = cg.EngineTimeSlot.from_proto(proto) assert actual_from_proto == time_slot actual_to_proto = cg.EngineTimeSlot.to_proto(time_slot) assert actual_to_proto == proto
) assert ( p.get_schedule( from_time=_time(1200000), to_time=_time(1500000), ) == [] ) @pytest.mark.parametrize( ('time_slot'), ( qtypes.QuantumTimeSlot( processor_name='test', start_time=Timestamp(seconds=1000000), end_time=Timestamp(seconds=2000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.OPEN_SWIM, ), qtypes.QuantumTimeSlot( processor_name='test', start_time=Timestamp(seconds=1000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.OPEN_SWIM, ), qtypes.QuantumTimeSlot( processor_name='test', end_time=Timestamp(seconds=2000000), slot_type=qenums.QuantumTimeSlot.TimeSlotType.OPEN_SWIM, ), ), ) def test_create_reservation_not_available(time_slot):
def _insert_reservation_into(self, time_slot: qtypes.QuantumTimeSlot) -> None: """Inserts a new reservation time slot into the ordered schedule. If this reservation overlaps with existing time slots, these slots will be shortened, removed, or split to insert the new reservation. """ new_schedule = [] time_slot_inserted = False for t in self._schedule: if t.end_time.seconds and t.end_time.seconds <= time_slot.start_time.seconds: # [--time_slot--] # [--t--] new_schedule.append(t) continue if t.start_time.seconds and t.start_time.seconds >= time_slot.end_time.seconds: # [--time_slot--] # [--t--] new_schedule.append(t) continue if t.start_time.seconds and time_slot.start_time.seconds <= t.start_time.seconds: if not time_slot_inserted: new_schedule.append(time_slot) time_slot_inserted = True if not t.end_time.seconds or t.end_time.seconds > time_slot.end_time.seconds: # [--time_slot---] # [----t-----] t.start_time.seconds = time_slot.end_time.seconds new_schedule.append(t) # if t.end_time < time_slot.end_time # [------time_slot-----] # [-----t-----] # t should be removed else: if not t.end_time.seconds or t.end_time.seconds > time_slot.end_time.seconds: # [---time_slot---] # [-------------t---------] # t should be split start = qtypes.QuantumTimeSlot( processor_name=self._processor_id, end_time=Timestamp( seconds=time_slot.start_time.seconds), slot_type=t.slot_type, ) if t.start_time.seconds: start.start_time.seconds = t.start_time.seconds end = qtypes.QuantumTimeSlot( processor_name=self._processor_id, start_time=Timestamp( seconds=time_slot.end_time.seconds), slot_type=t.slot_type, ) if t.end_time.seconds: end.end_time.seconds = t.end_time.seconds new_schedule.append(start) new_schedule.append(time_slot) new_schedule.append(end) else: # [---time_slot---] # [----t-----] t.end_time.seconds = time_slot.start_time.seconds new_schedule.append(t) new_schedule.append(time_slot) time_slot_inserted = True if not time_slot_inserted: new_schedule.append(time_slot) self._schedule = new_schedule