Serialization
Qadence offers convenience functions for serializing and deserializing any quantum program. This is useful for storing quantum programs and sending them for execution over the network via an API.
serialize/deserialize: serialize and deserialize a Qadence object into a dictionarysave/load: save and load a Qadence object to a file with one of the supported formats. Currently, these are.jsonand the PyTorch-compatible.ptformat.
Let's start with serialization into a dictionary.
import torchfrom qadence import QuantumCircuit, QuantumModel, DiffModefrom qadence import chain, hamiltonian_factory, feature_map, hea, Zfrom qadence.serialization import serialize, deserialize
n_qubits = 4
my_block = chain(feature_map(n_qubits, param="x"), hea(n_qubits, depth=2))obs = hamiltonian_factory(n_qubits, detuning=Z)
# Use the block defined above to create a quantum circuit# serialize/deserialize itqc = QuantumCircuit(n_qubits, my_block)qc_dict = serialize(qc)qc_deserialized = deserialize(qc_dict)assert qc == qc_deserialized
# Let's wrap it in a QuantumModel# and serialize itqm = QuantumModel(qc, obs, diff_mode=DiffMode.AD)qm_dict = serialize(qm)qm_deserialized = deserialize(qm_dict)
# Check if the loaded QuantumModel returns the same expectationvalues = {"x": torch.rand(10)}assert torch.allclose(qm.expectation(values=values), qm_deserialized.expectation(values=values))Finally, we can save the quantum circuit and the model with the two supported formats.
from qadence.serialization import serialize, deserialize, save, load, SerializationFormat
qc_fname = "circuit"save(qc, folder=".", file_name=qc_fname, format=SerializationFormat.PT)loaded_qc = load(f"{qc_fname}.pt")assert qc == loaded_qc
qm_fname = "model"save(qm, folder=".", file_name=qm_fname, format=SerializationFormat.JSON)model = load(f"{qm_fname}.json")assert isinstance(model, QuantumModel)