'function' object has no attribute 'PauliZ

Hie l am pretty new to pennylane and trying to implement some tutorials. I was trying the variational quantum circuit.

import pennylane as qml
from pennylane import numpy as np

dev = qml.device(‘default.qubit’, wires=1)

@qml.qnode(dev)
def circuit(params):
qml.RY(params[0], wires=0)
return qml.expval.PauliZ(0)

print(‘The expectation value {}’.format(circuit([0])))
print(‘The expectation value {}’.format(circuit([np.pi/3])))
print(‘The expectation value {}’.format(circuit([np.pi])))

I am getting this area:

AttributeError: ‘function’ object has no attribute ‘PauliZ’

I am not sure why this error is generated or the cause, can you please help. l was running it on colab.

Hi @munya,
In PennyLane, you have to specify the type of measurement to make (expectation value, variance, or sample) using the observable (in this case, PauliZ) as the argument.

Try replacing your return statement with return qml.expval(qml.PauliZ(0))

1 Like

It worked like magic thank you, this was more like my baby steps in pennylane can you please refer me to some literature or useful tutorials if there are any?

Glad to hear you got it working.
All our tutorials, demos, and resources are located at pennylane.ai/qml

1 Like

I want to ask more about the result that for the function cosine^2 - sine^2. should generate 1, -0.5 and 1 respectively after plugging in 0, pi/3, and pi into the calculation. But the notebook gives the following results:

print('The expectation value {}'.format(circuit([0])))
print('The expectation value {}'.format(circuit([np.pi/3])))
print('The expectation value {}'.format(circuit([np.pi])))
The expectation value 1.0
The expectation value 0.5
The expectation value -1.0

Please tell me if I have calculated wrongly.
Thank you.

Hi @alanspace,

Thank you for the question! :slightly_smiling_face:

The rotation around the Y axis can be represented by the following matrix (where \phi is the rotation angle):
R_y(\phi) = e^{-i\phi\sigma_y/2} = \begin{bmatrix} \cos(\phi/2) & -\sin(\phi/2) \\ \sin(\phi/2) & \cos(\phi/2) \end{bmatrix}.

Applying a Y rotation to the zero state results in the following \psi state:

|\psi\rangle= R_y(\phi) |0\rangle = \begin{bmatrix} \cos(\phi/2) & -\sin(\phi/2) \\ \sin(\phi/2) & \cos(\phi/2) \end{bmatrix} \begin{bmatrix} 1 \\ 0 \\ \end{bmatrix} = \begin{bmatrix} \cos(\phi/2) \\ \sin(\phi/2) \\ \end{bmatrix}

Putting together the pieces, we are computing the following expectation value:
\langle\psi|Z|\psi\rangle= \langle0|R_y(\phi)Z R_y(\phi) |0\rangle = \begin{bmatrix} \cos(\phi/2) & \sin(\phi/2) \\ \end{bmatrix} \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} \begin{bmatrix} \cos(\phi/2) \\ \sin(\phi/2) \\ \end{bmatrix}

This is then equal to:

\langle\psi|Z|\psi\rangle=\cos^{2}(\phi/2) - \sin^{2}(\phi/2)

Substituting the values of \phi into the \cos^{2}(\phi/2) - \sin^{2}(\phi/2) expression would yield the results obtained by the notebook. Care should be taken, as the argument of the trigonometric functions is \phi/2 (instead of \phi).

Hope this helps, let us know if you would have further questions!

3 Likes

@antalszava Thanks a lot for your detailed explanation!