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 hinton visualization bug #8447

Merged
merged 15 commits into from
Oct 19, 2022
Merged

fix hinton visualization bug #8447

merged 15 commits into from
Oct 19, 2022

Conversation

wyqian1027
Copy link
Contributor

@wyqian1027 wyqian1027 commented Aug 4, 2022

Summary

Fixes #8446, fixes #8324

Details and comments

I corrected
ax2.set_xlim([0, lx]) ax2.set_ylim([ly, 0])
and added
fig.tight_layout()

Tests are done on my computer to cover the change so it looks aligned:
Screenshot from 2022-08-04 11-25-29

Screenshot from 2022-08-04 11-26-03

And the spacing is fixed for a larger density matrix:
Screenshot from 2022-08-04 11-26-12

Screenshot from 2022-08-04 11-26-25

One thing is that now the diagonal of the density matrix runs from lower left to upper right, which is changed from the previous function, but it is just a style thing.

@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the the following people are requested to review this:

@CLAassistant
Copy link

CLAassistant commented Aug 4, 2022

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@1ucian0 1ucian0 added documentation Something is not clear or an error documentation Community PR PRs from contributors that are not 'members' of the Qiskit repo labels Aug 4, 2022
@HuangJunye HuangJunye self-assigned this Aug 4, 2022
@HuangJunye
Copy link
Collaborator

HuangJunye commented Aug 4, 2022

Thanks for submitting the PR for fixing the issue you found. As this is your first time contributing to Qiskit Terra, please have a look at the general Qiskit contributing guideline (especially the "Pull Request Checklist" section) and the CONTRIBUTING.md in Terra.

If you are unsure about anything, please feel free to ping me. Thanks!

@HuangJunye
Copy link
Collaborator

Can you please write a release note and sign the Contributor License Agreement?

@coveralls
Copy link

coveralls commented Aug 4, 2022

Pull Request Test Coverage Report for Build 3277537504

  • 7 of 7 (100.0%) changed or added relevant lines in 1 file are covered.
  • 117 unchanged lines in 5 files lost coverage.
  • Overall coverage decreased (-0.2%) to 84.602%

Files with Coverage Reduction New Missed Lines %
qiskit/extensions/quantum_initializer/squ.py 2 79.78%
src/sabre_swap/neighbor_table.rs 7 69.23%
src/sampled_exp_val.rs 14 63.93%
src/results/marginalization.rs 37 67.82%
src/optimize_1q_gates.rs 57 4.84%
Totals Coverage Status
Change from base Build 3276042896: -0.2%
Covered Lines: 61823
Relevant Lines: 73075

💛 - Coveralls

@wyqian1027
Copy link
Contributor Author

wyqian1027 commented Aug 4, 2022

Can you please write a release note and sign the Contributor License Agreement?

I think I have signed the Contributor License earlier today, but I am not sure about where to write a release note. Could you guide me through that? Thank you!

edits: Ok I see, I am trying to add the release note with reno now.
edits: Ok, I added release notes under releasenotes/notes/fix-hinton-bug-1141a297050f55bb.yaml

Copy link
Collaborator

@HuangJunye HuangJunye left a comment

Choose a reason for hiding this comment

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

Thanks for adding the release note. I left some comments for that.

Reading your original message on top, I realize the y-axis now is reverted to the normal direction (lower at the bottom, higher at the top). I am not sure about the original intention, but I believe there is a reason for the inverted direction. I will investigate that a bit.

Comment on lines 2 to 4
issues:
- |
Fix known bug in plot_state_hinton where axes are rendered improperly.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
issues:
- |
Fix known bug in plot_state_hinton where axes are rendered improperly.

This part is not needed. For PR that fixes an issue, you only need to write fixes section, as far as I know we don't use issues section that much in Qiskit. Thanks for reading through the description of different sections of the release note though.

I know writing release notes are not easy. I struggle with it all the time :D For the future, if you are unsure about this, you can read other examples in the release notes folder: https://github.com/Qiskit/qiskit-terra/tree/main/releasenotes/notes

Fix known bug in plot_state_hinton where axes are rendered improperly.
fixes:
- |
Fix known bug in plot_state_hinton. Refer to `#8446 <https://github.com/Qiskit/qiskit-terra/issues/8446>` for more details.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please describe the bug in a bit more details here? You can link to the issue but it's good to have a quick summary here. If you write :func: ~Qiskit.tools.visualization.plot_state_hinton, when the release notes are rendered to the webpage, it would automatically link to the function documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the help. I removed the "issues" section and added more details to "fixes".

@HuangJunye
Copy link
Collaborator

I corrected the ylim part which was a mistake introduced same PR that caused the issue this PR (8447) is trying to fix by #4811.

4811 mistakenly did ax1.set_ylim([ly, 0]) twice which basically cancel each other. And ax1.invert_yaxis() inverts the axis. On the other hand, ylim of ax2 was not set, and ax2.invert_yaxis() inverts the axis. The end results is that both y axis are inverted which are the desired plotting method.

When @wyqian1027 corrected ax1.set_ylim([ly,0]) to ax2.set_ylim([ly,0]), the inverted ylim and the invert axis cancel each other, that's why the y axis was back to normal order (lower values at the bottom)

HuangJunye
HuangJunye previously approved these changes Aug 9, 2022
Copy link
Collaborator

@HuangJunye HuangJunye 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 a lot for the fix. Congrats for getting your first PR merged! Hope you can continue making contributions to Qiskit. If you need any help in the future, feel free to reach out to me on GitHub or Qiskit Slack.

@wyqian1027
Copy link
Contributor Author

LGTM. Thanks a lot for the fix. Congrats for getting your first PR merged! Hope you can continue making contributions to Qiskit. If you need any help in the future, feel free to reach out to me on GitHub or Qiskit Slack.

Thank you! I will do my best.

@HuangJunye
Copy link
Collaborator

Looks like CI failed due to server errors. Can we trigger the CI to run again? @mtreinish

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

I'm concerned about the change in ordering the outputs here. It's not simply a style choice (and even if it were, we already picked a convention, and it's not good for us to change that without a very good reason). The original form makes the Hinton plot resemble the standard matrix order, which makes it easier to read.

@HuangJunye
Copy link
Collaborator

@jakelishman I reverted the y-axis direction in 4b43d59. It should be the same as the original direction.

@HuangJunye
Copy link
Collaborator

Related to that, currently the hinton plot seems to plot the matrix transversely. Is that the expected behaviour?

@HuangJunye
Copy link
Collaborator

@jakelishman

I'm concerned about the change in ordering the outputs here. It's not simply a style choice (and even if it were, we already picked a convention, and it's not good for us to change that without a very good reason). The original form makes the Hinton plot resemble the standard matrix order, which makes it easier to read.

I reverted the y-axis direction in 4b43d59. It should be the same as the original direction. Do you think the PR is good for merging?

HuangJunye and others added 4 commits August 23, 2022 09:50
The state labels in `plot_state_hinton` did not align with the correct
matrix entries.  This also updates the plot to display entries in the
natural "matrix" ordering, i.e. the same way that Numpy would display
the matrix if directly printed.  Previously, it was effectively a
transpose of what it should have been, with incorrect labels.
Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Sorry for the major delay here!

I fixed up the bit labels to match up with Qiskit's conventions, and so that DensityMatrix.from_label(...) will now look "the same" whether you print out its data matrix, or plot it on the Hinton plot. For example:

In [1]: from qiskit.visualization import plot_state_hinton
   ...:
   ...: arr = np.array([[1, 0, 0, 0], [0, 0, np.exp(1j * np.pi/4), 0], [0, np.exp(1j * 5*np.pi/4), 0, 0], [0, 0, 0, 1]])
   ...: plot_state_hinton(arr)
   ...: arr
Out[1]:
array([[ 1.        +0.j        ,  0.        +0.j        ,  0.        +0.j        ,  0.        +0.j        ],
       [ 0.        +0.j        ,  0.        +0.j        ,  0.70710678+0.70710678j,  0.        +0.j        ],
       [ 0.        +0.j        , -0.70710678-0.70710678j,  0.        +0.j        ,  0.        +0.j        ],
       [ 0.        +0.j        ,  0.        +0.j        ,  0.        +0.j        ,  1.        +0.j        ]])

now gives

Figure_1

Previously, the squares in the middle would be transposed relative to how the matrix prints, which is confusing (but what Matplotlib's documentation does, at least at the time of writing, which is where this function originally came from).

@jakelishman jakelishman added stable backport potential The bug might be minimal and/or import enough to be port to stable automerge mod: visualization qiskit.visualization and removed documentation Something is not clear or an error documentation labels Oct 18, 2022
@jakelishman
Copy link
Member

@Mergifyio requeue

@mergify
Copy link
Contributor

mergify bot commented Oct 18, 2022

requeue

❌ This pull request head commit has not been previously disembarked from queue.

@jakelishman
Copy link
Member

@Mergifyio refresh

@mergify
Copy link
Contributor

mergify bot commented Oct 18, 2022

refresh

✅ Pull request refreshed

@mergify mergify bot merged commit a53fc9a into Qiskit:main Oct 19, 2022
mergify bot pushed a commit that referenced this pull request Oct 19, 2022
* fix hinton visualization bug

* added release note for fix-hinton-bug

* remove issues and add more details

* correct set ylim

* Fix display and labelling of Hinton plot

The state labels in `plot_state_hinton` did not align with the correct
matrix entries.  This also updates the plot to display entries in the
natural "matrix" ordering, i.e. the same way that Numpy would display
the matrix if directly printed.  Previously, it was effectively a
transpose of what it should have been, with incorrect labels.

* Fixup release note

Co-authored-by: Junye Huang <h.jun.ye@gmail.com>
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit a53fc9a)
mergify bot added a commit that referenced this pull request Oct 19, 2022
* fix hinton visualization bug

* added release note for fix-hinton-bug

* remove issues and add more details

* correct set ylim

* Fix display and labelling of Hinton plot

The state labels in `plot_state_hinton` did not align with the correct
matrix entries.  This also updates the plot to display entries in the
natural "matrix" ordering, i.e. the same way that Numpy would display
the matrix if directly printed.  Previously, it was effectively a
transpose of what it should have been, with incorrect labels.

* Fixup release note

Co-authored-by: Junye Huang <h.jun.ye@gmail.com>
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit a53fc9a)

Co-authored-by: WQ <42972544+wyqian1027@users.noreply.github.com>
@HuangJunye
Copy link
Collaborator

@jakelishman Thanks for following up and checking it thoroughly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community PR PRs from contributors that are not 'members' of the Qiskit repo mod: visualization qiskit.visualization stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
Status: Done
Status: Done
Development

Successfully merging this pull request may close these issues.

plot_state_hinton axis is misplaced. Plot_hinton imaginary section displays incorrect elements
7 participants