Skip to content

Commit

Permalink
feat: Add maximize option to fit_ampl_calib_data for flexible data ha…
Browse files Browse the repository at this point in the history
…ndling; update data normalization in Experiment class
  • Loading branch information
Akinori Machino committed Dec 29, 2024
1 parent f40480c commit c3f88b7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
66 changes: 36 additions & 30 deletions src/qubex/analysis/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,7 @@ def fit_ampl_calib_data(
amplitude_range: npt.NDArray[np.float64],
data: npt.NDArray[np.float64],
p0=None,
maximize: bool = True,
plot: bool = True,
title: str = "Amplitude calibration",
xaxis_title: str = "Amplitude (arb. unit)",
Expand All @@ -1151,6 +1152,8 @@ def fit_ampl_calib_data(
Measured values for the calibration data.
p0 : optional
Initial guess for the fitting parameters.
maximize : bool, optional
Whether to maximize or minimize the data.
plot : bool, optional
Whether to plot the data and the fit.
Expand All @@ -1160,6 +1163,9 @@ def fit_ampl_calib_data(
Fitted parameters and the figure.
"""

if maximize:
data = -data

def cos_func(t, ampl, omega, phi, offset):
return ampl * np.cos(omega * t + phi) + offset

Expand Down Expand Up @@ -1191,38 +1197,38 @@ def cos_func(t, ampl, omega, phi, offset):
x_fine = np.linspace(np.min(amplitude_range), np.max(amplitude_range), 1000)
y_fine = cos_func(x_fine, *popt)

if plot:
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=x_fine,
y=y_fine,
mode="lines",
name="Fit",
)
)
fig.add_trace(
go.Scatter(
x=amplitude_range,
y=data,
mode="markers",
name="Data",
)
)
fig.add_annotation(
x=min_x,
y=min_y,
text=f"min: {min_x:.6g}",
showarrow=True,
arrowhead=1,
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=x_fine,
y=-y_fine if maximize else y_fine,
mode="lines",
name="Fit",
)
fig.update_layout(
title=f"{title} : {target}",
xaxis_title=xaxis_title,
yaxis_title=yaxis_title,
xaxis_type=xaxis_type,
yaxis_type=yaxis_type,
)
fig.add_trace(
go.Scatter(
x=amplitude_range,
y=-data if maximize else data,
mode="markers",
name="Data",
)
)
fig.add_annotation(
x=min_x,
y=-min_y if maximize else min_y,
text=f"max: {min_x:.6g}" if maximize else f"min: {min_x:.6g}",
showarrow=True,
arrowhead=1,
)
fig.update_layout(
title=f"{title} : {target}",
xaxis_title=xaxis_title,
yaxis_title=yaxis_title,
xaxis_type=xaxis_type,
yaxis_type=yaxis_type,
)
if plot:
fig.show(config=_plotly_config(f"ampl_calib_{target}"))

print(f"Calibrated amplitude: {min_x:.6g}")
Expand Down
9 changes: 6 additions & 3 deletions src/qubex/experiment/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2974,9 +2974,10 @@ def calibrate(target: str) -> AmplCalibData:
fit_result = fitting.fit_ampl_calib_data(
target=target,
amplitude_range=ampl_range,
data=-sweep_data.normalized,
data=sweep_data.normalized,
plot=plot,
title=f"{pulse_type} pulse calibration",
yaxis_title="Normalized signal",
)

return AmplCalibData.new(
Expand Down Expand Up @@ -3083,8 +3084,9 @@ def sequence(x: float) -> PulseSchedule:
fit_result = fitting.fit_ampl_calib_data(
target=ef_label,
amplitude_range=ampl_range,
data=-sweep_data.normalized,
data=sweep_data.normalized,
title=f"ef {pulse_type} pulse calibration",
yaxis_title="Normalized signal",
)

return AmplCalibData.new(
Expand Down Expand Up @@ -3209,8 +3211,9 @@ def calibrate(target: str) -> float:
fit_result = fitting.fit_ampl_calib_data(
target=target,
amplitude_range=ampl_range,
data=-sweep_data.normalized,
data=sweep_data.normalized,
title=f"DRAG {pulse_type} amplitude calibration",
yaxis_title="Normalized signal",
)

return fit_result["amplitude"]
Expand Down
2 changes: 1 addition & 1 deletion src/qubex/experiment/experiment_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def fit(self) -> dict:
return fitting.fit_ampl_calib_data(
target=self.target,
amplitude_range=self.sweep_range,
data=-self.normalized,
data=self.normalized,
)


Expand Down

0 comments on commit c3f88b7

Please sign in to comment.