Hi,
I am studying the Iris classification part of this tutorial: Variational classifier — PennyLane documentation
I am trying to figure out the complete encoding process by adding some print()
in Pennylane’s source code:
@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 in the tutorial code where it does the encoding:
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 something like this:
encoding...
theta and RY matrix
encoding done!
but instead I had this:
How can I get expected output? Why did I get output from
qml.RY
after statepreparation()
is done?