-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Inconsistent ordering of marginal counts #6230
Comments
@nonhermitian |
Yeah, I am not disputing that is the way it has been. More that it doesn't make much sense to do an implicit conversion like this, where the ordering specified by the user is ignored. However, It is easy enough to write ones own marginal counts function so I am also fine if this gets close as expected behavior. |
Yes the fact that The other thing I noticed was that the docstring is confusing a bit. It says:
The bold parts to me indicate that the indices you give are the ones ignored (tossed out), but in reality those are the indices that are kept. This wording should be fixed too. |
HI, Can I pick this issue? if yes please assign it to me, I would love to work on. |
Hi @AshwinKul28 sure go for it! Assigning to you 😄 |
@AshwinKul28 @javabster Can i take this issue over? |
@AshwinKul28 are you still working on this? If not please let us know so I can assign to @mattwright99 😄 |
Hi @mattwright99 it looks like @AshwinKul28 is MIA so I've reassigned to you, let us know if you have any questions 😄 |
@javabster Sounds good, thanks! @nonhermitian I just wanted to double check before I start working on this issue but it seems that the fix here is essentially clearifying some documentation and setting the expected type to be a |
That is the fix that @ajavadia suggested yes. |
@nonhermitian I found a new bug when calling
This won't be a difficult fix so can I just include it in the PR for this ticket? I think the best way to fix this would be to just return the |
I arrived here after spending an embarrassing amount of time trying to figure out why my code using Regarding the notion that the current behavior is acceptable since one can write one's own marginal_counts function if one wants a different functionality, maybe it's worth elaborating a little bit more on how I arrived here. I was overhauling and expanding on some code written by someone else, and as part of that effort, I discovered that they'd effectively written their own marginal_counts function. I said to myself "this is silly -- Qiskit provides this function, so I'll simplify things by using that." I missed the subtle difference in behavior between the two, and because I considered this simplification to be a trivial change amidst a much larger re-working of code, it took me quite a while to get to the bottom of the problem. To me this illustrates the danger in encouraging users to write functions that are substantially the same as ones provided by Qiskit but with a "minor" twist. It is nicer to just make the built-in functions work the way users want, especially when doing so is trivial and would not require any significant breaking changes. In particular, if there is a concern that folks out there are really passing in unsorted lists to If no one objects, I would be happy to take a stab at writing the code I proposed in the previous paragraph. |
Yeah this sounds good to me @dtmcclure |
This commit re-implements the marginal counts function in rust for performance at the same time it makes the order of the bit indices significant for the purpose of permuting the bits while marginalizing. Fixes Qiskit#6230
This commit adds a new function marginal_distribution which performs marginalization similar to the existing marginal_counts() function. This new function however differs in a few key ways. Most importantly the order of the bit indices is significant for the purpose of permuting the bits while marginalizing, while marginal_counts() doesn't do this. Additionally, this function only works with dictionaries and not Result objects and is written in Rust for performance. While the purposed of this function is mostly identical to marginal_counts(), because making the bit indices order significant is a breaking change this was done as a separate function to avoid that. Once this new function is released we can look at deprecated and eventually removing the existing marginal_counts() function. Fixes Qiskit#6230
@dtmcclure I hope you don't mind, but I just pushed up #8026 to do this, I was looking at speeding up the function generally anyway and it was easy to do fix this at the same time. For backwards compatibility reasons I opted to make the version that respects the index order a separate function because changing that in place would be a breaking api change that could disrupt existing users (it breaks the ignis measurement mitigation tests). We can look at deprecating and removing the old function after the new one is included in released (per the deprecation policy). |
* Add a marginal_distribution function This commit adds a new function marginal_distribution which performs marginalization similar to the existing marginal_counts() function. This new function however differs in a few key ways. Most importantly the order of the bit indices is significant for the purpose of permuting the bits while marginalizing, while marginal_counts() doesn't do this. Additionally, this function only works with dictionaries and not Result objects and is written in Rust for performance. While the purposed of this function is mostly identical to marginal_counts(), because making the bit indices order significant is a breaking change this was done as a separate function to avoid that. Once this new function is released we can look at deprecated and eventually removing the existing marginal_counts() function. Fixes #6230 * Fix typos in python function * Handle missing indices In case a bit string has stripped leading zeros this commit adds handling to treat a missing index from the bit string as a '0'. * Apply suggestions from code review Co-authored-by: Jake Lishman <[email protected]> * Remove unecessary rust index * Update test Co-authored-by: Jake Lishman <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
* Add a marginal_distribution function This commit adds a new function marginal_distribution which performs marginalization similar to the existing marginal_counts() function. This new function however differs in a few key ways. Most importantly the order of the bit indices is significant for the purpose of permuting the bits while marginalizing, while marginal_counts() doesn't do this. Additionally, this function only works with dictionaries and not Result objects and is written in Rust for performance. While the purposed of this function is mostly identical to marginal_counts(), because making the bit indices order significant is a breaking change this was done as a separate function to avoid that. Once this new function is released we can look at deprecated and eventually removing the existing marginal_counts() function. Fixes Qiskit#6230 * Fix typos in python function * Handle missing indices In case a bit string has stripped leading zeros this commit adds handling to treat a missing index from the bit string as a '0'. * Apply suggestions from code review Co-authored-by: Jake Lishman <[email protected]> * Remove unecessary rust index * Update test Co-authored-by: Jake Lishman <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Information
What is the current behavior?
marginal_counts
ordering does not obey a standard convention when returning its output. For example, consider the counts:using
indices = [0,1,2,3,4]
give the identity mapping, suggesting that the 0th index in the indices list maps the input counts bit to the least significant bit in the new marginal counts, the next least significant is set by the 1st index value etc. However, this does not hold true in general. Namelyindices = [0,1,2,4,3]
does not follow the prescribed convention and returns:whereas one would expect the following:
i.e. the output bits are in the order q3q4q2q1q0
I believe it is due to the line: https://github.com/Qiskit/qiskit-terra/blob/40ffd2b07c03e220a174b2bfd0bf8c23aa021c5f/qiskit/result/utils.py#L111
that implicitly reorders any permutation of indices. This is mentioned no where in the docs, and is confusing
Steps to reproduce the problem
What is the expected behavior?
I would expect the order of indices given to have a one to one mapping to those in the output.
Suggested solutions
The text was updated successfully, but these errors were encountered: