Closed alecandido closed 3 months ago
The fewer casts the better. So, a cast to NumPy from another framework should happen as late as possible, and explicitly by the user (not internally within the backend/Qibo)
Not to numpy
, but out of numpy
.
Not to
numpy
, but out ofnumpy
.
Uhm, I imagine samples to be computed by the backend. If the backend is a GPU one, they should be computed on the GPU, and the result should be a GPU array. In order to become NumPy, it had to be downloaded and cast. Then, if you want to reuse it on the GPU, you have to upload it back.
Replace GPU with PyTorch, and it should be the same.
Sorry if this is an off-topic but is there a standard way to dumpy shot data to files?
If no, then one could imagine a standard from qibo that each backend needs to implement and then there will be a standard way of loading and storing in files. Backends could be passing to each other in the same way and the samples
method could be the user-facing data array so e.g. np.array
while __samples
could be the backend-specific type and then samples
would be casting to numpy?
Sorry if this is an off-topic but is there a standard way to dumpy shot data to files?
np.save("shots.npy", backend.to_numpy(shots))
If no, then one could imagine a standard from qibo that each backend needs to implement and then there will be a standard way of loading and storing in files. Backends could be passing to each other in the same way and the
samples
method could be the user-facing data array so e.g.np.array
while__samples
could be the backend-specific type and thensamples
would be casting to numpy?
In any case, backends should not communicate to each other through disk, but through memory. And the common lingo among all backends is NumPy.
If you want to extract shots from one backend, and consume them somehow in another one, you first download with .to_numpy()
, and then cast in the other backend with .cast()
.
Passing through files (which most likely are on disk, but in any case require a file system to process them) would be much more expensive.
Thank you Ale, to_numpy
answers my question. (I didn't suggest to
interface via disk, rather there could be an internal method and then
samples
before returning could cast to numpy as you showed. Based on what
you said - why not have samples return always the ndarray in the shared
lingo?)
On Thu, 8 Aug 2024, 17:33 Alessandro Candido, @.***> wrote:
If no, then one could imagine a standard from qibo that each backend needs to implement and then there will be a standard way of loading and storing in files. Backends could be passing to each other in the same way and the samples method could be the user-facing data array so e.g. np.array while __samples could be the backend-specific type and then samples would be casting to numpy?
In any case, backends should not communicate to each other through disk, but through memory. And the common lingo among all backends is NumPy. If you want to extract shots from one backend, and consume them somehow in another one, you first download with .to_numpy(), and then cast in the other backend with .cast().
Passing through files (which most likely are on disk, but in any case require a file system to process them) would be much more expensive.
— Reply to this email directly, view it on GitHub https://github.com/qiboteam/qibo/issues/1416#issuecomment-2276119799, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABU6DWHTTT7WVJJSK5SD5ADZQOFVNAVCNFSM6AAAAABMGG4INOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENZWGEYTSNZZHE . You are receiving this because you commented.Message ID: @.***>
I didn't suggest to interface via disk, rather there could be an internal method and then
samples
before returning could cast to numpy as you showed.
Sorry, I didn't fully understand, and I wanted to make sure of that point 😅
Based on what you said - why not have samples return always the ndarray in the shared lingo?
I see two reasons:
.to_numpy()
, so you already have a way to get it - if instead you immediately returned NumPy, but you would have liked the original object, you should convert it back; i.e. breaking the operation in two steps is more modular and composable (since internally you have to go through the two steps anyhow)
Yes, it is due to
torch
. Without thecast
, theMeasurementOutcumes.samples
breaks fortorch
because it returns a list ofnp.ndarray
s and that breaks furthertorch
functions.torch
needs it to be a list oftorch.Tensor
s. I do believe that @stavros11's patch is the easiest, most painless way to solve this.Originally posted by @renatomello in https://github.com/qiboteam/qibo/issues/1414#issuecomment-2275006312
The fewer casts the better. So, a cast to NumPy from another framework should happen as late as possible, and explicitly by the user (not internally within the backend/Qibo)