"AttributeError: 'PauliZ' object has no attribute 'draw' "

Hi.
i have this circuit function:

def circuit(x):
    
    qml.templates.MottonenStatePreparation(x, wires=[1, 2, 3])
    qml.Hadamard(wires=0)

    return qml.expval(qml.PauliZ(0))

i am going to draw the circuit only for a sample of my data with this command

print(circuit(x[0]).draw())

i got this error:

"AttributeError: 'PauliZ' object has no attribute 'draw' "

Hi @sassan_moradi!

It looks like you are missing the QNode decorator β€” this converts your python function circuit into a quantum function, that executes on a quantum device :slight_smile:

For example:

# create a quantum device
dev = qml.device("default.qubit", wires=4)

# turn circuit into a QNode that runs on device
@qml.qnode(dev) 
def circuit(x):
    qml.templates.MottonenStatePreparation(x, wires=[1, 2, 3])
    qml.Hadamard(wires=0)
    return qml.expval(qml.PauliZ(0))

You can now evaluate your quantum function:

>>> x = np.array([1, 0, 0, 0, 0, 0, 0, 0])
>>> circuit(x)
0.0

and draw it:

>>> print(circuit.draw())
 0: ───H─────────────────────────────────────────────── ⟨Z⟩
 1: ──╭C──╭C──────╭C──────╭C──╭C──╭C──────╭C──────╭C───
 2: ──╰X──╰X──╭C──│───╭C──│───╰X──╰X──╭C──│───╭C──│────
 3: ──────────╰X──╰X──╰X──╰X──────────╰X──╰X──╰X──╰X───

thanks. i forgot @qml.qnode(dev) and circuit(x). it worked. Many thanks for your quick and useful response.

1 Like

Happy it worked @sassan_moradi. @Josh, thanks for your help!

Many thanks, Maria. you’re the one who encouraged me to love quantum machine learning and generally speaking quantum computing. I am learning a lot from you and your team.

1 Like

Awesome, happy we can help!