Qiskit / qiskit-serverless

A programming model for leveraging quantum and classical resources
https://qiskit.github.io/qiskit-serverless/
Apache License 2.0
67 stars 27 forks source link

Client: `save_result` should support all types that `Program.arguments` support #492

Closed IceKhan13 closed 1 year ago

IceKhan13 commented 1 year ago

What is the expected enhancement?

Client: save_result should support all types that Program.arguments support.

Currently save_result only support dictionaries.

This can be achieved just by using encoder and decoder from program arguments.

yonMaor commented 1 year ago

Hi, I'd like to help with this. Are there any other details you can point out about this issue?

IceKhan13 commented 1 year ago

Hey @yonMaor ! Thank you for jumping in!

We have custom json decoder and encoder in serverless client here

https://github.com/Qiskit-Extensions/quantum-serverless/blob/7060fca8d0b662d80028fbfdf0b33fe4f2004a72/client/quantum_serverless/serializers/program_serializers.py#L38

and here

https://github.com/Qiskit-Extensions/quantum-serverless/blob/7060fca8d0b662d80028fbfdf0b33fe4f2004a72/client/quantum_serverless/serializers/program_serializers.py#L50

We are using them to encode and decode arguments to a program here https://github.com/Qiskit-Extensions/quantum-serverless/blob/7060fca8d0b662d80028fbfdf0b33fe4f2004a72/client/quantum_serverless/core/job.py#L171 or here

https://github.com/Qiskit-Extensions/quantum-serverless/blob/7060fca8d0b662d80028fbfdf0b33fe4f2004a72/client/quantum_serverless/serializers/program_serializers.py#L63-L69

We want to use those encoder/decoder in places where we are saving results of program and reading them back. In this function for saving results https://github.com/Qiskit-Extensions/quantum-serverless/blob/7060fca8d0b662d80028fbfdf0b33fe4f2004a72/client/quantum_serverless/core/job.py#L286 and in this function to get them back

https://github.com/Qiskit-Extensions/quantum-serverless/blob/7060fca8d0b662d80028fbfdf0b33fe4f2004a72/client/quantum_serverless/core/job.py#L245

Let me know if you need further clarification and if I need to assign this task to you.

Thank you!

yonMaor commented 1 year ago

In the issue description you refer to all of the types that the Program arguments support. I'm not sure that I understand what types you mean exactly

IceKhan13 commented 1 year ago

We have Program class that has constructor parameter arguments, which is python dictionary. This dictionary has type Dict[str, Union[<python_primitives>, QiskitRuntimeService, QuantumCircuit, np.ndarray, Operator, ...]]. This dictionary is serialized and passed as argument to execution. Serialization from dict to json format is happening with encoders and decoder.

Encoder and decoder have all types necessary.

You can check implementation here of QiskitObjectsEncoder or/and check parent class RuntimeEncoder https://github.com/Qiskit-Extensions/quantum-serverless/blob/7060fca8d0b662d80028fbfdf0b33fe4f2004a72/client/quantum_serverless/serializers/program_serializers.py#L38 Same with decoder :)

Only thing necessary here is use this encoder and decoder

yonMaor commented 1 year ago

Cool, I'll take a look and see if I have more questions. Thanks for the explanations

scottmason88 commented 1 year ago

I took a look at this issue since it's been a couple months since it was previously assigned. I think I have the solution and will be submitting a pull request soon.