Skip to content

Conversation

mtreinish
Copy link
Contributor

Summary

This commit updates the Qiskit marginalization function used in qiskit-experiments. Qiskit has 2 different function available for marginalizing counts: marginal_counts() [1] which is designed to work with Counts or Result objects and does not respect ordering of the indices passed in. The other is marginal_distribution() [2] which is designed to work with Counts, Probabilities, QuasiProbabilities, or a dictionary with string keys and integer or float values that respects the order of the indices passed in (so it can be used to permute the bits). In general if you're not working with a Result object it is better to use marginal_distribution() because it is written primarily in Rust and 10-100x faster than marginal_counts().

Details and comments

[1] https://qiskit.org/documentation/stubs/qiskit.result.marginal_counts.html
[2] https://qiskit.org/documentation/stubs/qiskit.result.marginal_distribution.html

This commit updates the Qiskit marginalization function used in
qiskit-experiments. Qiskit has 2 different function available for
marginalizing counts: `marginal_counts()` [1] which is designed to work
with `Counts` or `Result` objects and does **not** respect ordering of
the indices passed in. The other is `marginal_distribution()` [2] which is
designed to work with `Counts`, `Probabilities`, `QuasiProbabilities`, or
a dictionary with string keys and integer or float values that respects
the order of the indices passed in (so it can be used to permute the bits).
In general if you're not working with a `Result` object it is better to use
`marginal_distribution()` because it is written primarily in Rust and 10-100x
faster than `marginal_counts()`.

[1] https://qiskit.org/documentation/stubs/qiskit.result.marginal_counts.html
[2] https://qiskit.org/documentation/stubs/qiskit.result.marginal_distribution.html
@mtreinish
Copy link
Contributor Author

The other function which I thought we added for experiments but I didn't see anything that looked similar through some quick grepping is marginal_memory: https://qiskit.org/documentation/stubs/qiskit.result.marginal_memory.html#qiskit.result.marginal_memory which is used for marginalizing different results levels. If there is a need for that kind of marginalization using that qiskit function would be a good choice because it's also written in Rust and also multithreaded.

@mtreinish
Copy link
Contributor Author

mtreinish commented Apr 17, 2023

Oh interesting it's failing because the value type is np.int64 instead of a python int. That hasn't come up before, it's an easy fix on the qiskit-side to add support to this (especially because there isn't any type conversion needed in that case as int64->i64 directly). But in the meantime I'll just cast it to an int.

@nkanazawa1989 nkanazawa1989 self-assigned this Apr 17, 2023
Copy link
Collaborator

@nkanazawa1989 nkanazawa1989 left a comment

Choose a reason for hiding this comment

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

Thanks Matthew. This looks good to me. In my local profiling with a composite curve analysis for parallel 27 qubit Rabi experiment, this PR reduces total processing time from 15.0s to 9.26s (roughly 40% time reduction). This is awesome improvement.

Since Qiskit/qiskit#9976 is going to be merged, do you want to remove safety typecast and turn this into on hold until next Terra release? Otherwise I'll merge.

@mtreinish
Copy link
Contributor Author

I guess it depends on whether you think we should backport this for a potential 0.5.1 or not? If you want to include it for 0.5.1 and backport it we should leave it as is. But if you just want to save it for 0.6.0 lets just wait for the terra release when we can bump the minimum qiskit-terra version to 0.24.0 and remove the type cast.

@nkanazawa1989
Copy link
Collaborator

I think this should be released with 0.6 because this marginal_counts code has been existing from 0.1 (not a bugfix for 0.5). But this PR is a good motivation for us to cut next release soon :)

@coruscating coruscating added this to the Release 0.6 milestone Apr 18, 2023
@mtreinish mtreinish added the on hold On hold until something else is done. label Apr 18, 2023
@mtreinish
Copy link
Contributor Author

Ok, then I'll mark this as on hold then. I'll update this after the 0.24.0 release in a a couple of weeks to remove the type cast and update the minimum terra version to 0.24.0

@coruscating
Copy link
Collaborator

coruscating commented May 3, 2023

We decided to include this in the 0.5.2 release since users have been asking for more performance improvements on large experiments. Once Terra 0.24 is released and this is updated and merged I'll tag the patch release. Can you also add a release note when you update this @mtreinish?

@coruscating coruscating added the backport stable potential The issue or PR might be minimal and/or import enough to backport to stable label May 3, 2023
@mtreinish
Copy link
Contributor Author

I think for a patch release you probably don't want to bump the minimum qiskit-terra version for this (unless you need to for other reasons) just so you're not requiring people to bump their installed terra version to get the fixes in 0.5.2.

The key behavioral difference for the marginal_distribution() function
is that the input order of the bits of interest is significant and the
bits are passed in out of order this will permute the output from the
marginalization function. In the process tomography case the bits of
interest aren't always in order and if they're out of order the output
doesn't match the expected format. To correct this prior to calling
marginal_distribution() this commit sorts the bits of interest to ensure
the output order is as expected by the fitter.
@coruscating
Copy link
Collaborator

@mtreinish You're right, I got mixed up there, our decision was actually to include this in the 0.5.2 release without bumping the required terra version to 0.24. @nkanazawa1989 are you ok with merging this as is?

@nkanazawa1989 nkanazawa1989 removed the on hold On hold until something else is done. label May 10, 2023
Copy link
Collaborator

@nkanazawa1989 nkanazawa1989 left a comment

Choose a reason for hiding this comment

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

Please merge this for patch release. Once we bump terra version we should remove the typecast logic (it is another slight performance regression).

@coruscating coruscating added this pull request to the merge queue May 10, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 10, 2023
@coruscating coruscating added this pull request to the merge queue May 10, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 10, 2023
@coruscating coruscating added this pull request to the merge queue May 11, 2023
Merged via the queue into qiskit-community:main with commit 10b6182 May 11, 2023
mergify bot pushed a commit that referenced this pull request May 11, 2023
### Summary

This commit updates the Qiskit marginalization function used in
qiskit-experiments. Qiskit has 2 different function available for
marginalizing counts: `marginal_counts()` [1] which is designed to work
with `Counts` or `Result` objects and does **not** respect ordering of
the indices passed in. The other is `marginal_distribution()` [2] which
is designed to work with `Counts`, `Probabilities`,
`QuasiProbabilities`, or a dictionary with string keys and integer or
float values that respects the order of the indices passed in (so it can
be used to permute the bits). In general if you're not working with a
`Result` object it is better to use `marginal_distribution()` because it
is written primarily in Rust and 10-100x faster than
`marginal_counts()`.

### Details and comments

[1]
https://qiskit.org/documentation/stubs/qiskit.result.marginal_counts.html
[2]
https://qiskit.org/documentation/stubs/qiskit.result.marginal_distribution.html

(cherry picked from commit 10b6182)
@mtreinish mtreinish deleted the marginal-this-marginal-that branch May 11, 2023 02:54
coruscating pushed a commit that referenced this pull request May 11, 2023
) (#1166)

This is an automatic backport of pull request #1144 done by
[Mergify](https://mergify.com).


Co-authored-by: Matthew Treinish <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport stable potential The issue or PR might be minimal and/or import enough to backport to stable

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants