qml.ControlledQubitUnitary

We want to built a quantum circuit by PennyLane, however, we had trouble using the control-U gate. Could you help us?
first, we defined a single gate:
def single_U(weight, wires = None):
qml.RX(weight[0], wires = wires)
qml.RY(weight[1], wires = wires)
qml.RZ(weight[2], wires = wires)
then, defined a control_U by “qml.ControlledQubitUnitary”
qml.ControlledQubitUnitary(single_U(weight[0:3], wires = wires[1]), control_wires=0, wires=1)

But when running the code it prompts me: ControlledQubitUnitary: wrong number(s) of dimensions in parameters. Parameters with ndims (0,) passed, (2,) expected.
So, how can we embed single_U into control_U?

Hi @zj-lucky ,

It looks like your weights may not be defined properly. Also, I would recommend using qml.ctrl() instead of qml.ControlledQubitUnitary() because the latter is really designed for you to input a matrix so if you’re not careful it can cause some trouble where you may end up adding gates inadvertently to your circuit.

Here’s an example code for what you want to do, using qml.ctrl()

import pennylane as qml

def single_U(weight, wires = None):
    qml.RX(weight[0], wires = wires)
    qml.RY(weight[1], wires = wires)
    qml.RZ(weight[2], wires = wires)

n_wires = 2

dev = qml.device('default.qubit', wires=n_wires)

@qml.qnode(dev)
def circuit(weight, wires):
    qml.ctrl(single_U,control=1)(weight, wires=0)
    return qml.expval(qml.PauliZ(0))

weight = [1,2,3]
print(circuit(weight, wires=range(n_wires)))

fig,ax = qml.draw_mpl(circuit)(weight, wires=range(n_wires))
fig.show()

Please let me know if this is clear or if you have any further questions!
I hope this is helpful for you.

Thank you very much, your response is very useful