# 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."""ResultHandle class"""fromtypingimportTuple,Type,Union,Iterator,overloadfromastimportliteral_evalfromcollections.abcimportSequence# mypy doesn't think you can pass the tuple to UnionBasicHashType=Union[int,float,complex,str,bool,bytes]_ResultIdTuple=Tuple[Union[Type[int],Type[float],Type[complex],Type[str],Type[bool],Type[bytes]],...,]
[docs]classResultHandle(Sequence):"""Object to store multidimensional identifiers for a circuit sent to a backend for execution. Initialisation arguments must be hashable basic types. Note that a `ResultHandle` may be either persistent or transient, depending on the backend: consult the :py:attr:`pytket.backends.Backend.persistent_handles` property to determine this. """def__init__(self,*args:BasicHashType):self._identifiers=tuple(args)
[docs]@classmethoddeffrom_str(cls,string:str)->"ResultHandle":"""Construct ResultHandle from string (output from str()) :raises ValueError: If string format is invalid :return: Instance of ResultHandle :rtype: ResultHandle """try:evaltuple=literal_eval(string)# will raise ValueError if failedif(notisinstance(evaltuple,tuple))or(notall(isinstance(arg,(int,float,complex,str,bool,bytes))forarginevaltuple)):raiseValueError# type check failedreturncls(*evaltuple)exceptValueError:raiseValueError("ResultHandle string format invalid.")
def__hash__(self)->int:returnhash(self._identifiers)def__eq__(self,other:object)->bool:ifnotisinstance(other,ResultHandle):returnNotImplementedreturnbool(self._identifiers==other._identifiers)def__str__(self)->str:returnstr(self._identifiers)def__repr__(self)->str:return"ResultHandle"+repr(self._identifiers)def__iter__(self)->Iterator:returniter(self._identifiers)def__len__(self)->int:returnlen(self._identifiers)@overloaddef__getitem__(self,key:int)->BasicHashType:...@overloaddef__getitem__(self,key:slice)->Tuple[BasicHashType,...]:...def__getitem__(self,key:Union[int,slice])->Union[BasicHashType,Tuple[BasicHashType,...]]:# weird logic required to make mypy happy, can't just# return self._identifiers[key]ifisinstance(key,slice):returnself._identifiers[key]returnself._identifiers[key]