# ══════════════════════════════════════════════════════════════════════════
# ___ ___ ___ _ _ ___
# / _ \ / __|_ ) | | /_\ | _ )
# | (_) | (__ / / | |__ / _ \| _ \
# \__\_\\___/___| |____/_/ \_\___/
#
# 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
# ══════════════════════════════════════════════════════════════════════════
# This code is part of Tergite
#
# (C) Copyright Eleftherios Moschandreou 2024
# (C) Copyright Stefan Hill 2024, 2025
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
#
# Modified from tergite-autocalibration
# (https://github.com/tergite/tergite-autocalibration), file:
# tergite_autocalibration/lib/utils/validators.py
from numpy._typing import NDArray
from pydantic import ConfigDict, RootModel
[docs]
class SimpleSamplespace(RootModel):
root: dict[str, dict[str, NDArray]]
model_config = ConfigDict(arbitrary_types_allowed=True)
[docs]
class MixedSamplespace(RootModel):
root: dict[str, dict[str, NDArray] | dict[str, list[NDArray]]]
model_config = ConfigDict(arbitrary_types_allowed=True)
[docs]
class Samplespace(RootModel):
root: SimpleSamplespace | MixedSamplespace
model_config = ConfigDict(arbitrary_types_allowed=True)
[docs]
class BatchedArray(RootModel):
root: dict[str, list[NDArray]]
model_config = ConfigDict(arbitrary_types_allowed=True)
[docs]
class SimpleArray(RootModel):
root: dict[str, NDArray]
model_config = ConfigDict(arbitrary_types_allowed=True)
[docs]
class Array(RootModel):
root: BatchedArray | SimpleArray
model_config = ConfigDict(arbitrary_types_allowed=True)
[docs]
def get_number_of_batches(
samplespace: dict[str, dict[str, NDArray] | dict[str, list[NDArray]]],
):
"""
if the samplespace is a list of NDarrays,
return the number of individual arrays per qubit
"""
for qubit_dict in samplespace.values():
qubit_dict = dict(qubit_dict)
q = Array(qubit_dict)
if isinstance(q.root, BatchedArray):
for element, samples in qubit_dict.items():
number_of_batches = len(samples)
else:
number_of_batches = 1
return number_of_batches
[docs]
def get_batched_dimensions(
samplespace: dict[str, dict[str, list[NDArray]]],
) -> list[str]:
"""
if the samplespace is a list of NDarrays,
return the name of the settable, e.g. 'frequencies'
"""
batched_dimensions = []
for settable, qubit_dict in samplespace.items():
qubit_dict = dict(qubit_dict)
q = Array(qubit_dict)
if isinstance(q.root, BatchedArray):
for qubit in qubit_dict:
batched_dimensions.append(str(settable) + str(qubit))
return batched_dimensions
[docs]
def reduce_batch(samplespace, batch: int):
"""
if the samplespace is a list of NDarrays,
keep only the array of index=batch. the reduced samplespace
has the same structure as a regular samplespace.
"""
reduced_samplespace = {}
for settable, qubit_dict in samplespace.items():
qubit_dict = dict(qubit_dict)
q = Array(qubit_dict)
if isinstance(q.root, SimpleArray):
reduced_samplespace[settable] = qubit_dict
elif isinstance(q.root, BatchedArray):
reduced_samplespace[settable] = {}
for element, samples in qubit_dict.items():
reduced_samplespace[settable][element] = samples[batch]
return reduced_samplespace