# ══════════════════════════════════════════════════════════════════════════
# ___ ___ ___ _ _ ___
# / _ \ / __|_ ) | | /_\ | _ )
# | (_) | (__ / / | |__ / _ \| _ \
# \__\_\\___/___| |____/_/ \_\___/
#
# QC2 @ CHALMERS CALIBRATION TOOLKIT
# (c) 2026 Chalmers University of Technology and Project Contributors
# ──────────────────────────────────────────────────────────────────────────
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ──────────────────────────────────────────────────────────────────────────
# CONTRIBUTORS list:
#
# Michele Faucci Giannelli, faucci@chalmers.se
# Matteo Robbiati, robbiati@chalmers.se
# Axel Andersson, axean@chalmers.se
# Yanchen Lin, linya@chalmers.se
# ══════════════════════════════════════════════════════════════════════════
from typing import Literal, override
from pydantic import Field
from bragi_quantum.experiments.abstract.parameter_fields import (
acquisition_field,
analysis_field,
)
from bragi_quantum.experiments.abstract.parameters import (
BaseParameters,
BaseQoi,
)
[docs]
class ResonatorSpectroscopyParameters(BaseParameters):
"""Parameters for Resonator Spectroscopy & Punchout."""
readout_state: Literal[0, 1, 2] = acquisition_field(
default=0,
ge=0,
lt=3,
variant=True,
description="Basis state the resonator is prepared to before readout.",
)
# Dynamic sweeping parameters — data acquisition
samples: int | None = acquisition_field(
default=100, description="Number of frequency samples."
)
repetitions: int | None = acquisition_field(
default=100, description="Number of repetitions (averages) per frequency point."
)
half_width: float | None = acquisition_field(
default=1.0e6, description="Half-width of the sweep window in Hz."
)
# Sliding window analysis parameters
sample_size_for_sliding_window_analysis: int = analysis_field(
default=200,
description="Frequency-point count above which a resonator sweep is "
"treated as wide-range and routed through the sliding-window fit "
"instead of a single fit over the full window. A wide sweep can "
"contain more than one dip-like feature, so it needs to be "
"localized onto one resonance first.",
)
slide_window_size: int = analysis_field(
default=100,
description="Number of frequency points in each sliding sub-window "
"fit during the wide-range path.",
)
slide_window_step: int = analysis_field(
default=50,
description="Number of frequency points the sliding window advances "
"between successive fit attempts.",
)
slide_window_min_ql: float = analysis_field(
default=7000.0,
description="Minimum loaded quality factor Ql a sliding-window fit "
"must reach (in addition to the R^2 gate) to be accepted as the "
"resonance and stop the window scan.",
)
slide_window_max_qe: float = analysis_field(
default=60000.0,
description="Maximum external quality factor Qe a sliding-window fit "
"may have (in addition to the R^2 gate) to be accepted as the "
"resonance and stop the window scan.",
)
# Fit-quality gate — used by every caller (plain resonator_spectroscopy,
# punchout, punchout_mist all call the same is_good_fit()).
min_r_squared: float = analysis_field(
default=0.9,
description="Minimum R^2 (coefficient of determination, data vs. fit) "
"accepted for a resonator fit. R^2 "
"doesn't need an assumed per-point error, so its scale is fixed "
"regardless of the qubit's signal level.",
)
max_abs_theta: float = analysis_field(
default=0.25,
description="Maximum allowed magnitude of the fitted impedance-"
"mismatch angle theta (radians). A fit whose theta magnitude reaches "
"this bound indicates an implausible impedance mismatch and fails "
"is_good_fit, regardless of R^2.",
)
[docs]
class ResonatorSpectroscopyBaseQOI(BaseQoi):
"""Base Quantities of Interest for Resonator Spectroscopy."""
fitted_frequency: float = Field(default=0.0)
fitted_frequency_error: float | None = Field(default=None)
ql: float = Field(default=0.0)
ql_error: float | None = Field(default=None)
qe: float = Field(default=0.0)
qe_error: float | None = Field(default=None)
theta: float = Field(default=0.0)
theta_error: float | None = Field(default=None)
r_squared: float = Field(
default=0.0, description="Coefficient of determination; gates is_good_fit."
)
amplitude: float = Field(default=0.0)
amplitude_error: float | None = Field(default=None)
minimum_frequency: float = Field(default=0.0)
minimum_frequency_error: float | None = Field(default=None)
kappa: float = Field(default=0.0, description="Linewidth fr/Ql, in Hz.")
kappa_error: float | None = Field(default=None)
propagation_delay: float = Field(
default=0.0,
description="Cable propagation delay -phi_v/(2*pi), in seconds.",
)
propagation_delay_error: float | None = Field(default=None)
[docs]
class ResonatorSpectroscopyQOI(ResonatorSpectroscopyBaseQOI):
"""Quantities of Interest for Resonator Spectroscopy (State 0)."""
[docs]
@override
def to_dict(self) -> dict[str, float]:
"""Convert the QOI to a dictionary representation."""
return {
"fitted_frequency": self.fitted_frequency,
"fitted_frequency_error": self.fitted_frequency_error,
"ql": self.ql,
"ql_error": self.ql_error,
"qe": self.qe,
"qe_error": self.qe_error,
"theta": self.theta,
"theta_error": self.theta_error,
"r_squared": self.r_squared,
"amplitude": self.amplitude,
"amplitude_error": self.amplitude_error,
"minimum_frequency": self.minimum_frequency,
"minimum_frequency_error": self.minimum_frequency_error,
"kappa": self.kappa,
"kappa_error": self.kappa_error,
"propagation_delay": self.propagation_delay,
"propagation_delay_error": self.propagation_delay_error,
}
[docs]
def to_redis_dict(self) -> dict[str, dict[str, float]]:
"""Convert the QOI to a Redis dictionary representation."""
return {
"clock_freqs:readout": {
"value": self.minimum_frequency,
"error": self.minimum_frequency_error,
},
"Ql": {"value": self.ql, "error": self.ql_error},
"Qe": {"value": self.qe, "error": self.qe_error},
"theta": {"value": self.theta, "error": self.theta_error},
"r_squared": {"value": self.r_squared, "error": None},
"resonator_fit": {
"value": self.fitted_frequency,
"error": self.fitted_frequency_error,
},
"kappa": {"value": self.kappa, "error": self.kappa_error},
"propagation_delay": {
"value": self.propagation_delay,
"error": self.propagation_delay_error,
},
}
[docs]
class ResonatorSpectroscopy1QOI(ResonatorSpectroscopyBaseQOI):
"""Quantities of Interest for Resonator Spectroscopy (State 1)."""
[docs]
@override
def to_dict(self) -> dict[str, float]:
"""Convert the QOI to a dictionary representation."""
return {
"fitted_frequency": self.fitted_frequency,
"fitted_frequency_error": self.fitted_frequency_error,
"ql": self.ql,
"ql_error": self.ql_error,
"qe": self.qe,
"qe_error": self.qe_error,
"theta": self.theta,
"theta_error": self.theta_error,
"r_squared": self.r_squared,
"amplitude": self.amplitude,
"amplitude_error": self.amplitude_error,
"minimum_frequency": self.minimum_frequency,
"minimum_frequency_error": self.minimum_frequency_error,
"kappa": self.kappa,
"kappa_error": self.kappa_error,
"propagation_delay": self.propagation_delay,
"propagation_delay_error": self.propagation_delay_error,
}
[docs]
def to_redis_dict(self) -> dict[str, dict[str, float]]:
"""Convert the QOI to a Redis dictionary representation."""
return {
"extended_clock_freqs:readout_1": {
"value": self.minimum_frequency,
"error": self.minimum_frequency_error,
},
"Ql_1": {"value": self.ql, "error": self.ql_error},
"Qe_1": {"value": self.qe, "error": self.qe_error},
"theta_1": {"value": self.theta, "error": self.theta_error},
"r_squared_1": {"value": self.r_squared, "error": None},
"resonator_fit_1": {
"value": self.fitted_frequency,
"error": self.fitted_frequency_error,
},
"kappa_1": {"value": self.kappa, "error": self.kappa_error},
"propagation_delay_1": {
"value": self.propagation_delay,
"error": self.propagation_delay_error,
},
}
[docs]
class ResonatorSpectroscopy2QOI(ResonatorSpectroscopyBaseQOI):
"""Quantities of Interest for Resonator Spectroscopy (State 2)."""
[docs]
@override
def to_dict(self) -> dict[str, float]:
"""Convert the QOI to a dictionary representation."""
return {
"fitted_frequency": self.fitted_frequency,
"fitted_frequency_error": self.fitted_frequency_error,
"ql": self.ql,
"ql_error": self.ql_error,
"qe": self.qe,
"qe_error": self.qe_error,
"theta": self.theta,
"theta_error": self.theta_error,
"r_squared": self.r_squared,
"amplitude": self.amplitude,
"amplitude_error": self.amplitude_error,
"minimum_frequency": self.minimum_frequency,
"minimum_frequency_error": self.minimum_frequency_error,
"kappa": self.kappa,
"kappa_error": self.kappa_error,
"propagation_delay": self.propagation_delay,
"propagation_delay_error": self.propagation_delay_error,
}
[docs]
def to_redis_dict(self) -> dict[str, dict[str, float]]:
"""Convert the QOI to a Redis dictionary representation."""
return {
"extended_clock_freqs:readout_2": {
"value": self.minimum_frequency,
"error": self.minimum_frequency_error,
},
"Ql_2": {"value": self.ql, "error": self.ql_error},
"Qe_2": {"value": self.qe, "error": self.qe_error},
"theta_2": {"value": self.theta, "error": self.theta_error},
"r_squared_2": {"value": self.r_squared, "error": None},
"resonator_fit_2": {
"value": self.fitted_frequency,
"error": self.fitted_frequency_error,
},
"kappa_2": {"value": self.kappa, "error": self.kappa_error},
"propagation_delay_2": {
"value": self.propagation_delay,
"error": self.propagation_delay_error,
},
}