See also
This notebook can be downloaded here
Serialization#
Quantify allows for serialization of :class:~quantify_scheduler.QuantumDevice
, :class:~quantify_scheduler.DeviceElement
(e.g. :class:~quantify_scheduler.BasicTransmonElement
) and :class:~quantify_scheduler.Schedule
objects to json strings and json files. Each class has the following methods:
to_json
: Converts the object into a json string.from_json
: Converts a json string back into the object.to_json_file
: Stores the json string to a file.from_json_file
: Reads the json string from a file and converts it back into the object.
Examples#
(De)Serializing a QuantumDevice
object to json string#
from quantify_scheduler import BasicTransmonElement, QuantumDevice
import json
QuantumDevice.close_all()
device = QuantumDevice("single_qubit_device")
q0 = BasicTransmonElement("q0")
device.cfg_sched_repetitions(512)
device.add_element(q0)
...
device_json = device.to_json()
print(json.dumps(json.loads(device_json), indent=4))
{
"deserialization_type": "quantify_scheduler.device_under_test.quantum_device.QuantumDevice",
"data": {
"name": "single_qubit_device",
"elements": {
"q0": "{\"deserialization_type\": \"quantify_scheduler.device_under_test.transmon_element.BasicTransmonElement\", \"mode\": \"__init__\", \"data\": {\"name\": \"q0\", \"reset\": {\"duration\": 0.0002}, \"rxy\": {\"amp180\": NaN, \"motzoi\": 0, \"duration\": 2e-08}, \"measure\": {\"pulse_type\": \"SquarePulse\", \"pulse_amp\": 0.25, \"pulse_duration\": 3e-07, \"acq_channel\": 0, \"acq_delay\": 0, \"integration_time\": 1e-06, \"reset_clock_phase\": true, \"acq_weights_a\": {\"deserialization_type\": \"ndarray\", \"mode\": \"__init__\", \"data\": []}, \"acq_weights_b\": {\"deserialization_type\": \"ndarray\", \"mode\": \"__init__\", \"data\": []}, \"acq_weights_sampling_rate\": null, \"acq_weight_type\": \"SSB\", \"acq_rotation\": 0, \"acq_threshold\": 0, \"num_points\": null}, \"pulse_compensation\": {\"max_compensation_amp\": NaN, \"time_grid\": NaN, \"sampling_rate\": NaN}, \"ports\": {\"microwave\": \"q0:mw\", \"flux\": \"q0:fl\", \"readout\": \"q0:res\"}, \"clock_freqs\": {\"f01\": NaN, \"f12\": NaN, \"readout\": NaN}}}"
},
"edges": {},
"cfg_sched_repetitions": "512"
}
}
Loading the object from the json string is done using the from_json
method:
deserialized_device = QuantumDevice.from_json(device_json)
(De)Serializing a QuantumDevice
object to json file#
You can optionally specify the path as an argument to the to_json_file
method.
device.to_json_file("/tmp")
'/tmp/single_qubit_device_2024-11-20_18-32-34_UTC.json'
or save it automatically to the current data directory using the objects name:
from quantify_core.data.handling import set_datadir
set_datadir("/tmp")
device.to_json_file() # Saves to "/tmp/single_qubit_device_2024-11-14_13-36-59_UTC.json"
'/tmp/single_qubit_device_2024-11-20_18-32-34_UTC.json'
and the timestamp can be omitted by setting add_timestamp=False
:
device.to_json_file(add_timestamp=False) # Saves to "/tmp/single_qubit_device.json"
'/tmp/single_qubit_device.json'
loading the object from the json file is done using the from_json_file
method:
deserialized_device = QuantumDevice.from_json_file("/tmp/single_qubit_device.json")