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 a bug in QuantumCircuit.draw related to vertical_compression #9855

Merged
merged 6 commits into from
Apr 3, 2023

Conversation

rainij
Copy link
Contributor

@rainij rainij commented Mar 26, 2023

Summary

When using QuantumCircuit.draw('text') there was a wrong character used for drawing boxes in case of vertical_compression="low". I fixed this bug by a oneliner and updated the tests. I did not find a corresponding open issue.

Details and comments

The bug is appears in this example:

from qiskit import QuantumCircuit

qc = QuantumCircuit(2)
qc.h(0)
qc.h(1)

drawing_high = qc.draw("text", vertical_compression="high")
drawing_normal = qc.draw("text")
drawing_low = qc.draw("text", vertical_compression="low")

# Skip drawing_normal since it leads to the same result as drawing_high
print(drawing_high)
print(drawing_low)

The "high"-circuit is drawn correctly, but the second Hadamard of the seconed circuit is drawn incorrectly. It incorrectly uses the character for the upper right corner of the Box of the Gate.

     ┌───┐
q_0: ┤ H ├
     ├───┤
q_1: ┤ H ├
     └───┘
     ┌───┐
q_0: ┤ H ├
     └───┘
     ┌───┤
q_1: ┤ H ├
     └───┘

After the fix the output looks like this:

     ┌───┐
q_0: ┤ H ├
     ├───┤
q_1: ┤ H ├
     └───┘
     ┌───┐
q_0: ┤ H ├
     └───┘
     ┌───┐
q_1: ┤ H ├
     └───┘

Btw: I observed that the argument icod of the method merge_lines is somewhat "abused". Its docstring says icod (top or bot): in case of doubt, which line should have priority? Default: "top".. I am aware that I did not adhere to this statement. But My code change is consistent with other similar cases. See for example the case for the upper left corner which is treated in the same way. If somebody feels that this inconsistency is an issue I would propose to open an issue, but right now my change just fixes the bug in the simplest way possible without introducing more inconsistencies.

Tests: I actually found another kind of bug in the tests. The test class TestTextDrawerVerticalCompressionLow certainly wants to test the vertical_compression="low" feature. But two of those tests actually did not use low compression. I changed those to use low compression hence establishing two tests for my fix. I carefully checked that otherwise the output looks as before.

@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Mar 26, 2023
@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 Mar 26, 2023

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.

@rainij rainij marked this pull request as ready for review March 26, 2023 10:28
@rainij rainij requested review from a team and nonhermitian as code owners March 26, 2023 10:28
@rainij rainij marked this pull request as draft March 26, 2023 11:09
@rainij
Copy link
Contributor Author

rainij commented Mar 26, 2023

As far as I can see the two failing tests already fail on the main branch (and seem to be completely unrelated to what I did). I observed also a linter error in the file I updated but in line 647, far away from my change. I assume that the linter is run "lazily" in some way and some update on the linter side (in the past) triggered this now.

@rainij rainij marked this pull request as ready for review March 26, 2023 12:07
@rainij rainij force-pushed the fix-draw-text-vertical-compression branch from fd0c74e to bdd7589 Compare March 30, 2023 14:32
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.

Thanks for spotting this, and for the fix! Please could you add a one-line bugfix release note (reno new --edit fix-circuit-drawing-low-compression, then delete all but the "fixes" bit and edit) saying that you've fixed the top-right corner of low-compression circuit visualisations?

I didn't happen to see what the test failures were, but given the time you opened the PR, it probably coincided with the time that symengine version 0.10 had just released, which broke our CI temporarily.

@coveralls
Copy link

coveralls commented Mar 30, 2023

Pull Request Test Coverage Report for Build 4597914946

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • 313 unchanged lines in 16 files lost coverage.
  • Overall coverage increased (+0.03%) to 85.411%

Files with Coverage Reduction New Missed Lines %
qiskit/primitives/backend_estimator.py 1 95.37%
qiskit/primitives/backend_sampler.py 1 97.73%
qiskit/transpiler/passes/calibration/pulse_gate.py 1 95.45%
qiskit/transpiler/preset_passmanagers/level3.py 5 92.5%
qiskit/pulse/instructions/instruction.py 6 86.09%
qiskit/pulse/transforms/alignments.py 8 94.08%
qiskit/circuit/library/standard_gates/multi_control_rotation_gates.py 10 93.38%
qiskit/transpiler/passmanager.py 12 93.68%
qiskit/circuit/quantumcircuitdata.py 14 87.04%
qiskit/pulse/library/symbolic_pulses.py 16 94.79%
Totals Coverage Status
Change from base Build 4564463001: 0.03%
Covered Lines: 67422
Relevant Lines: 78938

💛 - Coveralls

@rainij
Copy link
Contributor Author

rainij commented Apr 1, 2023

@jakelishman I added the release note. Thanks for the info on symengine.

@rainij rainij requested a review from jakelishman April 1, 2023 11:16
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.

Super, thanks for this!

@jakelishman jakelishman added type: qa Issues and PRs that relate to testing and code quality Changelog: Bugfix Include in the "Fixed" section of the changelog mod: visualization qiskit.visualization labels Apr 3, 2023
@jakelishman jakelishman added this to the 0.24.0 milestone Apr 3, 2023
@jakelishman jakelishman added this pull request to the merge queue Apr 3, 2023
Merged via the queue into Qiskit:main with commit 751fa46 Apr 3, 2023
@rainij rainij deleted the fix-draw-text-vertical-compression branch April 3, 2023 16:27
giacomoRanieri pushed a commit to giacomoRanieri/qiskit-terra that referenced this pull request Apr 16, 2023
…kit#9855)

* Fix a bug in QuantumCircuit.draw related to vertical_compression

* Update tests so that they would catch the bug

* Fix black-errors

* Remove TODO comment

* Add release note

* Fixup release note

---------

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
king-p3nguin pushed a commit to king-p3nguin/qiskit-terra that referenced this pull request May 22, 2023
…kit#9855)

* Fix a bug in QuantumCircuit.draw related to vertical_compression

* Update tests so that they would catch the bug

* Fix black-errors

* Remove TODO comment

* Add release note

* Fixup release note

---------

Co-authored-by: Jake Lishman <jake.lishman@ibm.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 Community PR PRs from contributors that are not 'members' of the Qiskit repo mod: visualization qiskit.visualization type: qa Issues and PRs that relate to testing and code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants