def run( self, experiment_data: ExperimentData, **options, ) -> ExperimentData: """Run analysis and update ExperimentData with analysis result. Args: experiment_data: the experiment data to analyze. options: additional analysis options. See class documentation for supported options. Returns: An experiment data object containing the analysis results and figures. Raises: QiskitError: if experiment_data container is not valid for analysis. """ if not isinstance(experiment_data, self.__experiment_data__): raise QiskitError( f"Invalid experiment data type, expected {self.__experiment_data__.__name__}" f" but received {type(experiment_data).__name__}") # Get experiment device components if "physical_qubits" in experiment_data.metadata: experiment_components = [ Qubit(qubit) for qubit in experiment_data.metadata["physical_qubits"] ] else: experiment_components = [] # Get analysis options analysis_options = self._default_options() analysis_options.update_options(**options) analysis_options = analysis_options.__dict__ # Run analysis results, figures = self._run_analysis(experiment_data, **analysis_options) # Add components analysis_results = [ self._format_analysis_result(result, experiment_data.experiment_id, experiment_components) for result in results ] # Update experiment data with analysis results experiment_data.add_analysis_results(analysis_results) if figures: experiment_data.add_figures(figures) return experiment_data
def get_value(exp_data: ExperimentData, param_name: str, index: Optional[int] = -1) -> float: """A helper method to extract values from experiment data instances.""" candidates = exp_data.analysis_results(param_name) if isinstance(candidates, list): return candidates[index].value.value else: return candidates.value.value
def get_value(exp_data: ExperimentData, param_name: str, index: Optional[int] = -1) -> float: """A helper method to extract values from experiment data instances.""" # Because this is called within analysis callbacks the block=False kwarg # must be passed to analysis results so we don't block indefinitely candidates = exp_data.analysis_results(param_name, block=False) if isinstance(candidates, list): return candidates[index].value.value else: return candidates.value.value
def load(cls, experiment_id: str, service: DatabaseServiceV1) -> "CompositeExperimentData": expdata = ExperimentData.load(experiment_id, service) expdata.__class__ = CompositeExperimentData expdata._components = [] for comp_id, comp_class in zip(expdata.metadata["component_ids"], expdata.metadata["component_classes"]): load_class = globals()[comp_class] load_func = getattr(load_class, "load") loaded_comp = load_func(comp_id, service) expdata._components.append(loaded_comp) return expdata
def run( self, experiment_data: ExperimentData, replace_results: bool = False, **options, ) -> ExperimentData: """Run analysis and update ExperimentData with analysis result. Args: experiment_data: the experiment data to analyze. replace_results: if True clear any existing analysis results and figures in the experiment data and replace with new results. See note for additional information. options: additional analysis options. See class documentation for supported options. Returns: An experiment data object containing the analysis results and figures. Raises: QiskitError: if experiment_data container is not valid for analysis. .. note:: **Updating Results** If analysis is run with ``replace_results=True`` then any analysis results and figures in the experiment data will be cleared and replaced with the new analysis results. Saving this experiment data will replace any previously saved data in a database service using the same experiment ID. If analysis is run with ``replace_results=False`` and the experiment data being analyzed has already been saved to a database service, or already contains analysis results or figures, a copy with a unique experiment ID will be returned containing only the new analysis results and figures. This data can then be saved as its own experiment to a database service. """ # Make a new copy of experiment data if not updating results if not replace_results and _requires_copy(experiment_data): experiment_data = experiment_data.copy() experiment_components = self._get_experiment_components( experiment_data) # Set Analysis options if not options: analysis = self else: analysis = self.copy() analysis.set_options(**options) def run_analysis(expdata): results, figures = analysis._run_analysis(expdata) # Add components analysis_results = [ analysis._format_analysis_result(result, expdata.experiment_id, experiment_components) for result in results ] # Update experiment data with analysis results experiment_data._clear_results() if analysis_results: expdata.add_analysis_results(analysis_results) if figures: expdata.add_figures(figures) experiment_data.add_analysis_callback(run_analysis) return experiment_data
def _initialize_experiment_data(self) -> ExperimentData: """Initialize the return data container for the experiment run""" return ExperimentData(experiment=self)