qiboteam / qibo-client

Qibo remote connection client.
https://qibo.science
Apache License 2.0
2 stars 1 forks source link

Add `initial_state` to `_post_circuit` #27

Closed MatteoRobbiati closed 7 months ago

codecov[bot] commented 7 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (463aa85) 100.00% compared to head (c15c2df) 100.00%. Report is 7 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #27 +/- ## ========================================= Coverage 100.00% 100.00% ========================================= Files 4 4 Lines 119 120 +1 ========================================= + Hits 119 120 +1 ``` | [Flag](https://app.codecov.io/gh/qiboteam/qibo-client/pull/27/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=qiboteam) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/qiboteam/qibo-client/pull/27/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=qiboteam) | `100.00% <100.00%> (ø)` | | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=qiboteam#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

scarrazza commented 7 months ago

The initial state is the first argument when executing a circuit (before the nshots option).

marcorossi5 commented 7 months ago

I see. How do you want to pass it to Django? JSON? If np.ndarray is an option you might want to serialize to bytes with pickle. My question is: is it safe to execute pickled objects received from http requests?

scarrazza commented 7 months ago

That is a good point, @MatteoRobbiati does ibmq supports this feature? Usually passing initial states are not possible for hardware execution. If this is the case we should not support this feature.

alecandido commented 7 months ago

If np.ndarray is an option you might want to serialize to bytes with pickle.

There is a safe (and portable) way to serialize NumPy arrays to bytes, i.e. np.save.

Unfortunately, it is writing to a file-like object, so you have to do a little bit of gymnastics to get it in memory (without passing through disk), but it's not terrible. It's something like:

import io

buffer = io.BytesIO()
np.save(buffer, myarray)
buffer.seek(0)
mybytes = buffer.read()

and io is standard library.

I didn't check it, but if that doesn't work should be something very close.

alecandido commented 7 months ago

Usually passing initial states are not possible for hardware execution.

For hardware, the only way would be to disassemble it as a circuit, isn't it?

scarrazza commented 7 months ago

Indeed, so given that the major goal of this project is to execute on quantum hardware, we should give up on this setup which might be even more complex, i.e. if a crazy user decides to send 1TB of state vector...

MatteoRobbiati commented 7 months ago

I agree with the direction you suggested. Together with @BrunoLiegiBastonLiegi we tested the Qiskit backend and it seems they adopt the "hardware-like" approach (circuits as input, frequencies as output). Fine with closing this and, then, we should just remove the initial_state argument from the qibo-cloud-backend/qibo_client.py.

scarrazza commented 7 months ago

Thanks @MatteoRobbiati for checking, please go ahead with changes in the qibo-cloud-backends.