Skip to content

Commit

Permalink
fix test and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nkanazawa1989 committed Jun 27, 2022
1 parent 32e0fcf commit 8515ef2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 36 deletions.
12 changes: 6 additions & 6 deletions qiskit_experiments/curve_analysis/composite_curve_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class CompositeCurveAnalysis(BaseCurveAnalysis):
This mapping is usually done with the analysis option ``filter_data`` dictionary.
Otherwise, all analyses are performed on the same set of outcomes.
.. example::
Examples:
In this example, we set up a composite analysis consisting of two oscillation
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.
Expand All @@ -66,12 +66,12 @@ class CompositeCurveAnalysis(BaseCurveAnalysis):
)
analysis = CompositeCurveAnalysis(analyses=analyses)
This analysis returns two analysis result data for the fit parameter "freq"
for the experiment with initial state :math:`|0\rangle` and :math:`|1\rangle`.
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 "freq" values, you can
If you want to compute another quantity using two fitting outcomes, you can
override :meth:`CompositeCurveAnalysis._create_curve_data` in subclass.
:class:`.CompositeCurveAnalysis` may override following methods.
Expand Down Expand Up @@ -301,7 +301,7 @@ def _default_options(cls) -> Options:

def set_options(self, **fields):
for field in fields:
if field not in self.options:
if not hasattr(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, "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CrossResonanceHamiltonianAnalysis(curve.CompositeCurveAnalysis):
# section: fit_model
This analysis performs :class:`.BlochAnalysis` on the target qubit
This analysis performs :class:`.BlochTrajectoryAnalysis` on the target qubit
with the control qubit states in :math:`\in \{ |0\rangle, |1\rangle \}`.
Based on the fit result, cross resonance Hamiltonian coefficients can be determined by
Expand All @@ -38,8 +38,8 @@ class CrossResonanceHamiltonianAnalysis(curve.CompositeCurveAnalysis):
IY &= \frac{p_{y, |0\rangle} + p_{y, |1\rangle}}{2}, \\
IZ &= \frac{p_{z, |0\rangle} + p_{z, |1\rangle}}{2},
where :math:`p_{\beta, |j\rangle}` is a fit parameter of :class:`.BlochAnalysis`
of the projection axis :math:`\beta` with control qubit state :math:`|j\rangle`.
where :math:`p_{\beta, |j\rangle}` is a fit parameter of :class:`.BlochTrajectoryAnalysis`
for the projection axis :math:`\beta` with the control qubit state :math:`|j\rangle`.
# section: see_also
Expand Down
68 changes: 41 additions & 27 deletions test/curve_analysis/test_baseclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from lmfit.models import ExpressionModel
from qiskit.qobj.utils import MeasLevel

from qiskit_experiments.curve_analysis import CurveAnalysis, MultiGroupCurveAnalysis, fit_function
from qiskit_experiments.curve_analysis import CurveAnalysis, CompositeCurveAnalysis, fit_function
from qiskit_experiments.curve_analysis.curve_data import (
SeriesDef,
CurveFitResult,
Expand Down Expand Up @@ -457,28 +457,41 @@ def test_get_init_params(self):
y_reproduced = analysis.models[0].eval(x=x, **overview.init_params)
np.testing.assert_array_almost_equal(y_ref, y_reproduced)

def test_multi_group_curve_analysis(self):
"""Integration test for multi group curve analysis."""
def test_multi_composite_curve_analysis(self):
"""Integration test for composite curve analysis.
analysis = MultiGroupCurveAnalysis(
groups=["group_A", "group_B"],
group_data_sort_key=[{"cond": 0}, {"cond": 1}],
models=[
ExpressionModel(
expr="amp * cos(2 * pi * freq * x) + b",
data_sort_key={"type": "cos"},
),
ExpressionModel(
expr="amp * sin(2 * pi * freq * x) + b",
data_sort_key={"type": "sin"},
),
],
)
analysis.set_options(
p0=[{"amp": 0.3, "freq": 2.1, "b": 0.5}, {"amp": 0.5, "freq": 3.2, "b": 0.5}],
result_parameters=["amp"],
plot=False,
)
This analysis consists of two curve fittings for cos and sin series.
This experiment is executed twice for different setups of "A" and "B".
"""
analyses = []

group_names = ["group_A", "group_B"]
setups = ["setup_A", "setup_B"]
for group_name, setup in zip(group_names, setups):
analysis = CurveAnalysis(
models=[
ExpressionModel(
expr="amp * cos(2 * pi * freq * x) + b",
data_sort_key={"type": "cos"},
),
ExpressionModel(
expr="amp * sin(2 * pi * freq * x) + b",
data_sort_key={"type": "sin"},
),
],
name=group_name,
)
analysis.set_options(
filter_data={"setup": setup},
result_parameters=["amp"],
data_processor=DataProcessor(input_key="counts", data_actions=[Probability("1")]),
)
analyses.append(analysis)

group_analysis = CompositeCurveAnalysis(analyses)
group_analysis.analyses("group_A").set_options(p0={"amp": 0.3, "freq": 2.1, "b": 0.5})
group_analysis.analyses("group_B").set_options(p0={"amp": 0.5, "freq": 3.2, "b": 0.5})
group_analysis.set_options(plot=False)

amp1 = 0.2
amp2 = 0.4
Expand All @@ -493,10 +506,11 @@ def test_multi_group_curve_analysis(self):
y1b = amp2 * np.cos(2 * np.pi * freq2 * x) + b2
y2b = amp2 * np.sin(2 * np.pi * freq2 * x) + b2

test_data1a = self.single_sampler(x, y1a, type="cos", cond=0)
test_data2a = self.single_sampler(x, y2a, type="sin", cond=0)
test_data1b = self.single_sampler(x, y1b, type="cos", cond=1)
test_data2b = self.single_sampler(x, y2b, type="sin", cond=1)
# metadata must contain key for filtering, specified in filter_data option.
test_data1a = self.single_sampler(x, y1a, type="cos", setup="setup_A")
test_data2a = self.single_sampler(x, y2a, type="sin", setup="setup_A")
test_data1b = self.single_sampler(x, y1b, type="cos", setup="setup_B")
test_data2b = self.single_sampler(x, y2b, type="sin", setup="setup_B")

expdata = ExperimentData(experiment=FakeExperiment())
expdata.add_data(test_data1a.data())
Expand All @@ -505,7 +519,7 @@ def test_multi_group_curve_analysis(self):
expdata.add_data(test_data2b.data())
expdata.metadata["meas_level"] = MeasLevel.CLASSIFIED

result = analysis.run(expdata).block_for_results()
result = group_analysis.run(expdata).block_for_results()
amps = result.analysis_results("amp")

# two entries are generated for group A and group B
Expand Down

0 comments on commit 8515ef2

Please sign in to comment.