Skip to content

Commit

Permalink
fix: Fix rabi fitting by including phase in period estimate
Browse files Browse the repository at this point in the history
Fixes #656 but it requires more tests.
  • Loading branch information
andrea-pasquale committed Dec 7, 2023
1 parent b319f13 commit d5445f4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/qibocal/protocols/characterization/rabi/amplitude.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def _fit(data: RabiAmplitudeData) -> RabiAmplitudeResults:
index = local_maxima[0] if len(local_maxima) > 0 else None
# 0.5 hardcoded guess for less than one oscillation
f = x[index] / (x[1] - x[0]) if index is not None else 0.5
pguess = [0.5, 0.5, 1 / f, np.pi / 2]
pguess = [0.5, 0.5, 1 / f, 0]
try:
popt, perr = curve_fit(
utils.rabi_amplitude_function,
Expand All @@ -165,7 +165,7 @@ def _fit(data: RabiAmplitudeData) -> RabiAmplitudeResults:
sigma=qubit_data.error,
)
perr = np.sqrt(np.diag(perr))
pi_pulse_parameter = np.abs(popt[2] / 2)
pi_pulse_parameter = utils.correct_period(period=popt[2], phase=popt[3])

except:
log.warning("rabi_fit: the fitting was not succesful")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ def _fit(data: RabiAmplitudeVoltData) -> RabiAmplitudeVoltResults:
popt[2] * (x_max - x_min),
popt[3] - 2 * np.pi * x_min / (x_max - x_min) / popt[2],
]
pi_pulse_parameter = np.abs((translated_popt[2]) / 2)
pi_pulse_parameter = utils.correct_period(
period=translated_popt[2], phase=translated_popt[3]
)

except:
log.warning("rabi_fit: the fitting was not succesful")
Expand Down
2 changes: 1 addition & 1 deletion src/qibocal/protocols/characterization/rabi/length.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def _fit(data: RabiLengthData) -> RabiLengthResults:
sigma=qubit_data.error,
)
perr = np.sqrt(np.diag(perr))
pi_pulse_parameter = np.abs(popt[2] / 2)
pi_pulse_parameter = utils.correct_period(period=popt[2], phase=popt[3])
except:
log.warning("rabi_fit: the fitting was not succesful")
pi_pulse_parameter = 0
Expand Down
5 changes: 4 additions & 1 deletion src/qibocal/protocols/characterization/rabi/length_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ def _fit(data: RabiLengthVoltData) -> RabiLengthVoltResults:
popt[3] - 2 * np.pi * x_min / popt[2] / (x_max - x_min),
popt[4] / (x_max - x_min),
]
pi_pulse_parameter = np.abs(translated_popt[2] / 2)
pi_pulse_parameter = utils.correct_period(
period=translated_popt[2], phase=translated_popt[3]
)

except:
log.warning("rabi_fit: the fitting was not succesful")
pi_pulse_parameter = 0
Expand Down
16 changes: 15 additions & 1 deletion src/qibocal/protocols/characterization/rabi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def rabi_amplitude_function(x, offset, amplitude, period, phase):
Args:
x: Input data.
"""
return offset + amplitude * np.sin(2 * np.pi * x / period + phase)
return offset + amplitude * np.cos(2 * np.pi * x / period + phase)


def rabi_length_function(x, offset, amplitude, period, phase, t2_inv):
Expand Down Expand Up @@ -190,3 +190,17 @@ def extract_rabi(data):
if "RabiLength" in data.__class__.__name__:
return "length", "Time [ns]", rabi_length_function
raise RuntimeError("Data has to be a data structure of the Rabi routines.")


def correct_period(period: float, phase: float):
"""Correct period by taking phase into account.
https://github.com/qiboteam/qibocal/issues/656
"""

naive_half_period = period / 2
# solution of cos (2 pi x / period + phase) = +/- 1 for k in [-2,2]
solutions = [naive_half_period * (k - phase / np.pi) for k in [-2, -1, 0, 1, 2]]
# chose solution closest to naive_half period

return solutions[np.argmin(np.abs(solutions - naive_half_period))]

0 comments on commit d5445f4

Please sign in to comment.