json_utils#
Module containing quantify JSON utilities.
Module Contents#
Classes#
| A mixin that adds validation utilities to classes that have | |
| The Quantify Scheduler JSONDecoder. | |
| Custom JSONEncoder which encodes a Quantify Scheduler object into a JSON file format | |
| Mixin to allow de/serialization of arbitrary objects using  | 
Functions#
| 
 | Validate schema using jsonschema-rs. | 
| 
 | Load a JSON schema from file. Expects a 'schemas' directory in the same directory | 
| 
 | Load a JSON validator from file. Expects a 'schemas' directory in the same directory | 
| 
 | 
Attributes#
- load_json_schema(relative_to: str | pathlib.Path, filename: str) str[source]#
- Load a JSON schema from file. Expects a ‘schemas’ directory in the same directory as - relative_to.- Tip - Typical usage of the form - schema = load_json_schema(__file__, 'definition.json')- Parameters:
- relative_to – the file to begin searching from 
- filename – the JSON file to load 
 
- Returns:
- the schema 
- Return type:
 
- load_json_validator(relative_to: str | pathlib.Path, filename: str) Callable[source]#
- Load a JSON validator from file. Expects a ‘schemas’ directory in the same directory as - relative_to.- Parameters:
- relative_to – the file to begin searching from 
- filename – the JSON file to load 
 
- Returns:
- The validator 
- Return type:
- Callable 
 
- exception UnknownDeserializationTypeError[source]#
- Bases: - Exception- Raised when an unknown deserialization type is encountered. 
- class JSONSchemaValMixin[source]#
- A mixin that adds validation utilities to classes that have a data attribute like a - UserDictbased on JSONSchema.- This requires the class to have a class variable “schema_filename” - classmethod is_valid(object_to_be_validated: quantify_scheduler.operations.Operation) bool[source]#
- Checks if the object is valid according to its schema. - Raises:
- fastjsonschema.JsonSchemaException – if the data is invalid 
 
 
- class SchedulerJSONDecoder(*args, **kwargs)[source]#
- Bases: - json.JSONDecoder- The Quantify Scheduler JSONDecoder. - The SchedulerJSONDecoder is used to convert a string with JSON content into instances of classes in quantify-scheduler. - For a few types, - DEFAULT_TYPEScontains the mapping from type name to the python object. This dictionary can be expanded with classes from modules specified in the keyword argument- modules.- Classes not contained in - DEFAULT_TYPESby default must implement- __getstate__, such that it returns a dictionary containing at least the keys- "deserialization_type"and- "data", and- __setstate__, which should be able to parse the data from- __getstate__.- The value of - "deserialization_type"must be either the name of the class specified in- DEFAULT_TYPESor the fully qualified name of the class, which can be obtained from- export_python_object_to_path_string().- Keyword Arguments:
- modules (list[ModuleType], optional) – A list of custom modules containing serializable classes, by default [] 
 - decode_dict(obj: dict[str, Any]) dict[str, Any] | numpy.ndarray | object | qcodes.instrument.Instrument[source]#
- Returns the deserialized JSON dictionary. - Parameters:
- obj – The dictionary to deserialize. 
- Returns:
- The deserialized result. 
 
 - custom_object_hook(obj: object) object[source]#
- The - object_hookhook will be called with the result of every JSON object decoded and its return value will be used in place of the given- dict.- Parameters:
- obj – A pair of JSON objects. 
- Returns:
- The deserialized result. 
 
 - _get_type_from_string(deserialization_type: str) type[source]#
- Get the python type based on the description string. - The following methods are tried, in order: - Try to find the string in DEFAULT_TYPESor the extended modules
- passed to this class’ initializer. 
 
- Try to find the string in 
- Try to import the type. This works only if deserialization_typeis
- formatted as a dot-separated path to the type. E.g. - quantify_scheduler.json_utils.SchedulerJSONDecoder.
 
- Try to import the type. This works only if 
- (deprecated) Try to find the class by its __name__in a predefined
- selection of types present in - quantify_scheduler.
 
- (deprecated) Try to find the class by its 
 - Parameters:
- deserialization_type – Description of a type. 
- Raises:
- UnknownDeserializationTypeError – If the type cannot be found by any of the methods described. 
- Returns:
- The - Typefound.
- Return type:
- Type 
 
 
- class SchedulerJSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]#
- Bases: - json.JSONEncoder- Custom JSONEncoder which encodes a Quantify Scheduler object into a JSON file format string. - default(o: object) object[source]#
- Overloads the json.JSONEncoder default method that returns a serializable object. It will try 3 different serialization methods which are, in order, check if the object is to be serialized to a string using repr. If not, try to use - __getstate__. Finally, try to serialize the- __dict__property.
 
- class JSONSerializableMixin[source]#
- Mixin to allow de/serialization of arbitrary objects using - SchedulerJSONEncoderand- SchedulerJSONDecoder.- to_json() str[source]#
- Convert the object’s data structure to a JSON string. - Returns:
- The json string containing the serialized object. 
 
 - to_json_file(path: str | None = None, add_timestamp: bool = True) str[source]#
- Convert the object’s data structure to a JSON string and store it in a file. - Examples - Saving a - QuantumDevicewill use its name and current timestamp- from quantify_scheduler import QuantumDevice single_qubit_device = QuantumDevice("single_qubit_device") ... single_qubit_device.to_json_file() single_qubit_device.close() single_qubit_device = QuantumDevice.from_json_file("/tmp/single_qubit_device_2024-11-14_13-36-59_UTC.json") - Parameters:
- path – The path to the directory where the file is created. Default is None, in which case the file will be saved in the directory determined by - get_datadir().
- add_timestamp – Specify whether to append timestamp to the filename. Default is True. 
 
- Returns:
- The name of the file containing the serialized object. 
 
 - classmethod from_json(data: str) typing_extensions.Self[source]#
- Convert the JSON data to an instance of the attached class. - Parameters:
- data – The JSON data in str format. 
- Returns:
- The deserialized object. 
 
 - classmethod from_json_file(filename: str) typing_extensions.Self[source]#
- Read JSON data from a file and convert it to an instance of the attached class. - Examples - from quantify_scheduler import QuantumDevice single_qubit_device = QuantumDevice.from_json_file("/tmp/single_qubit_device_2024-11-14_13-36-59_UTC.json") - Parameters:
- filename – The name of the file containing the serialized object. 
- Returns:
- The deserialized object.