Source code for qiskit_experiments.calibration_management.parameter_value
# This code is part of Qiskit.## (C) Copyright IBM 2021.## This code is licensed under the Apache License, Version 2.0. You may# obtain a copy of this license in the LICENSE.txt file in the root directory# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.## Any modifications or derivative works of this code must retain this# copyright notice, and modified files need to carry a notice indicating# that they have been altered from the originals."""Data class for parameter values."""fromdataclassesimportdataclassfromdatetimeimportdatetimefromtypingimportUnionfromqiskit_experiments.exceptionsimportCalibrationError
[docs]@dataclassclassParameterValue:"""A data class to store parameter values."""# Value assumed by the parametervalue:Union[int,float,complex]=None# Data time when the value of the parameter was generateddate_time:datetime=datetime.fromtimestamp(0)# A bool indicating if the parameter is validvalid:bool=True# The experiment from which the value of this parameter was generated.exp_id:str=None# The group of calibrations to which this parameter belongsgroup:str="default"def__post_init__(self):""" Ensure that the variables in self have the proper types. This allows us to give strings to self.__init__ as input which is useful when loading serialized parameter values. """ifisinstance(self.valid,str):ifself.valid=="True":self.valid=Trueelse:self.valid=Falseifisinstance(self.value,str):self.value=self._validated_value(self.value)ifisinstance(self.date_time,str):base_fmt="%Y-%m-%d %H:%M:%S.%f"zone_fmts=["%z","","Z"]fortime_zoneinzone_fmts:date_format=base_fmt+time_zonetry:self.date_time=datetime.strptime(self.date_time,date_format)breakexceptValueError:passelse:formats=list(base_fmt+zoneforzoneinzone_fmts)raiseCalibrationError(f"Cannot parse {self.date_time} in either of {formats} formats.")self.date_time=self.date_time.astimezone()ifnotisinstance(self.value,(int,float,complex)):raiseCalibrationError(f"Values {self.value} must be int, float or complex.")ifnotisinstance(self.date_time,datetime):raiseCalibrationError(f"Datetime {self.date_time} must be a datetime.")ifnotisinstance(self.valid,bool):raiseCalibrationError(f"Valid {self.valid} is not a boolean.")ifself.exp_idandnotisinstance(self.exp_id,str):raiseCalibrationError(f"Experiment id {self.exp_id} is not a string.")ifnotisinstance(self.group,str):raiseCalibrationError(f"Group {self.group} is not a string.")@staticmethoddef_validated_value(value:str)->Union[int,float,complex]:""" Convert the string representation of value to the correct type. Args: value: The string to convert to either an int, float, or complex. Returns: value converted to either int, float, or complex. Raises: CalibrationError: If the conversion fails. """try:returnint(value)exceptValueError:passtry:returnfloat(value)exceptValueError:passtry:returncomplex(value)exceptValueErrorasval_err:raiseCalibrationError(f"Could not convert {value} to int, float, or complex.")fromval_err