Source code for bragi_quantum.experiments.nodes.coupler.tqg_randomized_benchmarking.utils.randomized_benchmarking

# ══════════════════════════════════════════════════════════════════════════
#     ___   ___ ___   _      _   ___
#    / _ \ / __|_  ) | |    /_\ | _ )
#   | (_) | (__ / /  | |__ / _ \| _ \
#    \__\_\\___/___| |____/_/ \_\___/
#
#   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
# ══════════════════════════════════════════════════════════════════════════


# This code is part of Tergite
#
# (C) Copyright Pontus Vikstål 2025
#
# This program is derived from the PycQED with the following license:
#
# MIT License
#
# Copyright (c) 2016 DiCarlo lab-QuTech-Delft University of Technology
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Modified from tergite-autocalibration
# (https://github.com/tergite/tergite-autocalibration), file:
#   tergite_autocalibration/lib/nodes/coupler/tqg_randomized_benchmarking/utils/randomized_benchmarking.py


from typing import Literal

import numpy as np

from bragi_quantum.experiments.nodes.coupler.tqg_randomized_benchmarking.utils.clifford_group import (
    Clifford,
    SingleQubitClifford,
    TwoQubitClifford,
)


[docs] def calculate_net_clifford( clifford_indices: np.ndarray, CliffordClass: type["Clifford"], ) -> "Clifford": """ Calculate the net-clifford from a list of cliffords indices. Args: clifford_indices (np.ndarray): Array of integers specifying the Cliffords. CliffordClass: Clifford class used to determine the inversion technique and valid indices. Valid choices are `SingleQubitClifford` and `TwoQubitClifford`. Returns: net_clifford: a `Clifford` object containing the net-clifford. the Clifford index is contained in the Clifford.idx attribute. Note: the order corresponds to the order in a pulse sequence but is the reverse of what it would be in a chained dot product. """ # Calculate the net clifford net_clifford = CliffordClass(0) # assumes element 0 is the Identity for idx in clifford_indices: clifford = CliffordClass(idx % 100_000) # order of operators applied in is right to left, therefore # the new operator is applied on the left side. net_clifford = clifford * net_clifford return net_clifford
[docs] def add_interleaved_clifford( clifford_sequence: np.ndarray, interleaved_clifford: int ) -> np.ndarray: """ Adds an interleaved Clifford gate to the sequence. Args: clifford_sequence (np.ndarray): Array of Clifford indices. Returns: np.ndarray: Array with interleaved Clifford. """ interleaved_sequence = np.empty(clifford_sequence.size * 2, dtype=int) interleaved_sequence[0::2] = clifford_sequence interleaved_sequence[1::2] = interleaved_clifford return interleaved_sequence
[docs] def add_inverse_clifford( clifford_sequence: np.ndarray, CliffordClass: type["Clifford"], ) -> np.ndarray: """ Adds the inverse of the total sequence to the end of the sequence. Args: clifford_sequence (np.ndarray): Array of Clifford indices. CliffordClass: The class of the Clifford group used. Returns: np.ndarray: Array with appended inverse Clifford. """ # Calculate the net Clifford net_clifford = calculate_net_clifford(clifford_sequence, CliffordClass) # Get the inverse of the net clifford to find the Clifford that inverts the sequence inverse_clifford = CliffordClass(net_clifford.idx).get_inverse() return np.append(clifford_sequence, inverse_clifford.idx)
[docs] def randomized_benchmarking_sequence( number_of_cliffords: int, apply_inverse: bool = True, clifford_group: Literal[1, 2] = 1, interleaved_clifford_idx: int | None = None, seed: int | None = None, ) -> np.ndarray: """ Generates a randomized benchmarking sequence using the one- or two-qubit Clifford group. Args: number_of_cliffords (int): Number of Clifford gates in the sequence (excluding the optional inverse). apply_inverse (bool): Whether to append the recovery Clifford that inverts the total sequence. clifford_group (int): Specifies which Clifford group to use. 1 for single-qubit (24 elements), 2 for two-qubit (11,520 elements). interleaved_clifford_idx (Optional[int]): Optional ID for interleaving a specific Clifford gate. seed (Optional[int]): Optional seed for reproducibility. Returns: np.ndarray: Array of Clifford indices representing the randomized benchmarking sequence. Raises: ValueError: If `number_of_cliffords` is negative. NotImplementedError: If an unsupported Clifford group is specified. """ if number_of_cliffords < 0: raise ValueError("Number of Cliffords can't be negative.") clifford_classes = { 1: (24, SingleQubitClifford), 2: (11_520, TwoQubitClifford), } if clifford_group not in clifford_classes: raise NotImplementedError( "Only one- and two-qubit Clifford groups (1 or 2) are supported." ) group_size, CliffordClass = clifford_classes[clifford_group] rng = np.random.default_rng(seed) clifford_indices = rng.integers(low=0, high=group_size, size=number_of_cliffords) # Add interleaving Clifford if applicable if interleaved_clifford_idx is not None: clifford_indices = add_interleaved_clifford( clifford_indices, interleaved_clifford_idx ) # Add inverse Clifford if applicable if apply_inverse: clifford_indices = add_inverse_clifford(clifford_indices, CliffordClass) return clifford_indices