4

I'm learning how to use qiskit and I'm using the jupyter notebook, but everytime I try to visualize the circuit with the attribute draw I get this error:

import qiskit
from qiskit import *
from qiskit import IBMQ
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
circuit = QuantumCircuit(qr, cr)
%matplotlib inline
circuit.draw(output='mpl')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-bd220039ee1c> in <module>
----> 1 circuit.draw(output='mpl')

AttributeError: module 'qiskit.circuit' has no attribute 'draw'

I also try applying a Hadamard gate and I get:

circuit.h(qr(0))
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-59-c8b4318b743b> in <module>
----> 1 circuit.h(qr(0))

AttributeError: module 'qiskit.circuit' has no attribute 'h'
SAR_90
  • 71
  • 7
  • This code seems to work for me, if as @luciano suggested it's an import problem, maybe try `from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit` instead of the imports you have now? – Frank Nov 06 '20 at 09:25
  • 1
    Also, instead of `circuit.h(qr(0))`, you will need square brackets to return the qubit, i.e. `circuit.h(qr[0])` – Frank Nov 06 '20 at 09:26

2 Answers2

0

It seems that there is a name conflict. It is taking the circuit in from qiskit import circuit instead of circuit = ....

You just probably need to restat your notebook kernel.

luciano
  • 399
  • 4
  • 13
  • Can you run just the example you posted in a fresh notebook? As @Frank, I'm unable to reproduce the problem – luciano Nov 06 '20 at 12:10
0

Try another name for your circuit variable, right now python thinks you want the qiskit.circuit module to draw something. QuantumCircuit objects are the ones that have a draw method. You can see these two objects here if you call both, note I put one qubit and classical bit in the QuantumCircuit just per example as well you do not need the dots here it is just to make it more clear, just running circuit and QuantumCircuit(1,1) respectively would yield the same result.

Difference between qiskit.circuit and qiskit.QuantumCircuit

You would get desired results if you tried a different variable name: Draw a Quantum Circuit

When I try using the variable name circuit it works for me, but try to use descriptive variable names that also could never be confused with modules or classes from the packages you import.

Also all your import statements can be combined into 1:

from qiskit import *

The star lets you import everything from qiskit including IBMQ. It can help you save a line or two.

Dulah
  • 66
  • 5