Source code for qblox_scheduler.device_under_test.hardware_config
# Repository: https://gitlab.com/qblox/packages/software/qblox-scheduler
# Licensed according to the LICENSE file on the main branch
#
# Copyright 2020-2025, Quantify Consortium
# Copyright 2025, Qblox B.V.
"""
Module containing the HardwareConfig object.
Extends ManualParameter to add methods to load from/to file and reload.
Note: ManualParameter might be refactored out at some point in the future.
"""
from __future__ import annotations
import json
from collections import UserDict
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from qblox_scheduler import QuantumDevice
[docs]
class HardwareConfig(UserDict):
"""
The input dictionary used to generate a valid HardwareCompilationConfig.
This configures the compilation from the quantum-device layer to the control-hardware layer.
Parameters
----------
configuration
A dictionary with the hardware configuration.
"""
def __init__(
self,
configuration: dict | None = None,
instrument: QuantumDevice | None = None, # noqa: ARG002
) -> None:
super().__init__(configuration)
[docs]
def set(self, value: dict) -> None:
"""Set the hardware configuration onto the dict itself."""
self.data = value
[docs]
def load_from_json_file(self, file_path: str | Path) -> None:
"""
Reload the object's configuration from a file.
Updates the object's data using the contents of the file.
Parameters
----------
file_path
The path to the file to reload from.
Raises
------
FileNotFoundError
If the provided file path does not exist.
IOError
If an I/O error occurs during file reading.
"""
file_path = Path(file_path) # note that Path(Path(path)) == Path(path)
if file_path.is_dir():
raise IsADirectoryError(f"{file_path.name} is a directory")
with file_path.open("r") as file:
self.data = json.load(file)
[docs]
def write_to_json_file(self, file_path: str | Path) -> None:
"""
Write the current configuration to a specified file.
If the file does not exist, it is created.
The data is written in JSON format, and an indentation of 2.
Parameters
----------
file_path
The path to the file where data will be written.
Raises
------
ValueError
If neither a file path is provided nor a previously known file path exists.
IOError
If an I/O error occurs during file creation or writing.
"""
file_path = Path(file_path)
if file_path.is_dir():
raise IsADirectoryError(f"{file_path.name} is a directory")
with file_path.open("w") as file:
json.dump(self.data, file, ensure_ascii=False, indent=2)