Skip to content

Commit

Permalink
Merge pull request #754 from qiboteam/fix_rabi_length
Browse files Browse the repository at this point in the history
Hotfix for rabi length following #744
  • Loading branch information
andrea-pasquale authored Mar 20, 2024
2 parents e1a3628 + 6cd7185 commit d826492
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 29 deletions.
46 changes: 29 additions & 17 deletions src/qibocal/protocols/characterization/rabi/length.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
from scipy.signal import find_peaks

from qibocal import update
from qibocal.auto.operation import Data, Parameters, Results, Routine
from qibocal.auto.operation import Parameters, Routine
from qibocal.config import log
from qibocal.protocols.characterization.rabi.length_signal import (
RabiLengthVoltData,
RabiLengthVoltResults,
)

from ..utils import chi2_reduced
from . import utils
Expand All @@ -34,15 +38,9 @@ class RabiLengthParameters(Parameters):


@dataclass
class RabiLengthResults(Results):
class RabiLengthResults(RabiLengthVoltResults):
"""RabiLength outputs."""

length: dict[QubitId, tuple[int, Optional[float]]]
"""Pi pulse duration for each qubit."""
amplitude: dict[QubitId, tuple[float, Optional[float]]]
"""Pi pulse amplitude. Same for all qubits."""
fitted_parameters: dict[QubitId, dict[str, float]]
"""Raw fitting output."""
chi2: dict[QubitId, tuple[float, Optional[float]]] = field(default_factory=dict)


Expand All @@ -53,11 +51,9 @@ class RabiLengthResults(Results):


@dataclass
class RabiLengthData(Data):
class RabiLengthData(RabiLengthVoltData):
"""RabiLength acquisition outputs."""

amplitudes: dict[QubitId, float] = field(default_factory=dict)
"""Pulse durations provided by the user."""
data: dict[QubitId, npt.NDArray[RabiLenType]] = field(default_factory=dict)
"""Raw data acquired."""

Expand Down Expand Up @@ -140,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 @@ -168,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
42 changes: 30 additions & 12 deletions src/qibocal/protocols/characterization/rabi/length_signal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from dataclasses import dataclass
from dataclasses import dataclass, field
from typing import Optional

import numpy as np
import numpy.typing as npt
from qibolab import AcquisitionType, AveragingMode, ExecutionParameters
from qibolab.platform import Platform
from qibolab.pulses import PulseSequence
Expand All @@ -10,25 +12,36 @@
from scipy.signal import find_peaks

from qibocal import update
from qibocal.auto.operation import Routine
from qibocal.auto.operation import Data, Parameters, Results, Routine
from qibocal.config import log
from qibocal.protocols.characterization.rabi.length import (
RabiLengthData,
RabiLengthParameters,
RabiLengthResults,
)

from . import utils


@dataclass
class RabiLengthVoltParameters(RabiLengthParameters):
"""RabiLength runcard inputs."""
class RabiLengthVoltParameters(Parameters):
"""RabiLengthVolt runcard inputs."""

pulse_duration_start: float
"""Initial pi pulse duration [ns]."""
pulse_duration_end: float
"""Final pi pulse duration [ns]."""
pulse_duration_step: float
"""Step pi pulse duration [ns]."""
pulse_amplitude: Optional[float] = None
"""Pi pulse amplitude. Same for all qubits."""


@dataclass
class RabiLengthVoltResults(RabiLengthResults):
"""RabiLength outputs."""
class RabiLengthVoltResults(Results):
"""RabiLengthVolt outputs."""

length: dict[QubitId, tuple[int, Optional[float]]]
"""Pi pulse duration for each qubit."""
amplitude: dict[QubitId, tuple[float, Optional[float]]]
"""Pi pulse amplitude. Same for all qubits."""
fitted_parameters: dict[QubitId, dict[str, float]]
"""Raw fitting output."""


RabiLenVoltType = np.dtype(
Expand All @@ -38,9 +51,14 @@ class RabiLengthVoltResults(RabiLengthResults):


@dataclass
class RabiLengthVoltData(RabiLengthData):
class RabiLengthVoltData(Data):
"""RabiLength acquisition outputs."""

amplitudes: dict[QubitId, float] = field(default_factory=dict)
"""Pulse durations provided by the user."""
data: dict[QubitId, npt.NDArray[RabiLenVoltType]] = field(default_factory=dict)
"""Raw data acquired."""


def _acquisition(
params: RabiLengthVoltParameters, platform: Platform, targets: list[QubitId]
Expand Down

0 comments on commit d826492

Please sign in to comment.