Qiskit / qiskit-metapackage

Qiskit is an open-source SDK for working with quantum computers at the level of circuits, algorithms, and application modules.
https://qiskit.org
Apache License 2.0
3.03k stars 749 forks source link

.draw(output = 'mpl') always produces two plots in Jupyter #1265

Closed Mdcrab02 closed 3 years ago

Mdcrab02 commented 3 years ago

Information

What is the current behavior?

In the version I have installed, the .draw(output = 'mpl') method always produces two plots in Jupyter notebook. Previous versions did not do this.

The normal .draw() method with no parameters behaves as expected.

Steps to reproduce the problem

# Create a quantum circuit the old fashioned way.  Make the regesters and pass them into QuantumCircuit()
quant_reg = qs.QuantumRegister(1)
class_reg = qs.ClassicalRegister(1)

# Create the circuit
quant_circuit = qs.QuantumCircuit(quant_reg, class_reg)

# Add an X gate with .x()
quant_circuit.x(0)

# Draw the circuit with matplotlib
quant_circuit.draw(output = 'mpl')

If you remove output = 'mpl' from the .draw() method, it produces one plot as expected.

What is the expected behavior?

I'd expect that only one plot be produced.

Suggested solutions

divshacker commented 3 years ago

Hey I have ran your code in local and cloud machines and I didn't find any two outputs when running this code, Can you please post the output clarifying your issue

singhmeet11 commented 3 years ago

Yeah same, I did not get two images. Plus I would like to add I have used output = 'mpl' a few times before and have never run into this problem. Is it possible that there is something wrong with your matplotlib. Maybe try running this code on IBMQ and then see how many images you get.

Mdcrab02 commented 3 years ago

I have recreated a second qiskit environment and added only the bare minimum packages. This is the output I always get in Jupyter notebook and Jupyter lab.

qiskit_draw_error_output_1

Mdcrab02 commented 3 years ago

It looks like there's a possibly related issue in Qiskit/qiskit-tutorials here 1180

divshacker commented 3 years ago

Now Here is the explanation according to me. In the new update of 0.25 the default style .draw() is changed from text to 'mpl'. There is a separate config file made to change the default settings in qiskit. So if you use %matplotlib inline and .draw(output='mpl') then it will use the matplotlib representation twice and will end up with two plots. If you don't use anything then by default run .draw() as .draw('mpl). So I think if you remove every thing and use only draw() then it will fix your problem

Mdcrab02 commented 3 years ago

Thanks for the help @divshacker it turns out %matplotlib inline is indeed part of the issue. I commented that out of the code and it now only shows one graph as expected.