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

Fix bit ordering and probabilities of samples in optimization_algorithm.py #97

Merged
merged 71 commits into from May 13, 2021

Conversation

a-matsuo
Copy link
Contributor

@a-matsuo a-matsuo commented Apr 22, 2021

Summary

  • Fix bit ordering issue and add unit tests.
  • Fix probabilities of samples.

Fixes #92

Details and comments

This PR should be merged after #96

Bit ordering

We need to reverse bit ordering from qiskit bit ordering to conventional bit ordering for the actual outputs.

Probabilities of samples

This PR fixes the following lines that calculate the probabilities of solutions.
When eigenvector is a dict, we need to square the values since the values are normalized.
https://github.com/Qiskit/qiskit-optimization/blob/724158ba42f4b75758edc1de42f6f665660ec27e/qiskit_optimization/algorithms/optimization_algorithm.py#L554-L557

I will explain it in details for the record.
Originally, when we were using Qiskit Aqua, this eigenvector was supposed to be the resultant counts of a qasm simulator. Thus, the above code was working correctly.
https://github.com/Qiskit/qiskit-aqua/blob/aa8bc0c8011405d382fc05d6ab576dd926bdd2a7/qiskit/aqua/algorithms/minimum_eigen_solvers/vqe.py#L578

However, after the code was moved into Qiskit terra, there was some bug and it was fixed in Qiskit/qiskit#5496 .
As a result, this eigenvector became the square root of the normalized counts, i.e. from {bitstr: counts} to {bitstr: np.sqrt(counts / shots)} .
https://github.com/Qiskit/qiskit-terra/blob/3c979ebdacecbbd213757a8149bddc309bfa58c0/qiskit/algorithms/minimum_eigen_solvers/vqe.py#L520

If we change the value of this eigenvector in Qiskit terra in the future, we need to change the above code in MinimumEigenSolvers as well.

@t-imamichi t-imamichi added the stable backport potential The bug might be minimal and/or import enough to be port to stable label Apr 22, 2021
@t-imamichi t-imamichi changed the title Fix bit ordering in optimization_alogirhtm.py Fix bit ordering in optimization_algorithm.py Apr 23, 2021
@t-imamichi
Copy link
Collaborator

We don't need to wait for #96 because CI builds Aer on Ubuntu.

@woodsp-ibm woodsp-ibm added the Changelog: Bugfix Include in the Fixed section of the changelog label Apr 23, 2021
@t-imamichi
Copy link
Collaborator

Could you add bit ordering test of QAOA just in case?

t-imamichi
t-imamichi previously approved these changes May 7, 2021
Copy link
Collaborator

@t-imamichi t-imamichi left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@t-imamichi t-imamichi changed the title Fix bit ordering in optimization_algorithm.py Fix bit ordering and probabilities of samples in optimization_algorithm.py May 7, 2021
@t-imamichi t-imamichi dismissed their stale review May 7, 2021 10:22

Need to wait for discussion #92

@woodsp-ibm
Copy link
Member

Does this in anyway depend on Qiskit/qiskit#6365 ? Since all the unit tests pass here I am guessing not, but that is another reversal of order right?

@a-matsuo
Copy link
Contributor Author

Does this in anyway depend on Qiskit/qiskit#6365 ? Since all the unit tests pass here I am guessing not, but that is another reversal of order right?

That's another reversal of order, but actually this does not depend on Qiskit/qiskit#6365. It's simply a bug caused by inconsistency. Currently, oracle_evaluation in PhaseOracle supports the conventional bit order, so I'm planning to change it to support the qiskit bit order in Qiskit/qiskit#6365.

t-imamichi
t-imamichi previously approved these changes May 12, 2021
@t-imamichi t-imamichi self-requested a review May 13, 2021 02:16
@t-imamichi t-imamichi merged commit 4bce259 into qiskit-community:main May 13, 2021
mergify bot pushed a commit that referenced this pull request May 13, 2021
…hm.py (#97)

* Fix bit ordering of `MinimumEigenOptimizer` with qasm_simulator.
* Fix probabilities of solution samples with qasm_simulator.

Co-authored-by: Takashi Imamichi <31178928+t-imamichi@users.noreply.github.com>
Co-authored-by: Takashi Imamichi <t.imamichi@gmail.com>
Co-authored-by: Manoel Marques <manoel.marques@ibm.com>
Co-authored-by: Takashi Imamichi <imamichi@jp.ibm.com>
(cherry picked from commit 4bce259)

# Conflicts:
#	qiskit_optimization/algorithms/grover_optimizer.py
#	qiskit_optimization/algorithms/optimization_algorithm.py
#	test/algorithms/test_grover_optimizer.py
#	test/algorithms/test_min_eigen_optimizer.py
t-imamichi added a commit that referenced this pull request May 16, 2021
* cherry-pick 4bce259

* cherry-pick b0d411a

* fix style

Co-authored-by: a-matsuo <47442626+a-matsuo@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the Fixed section of the changelog stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MinimumEigenOptimizer returns a wrong solution
4 participants