Hi,
I am currently studying the “Iris classification” part of this tutorial: Variational classifier | PennyLane Demos.
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[0], wires=0)
qml.CNOT(wires=[0, 1])
qml.RY(a[1], wires=1)
qml.CNOT(wires=[0, 1])
qml.RY(a[2], wires=1)
qml.PauliX(wires=0)
qml.CNOT(wires=[0, 1])
qml.RY(a[3], wires=1)
qml.CNOT(wires=[0, 1])
qml.RY(a[4], 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?