# 'function' object has no attribute 'PauliZ

**URL:** <https://discuss.pennylane.ai/t/function-object-has-no-attribute-pauliz/279>\
**Category:** PennyLane Help\
**Created:** [November 27, 2019, 9:16pm UTC](https://discuss.pennylane.ai/t/function-object-has-no-attribute-pauliz/279 "2019-11-27T21:16:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![munya](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/munya/32/114_2.png) [@munya](https://discuss.pennylane.ai/u/munya)\
**Post date:** [November 27, 2019, 9:16pm UTC](https://discuss.pennylane.ai/t/function-object-has-no-attribute-pauliz/279/1 "2019-11-27T21:16:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nathan](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nathan/32/85_2.png) [@nathan](https://discuss.pennylane.ai/u/nathan)\
**Post date:** [November 27, 2019, 9:43pm UTC](https://discuss.pennylane.ai/t/function-object-has-no-attribute-pauliz/279/2 "2019-11-27T21:43:24Z")

</div>

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))`

---

<div class="post-metadata">

**Author:** ![munya](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/munya/32/114_2.png) [@munya](https://discuss.pennylane.ai/u/munya)\
**Post date:** [November 28, 2019, 2:50am UTC](https://discuss.pennylane.ai/t/function-object-has-no-attribute-pauliz/279/3 "2019-11-28T02:50:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![nathan](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nathan/32/85_2.png) [@nathan](https://discuss.pennylane.ai/u/nathan)\
**Post date:** [November 28, 2019, 3:06pm UTC](https://discuss.pennylane.ai/t/function-object-has-no-attribute-pauliz/279/4 "2019-11-28T15:06:01Z")

</div>

Glad to hear you got it working.  
All our tutorials, demos, and resources are located at [pennylane.ai/qml](http://pennylane.ai/qml)

---

<div class="post-metadata">

**Author:** ![alanspace](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/alanspace/32/320_2.png) [@alanspace](https://discuss.pennylane.ai/u/alanspace)\
**Post date:** [September 24, 2020, 7:21am UTC](https://discuss.pennylane.ai/t/function-object-has-no-attribute-pauliz/279/5 "2020-09-24T07:21:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![antalszava](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/antalszava/32/171_2.png) [@antalszava](https://discuss.pennylane.ai/u/antalszava)\
**Post date:** [September 24, 2020, 6:34pm UTC](https://discuss.pennylane.ai/t/function-object-has-no-attribute-pauliz/279/6 "2020-09-24T18:34:10Z")

</div>

Hi @alanspace,

Thank you for the question! 🙂

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!

---

<div class="post-metadata">

**Author:** ![alanspace](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/alanspace/32/320_2.png) [@alanspace](https://discuss.pennylane.ai/u/alanspace)\
**Post date:** [September 27, 2020, 12:05pm UTC](https://discuss.pennylane.ai/t/function-object-has-no-attribute-pauliz/279/7 "2020-09-27T12:05:48Z")

</div>

@antalszava Thanks a lot for your detailed explanation!
