# ══════════════════════════════════════════════════════════════════════════
# ___ ___ ___ _ _ ___
# / _ \ / __|_ ) | | /_\ | _ )
# | (_) | (__ / / | |__ / _ \| _ \
# \__\_\\___/___| |____/_/ \_\___/
#
# 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
# Joel Sandås, joel.sandas@chalmers.se
# Matteo Robbiati, robbiati@chalmers.se
# ══════════════════════════════════════════════════════════════════════════
from typing import override
from pydantic import Field
from bragi_quantum.experiments.abstract.parameters import BaseParameters, BaseQoi
from bragi_quantum.experiments.nodes.coupler.cz_transition_frequency import (
REDIS_KEY_BY_TRANSITION,
)
[docs]
class CZCurrentsFromSpectroscopiesParameters(BaseParameters):
"""
Parameters for CZCurrentsFromSpectroscopies.
Analysis-only node: no measurement is performed. The coupler-frequency
model is fitted to the crossing points stored in Redis by
qubit_spectroscopy_vs_current and resonator_spectroscopy_vs_current, and
candidate CZ working-point currents are computed from the fit. Ported
from tergite-autocalibration (CZCurrentsFromSpectroscopiesNode).
"""
# Fit initial values and bounds for the coupler-frequency model, in the
# parameter order (bare_coupler_frequency_GHz, amplitude, offset_mA,
# minimum_coupler_frequency_GHz). The offset guess is replaced per coupler
# by an estimate from the qubit crossing points.
guess: list[float] = Field(default_factory=lambda: [6.5, 0.29, 0.1, 0.1])
lower_bounds: list[float] = Field(default_factory=lambda: [6, 0.20, -2.0, 0])
upper_bounds: list[float] = Field(default_factory=lambda: [7.5, 0.40, 2.0, 3])
# Crossing-point selection: qubit/resonator groups below these minimums
# are considered unreliable and excluded from the fit. There is no upper
# cap — the fitter removes outliers itself via iterative outlier removal.
min_total_crossing_points: int = Field(
default=4, description="Minimum overall crossings required to fit."
)
minimum_qubit_crossing_points: int = Field(default=2)
minimum_resonator_crossing_points: int = Field(default=0)
# Maximum per-point RMSE (GHz) for a subset to be accepted as a good fit.
# Outlier points are removed one at a time until RMSE falls below this.
fit_quality_threshold: float = Field(default=0.12)
# Working-point calculation
calculate_wp: bool = Field(
default=True,
description="Compute candidate CZ working-point currents from the "
"fitted coupler frequency (otherwise only the fit results are stored).",
)
max_cz_frequency_in_GHz: float = Field(
default=1.0,
description="CZ modulation frequencies above this are outside the "
"electronics range; working points are not calculated for them.",
)
max_number_of_wp: int = Field(
default=15, description="Maximum number of working points to scan for."
)
use_fix_frequency_step: bool = Field(
default=False,
description="Scan working points on a fixed frequency grid instead of "
"the CZ frequencies derived from the qubit transition frequencies.",
)
fixed_frequency_step_in_GHz: float = Field(default=0.25)
[docs]
class CZCurrentsFromSpectroscopiesQOI(BaseQoi):
"""
Quantities of Interest for CZCurrentsFromSpectroscopies on one coupler:
the fitted coupler-frequency model parameters and the candidate CZ
working-point currents for the 20 and 02 avoided crossings.
"""
cz_currents_from_spectroscopies_20: list[float] = Field(default_factory=list)
cz_currents_from_spectroscopies_02: list[float] = Field(default_factory=list)
cz_transition_frequency_20: float = 0.0
cz_transition_frequency_02: float = 0.0
coupler_frequency: float = 0.0
coupler_frequency_error: float = 0.0
coupler_amplitude: float = 0.0
coupler_amplitude_error: float = 0.0
current_offset: float = 0.0
current_offset_error: float = 0.0
coupler_minimum_frequency: float = 0.0
coupler_minimum_frequency_error: float = 0.0
[docs]
def items(self):
"""
Iterate like a mapping so bragi_quantum.datalayer's ``update_redis_trusted_values`` can
inspect the QOI keys when routing coupler results.
"""
return self.to_dict().items()
[docs]
@override
def to_dict(self) -> dict:
return {
"cz_currents_from_spectroscopies_20": self.cz_currents_from_spectroscopies_20,
"cz_currents_from_spectroscopies_02": self.cz_currents_from_spectroscopies_02,
"cz_transition_frequency_20": self.cz_transition_frequency_20,
"cz_transition_frequency_02": self.cz_transition_frequency_02,
"coupler_frequency": self.coupler_frequency,
"coupler_amplitude": self.coupler_amplitude,
"current_offset": self.current_offset,
"coupler_minimum_frequency": self.coupler_minimum_frequency,
}
[docs]
def to_redis_dict(self) -> dict[str, dict]:
# Current lists are stored as the string repr of the list, since Redis
# hash fields cannot hold Python lists. Keys deliberately contain no
# ':' so they are not interpreted as device submodule fields.
return {
"cz_currents_from_spectroscopies_20": {
"value": str(
[float(c) for c in self.cz_currents_from_spectroscopies_20]
),
"error": 0.0,
},
"cz_currents_from_spectroscopies_02": {
"value": str(
[float(c) for c in self.cz_currents_from_spectroscopies_02]
),
"error": 0.0,
},
REDIS_KEY_BY_TRANSITION["20"]: {
"value": self.cz_transition_frequency_20,
"error": 0.0,
},
REDIS_KEY_BY_TRANSITION["02"]: {
"value": self.cz_transition_frequency_02,
"error": 0.0,
},
"coupler_frequency": {
"value": self.coupler_frequency,
"error": self.coupler_frequency_error,
},
"coupler_amplitude": {
"value": self.coupler_amplitude,
"error": self.coupler_amplitude_error,
},
"current_offset": {
"value": self.current_offset,
"error": self.current_offset_error,
},
"coupler_minimum_frequency": {
"value": self.coupler_minimum_frequency,
"error": self.coupler_minimum_frequency_error,
},
}