# ══════════════════════════════════════════════════════════════════════════
# ___ ___ ___ _ _ ___
# / _ \ / __|_ ) | | /_\ | _ )
# | (_) | (__ / / | |__ / _ \| _ \
# \__\_\\___/___| |____/_/ \_\___/
#
# 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
# Matteo Robbiati, robbiati@chalmers.se
# Michele Faucci Giannelli, faucci@chalmers.se
# ══════════════════════════════════════════════════════════════════════════
from typing import Literal, override
import numpy as np
from pydantic import Field
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,
)
def _default_t2_echo_delays() -> list[float]:
"""Uniformly spaced total echo delays, 0 to 480 us in 12 us steps.
Must be a true linear sweep (constant, whole-nanosecond step): the
schedule expresses half of it (one side of the echo) as a single hardware
loop register/unrolled domain, which can only represent an evenly spaced
range.
"""
return np.linspace(0, 480e-6, 41).tolist()
[docs]
class T2EchoParameters(CoherenceParameters):
"""Parameters for T2 Echo (Hahn echo) characterization."""
transition: Literal["01"] = acquisition_field(default="01", variant=True)
# Total free-evolution delays (s) between the two pi/2 pulses, with the
# echo (pi) pulse centered at delay/2.
delays: list[float] = acquisition_field(
default_factory=_default_t2_echo_delays,
description="Total echo delays between the two pi/2 pulses, in seconds.",
)
repetitions: int = acquisition_field(
default=1024,
description="Number of hardware-averaged shots per delay point.",
)
t2_echo_repetitions: int = acquisition_field(
default=3,
description=(
"Number of independent repeats of the full delay sweep (and its "
"own calibration points). T2 echo is fitted separately per "
"repeat; the reported t2_echo is the mean across repeats and "
"t2_echo_error is their standard deviation."
),
)
histogram_bins: int = analysis_field(
default=10,
description="Number of bins for the per-repeat T2 echo histogram plot.",
)
[docs]
class T2EchoQOI(BaseQoi):
"""Quantities of Interest from T2 Echo characterization.
Times are stored in microseconds, matching the fitting units.
"""
t2_echo: float = Field(default=0.0)
t2_echo_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)
[docs]
@override
def to_dict(self) -> dict[str, float]:
# T2 echo 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_echo": {"value": self.t2_echo, "error": self.t2_echo_error},
"t2_echo_offset": {"value": self.offset, "error": self.offset_error},
"t2_echo_amplitude": {
"value": self.amplitude,
"error": self.amplitude_error,
},
}