Controlled Unitary Gate problem

Hi, I had some problems using Controlled-Unitary, the Unitary in my circuit should be the Amplitude Embedding circuit based on the given inputs. The Quantum Circuit that I want might look like the figure shown below

I tried to build the Quantum Circuit with the code:

%matplotlib inline

import pennylane as qml
import torch

total_qubit = 3

dev = qml.device("default.qubit", wires=total_qubit)


@qml.qnode(dev)
def q_node(inputs):

    qml.Hadamard(wires=0)

    qml.ctrl(qml.AmplitudeEmbedding(features=inputs, wires=range(1, total_qubit), normalize=True), control=0, control_values=(0))

    return qml.probs(wires=range(total_qubit))


qml.draw_mpl(q_node)(inputs=torch.arange(1,5))  
output = q_node(inputs=torch.arange(1,5)) 

And the error message shows

ValueError: prep operation Controlled(QubitStateVector(tensor([0.1826+0.j, 0.3651+0.j, 0.5477+0.j, 0.7303+0.j], dtype=torch.complex128), wires=[1, 2]), control_wires=[0]) must occur prior to ops. Please place earlier in the queue.

Quantum Circuit seems to operate the Amplitude Embedding circuit before the Control Gate, which is incorrect.

How can I fix this error and make my system work?

Hello @mini,

This documentation contains all of the restrictions of AmplitudeEmbedding with other notes. qml.AmplitudeEmbedding — PennyLane 0.31.0 documentation

Code:
control=0, control_values=(0)
Would have to fit this pattern: class AmplitudeEmbedding(features, wires, pad_with=None, normalize=False, do_queue=None, id=None)

Best regards.

Hi :smile: Thanks for the question @mini and the answer @kevinkawchak !
Another option is to use qml.MottonenStatePreparation , since it has no restriction of being at the beginning of the circuit:


import pennylane as qml
import torch
import numpy as np

total_qubit = 3

dev = qml.device("default.qubit", wires=total_qubit)


@qml.qnode(dev)
def q_node(inputs):

    qml.Hadamard(wires=0)

    qml.ctrl(qml.MottonenStatePreparation(inputs, wires=range(1, total_qubit)), control=0, control_values=(0))

    return qml.probs(wires=range(total_qubit))


qml.draw_mpl(q_node)(inputs=torch.arange(1,5) / np.sqrt(30) )  
output = q_node(inputs=torch.arange(1,5)/ np.sqrt(30)) 

Note that in this case, the template does not support normalize so you have to do it manually before sending the vector. Hope this helps!