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

Add display of expressions to circuit drawers #10869

Merged
merged 45 commits into from
Oct 19, 2023

Conversation

enavarro51
Copy link
Contributor

@enavarro51 enavarro51 commented Sep 20, 2023

Summary

Adds expressions to mpl and text drawers

Details and comments

Close #10236

This PR adds displays of expressions to ControlFlowOps in the mpl and text circuit drawers. The expressions are parsed using the QASM3 parser, so they appear the same as they would in a QASM3 string output.

Some of the changes in #10804 are incorporated in this PR, so that PR should be merged first.

qr = QuantumRegister(3, "qr")
cr = ClassicalRegister(3, "cr")
cr1 = ClassicalRegister(3, "cr1")
cr2 = ClassicalRegister(3, "cr2")
cr3 = ClassicalRegister(3, "cr3")
circuit = QuantumCircuit(qr, cr, cr1, cr2, cr3)

circuit.h(0)
with circuit.if_test(expr.equal(expr.bit_and(cr1, expr.bit_and(cr2, cr3)), 3)):
    circuit.z(0)

circuit.draw()

image

       ┌───┐┌─────────────────────────────── ┌───┐ ───────┐ 
 qr_0: ┤ H ├┤ If-0 (cr1 & (cr2 & cr3)) == 3  ┤ Z ├  End-0 ├─
       └───┘└───────────────╥─────────────── └───┘ ───────┘ 
 qr_1: ─────────────────────╫───────────────────────────────
                            ║                               
 qr_2: ─────────────────────╫───────────────────────────────
                            ║                               
 cr: 3/═════════════════════╬═══════════════════════════════
                        ┌───╨────┐                          
cr1: 3/═════════════════╡ [expr] ╞══════════════════════════
                        ├───╨────┤                          
cr2: 3/═════════════════╡ [expr] ╞══════════════════════════
                        ├───╨────┤                          
cr3: 3/═════════════════╡ [expr] ╞══════════════════════════
                        └────────┘                          

@enavarro51 enavarro51 requested review from nonhermitian and a team as code owners September 20, 2023 16:28
@qiskit-bot
Copy link
Collaborator

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

@coveralls
Copy link

Pull Request Test Coverage Report for Build 6252135361

  • 70 of 116 (60.34%) changed or added relevant lines in 2 files are covered.
  • 8 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.03%) to 87.281%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/visualization/circuit/text.py 47 49 95.92%
qiskit/visualization/circuit/matplotlib.py 23 67 34.33%
Files with Coverage Reduction New Missed Lines %
qiskit/visualization/circuit/text.py 1 95.39%
qiskit/visualization/circuit/matplotlib.py 3 49.43%
crates/qasm2/src/lex.rs 4 91.41%
Totals Coverage Status
Change from base Build 6250386385: 0.03%
Covered Lines: 74383
Relevant Lines: 85222

💛 - Coveralls

@enavarro51
Copy link
Contributor Author

@jakelishman This PR has now been merged with main and conflicts resolved. Should be ready to go.

@jakelishman jakelishman added this to the 0.45.0 milestone Oct 4, 2023
@jakelishman jakelishman added the Changelog: New Feature Include in the "Added" section of the changelog label Oct 10, 2023
@jakelishman jakelishman self-assigned this Oct 10, 2023
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 pushed up a minor tweak to some bits of documentation to make things easier. I had one (very minor) inline question about a magic number, and my other thing is: could we add a test or two to each of the text and MPL drawers that conditionals in nested control-flow scopes work correctly?

@@ -1543,6 +1584,17 @@ def _flow_op_gate(self, node, node_data, glob_data):
clip_on=True,
zorder=PORDER_FLOW,
)
self._ax.text(
xpos - x_shift - 0.08,
Copy link
Member

Choose a reason for hiding this comment

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

Should this 0.08 be spacer?

@enavarro51
Copy link
Contributor Author

In straightening out the mpl spacing, I found that for loops and case statements with only 1 qarg were printing outside the box in mpl. Also I redid display of the for loop range to fit for 1 qarg cases. Added tests for all this plus the nested expr's in both text and mpl.

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.

Wonderful, thank you very very much!

@jakelishman jakelishman added this pull request to the merge queue Oct 19, 2023
Merged via the queue into Qiskit:main with commit 19862cc Oct 19, 2023
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog mod: visualization qiskit.visualization
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for visualising Expr values in control flow
4 participants