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 internal circuits for ControlFlowOps to mpl circuit drawer #10207

Merged
merged 82 commits into from
Jul 20, 2023

Conversation

enavarro51
Copy link
Contributor

@enavarro51 enavarro51 commented Jun 5, 2023

Summary

This PR adds display of the circuits inside ForLoopOps and SwitchCaseOps to the mpl circuit drawer

Details and comments

This PR should be merged after #10096 and #10170. This adds the display of ForLoopOps and SwitchCaseOps in the same manner as was done in #10170 for IfElseOps and WhileLoopOps. This also fixes a bug in how gates were folding introduced in #10170.

  • Add tests
  • Release note
qr = QuantumRegister(4, "q")
cr = ClassicalRegister(3, "cr")
qc = QuantumCircuit(qr, cr)
qc.h(0)
with qc.for_loop((2, 4, 8, 16)) as i:
    qc.h(0)
    qc.h(1)
    qc.cx(0, 1)
    qc.rx(pi/i, 1)
    qc.measure(0, 0)
    qc.measure(1, 1)
    with qc.if_test((cr[2], 1)):
        qc.z(0)
qc.draw(style={"showindex": True})

image

from qiskit.circuit import QuantumCircuit, ClassicalRegister, QuantumRegister
qreg = QuantumRegister(3)
creg = ClassicalRegister(3)
qc = QuantumCircuit(qreg, creg)
qc.h([0, 1, 2])
qc.measure([0, 1, 2], [0, 1, 2])

with qc.switch(creg) as case:
    with case(0):
        qc.x(0)
    with case(1):
        qc.z(1)
        qc.z(1)
    with case(2, 3, 4, 5, 6):
        qc.y(0)
        qc.y(0)
        qc.y(0)
    with case(case.DEFAULT):
        qc.cx(0, 1)
qc.h(0)
qc.draw(style={"showindex": True})

image

@jakelishman jakelishman added this to the 0.25.0 milestone Jul 6, 2023
@jakelishman jakelishman self-assigned this Jul 6, 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.

Thanks so much for this Edwin. I've left a couple of minor comments in line, but mostly I'm judging by the test output for this, and it looks fantastic. I think it might be one of those things where we need to put it out in the wild, and see if other people find any issues with visualisation.

qiskit/visualization/circuit/_utils.py Outdated Show resolved Hide resolved
qiskit/visualization/circuit/matplotlib.py Outdated Show resolved Hide resolved
qiskit/visualization/circuit/matplotlib.py Outdated Show resolved Hide resolved
qiskit/visualization/circuit/matplotlib.py Outdated Show resolved Hide resolved
qiskit/visualization/circuit/matplotlib.py Outdated Show resolved Hide resolved
qiskit/visualization/circuit/matplotlib.py Outdated Show resolved Hide resolved
qiskit/visualization/circuit/matplotlib.py Show resolved Hide resolved
qiskit/visualization/circuit/matplotlib.py Outdated Show resolved Hide resolved
@enavarro51
Copy link
Contributor Author

I added a test using textbook style in order to test the bug I found with text color.

@mtreinish mtreinish added the Changelog: New Feature Include in the "Added" section of the changelog label Jul 20, 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.

Thanks so much for this, Edwin - I know it was a huge amount of work, both in design and actual execution, and I really appreciate it! These look fantastic, and I'm really keen to get them into the hands of our users, so we've got better ways to actually understand what's going on in a dynamic circuit.

@jakelishman jakelishman added this pull request to the merge queue Jul 20, 2023
Merged via the queue into Qiskit:main with commit 9a78e11 Jul 20, 2023
to24toro pushed a commit to to24toro/qiskit-terra that referenced this pull request Aug 3, 2023
…awer (Qiskit#10207)

* Remove deprecated args from mpl drawer

* Reno mod

* Convert to wire_map, node_data, etc

* Lint

* Minor changes to match flow changes

* Cleanup

* Fix layer width per node and comments

* Lint

* Adjust layer num loading

* Revert n_lines

* Lint

* Add glob_data

* Lint and cleanup

* Initial PR for if-else

* Unused arg

* Lint

* Iniital box fold

* Fold flow boxes

* Add reno

* Fix initial mpl and window load

* Move figure, mpl, and style info to draw()

* Comments

* Change font vars and fix ax

* Make patches_mod global

* Minor fixes after pre merege

* Lint

* Add while

* Add for loop

* Finish SwitchCase and cleanup and utils gate_span

* Cleanup

* Fix gate_span bug and comments

* Fix layer bug by using all qubits in _get_layered

* Added final release note and deleted old one

* Minor spacing changes

* Add ipynb/mpl/circuit tests and references

* Lint

* Minor comments

* Adjust for_loop test

* Add test for IfElseOp with body

* Lint

* Update state_city image

* Comments, PORDER changes, cleanup

* Adjust MASK zorder

* Fix text color issue with other styles

* Final fixes
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants