CurveAnalysis refactoring #737
Labels
Changelog: API Change
Include in the "Changed" section of the changelog
Changelog: New Feature
Include in the "Added" section of the changelog
enhancement
New feature or request
Milestone
Background
The
CurveAnalysis
can perform multi-objective optimization with different fit functions. However, if we want to perform two independent fittings, we need to implement the experiment as batch experiment. This is quite heavy coding/maintenance overhead and doesn't scale. For example, we need to update CR Ham tomo analysis from the fitting ofF(px0, py0, pz0, px1, py1, pz1, b)
to a set ofF0(px, py, pz, b) @ ctrl=0
andF1(px, py, pz, b) @ ctrl=1
for speedup (see #668). This is naive implementation of this analysisCurrent structure
CrossResonanceHamiltonian
(experiment)CrossResonanceHamiltonianAnalysis
(analysis)Naive implementation
TomographyElement(BaseExperiment)
(experiment for each control state)CrossResonanceHamiltonian(BatchExperiment)
(experiment for batch, instantiate sub experiments)TomographyElementAnalysis(CurveAnalysis)
(analysis for each control state)CrossResonanceHamiltonianAnalysis(CompositeAnalysis)
(analysis for batch; compute Hamiltonian coefficient)Here
CrossResonanceHamiltonian
should implement weird logic to synchronize all experiments, analysis, transpile, run options of itself to sub experiments so that it behaves as if a single experiment (since user doesn't expect one is running it as a batch experiment). As you can see, this is pretty much overhead for just separating fit functions.New implementation
Here we are going to introduce new concept
group
to the curve analysis.To avoid complication, the group is implemented on the separate class.
New CR Hamiltonian analysis may look like
Since group curve analysis is an outer-loop of
CurveAnalysis
, i.e. it repeats curve fitting for each group, the inheritance of curve analysisGroupCurveAnalysis(CurveAnalysis)
doesn't make sense (we need to implement_run_analysis
in a loop). In addition, there is attribute name inconsistency, for example,self._fit_model
(curve analysis) ->self._fit_models
(group curve analysis), it would be better to introduce base curve analysis that doesn't implement_run_analysis
, where the reusable functionalities are implemented as hook-like methods.Then each curve analysis subclass can be implemented as
Note: Separation of fit function will drastically reduce unittest execution time for CR Ham tomo from ~100s to ~10s
Currently there are some blockers to implement this framework.
This will be solved with Add fit model to CurveAnalysis #726 where a self-contained and portable fit model represetation is introduced.
Currently curve analysis saves raw data and formatted data as internal state, and calls these data set from the method
self._data
. This is problematic because this internal state is not aware of group, so it will be overridden in the loop. It is possible to make it group-aware, but this will introduce unnecessary coupling to group in the standard curve analysis class. Fortunately, this internal state is only used by RB experiments, thus we can drop this internal state with refactoring of RB.Another reason of having processed data is to draw raw data together with the formatted data. In the figures of this tutorial, transparent gray crosses are the raw values and bold symbols are formatted (weight averaged) values. To drop the processed data from the internal state, we need to update the drawer so that it can draw raw data points immediately.
Data processor can be moved to
BaseAnalysis
. This will solve many issues regarding the run option necessary to instantiate the processor. Basically the processor will be a base analysis option (DataProcessor
is already serializable), and a data processing subroutine will be inserted just before_run_analysis
is called. Experiment instance can instantiate a custom processor within the_finalize
call and attach this to analysis. This can eventually address Add an IQ plot for meas_level=1 #614 since the base analysis can draw IQ plot when the output of data processor is IQ format (then it appends IQ plot to figures). However, this indicates all experiments in the codebase should useDataProcessor
, and this is indeed overkill to just extract count dictionary (e.g. QV experiment) or compute probability without standard error (e.g. tomography, where the fitter doesn't take care of stdev). Performance optimization should be done before it is globally used within the experiments, perhaps rust?Steps
The step of refactoring will be
The text was updated successfully, but these errors were encountered: