Source code for qiskit_experiments.framework.configs
# 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."""Experiment and analysis config dataclasses."""importdataclassesfromtypingimportTuple,Dict,Anyfromqiskit.exceptionsimportQiskitErrorfromqiskit_experiments.versionimport__version__
[docs]@dataclasses.dataclass(frozen=True)classExperimentConfig:"""Store configuration settings for an Experiment class. This stores the current configuration of a :class:`.BaseExperiment` and can be used to reconstruct the experiment using either the :meth:`experiment` property if the experiment class type is currently stored, or the :meth:`~.BaseExperiment.from_config` class method of the appropriate experiment. """cls:type=Noneargs:Tuple[Any]=dataclasses.field(default_factory=tuple)kwargs:Dict[str,Any]=dataclasses.field(default_factory=dict)experiment_options:Dict[str,Any]=dataclasses.field(default_factory=dict)transpile_options:Dict[str,Any]=dataclasses.field(default_factory=dict)run_options:Dict[str,Any]=dataclasses.field(default_factory=dict)version:str=__version__
[docs]defexperiment(self):"""Return the experiment constructed from this config. Returns: BaseExperiment: The experiment reconstructed from the config. Raises: QiskitError: If the experiment class is not stored, was not successful deserialized, or reconstruction of the experiment fails. """cls=self.clsifclsisNone:raiseQiskitError("No experiment class in experiment config")ifisinstance(cls,dict):raiseQiskitError("Unable to load experiment class. Try manually loading ""experiment using `Experiment.from_config(config)` instead.")try:returncls.from_config(self)exceptExceptionasex:msg="Unable to construct experiments from config."ifcls.version!=__version__:msg+=(f" Note that config version ({cls.version}) differs from the current"f" qiskit-experiments version ({__version__}). You could try"" installing a compatible qiskit-experiments version.")raiseQiskitError(f"{msg}\nError Message:\n{str(ex)}")fromex
[docs]@dataclasses.dataclass(frozen=True)classAnalysisConfig:"""Store configuration settings for an Analysis class. This stores the current configuration of a :class:`.BaseAnalysis` and can be used to reconstruct the analysis class using either the :meth:`analysis` property if the analysis class type is currently stored, or the :meth:`~.BaseAnalysis.from_config` class method of the appropriate experiment. """cls:type=Noneargs:Tuple[Any]=dataclasses.field(default_factory=tuple)kwargs:Dict[str,Any]=dataclasses.field(default_factory=dict)options:Dict[str,Any]=dataclasses.field(default_factory=dict)version:str=__version__
[docs]defanalysis(self):"""Return the analysis class constructed from this config. Returns: BaseAnalysis: The analysis reconstructed from the config. Raises: QiskitError: If the analysis class is not stored, was not successful deserialized, or reconstruction of the analysis class fails. """cls=self.clsifclsisNone:raiseQiskitError("No analysis class in analysis config")ifisinstance(cls,dict):raiseQiskitError("Unable to load analysis class. Try manually loading ""analysis using `Analysis.from_config(config)` instead.")try:returncls.from_config(self)exceptExceptionasex:msg="Unable to construct analysis from config."ifcls.version!=__version__:msg+=(f" Note that config version ({cls.version}) differs from the current"f" qiskit-experiments version ({__version__}). You could try"" installing a compatible qiskit-experiments version.")raiseQiskitError(f"{msg}\nError Message:\n{str(ex)}")fromex