Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust DynamicsBackend level 2 result formatting to match BackendSamplerV2's expectations #373

Merged
merged 1 commit into from
Jan 24, 2025

Conversation

wshanks
Copy link
Contributor

@wshanks wshanks commented Jan 24, 2025

Qiskit Experiments 0.8.0 uses qiskit_ibm_runtime.SamplerV2 to execute circuits by default and that passes the execution to qiskit.primitives.BackendSamplerV2 for DynamicsBackend.

When BackendSamplerV2 converts the results from DynamicsBackend into the PubResult format, it uses the result memory in an if statement to check if it is empty. For a list that works but for a numpy array an exception is raised about needing to use all() to check the truth value of an array. Here the memory data is converted from numpy array into a list using tolist() before being passed into the result object. (Note: the function generating this numpy array has a type annotation stating that it returns a list, so this could be changed at that level of the stack instead but that might require changing other things expecting a numpy array).

After checking that the result memory is not empty, BackendSamplerV2 packs the state results into a BitArray and to do this it assumes that the memory results are formatted as hex strings but DynamicsBackend was returning binary strings with the format "10101". Here the strings are converted to hex. It is not clear to me if Qiskit specifies that one format is required or the other or if both should be valid (and BackendSamplerV2 really has the problem by not accepting the binary format). There could be downstream disruption if some code assumes DynamicsBackend returns the binary format. Additionally, does Dynamics support returning shots for higher levels up to 9? That would work the "binary" (really "decimal" at that point) format but forcing the casting to hex breaks it.

…lerV2's expectations

Qiskit Experiments 0.8.0 uses `qiskit_ibm_runtime.SamplerV2` to execute
circuits by default and that passes the execution to
`qiskit.primitives.BackendSamplerV2` for `DynamicsBackend`.

When `BackendSamplerV2` converts the results from `DynamicsBackend` into
the `PubResult` format, it uses the result `memory` in an if statement
to check if it is empty. For a list that works but for a numpy array an
exception is raised about needing to use `all()` to check the truth
value of an array. Here the memory data is converted from numpy array
into a list using `tolist()` before being passed into the result object.
(Note: the function generating this numpy array has a type annotation
stating that it returns a list, so this could be changed at that level
of the stack instead but that might require changing other things
expecting a numpy array).

After checking that the result memory is not empty, `BackendSamplerV2`
packs the state results into a `BitArray` and to do this it assumes that
the memory results are formatted as hex strings but `DynamicsBackend`
was returning binary strings with the format `"10101"`. Here the strings
are converted to hex. It is not clear to me if Qiskit specifies that one
format is required or the other or if both should be valid (and
`BackendSamplerV2` really has the problem by not accepting the binary
format). There could be downstream disruption if some code assumes
`DynamicsBackend` returns the binary format. Additionally, does Dynamics
support returning shots for higher levels up to 9? That would work the
"binary" (really "decimal" at that point) format but forcing the casting
to hex breaks it.
Copy link
Collaborator

@DanPuzzuoli DanPuzzuoli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @wshanks , looks good to me!

@DanPuzzuoli DanPuzzuoli merged commit e14af9d into qiskit-community:main Jan 24, 2025
2 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants