# Copyright 2019-2024 Cambridge Quantum Computing## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License."""Status classes for circuits submitted to backends."""fromcollections.abcimportCallablefromdatetimeimportdatetimefromenumimportEnumfromtypingimportAny,NamedTuple
[docs]classStatusEnum(Enum):"""Enumeration for the possible status of a circuit submitted to a backend."""COMPLETED="Circuit has completed. Results are ready."QUEUED="Circuit is queued."SUBMITTED="Circuit has been submitted."RUNNING="Circuit is running."RETRYING="Circuit is being retried."CANCELLING="Cancellation has been requested."CANCELLED="Circuit has been cancelled."ERROR="Circuit has errored. Check CircuitStatus.message for error message."
[docs]classCircuitStatus(NamedTuple):"""The status of a circuit along with an optional description. Optionally can also include extra fields such as: * Detailed error information. * Timestamps for changes in status. * Queue position. """status:StatusEnummessage:str=""error_detail:str|None=None# Timestamp for when a status was last entered.completed_time:datetime|None=Nonequeued_time:datetime|None=Nonesubmitted_time:datetime|None=Nonerunning_time:datetime|None=Nonecancelled_time:datetime|None=Noneerror_time:datetime|None=Nonequeue_position:int|None=None
[docs]@classmethoddeffrom_dict(cls,dic:dict[str,Any])->"CircuitStatus":"""Construct from JSON serializable dictionary."""invalid=ValueError(f"Dictionary invalid format for CircuitStatus: {dic}")if"message"notindicor"status"notindic:raiseinvalidtry:status=next(sforsinStatusEnumifdic["status"]==s.name)exceptStopIterationase:raiseinvalidfromeerror_detail=dic.get("error_detail")read_optional_datetime:Callable[[str],datetime|None]=lambdakey:(datetime.fromisoformat(x)if(x:=dic.get(key))isnotNoneelseNone)completed_time=read_optional_datetime("completed_time")queued_time=read_optional_datetime("queued_time")submitted_time=read_optional_datetime("submitted_time")running_time=read_optional_datetime("running_time")cancelled_time=read_optional_datetime("cancelled_time")error_time=read_optional_datetime("error_time")queue_position=dic.get("queue_position")returncls(status,dic["message"],error_detail,completed_time,queued_time,submitted_time,running_time,cancelled_time,error_time,queue_position,)