# ══════════════════════════════════════════════════════════════════════════
# ___ ___ ___ _ _ ___
# / _ \ / __|_ ) | | /_\ | _ )
# | (_) | (__ / / | |__ / _ \| _ \
# \__\_\\___/___| |____/_/ \_\___/
#
# 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:
#
# Axel Andersson, axean@chalmers.se
# Michele Faucci Giannelli, faucci@chalmers.se
# Matteo Robbiati, robbiati@chalmers.se
# Joel Sandås, joel.sandas@chalmers.se
# ══════════════════════════════════════════════════════════════════════════
import math
from typing import ClassVar, Literal, override
from pydantic import Field
from bragi_quantum.datalayer.models.qubit import ExtendedTransmon
from bragi_quantum.experiments.abstract.parameter_fields import (
acquisition_field,
analysis_field,
)
from bragi_quantum.experiments.abstract.parameters import (
BaseParameters,
BaseQoi,
ParameterCombination,
)
# Fallback spectroscopy pulse length (s) per transition, used when the
# device's spec.spec_duration / spec.spec_duration_12 is unset (NaN). 1->2 is
# much shorter since the X-prepared |1> decays during the pulse; 0.5 us gave
# the clearest f12 peaks in a pulse-length scan on fc8b (2026-10-04).
DEFAULT_SPEC_PULSE_DURATION = {"01": 20e-6, "12": 0.5e-6}
[docs]
class QubitSpectroscopyParameters(BaseParameters):
"""Parameters for Qubit Spectroscopy (two-tone experiment)."""
# Continuous drive puts the qubit in a steady state, so there is no
# reliable |1> to condition on and no 1->2 line to resolve.
invalid_variant_combinations: ClassVar[tuple[ParameterCombination, ...]] = (
{"transition": "12", "continuous_mode": True},
)
transition: Literal["01", "12"] = acquisition_field(default="01", variant=True)
continuous_mode: bool = acquisition_field(
default=False,
variant=True,
description=(
"Whether to drive the qubit with a continuous tone rather than a "
"pulse. Faster and more reliable than pulsed spectroscopy, but "
"only useable with the 0->1 transition."
),
)
# Amplitude sweep — data acquisition
min_amplitude: float = acquisition_field(
default=0.01, description="Minimum spectroscopy pulse amplitude."
)
max_amplitude: float = acquisition_field(
default=0.05, description="Maximum spectroscopy pulse amplitude."
)
amplitude_bins: int = acquisition_field(
default=5, description="Number of amplitude steps."
)
# Frequency sweep — data acquisition
samples: int | None = acquisition_field(
default=100, description="Number of frequency samples."
)
half_width: float | None = acquisition_field(
default=5.0e6, description="Half-width of the frequency sweep window in Hz."
)
# Pulse and acquisition
spec_pulse_duration: float | None = acquisition_field(
default=None,
gt=0,
description=(
"Duration of the spectroscopy drive pulse in seconds. None uses "
"the qubit's device value (spec.spec_duration for 0->1, "
"spec.spec_duration_12 for 1->2), or if that is unset 20 us for "
"0->1 and 0.5 us for 1->2. Ignored when continuous_mode is True."
),
)
continuous_reset_duration: float = acquisition_field(
default=20e-6,
description=(
"Idle time after each measurement in continuous mode, in seconds. "
"Continuous mode holds the drive on and so cannot reset the qubit "
"passively the way the pulsed schedule does; this wait instead "
"lets the readout resonator ring down (~1/kappa) so successive "
"measurements stay statistically independent. It exists as a "
"parameter because reset_system has no settable duration. "
"Ignored when continuous_mode is False."
),
)
repetitions: int | None = acquisition_field(
default=600, description="Number of repetitions (averages) per point."
)
# Analysis
significance_threshold: float = analysis_field(
default=2.9,
description="Minimum significance (sigma) for a peak to be accepted.",
)
[docs]
def resolve_spec_pulse_duration(self, transmon: ExtendedTransmon) -> float:
"""Return the pulse duration to use for ``transmon``.
An explicit ``spec_pulse_duration`` wins; otherwise the transition's
duration is read from the transmon's ``spec`` submodule, falling back
to ``DEFAULT_SPEC_PULSE_DURATION`` when that is unset (NaN).
"""
if self.spec_pulse_duration is not None:
return self.spec_pulse_duration
field = "spec_duration" if self.transition == "01" else "spec_duration_12"
duration = float(getattr(transmon.spec, field))
if math.isnan(duration):
return DEFAULT_SPEC_PULSE_DURATION[self.transition]
return duration
[docs]
class QubitSpectroscopyBaseQOI(BaseQoi):
"""Base Quantities of Interest for Qubit Spectroscopy."""
frequency: float = Field(default=0.0)
frequency_error: float = Field(default=0.0)
amplitude: float = Field(default=0.0)
significance: float = Field(default=0.0)
[docs]
@override
def to_dict(self) -> dict[str, float]:
return {
"frequency": self.frequency,
"frequency_error": self.frequency_error,
"amplitude": self.amplitude,
"significance": self.significance,
}
[docs]
class QubitSpectroscopy01QOI(QubitSpectroscopyBaseQOI):
"""Quantities of Interest for Qubit Spectroscopy (0→1 transition)."""
[docs]
@override
def to_dict(self) -> dict[str, float]:
return {
"clock_freqs.f01": self.frequency,
"spec.spec_ampl_optimal": self.amplitude,
}
[docs]
def to_redis_dict(self) -> dict[str, dict[str, float]]:
return {
"clock_freqs:f01": {
"value": self.frequency,
"error": self.frequency_error,
},
"spec:spec_ampl_optimal": {
"value": self.amplitude,
"error": 0.0,
},
}
[docs]
class QubitSpectroscopy12QOI(QubitSpectroscopyBaseQOI):
"""Quantities of Interest for Qubit Spectroscopy (1→2 transition)."""
[docs]
@override
def to_dict(self) -> dict[str, float]:
return {
"clock_freqs.f12": self.frequency,
"spec.spec_ampl_12_optimal": self.amplitude,
}
[docs]
def to_redis_dict(self) -> dict[str, dict[str, float]]:
return {
"clock_freqs:f12": {
"value": self.frequency,
"error": self.frequency_error,
},
"spec:spec_ampl_12_optimal": {
"value": self.amplitude,
"error": 0.0,
},
}