# ══════════════════════════════════════════════════════════════════════════
# ___ ___ ___ _ _ ___
# / _ \ / __|_ ) | | /_\ | _ )
# | (_) | (__ / / | |__ / _ \| _ \
# \__\_\\___/___| |____/_/ \_\___/
#
# 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
# Yanchen Lin, linya@chalmers.se
# Matteo Robbiati, robbiati@chalmers.se
# Michele Faucci Giannelli, faucci@chalmers.se
# ══════════════════════════════════════════════════════════════════════════
from typing import Literal, override
import numpy as np
from pydantic import Field, model_validator
from bragi_quantum.experiments.abstract.parameter_fields import (
acquisition_field,
analysis_field,
)
from bragi_quantum.experiments.abstract.parameters import (
BaseQoi,
)
from bragi_quantum.experiments.nodes.characterization.coherence_parameters import (
CoherenceParameters,
)
[docs]
class T2Parameters(CoherenceParameters):
"""Parameters for T2 relaxation-time characterization."""
transition: Literal["01"] = acquisition_field(default="01", variant=True)
delays: list[float] = acquisition_field(
default_factory=lambda: np.arange(4e-9, 32768e-9, 64e-9).tolist(),
description="Sweeping delay times tau between the two pi/2-pulses, in seconds, as an array of setpoints per qubit, keyed by qubit name.",
)
artificial_detuning_Hz: float = acquisition_field(
default=1e6,
description="Artificial detuning, in Hz, applied to every qubit via a clock-phase shift after the idle time, used to generate the Ramsey fringe.",
)
repetitions: int = acquisition_field(
default=1024,
description="Number of hardware-averaged shots per delay point.",
)
t2_repetitions: int = acquisition_field(
default=3,
description=(
"Number of independent repeats of the full delay sweep (and its "
"own calibration points). T2* is fitted separately per repeat; "
"the reported t2 is the mean across repeats and t2_error is "
"their standard deviation."
),
)
histogram_bins: int = analysis_field(
default=10,
description="Number of bins for the per-repeat T2* histogram plot.",
)
@model_validator(mode="after")
def _check_artificial_detuning_below_nyquist(self) -> "T2Parameters":
# Detuning at/beyond the Nyquist limit aliases the Ramsey fringe.
# Largest gap sets the tightest bound for non-uniform delays.
delay_spacing_s = float(np.max(np.diff(self.delays)))
nyquist_hz = 0.5 / delay_spacing_s
if abs(self.artificial_detuning_Hz) >= nyquist_hz:
raise ValueError(
f"artificial_detuning_Hz={self.artificial_detuning_Hz:.0f} Hz "
f"is at or beyond the {nyquist_hz:.0f} Hz Nyquist limit set "
f"by the delay sampling ({delay_spacing_s * 1e9:.1f} ns spacing) "
"— the resulting Ramsey fringe would alias and cannot be fitted "
"correctly. Reduce artificial_detuning_Hz or use finer delay "
"spacing."
)
return self
[docs]
class T2QOI(BaseQoi):
"""Quantities of Interest from T2* (Ramsey) characterization.
``t2`` and ``tau`` both hold the fitted decay time, in microseconds,
matching the fitting units (the same quantity — ``tau`` is the
underlying damped-oscillation fit parameter name, ``t2`` is the reported
characterization quantity). ``frequency`` and ``frequency_error`` are in
Hz, matching ``artificial_detuning_Hz``.
"""
t2: float = Field(default=0.0)
t2_error: float = Field(default=0.0)
offset: float = Field(default=0.0)
offset_error: float = Field(default=0.0)
amplitude: float = Field(default=0.0)
amplitude_error: float = Field(default=0.0)
frequency: float = Field(default=0.0)
frequency_error: float = Field(default=0.0)
tau: float = Field(default=0.0)
tau_error: float = Field(default=0.0)
[docs]
@override
def to_dict(self) -> dict[str, float]:
# T2 is a characterization quantity; there is no device-model
# attribute to write back.
return {}
[docs]
def to_redis_dict(self) -> dict[str, dict[str, float]]:
return {
"t2": {"value": self.t2, "error": self.t2_error},
"t2_offset": {"value": self.offset, "error": self.offset_error},
"t2_amplitude": {"value": self.amplitude, "error": self.amplitude_error},
"t2_frequency": {"value": self.frequency, "error": self.frequency_error},
"t2_tau": {"value": self.tau, "error": self.tau_error},
}