Source code for qiskit_experiments.curve_analysis.composite_curve_analysis
# 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."""Analysis class for multi-group curve fitting."""# pylint: disable=invalid-nameimportwarningsfromtypingimportDict,List,Optional,Tuple,Unionimportlmfitimportnumpyasnpfromuncertaintiesimportunumpyasunpfromqiskit_experiments.frameworkimport(AnalysisResultData,BaseAnalysis,ExperimentData,Options,)fromqiskit_experiments.visualizationimport(BaseDrawer,BasePlotter,CurvePlotter,LegacyCurveCompatDrawer,MplDrawer,)fromqiskit_experiments.warningsimportdeprecated_functionfrom.base_curve_analysisimportPARAMS_ENTRY_PREFIX,BaseCurveAnalysisfrom.curve_dataimportCurveFitResultfrom.utilsimporteval_with_uncertainties
[docs]classCompositeCurveAnalysis(BaseAnalysis):r"""Composite Curve Analysis. The :class:`.CompositeCurveAnalysis` takes multiple curve analysis instances and performs each analysis on the same experimental results. These analyses are performed independently, thus fit parameters have no correlation. Note that this is different from :class:`.CompositeAnalysis` which analyses the outcome of a composite experiment, in which multiple different experiments are performed. The :class:`.CompositeCurveAnalysis` is attached to a single experiment instance, which may execute similar circuits with slightly different settings. Experiments with different settings might be distinguished by the circuit metadata. The outcomes of the same set of experiments are assigned to a specific analysis instance in the composite curve analysis. This mapping is usually done with the analysis option ``filter_data`` dictionary. Otherwise, all analyses are performed on the same set of outcomes. Examples: In this example, we write up a composite analysis consisting of two oscillation analysis instances, assuming two Rabi experiments in 1-2 subspace starting with different initial states :math:`\in \{|0\rangle, |1\rangle\}`. This is a typical procedure to measure the thermal population of the qubit. .. code-block:: python from qiskit_experiments import curve_analysis as curve analyses = [] for qi in (0, 1): analysis = curve.OscillationAnalysis(name=f"init{qi}") analysis.set_options( return_fit_parameters=["freq"], filter_data={"init_state": qi}, ) analysis = CompositeCurveAnalysis(analyses=analyses) This ``analysis`` will return two analysis result data for the fit parameter "freq" for experiments with the initial state :math:`|0\rangle` and :math:`|1\rangle`. The experimental circuits starting with different initial states must be distinguished by the circuit metadata ``{"init_state": 0}`` or ``{"init_state": 1}``, along with the "xval" in the same dictionary. If you want to compute another quantity using two fitting outcomes, you can override :meth:`CompositeCurveAnalysis._create_curve_data` in subclass. :class:`.CompositeCurveAnalysis` subclass may override following methods. .. rubric:: _evaluate_quality This method evaluates the quality of the composite fit based on the all analysis outcomes. This returns "good" when all fit outcomes are evaluated as "good", otherwise it returns "bad". .. rubric:: _create_analysis_results This method is passed all the group fit outcomes and can return a list of new values to be stored in the analysis results. """def__init__(self,analyses:List[BaseCurveAnalysis],name:Optional[str]=None,):super().__init__()self._analyses=analysesself._name=nameorself.__class__.__name__@propertydefparameters(self)->List[str]:"""Return parameters of this curve analysis."""unite_params=[]foranalysisinself._analyses:# Respect ordering of parametersfornameinanalysis.parameters:ifnamenotinunite_params:unite_params.append(name)returnunite_params@propertydefname(self)->str:"""Return name of this analysis."""returnself._name@propertydefmodels(self)->Dict[str,List[lmfit.Model]]:"""Return fit models."""models={}foranalysisinself._analyses:models[analysis.name]=analysis.modelsreturnmodels@propertydefplotter(self)->BasePlotter:"""A short-cut to the plotter instance."""returnself._options.plotter@property@deprecated_function(last_version="0.6",msg="Replaced by `plotter` from the new visualization submodule.",)defdrawer(self)->BaseDrawer:"""A short-cut for curve drawer instance, if set. ``None`` otherwise."""ifhasattr(self._options,"curve_drawer"):returnself._options.curve_drawerelse:returnNone
[docs]defanalyses(self,index:Optional[Union[str,int]]=None)->Union[BaseCurveAnalysis,List[BaseCurveAnalysis]]:"""Return curve analysis instance. Args: index: Name of group or numerical index. Returns: Curve analysis instance. """ifindexisNone:returnself._analysesifisinstance(index,str):group_names=[analysis.nameforanalysisinself._analyses]num_index=group_names.index(index)returnself._analyses[num_index]returnself._analyses[index]
def_evaluate_quality(self,fit_data:Dict[str,CurveFitResult],)->Union[str,None]:"""Evaluate quality of the fit result. Args: fit_data: Fit outcome keyed on the analysis name. Returns: String that represents fit result quality. Usually "good" or "bad". """foranalysisinself._analyses:ifanalysis._evaluate_quality(fit_data[analysis.name])!="good":return"bad"return"good"# pylint: disable=unused-argumentdef_create_analysis_results(self,fit_data:Dict[str,CurveFitResult],quality:str,**metadata,)->List[AnalysisResultData]:"""Create analysis results based on all analysis outcomes. Args: fit_data: Fit outcome keyed on the analysis name. quality: Quality of fit outcome. Returns: List of analysis result data. """return[]@classmethoddef_default_options(cls)->Options:"""Default analysis options. Analysis Options: plotter (BasePlotter): A plotter instance to visualize the analysis result. plot (bool): Set ``True`` to create figure for fit result. This is ``True`` by default. return_fit_parameters (bool): Set ``True`` to return all fit model parameters with details of the fit outcome. Default to ``True``. return_data_points (bool): Set ``True`` to include in the analysis result the formatted data points given to the fitter. Default to ``False``. extra (Dict[str, Any]): A dictionary that is appended to all database entries as extra information. """options=super()._default_options()options.update_options(plotter=CurvePlotter(MplDrawer()),plot=True,return_fit_parameters=True,return_data_points=False,extra={},)# Set automatic validator for particular option valuesoptions.set_validator(field="plotter",validator_value=BasePlotter)returnoptions
[docs]defset_options(self,**fields):# TODO remove this in Qiskit Experiments 0.6if"curve_drawer"infields:warnings.warn("The option 'curve_drawer' is replaced with 'plotter'. ""This option will be removed in Qiskit Experiments 0.6.",DeprecationWarning,stacklevel=2,)# Set the plotter drawer to `curve_drawer`. If `curve_drawer` is the right type, set it# directly. If not, wrap it in a compatibility drawer.ifisinstance(fields["curve_drawer"],BaseDrawer):plotter=self.options.plotterplotter.drawer=fields.pop("curve_drawer")fields["plotter"]=plotterelse:drawer=fields["curve_drawer"]compat_drawer=LegacyCurveCompatDrawer(drawer)plotter=self.options.plotterplotter.drawer=compat_drawerfields["plotter"]=plotterforfieldinfields:ifnothasattr(self.options,field):warnings.warn(f"Specified option {field} doesn't exist in this analysis instance. "f"Note that {self.__class__.__name__} is a composite curve analysis instance, ""which consists of multiple child curve analyses. ""This options may exist in each analysis instance. ""Please try setting options to child analyses through '.analyses()'.",UserWarning,)super().set_options(**fields)
def_run_analysis(self,experiment_data:ExperimentData,)->Tuple[List[AnalysisResultData],List["matplotlib.figure.Figure"]]:analysis_results=[]fit_dataset={}red_chi={}foranalysisinself._analyses:analysis._initialize(experiment_data)metadata=analysis.options.extra.copy()metadata["group"]=analysis.nameprocessed_data=analysis._run_data_processing(raw_data=experiment_data.data(),models=analysis.models,)ifself.options.plotandanalysis.options.plot_raw_data:formodelinanalysis.models:sub_data=processed_data.get_subset_of(model._name)self.plotter.set_series_data(model._name+f"_{analysis.name}",x=sub_data.x,y=sub_data.y,)# Format dataformatted_data=analysis._format_data(processed_data)ifself.options.plot:formodelinanalysis.models:sub_data=formatted_data.get_subset_of(model._name)self.plotter.set_series_data(model._name+f"_{analysis.name}",x_formatted=sub_data.x,y_formatted=sub_data.y,y_formatted_err=sub_data.y_err,)# Run fittingfit_data=analysis._run_curve_fit(curve_data=formatted_data,models=analysis.models,)iffit_data.success:quality=analysis._evaluate_quality(fit_data)red_chi[analysis.name]=fit_data.reduced_chisqelse:quality="bad"ifself.options.return_fit_parameters:overview=AnalysisResultData(name=PARAMS_ENTRY_PREFIX+analysis.name,value=fit_data,quality=quality,extra=metadata,)analysis_results.append(overview)iffit_data.success:# Add extra analysis resultsanalysis_results.extend(analysis._create_analysis_results(fit_data=fit_data,quality=quality,**metadata.copy()))# Draw fit resultifself.options.plot:x_interp=np.linspace(np.min(formatted_data.x),np.max(formatted_data.x),num=100)formodelinanalysis.models:y_data_with_uncertainty=eval_with_uncertainties(x=x_interp,model=model,params=fit_data.ufloat_params,)y_interp=unp.nominal_values(y_data_with_uncertainty)# Add fit line dataself.plotter.set_series_data(model._name+f"_{analysis.name}",x_interp=x_interp,y_interp=y_interp,)iffit_data.covarisnotNone:# Add confidence interval datay_interp_err=unp.std_devs(y_data_with_uncertainty)ifnp.isfinite(y_interp_err).all():self.plotter.set_series_data(model._name+f"_{analysis.name}",y_interp_err=y_interp_err,)# Add raw data pointsifself.options.return_data_points:analysis_results.extend(analysis._create_curve_data(curve_data=formatted_data,models=analysis.models,**metadata,))fit_dataset[analysis.name]=fit_datatotal_quality=self._evaluate_quality(fit_dataset)ifred_chi:self.plotter.set_supplementary_data(fit_red_chi=red_chi)# Create analysis results by combining all fit dataifall(fit_data.successforfit_datainfit_dataset.values()):primary_results=self._create_analysis_results(fit_data=fit_dataset,quality=total_quality,**self.options.extra.copy())analysis_results.extend(primary_results)self.plotter.set_supplementary_data(primary_results=primary_results)ifself.options.plot:returnanalysis_results,[self.plotter.figure()]returnanalysis_results,[]