# ══════════════════════════════════════════════════════════════════════════
# ___ ___ ___ _ _ ___
# / _ \ / __|_ ) | | /_\ | _ )
# | (_) | (__ / / | |__ / _ \| _ \
# \__\_\\___/___| |____/_/ \_\___/
#
# 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:
#
# Joel Sandås, joel.sandas@chalmers.se
# Axel Andersson, axean@chalmers.se
# Matteo Robbiati, robbiati@chalmers.se
# Michele Faucci Giannelli, faucci@chalmers.se
# ══════════════════════════════════════════════════════════════════════════
from typing import override
from pydantic import Field
from bragi_quantum.experiments.abstract.parameter_fields import acquisition_field
from bragi_quantum.experiments.abstract.parameters import (
BaseParameters,
BaseQoi,
)
def _default_clifford_sequence_lengths() -> list[int]:
return [0, 2, 4, 8, 16, 128, 256, 512]
[docs]
class PurityBenchmarkingParameters(BaseParameters):
"""Parameters for single-qubit purity benchmarking."""
n_seeds: int = acquisition_field(
default=10,
description=(
"Number of independent random-Clifford-sequence realizations "
"(a fresh, differently seeded sequence per sequence length). "
"Recompiled and executed once per seed; purity is fit against "
"their seed-averaged values, weighted by the seed-to-seed "
"spread at each sequence length."
),
)
repetitions: int = acquisition_field(
default=1024,
description=(
"Hardware-averaged shots per (seed, sequence length, basis) "
"point, and for the automatic calibration-points block appended "
"by BaseNode.precompile."
),
)
clifford_sequence_lengths: list[int] = acquisition_field(
default_factory=_default_clifford_sequence_lengths,
description=(
"Numbers of random Cliffords applied per sequence. Each sequence "
"is measured in the X, Y and Z bases (no recovery Clifford) so "
"the Pauli expectation values can be combined into a purity."
),
)
[docs]
class PurityBenchmarkingQOI(BaseQoi):
"""Quantities of Interest from purity benchmarking.
``purity_fidelity`` is the raw purity-decay fit parameter, i.e. the
unitarity ``u`` of Wallman et al. ("Estimating the coherence of noise",
New J. Phys. 17 113020) / Zhu et al. (arXiv:2407.07960) — the name is
kept for backward compatibility with the original tergite-autocalibration
node. ``incoherent_error`` is the decoherence-limited error bound derived
from ``u`` alone (Zhu et al. eq. 13). ``coherent_error`` additionally
needs a companion randomized-benchmarking result for the same qubit
(read back from Redis, see ``PurityBenchmarkingAnalysis``); it is
``float("nan")`` when no such result is available.
"""
purity_fidelity: float = Field(default=0.0)
purity_fidelity_error: float = Field(default=0.0)
reduced_chi2: float = Field(default=0.0)
incoherent_error: float = Field(default=0.0)
incoherent_error_error: float = Field(default=0.0)
coherent_error: float = Field(default=0.0)
coherent_error_error: float = Field(default=0.0)
[docs]
@override
def to_dict(self) -> dict[str, float]:
# Purity benchmarking 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 {
"purity_fidelity": {
"value": self.purity_fidelity,
"error": self.purity_fidelity_error,
},
"purity_reduced_chi2": {"value": self.reduced_chi2, "error": 0.0},
"purity_incoherent_error": {
"value": self.incoherent_error,
"error": self.incoherent_error_error,
},
"purity_coherent_error": {
"value": self.coherent_error,
"error": self.coherent_error_error,
},
}