{ "cells": [ { "cell_type": "markdown", "id": "331e3202", "metadata": {}, "source": [ "```{seealso}\n", "This notebook can be downloaded {nb-download}`here `\n", "```\n", "\n", "# Serialization\n", "\n", "Quantify allows for serialization of `QuantumDevice` and `Schedule` objects to json strings. Serializing to json string is done using the method `to_json`, and deserializing using `from_json`. It is also possible to directly store in / read from a file using the methods `to_json_file` and `from_json_file`.\n", "\n", "The code block below shows the serialization of a `QuantumDevice` object into a json string:" ] }, { "cell_type": "code", "execution_count": 1, "id": "3c8798c4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Class: \n" ] } ], "source": [ "from quantify_scheduler.device_under_test.composite_square_edge import CompositeSquareEdge\n", "from quantify_scheduler.device_under_test.quantum_device import QuantumDevice\n", "from quantify_scheduler.device_under_test.transmon_element import BasicTransmonElement\n", "\n", "q0 = BasicTransmonElement(\"q0\")\n", "q1 = BasicTransmonElement(\"q1\")\n", "edge_q0_q1 = CompositeSquareEdge(parent_element_name=q0.name, child_element_name=q1.name)\n", "\n", "quantum_device = QuantumDevice(\"quantum_device\")\n", "quantum_device.add_element(q0)\n", "quantum_device.add_element(q1)\n", "quantum_device.add_edge(edge_q0_q1)\n", "\n", "quantum_device.cfg_sched_repetitions(512)\n", "\n", "# Serialize QuantumDevice into a json string\n", "serialized_quantum_device = quantum_device.to_json()\n", "# Close before deserialization to avoid duplicated instrument error: \n", "# this closes *any* open instrument\n", "QuantumDevice.close_all()\n", "\n", "print(f\"Class: {serialized_quantum_device.__class__}\")" ] }, { "cell_type": "markdown", "id": "6a5dfbe3", "metadata": {}, "source": [ "The previous `QuantumDevice` object can now be instantiated again from the json string:" ] }, { "cell_type": "code", "execution_count": 2, "id": "b588ee49", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Class: \n", "Elements: ['q0', 'q1']\n", "Edges: ['q0_q1']\n", "Repetitions: 512\n" ] } ], "source": [ "deserialized_quantum_device = QuantumDevice.from_json(serialized_quantum_device)\n", "\n", "print(f\"Class: {deserialized_quantum_device.__class__}\")\n", "print(f\"Elements: {deserialized_quantum_device.elements()}\")\n", "print(f\"Edges: {deserialized_quantum_device.edges()}\")\n", "print(f\"Repetitions: {deserialized_quantum_device.cfg_sched_repetitions()}\")" ] }, { "cell_type": "markdown", "id": "3dae2f9a", "metadata": {}, "source": [ "```{admonition} Note\n", "Inherited attributes from the QCoDeS `Instrument` class are not included when serializing `QuantumDevice`.\n", "```" ] } ], "metadata": { "file_format": "mystnb", "kernelspec": { "display_name": "python3", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.19" }, "source_map": [ 6, 17, 40, 44, 51 ] }, "nbformat": 4, "nbformat_minor": 5 }