# ══════════════════════════════════════════════════════════════════════════
# ___ ___ ___ _ _ ___
# / _ \ / __|_ ) | | /_\ | _ )
# | (_) | (__ / / | |__ / _ \| _ \
# \__\_\\___/___| |____/_/ \_\___/
#
# 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
# Yanchen Lin, linya@chalmers.se
# Matteo Robbiati, robbiati@chalmers.se
# Michele Faucci Giannelli, faucci@chalmers.se
# ══════════════════════════════════════════════════════════════════════════
"""
Parameters and QOIs for two-qubit-gate randomized benchmarking (TQG RB).
Ported from tergite-autocalibration (lib/nodes/coupler/tqg_randomized_benchmarking).
Standard RB (``TQGRandomizedBenchmarking``) and CZ-interleaved RB
(``TQGRandomizedBenchmarkingInterleaved``) share this one parameters class;
the two node classes differ only in whether the CZ Clifford is interleaved
into every sequence (see node.py).
"""
from typing import ClassVar, override
from pydantic import Field
from bragi_quantum.experiments.abstract.parameters import BaseParameters, BaseQoi
[docs]
class TQGRandomizedBenchmarkingParameters(BaseParameters):
"""
Parameters for two-qubit-gate randomized benchmarking on a coupler.
Runs ``n_seeds`` independent random Clifford sequences (drawn from the
two-qubit Clifford group) at each length in ``clifford_sequence_lengths``,
each averaged over ``number_of_shots`` single-shot repetitions. Negative
lengths in ``clifford_sequence_lengths`` are calibration points (-1: state
|1>, -2: state |2>) used by the analysis to correct for per-seed IQ drift.
"""
n_seeds: int = Field(
default=10, description="Number of random Clifford sequences per length."
)
number_of_shots: int = Field(
default=500, description="Single-shot repetitions averaged per point."
)
clifford_sequence_lengths: list[int] = Field(
default_factory=lambda: [-2, -1, 0, 1, 2, 4, 8, 16, 32],
description="Number of Cliffords per sequence. Negative values are "
"calibration points: -1 prepares |1>, -2 prepares |2>.",
)
apply_inverse_gate: bool = Field(
default=True,
description="Append the recovery Clifford that inverts each sequence.",
)
# Which avoided crossing the coupler's CZ pulse targets; determines which
# qubit of the coupler plays "Control" vs "Target" for the CZ virtual-Z
# phase corrections. Same field/convention as cz_parametrization's
# ``cz_transition``.
cz_transition: str = Field(
default="20", description="Targeted transition: '20' or '02'."
)
# Flux-line IF/LO scheme: the CZ pulse clock is programmed at
# -frequency + downconvert_frequency. Same convention as
# cz_parametrization's field of the same name.
downconvert_frequency: float = Field(
default=4.4e9, description="Flux-line downconversion offset (Hz)."
)
[docs]
class TQGRandomizedBenchmarkingBaseQOI(BaseQoi):
"""Shared QOI fields and redis layout for the SRB and IRB variants."""
# Suffix on every redis key, so SRB and IRB keep separate copies on the
# same coupler hash.
redis_key_suffix: ClassVar[str]
tqg_fidelity: float = 0.0
tqg_fidelity_error: float = 0.0
reduced_chi2: float = 0.0
leakage: float = 0.0
error_rate: float = 0.0
# Fit outputs, each None when its fit did not converge.
# P(00) = A * p^m + B
tqg_rb_p: float | None = None
tqg_rb_p_error: float | None = None
tqg_rb_p_A: float | None = None
tqg_rb_p_A_error: float | None = None
tqg_rb_p_B: float | None = None
tqg_rb_p_B_error: float | None = None
# Computational-subspace fit (decay p_L, offset a) and the leakage /
# seepage rates derived from it.
p_L: float | None = None
p_L_error: float | None = None
a: float | None = None
a_error: float | None = None
L_1: float | None = None
L_1_error: float | None = None
L_2: float | None = None
L_2_error: float | None = None
# Leakage-corrected decay of P(00) - P(comp) / 4.
p_r: float | None = None
p_r_error: float | None = None
[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 {
"tqg_fidelity": self.tqg_fidelity,
"reduced_chi2": self.reduced_chi2,
"leakage": self.leakage,
"error_rate": self.error_rate,
}
[docs]
def to_redis_dict(self) -> dict[str, dict]:
# No ParametricCZSubmodule field corresponds to these: RB is a
# benchmarking metric, not a calibration knob applied to hardware.
# Recorded as informational extended_state/Redis values only, so the
# interleaved run (and any later reanalysis) can read them back.
results = {
"tqg_fidelity": (self.tqg_fidelity, self.tqg_fidelity_error),
"reduced_chi2": (self.reduced_chi2, 0.0),
"leakage": (self.leakage, 0.0),
"error_rate": (self.error_rate, 0.0),
"tqg_rb_p": (self.tqg_rb_p, self.tqg_rb_p_error),
"tqg_rb_p_A": (self.tqg_rb_p_A, self.tqg_rb_p_A_error),
"tqg_rb_p_B": (self.tqg_rb_p_B, self.tqg_rb_p_B_error),
"p_L": (self.p_L, self.p_L_error),
"a": (self.a, self.a_error),
"L_1": (self.L_1, self.L_1_error),
"L_2": (self.L_2, self.L_2_error),
"p_r": (self.p_r, self.p_r_error),
}
# A fit that did not converge is left out, so its previous redis
# value is kept rather than overwritten.
return {
f"{name}_{self.redis_key_suffix}": {"value": value, "error": error}
for name, (value, error) in results.items()
if value is not None
}
[docs]
class TQGRandomizedBenchmarkingQOI(TQGRandomizedBenchmarkingBaseQOI):
"""QOI for standard (non-interleaved) two-qubit-gate RB."""
redis_key_suffix: ClassVar[str] = "SRB"
[docs]
class TQGRandomizedBenchmarkingInterleavedQOI(TQGRandomizedBenchmarkingBaseQOI):
"""QOI for CZ-interleaved two-qubit-gate RB.
``tqg_fidelity`` here is the CZ *gate* fidelity, derived by combining this
run's interleaved decay parameter with the standard RB run's decay
parameter (read back from Redis; see analysis.py).
"""
redis_key_suffix: ClassVar[str] = "IRB"