Source code for qiskit_serverless.serializers.serializers
# This code is a Qiskit project.## (C) Copyright IBM 2022.## This code is licensed under the Apache License, Version 2.0. You may# obtain a copy of this license in the LICENSE.txt file in the root directory# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.## Any modifications or derivative works of this code must retain this# copyright notice, and modified files need to carry a notice indicating# that they have been altered from the originals."""===============================================================Serializers (:mod:`qiskit_serverless.serializers.serializers`)===============================================================.. currentmodule:: qiskit_serverless.serializers.serializersQiskit Serverless serializers==============================.. autosummary:: :toctree: ../stubs/ register_all_serializers circuit_serializer circuit_deserializer service_serializer service_deserializer"""importbase64importioimportzlibimportrayfromqiskitimportQuantumCircuit,qpyfromqiskit_ibm_runtimeimportQiskitRuntimeService
[docs]defcircuit_serializer(circuit:QuantumCircuit)->str:"""Serializes QuantumCircuit into string. Args: circuit: Qiskit QuantumCircuit object to serialize Returns: circuit encoded in string """buff=io.BytesIO()qpy.dump(circuit,buff)buff.seek(0)serialized_data=buff.read()buff.close()serialized_data=zlib.compress(serialized_data)returnbase64.standard_b64encode(serialized_data).decode("utf-8")
[docs]defregister_all_serializers():"""Registers all serializers."""# serialization for QiskitRuntimeServiceray.util.register_serializer(QiskitRuntimeService,serializer=service_serializer,deserializer=service_deserializer,)# serialization for QuantumCircuitray.util.register_serializer(QuantumCircuit,serializer=circuit_serializer,deserializer=circuit_deserializer)