rigetti / qcs-sdk-rust

Rust SDK for Rigetti Quantum Cloud Services (QCS)
https://docs.rs/qcs
13 stars 5 forks source link

Don't enforce contiguous memory #26

Open kalzoo opened 2 years ago

kalzoo commented 2 years ago

Currently, in qcs/src/qpu/mod.rs, organize_ro_sources, there's a block which enforces that readout targets contiguous memory regions starting from 0, that is, this program is invalid because it skips ro[0..6]:

DECLARE ro BIT[8]
MEASURE 7 ro[7]

However, this is both valid Quil and accepted by pyQuil and the QCS API. When executed by pyQuil, he ro_sources returned look like this:

ro_sources={<MRef q7_unclassified[0]>: 'q7_unclassified', <MRef ro[7]>: 'q7'}

and the execution result looks like this (note the uninitialized memory in indices 0..6:

readout_data={'ro': array([[-3458764513820540928, -3458755728090571410,  8314613781408776197, 7598826450156352046,    72057793442439982,   146087754952868096, 654311424, 0]])}

The assertion that this Rust SDK is making makes sense in terms of being able to iterate safely through the result, esp via C, but it cannot add constraints to the Quil language in order to do so. We could consider a map (probably a BTreeMap to preserve ordering for iteration) or a sparse Vec (maybe a Vec<Option<_>>) to store these sparse results.

dbanty commented 2 years ago

The trickier thing is going to be figuring out the C API for this. Do we need a function call for accessing each slot of data (e.g. get_data(result.handle, "ro", 7))? I feel like that would add significant overhead in the run -> get results -> tweak params -> run loops.

We could allocate the whole array and just fill in 0s for any values we didn't get? What does pyQuil do?

kalzoo commented 2 years ago

We could allocate the whole array and just fill in 0s for any values we didn't get? What does pyQuil do?

Yes, I believe that's what we should do, to present the facade of initialized-but-not-written memory being returned from execution. It appears that pyQuil allocates but does not initialize a new array, which I'd describe as an oversight and likely a bug.