4

I am just starting with Quantum Computing and I tried the following code.

from qiskit import *

qr = QuantumRegister(2)
cr = ClassicalRegister(2)

Qc = QuantumCircuit(qr,cr)

print ('This is the initial state')
print(Qc.draw(output = 'mpl'))
print ('')

The problem I am facing is with drawing the circuit. When use qc.draw() it gives me a line representation of the circuit and its good enough for me to see what is on it. But when I put output='mpl' I am getting the following results.

This results in

This is the initial state
Figure(142.949x204.68)

I have been using spyder for all my programming but I also tried the same thing in jupyter and the result was the same. What is this Figure(.............) and where can I find it?

user2864740
  • 60,010
  • 15
  • 145
  • 220

2 Answers2

0

When you use 'mpl', the output is a figure and not a string that you can print. Remove print and try:

print ('This is the initial state')
Qc.draw('mpl').show()

The output is:

This is the initial state

output

akkh
  • 140
  • 1
  • 8
  • Hello, Thank you for the reply. I tired doing what you said and I got the following error: :9: UserWarning: Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure. qc.draw('mpl').show() – Utsav Niroula Sep 22 '21 at 05:01
  • Are you using Jupyter Notebooks? If yes, then remove .show() and let the last line of the cell be Qc.draw() – akkh Sep 22 '21 at 13:38
  • I am using Spyder. – Utsav Niroula Sep 22 '21 at 14:11
  • Try to use `pip install qiskit[visualization]`. If it still does not work, you might need to change the backend in Spyder settings. – akkh Sep 22 '21 at 17:13
0

Hey I tried this with a simple change in the draw command

from qiskit import *

qr = QuantumRegister(2)
cr = ClassicalRegister(2)

Qc = QuantumCircuit(qr,cr)

print ('This is the initial state')
Qc.draw(output = 'mpl')

Output:

enter image description here

If this didn't work try installing visualization tools

Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48