# ══════════════════════════════════════════════════════════════════════════
# ___ ___ ___ _ _ ___
# / _ \ / __|_ ) | | /_\ | _ )
# | (_) | (__ / / | |__ / _ \| _ \
# \__\_\\___/___| |____/_/ \_\___/
#
# 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:
#
# Matteo Robbiati, robbiati@chalmers.se
# Axel Andersson, axean@chalmers.se
# Michele Faucci Giannelli, faucci@chalmers.se
# Hong Quan Trinh, hongq@student.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 PunchoutParameters(BaseParameters):
"""Parameters for Punchout (readout amplitude) calibration.
``bin_mode`` selects the end point method the same way ``readout_state``
selects the readout clock: ``"average_append"`` hardware-averages each
point and finds the end point from a shift in the fitted resonator
frequency; ``"append"`` keeps every single-shot repetition instead and
finds the end point from that IQ cloud's circularity.
The readout-amplitude sweep is sampled more densely at low power, where the
resonator dip moves quickly, and more coarsely at high power. The two
segments are configured by ``crossover_amplitude`` and the two bin counts.
"""
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.",
)
# Frequency sweep (per amplitude slice, reused by the resonator fit) — data acquisition
samples: int = acquisition_field(
default=200, description="Number of frequency samples."
)
half_width: float = acquisition_field(
default=2.0e6, description="Half-width of the frequency sweep window in Hz."
)
# Readout-amplitude sweep (two-segment, dense at low power) — data acquisition
min_amplitude: float = acquisition_field(
default=0.015, description="Minimum readout pulse amplitude."
)
crossover_amplitude: float = acquisition_field(
default=0.06,
description="Amplitude where the dense low-power segment hands over to "
"the coarse high-power segment.",
)
max_amplitude: float = acquisition_field(
default=0.2, description="Maximum readout pulse amplitude."
)
low_amplitude_bins: int = acquisition_field(
default=10, description="Number of samples below the crossover amplitude."
)
high_amplitude_bins: int = acquisition_field(
default=15, description="Number of samples above the crossover amplitude."
)
# Acquisition
repetitions: int = acquisition_field(
default=800, description="Number of repetitions (averages) per point."
)
bin_mode: Literal["average_append", "append"] = acquisition_field(
default="average_append",
variant=True,
description="'average_append' hardware-averages each point "
"('repetitions' averages) and selects the frequency-shift end point; "
"'append' keeps every single-shot repetition instead and selects the "
"IQ-cloud-circularity end point.",
)
# Analysis
upper_threshold: float = analysis_field(
default=0.1e8,
description="Upward resonator-frequency shift (Hz) that ends the punchout.",
)
lower_threshold: float = analysis_field(
default=0.05e6,
description="Downward resonator-frequency shift (Hz) that ends the punchout.",
)
amplitude_scale_factor: float = analysis_field(
default=0.8,
description="Fraction of the end-point amplitude used as the operating point.",
)
max_consecutive_missing_points: int = analysis_field(
default=3,
description="Maximum number of consecutive amplitudes without a valid fit "
"before the punchout search is stopped.",
)
circularity_threshold: float = analysis_field(
default=0.8,
description="Minimum IQ-cloud circularity (ratio of the covariance "
"eigenvalues) required to consider an amplitude still in the linear "
"(non-discriminating) regime.",
)
# Fit-quality gate forwarded to ResonatorSpectroscopyParameters for each
# per-amplitude resonator fit.
min_r_squared: float = analysis_field(
default=0.9,
description="Minimum R^2 (coefficient of determination) accepted for "
"a per-amplitude resonator fit (forwarded to "
"ResonatorSpectroscopyParameters).",
)
max_abs_theta: float = analysis_field(
default=0.25,
description="Maximum theta magnitude (impedance-mismatch phase, rad) "
"accepted for a per-amplitude resonator fit (forwarded to "
"ResonatorSpectroscopyParameters).",
)
[docs]
class PunchoutBaseQOI(BaseQoi):
"""Base Quantities of Interest for Punchout."""
pulse_amp: float = Field(default=0.0)
[docs]
class PunchoutQOI(PunchoutBaseQOI):
"""Quantities of Interest for Punchout (State 0)."""
[docs]
@override
def to_dict(self) -> dict[str, float]:
return {"measure.pulse_amp": self.pulse_amp}
[docs]
def to_redis_dict(self) -> dict[str, dict[str, float]]:
return {"measure:pulse_amp": {"value": self.pulse_amp, "error": 0.0}}
[docs]
class Punchout1QOI(PunchoutBaseQOI):
"""Quantities of Interest for Punchout (State 1)."""
[docs]
@override
def to_dict(self) -> dict[str, float]:
return {"measure_1.pulse_amp": self.pulse_amp}
[docs]
def to_redis_dict(self) -> dict[str, dict[str, float]]:
return {"measure_1:pulse_amp": {"value": self.pulse_amp, "error": 0.0}}
[docs]
class Punchout2QOI(PunchoutBaseQOI):
"""Quantities of Interest for Punchout (State 2)."""
[docs]
@override
def to_dict(self) -> dict[str, float]:
return {"measure_2.pulse_amp": self.pulse_amp}
[docs]
def to_redis_dict(self) -> dict[str, dict[str, float]]:
return {"measure_2:pulse_amp": {"value": self.pulse_amp, "error": 0.0}}