Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.23k stars 2.36k forks source link

QuantumCircuit.draw(output='mpl') does not show circuit plots if called in a loop in Jupyter notebook #8280

Closed sgeaney-oqc closed 2 years ago

sgeaney-oqc commented 2 years ago

Informations

What is the current behaviour?

Generate some simple circuits in qiskit, put these circuits into a list and then in a for loop, try to draw each circuit. Snippet below: circ_inputs = [qc1, qc2, qc3, qc4] for i, circuit in enumerate(circ_inputs): circuit.draw(output='mpl')

Using the show() method in the loop: import matplotlib.pyplot as plt plt.show()

Does not resolve the issue as suggested in some qiskit issue posts.

Steps to reproduce the problem

  1. Create more than one quantum circuit in qiskit (four circuits were made in the example above).
  2. In a Jupyter notebook, use the QuantumCircuits.draw(output='mpl') method in a loop.
  3. Make sure to use the output='mpl' flag.
  4. Run the cell.
  5. Not plot will be produced. However, if one circuit is drawn out of the loop, it will be shown.

What is the expected behaviour?

In the above example, 4 plots should be shown, preferably one on top of the other.

Suggested solutions

May need a new implementation but returning the axes objects in some way from the 'QuantumCircuit' object method so that they can each be drawn in a loop.

Screengrab of a cell that has run some circuits in a loop but shows no plots.

enavarro51 commented 2 years ago

At least on the jupyter backend I use, you can wrap the draw command with the jupyter display command to see all 4 outputs. This works whether using 'mpl', 'text', or 'latex'.

display(circuit.draw('mpl'))
sgeaney-oqc commented 2 years ago

Yep, that seems to solve the issue. Thanks, @enavarro51!

Hussanulmaab commented 1 year ago

At least on the jupyter backend I use, you can wrap the draw command with the jupyter display command to see all 4 outputs. This works whether using 'mpl', 'text', or 'latex'.

display(circuit.draw('mpl'))

Thank you