Skip to content

Commit

Permalink
fix: Improve fit performance by normalizing x axis
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-pasquale committed Mar 19, 2024
1 parent 974ff50 commit 6cd7185
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/qibocal/protocols/characterization/rabi/length.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,26 @@ def _fit(data: RabiLengthData) -> RabiLengthResults:
qubits = data.qubits
fitted_parameters = {}
durations = {}
amplitudes = {}
chi2 = {}

for qubit in qubits:
qubit_data = data[qubit]
x = qubit_data.length
raw_x = qubit_data.length
min_x = np.min(raw_x)
max_x = np.max(raw_x)
y = qubit_data.prob

x = (raw_x - min_x) / (max_x - min_x)
# Guessing period using fourier transform
ft = np.fft.rfft(y)
mags = abs(ft)
local_maxima = find_peaks(mags, threshold=1)[0]
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, np.max(x) / f, 0, 0]
pguess = [0.5, 0.5, 1 / f, 0, 0]

try:
popt, perr = curve_fit(
utils.rabi_length_function,
Expand All @@ -164,17 +169,28 @@ def _fit(data: RabiLengthData) -> RabiLengthResults:
),
sigma=qubit_data.error,
)

translated_popt = [
popt[0],
popt[1] * np.exp(min_x * popt[4] / (max_x - min_x)),
popt[2] * (max_x - min_x),
popt[3] - 2 * np.pi * min_x / popt[2] / (max_x - min_x),
popt[4] / (max_x - min_x),
]

perr = np.sqrt(np.diag(perr))
pi_pulse_parameter = (
popt[2] / 2 * utils.period_correction_factor(phase=popt[3])
translated_popt[2]
/ 2
* utils.period_correction_factor(phase=translated_popt[3])
)
durations[qubit] = (pi_pulse_parameter, perr[2] / 2)
fitted_parameters[qubit] = popt.tolist()
durations[qubit] = (pi_pulse_parameter, perr[2] * (max_x - min_x) / 2)
fitted_parameters[qubit] = translated_popt
amplitudes = {key: (value, 0) for key, value in data.amplitudes.items()}
chi2[qubit] = (
chi2_reduced(
y,
utils.rabi_length_function(x, *popt),
utils.rabi_length_function(raw_x, *translated_popt),
qubit_data.error,
),
np.sqrt(2 / len(y)),
Expand Down

0 comments on commit 6cd7185

Please sign in to comment.