# How is functions like qml.RY, qml.CNOT get executed?

Hi,

I am currently studying the “Iris classification” part of this tutorial: Variational classifier — PennyLane documentation.

I added some `print()` in the PennyLane’s source code to find out how everything works:

``````@staticmethod
def compute_matrix(theta):  # pylint: disable=arguments-differ
r"""Representation of the operator as a canonical matrix in the computational basis (static method).

The canonical matrix is the textbook matrix representation that does not consider wires.
Implicitly, this assumes that the wires of the operator correspond to the global wire order.

.. seealso:: :meth:`~.RY.matrix`

Args:
theta (tensor_like or float): rotation angle

Returns:
tensor_like: canonical matrix

**Example**

>>> qml.RY.compute_matrix(torch.tensor(0.5))
tensor([[ 0.9689, -0.2474],
[ 0.2474,  0.9689]])
"""
print("Operation: RY")
print("theta: ", theta)
c = qml.math.cos(theta / 2)
s = qml.math.sin(theta / 2)
if qml.math.get_interface(theta) == "tensorflow":
c = qml.math.cast_like(c, 1j)
s = qml.math.cast_like(s, 1j)
# The following avoids casting an imaginary quantity to reals when backpropagating
c = (1 + 0j) * c
s = (1 + 0j) * s
print("RY matrix: ", qml.math.stack([stack_last([c, -s]), stack_last([s, c])], axis=-2))
return qml.math.stack([stack_last([c, -s]), stack_last([s, c])], axis=-2)
``````

and some `print()` in the tutorial code to study the flow of the complete encoding process:

``````def statepreparation(a):
print("encoding.....")

qml.RY(a, wires=0)
qml.CNOT(wires=[0, 1])
qml.RY(a, wires=1)
qml.CNOT(wires=[0, 1])
qml.RY(a, wires=1)
qml.PauliX(wires=0)
qml.CNOT(wires=[0, 1])
qml.RY(a, wires=1)
qml.CNOT(wires=[0, 1])
qml.RY(a, wires=1)
qml.PauliX(wires=0)
print("encoding done!")
``````

I expected the output would be
`encoding....`
`some encoding output`
`encoding done!`
But seems like it doesn’t print in the expected order:

How can I fix this?

Hi @pennyRocie,

Since I don’t have your full code I will try to guide you based on the Variational classifier demo. I have added the relevant code here with comments

``````import pennylane as qml
from pennylane import numpy as np

# Create a device
dev = qml.device("default.qubit", wires=2)

# Create a function to get the angles
def get_angles(x):

beta0 = 2 * np.arcsin(np.sqrt(x ** 2) / np.sqrt(x ** 2 + x ** 2 + 1e-12))
beta1 = 2 * np.arcsin(np.sqrt(x ** 2) / np.sqrt(x ** 2 + x ** 2 + 1e-12))
beta2 = 2 * np.arcsin(
np.sqrt(x ** 2 + x ** 2)
/ np.sqrt(x ** 2 + x ** 2 + x ** 2 + x ** 2)
)

return np.array([beta2, -beta1 / 2, beta1 / 2, -beta0 / 2, beta0 / 2])

# Define the statepreparation routine
def statepreparation(a):
print("encoding.....")

qml.RY(a, wires=0)
qml.CNOT(wires=[0, 1])
qml.RY(a, wires=1)
qml.CNOT(wires=[0, 1])
qml.RY(a, wires=1)
qml.PauliX(wires=0)
qml.CNOT(wires=[0, 1])
qml.RY(a, wires=1)
qml.CNOT(wires=[0, 1])
qml.RY(a, wires=1)
qml.PauliX(wires=0)
print("encoding done!")

x = np.array([0.53896774, 0.79503606, 0.27826503, 0.0], requires_grad=False)
# Run the get_angles function
ang = get_angles(x)

@qml.qnode(dev)
def test(angles):

statepreparation(angles)

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

As you can see, by running `test(ang)` you’re running your circuit, which is in turn running the `statepreparation` function. After you’re done running your circuit you print some additional values.