Issues with qml.ctrl operation

Hello, I am trying to implement the following pennylane circuit.

def layer(W):
    qml.Rot(W[0, 0], W[0, 1], W[0, 2], wires=1)
    qml.Rot(W[1, 0], W[1, 1], W[1, 2], wires=2)
    qml.CNOT(wires=[0, 1])

def U(weights):
    for W in weights:
        layer(W)


@qml.qnode(dev, interface='torch')  
def circuit(weights, data):
    qml.Hadamard(wires=0)
    qml.AmplitudeEmbedding(data, wires=range(1,3))
    qml.ctrl(op=U, control=0, control_values=0)(weights=weights)
    # qml.PauliX(wires=0)
    qml.ctrl(op=U, control=0, control_values=1)(weights=weights)
    return qml.expval(qml.PauliZ(0))



def variational_classifier(weights, bias, angles):
    return circuit(weights, angles) + bias

# Display the quantum circuit
num_layers = 2
num_qubits = 2
X = torch.rand(1,4)
norm = torch.sqrt(torch.sum(X**2))
X = X/norm
print(qml.draw(circuit)(torch.tensor(0.01*np.random.rand(num_layers, num_qubits, 3), requires_grad=True), X))

On running the code I get the following error.

ValueError: The control wires must be different from the base operation wires.

And, finally, here is the output of qml.about().

Name: PennyLane
Version: 0.28.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /home/akash/anaconda3/envs/qepy39/lib/python3.8/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, retworkx, scipy, semantic-version, toml
Required-by: PennyLane-Lightning

Platform info:           Linux-6.5.0-14-generic-x86_64-with-glibc2.17
Python version:          3.8.16
Numpy version:           1.20.3
Scipy version:           1.10.0
Installed devices:
- default.gaussian (PennyLane-0.28.0)
- default.mixed (PennyLane-0.28.0)
- default.qubit (PennyLane-0.28.0)
- default.qubit.autograd (PennyLane-0.28.0)
- default.qubit.jax (PennyLane-0.28.0)
- default.qubit.tf (PennyLane-0.28.0)
- default.qubit.torch (PennyLane-0.28.0)
- default.qutrit (PennyLane-0.28.0)
- null.qubit (PennyLane-0.28.0)
- lightning.qubit (PennyLane-Lightning-0.28.0)

I am not sure what wrong I am doing here. According to the code, qubit 0 is supposed to be the control qubit, and qubits 1 and 2 are supposed to be the qubits where I apply the unitary operations.

I found the error in the code. It was because the CNOT was being applied between wires 0 and 1

Hey @imakash

Glad you were able to find a fix! Don’t hesitate to post here if you run into any more issues, we’re here to help :smiley:

Alvaro