Source code for qiskit_serverless.serializers.program_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.program_serializers`)=======================================================================.. currentmodule:: qiskit_serverless.serializers.program_serializersQiskit Serverless program serializers======================================.. autosummary:: :toctree: ../stubs/ QiskitObjectsDecoder QiskitObjectsEncoder"""importjsonimportosfromtypingimportAny,Dictfromqiskit.primitivesimportSamplerResult,EstimatorResultfromqiskit_ibm_runtimeimportQiskitRuntimeServicefromqiskit_ibm_runtime.utils.jsonimportRuntimeDecoder,RuntimeEncoderclassQiskitObjectsEncoder(RuntimeEncoder):"""Json encoder for Qiskit objects."""defdefault(self,obj:Any)->Any:ifisinstance(obj,QiskitRuntimeService):return{"__type__":"QiskitRuntimeService","__value__":obj.active_account(),}ifisinstance(obj,SamplerResult):return{"__type__":"SamplerResult","__value__":{"quasi_dists":obj.quasi_dists,"metadata":obj.metadata},}ifisinstance(obj,EstimatorResult):return{"__type__":"EstimatorResult","__value__":{"values":obj.values,"metadata":obj.metadata},}returnsuper().default(obj)classQiskitObjectsDecoder(RuntimeDecoder):"""Json decoder for Qiskit objects."""defobject_hook(self,obj:Any)->Any:if"__type__"inobj:obj_type=obj["__type__"]ifobj_type=="QiskitRuntimeService":returnQiskitRuntimeService(**obj["__value__"])ifobj_type=="SamplerResult":returnSamplerResult(**obj["__value__"])ifobj_type=="EstimatorResult":returnEstimatorResult(**obj["__value__"])returnsuper().object_hook(obj)returnobj
[docs]defget_arguments()->Dict[str,Any]:"""Parses arguments for program and returns them as dict. Returns: Dictionary of arguments. """arguments="{}"ifos.path.isfile("arguments.serverless"):withopen("arguments.serverless","r",encoding="utf-8")asf:arguments=f.read()returnjson.loads(arguments,cls=QiskitObjectsDecoder)