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))
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?
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.
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:
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!