# ══════════════════════════════════════════════════════════════════════════
# ___ ___ ___ _ _ ___
# / _ \ / __|_ ) | | /_\ | _ )
# | (_) | (__ / / | |__ / _ \| _ \
# \__\_\\___/___| |____/_/ \_\___/
#
# 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
# Yanchen Lin, linya@chalmers.se
# ══════════════════════════════════════════════════════════════════════════
from typing import 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,
)
def _default_clifford_sequence_lengths() -> list[int]:
return [0, 9, 27, 81, 243, 729, 1800]
[docs]
class RBParameters(BaseParameters):
"""Parameters for single-qubit randomized benchmarking (SSRO-classified)."""
n_seeds: int = acquisition_field(
default=8,
description=(
"Number of independent random-Clifford-sequence realizations "
"(a fresh, differently seeded sequence per sequence length). "
"Recompiled and executed once per seed; fidelity is fit against "
"their seed-averaged state populations, weighted by the "
"seed-to-seed spread at each sequence length."
),
)
number_of_shots: int = acquisition_field(
default=500,
description="Single-shot repeats measured per (seed, sequence length) point.",
)
repetitions: int = acquisition_field(
default=1024,
description=(
"Hardware-averaged shots for the automatic calibration-points "
"block appended by BaseNode.precompile — independent of "
"number_of_shots, which drives the main single-shot RB loop."
),
)
clifford_sequence_lengths: list[int] = acquisition_field(
default_factory=_default_clifford_sequence_lengths,
description="Numbers of random Cliffords applied (then inverted) per sequence.",
)
seed_number: "str | None" = analysis_field(
default=None,
description=(
"1-based seed(s) to show in the IQ-cloud plot, e.g. 1; 3-5; 3,4,5. "
"Defaults to the first seed (1); several seeds render as a grid."
),
)
clifford_sequence_index: "str | None" = analysis_field(
default=None,
description=(
"1-based index/indices into clifford_sequence_lengths to show in "
"the IQ-cloud plot, e.g. 1; 3-5; 3,4,5. Defaults to the last one; "
"several indices render as a grid."
),
)
[docs]
class RBQOI(BaseQoi):
"""Quantities of Interest from randomized benchmarking."""
fidelity: float = Field(default=0.0)
fidelity_error: float = Field(default=0.0)
reduced_chi2: float = Field(default=0.0)
leakage: float = Field(default=0.0)
leakage_error: float = Field(default=0.0)
seepage: float = Field(default=0.0)
seepage_error: float = Field(default=0.0)
[docs]
@override
def to_dict(self) -> dict[str, float]:
# RB 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 {
"rb_fidelity": {"value": self.fidelity, "error": self.fidelity_error},
"rb_reduced_chi2": {"value": self.reduced_chi2, "error": 0.0},
"rb_leakage": {"value": self.leakage, "error": self.leakage_error},
"rb_seepage": {"value": self.seepage, "error": self.seepage_error},
}