Question about multiple amplitude encoding in one circuit(pytorch+pennylane)

When using pytorch and pennylane, multiple amplitude encoding is not work

def encode(inputs, n_qubits, embed_type="amplitude"):
    start, end = n_qubits
    # setting embedding
    if "amplitude" == embed_type:
            qml.templates.AmplitudeEmbedding(inputs, wires=range(start, end))
    if "angle" == embed_type:
            qml.templates.AngleEmbedding(inputs, wires=range(start, end))

def qnn(n_qubits, layer):
    dev = qml.device("default.qubit", wires=n_qubits)
    def _qcnn(weights,inputs):
        x1 = inputs[:, :128] 
        x2 = inputs[:, -128:] 
        inputs1 = F.normalize(x1, p=2, dim=1)
        encode(inputs1, (0,7), embed_type="amplitude") 
        inputs2 = F.normalize(x2, p=2, dim=1)
        encode(inputs2, (7,14), embed_type="amplitude")
        ansatz(weights,layer)
        return qml.probs(wires=[0,1,2,3,4,5,6,7,8,9,10,11,12,13])
    
    qlayer = qml.QNode(_qcnn, dev, interface="torch") 
    weight_shapes = {"weights": 49*layer}
    return  qml.qnn.TorchLayer(qlayer, weight_shapes)

error message below:

compute_decomposition
    raise ValueError(
ValueError: Broadcasting with MottonenStatePreparation is not supported. Please use the qml.transforms.broadcast_expand transform to use broadcasting with MottonenStatePreparation.

Using an amplitude encoding works fine.

Hi @SHAN ,

Your issue seems related to the issue mentioned in this other thread. As mentioned in post #6 in that thread, using qml.transforms.broadcast_expand might help you.

That being said, I don’t think qml.AmplitudeEmbedding was designed to be used more than once so the solution I mentioned may not work in this case. Why don’t you give it a try and let us know if this works for you?

I hope this helps!

Thank you very much, it solved my issue! :smiling_face_with_three_hearts:

1 Like