Qnode with data-reupload and basic entangler layers

Hello , i have a Qnode with data-reupload technique in which i use strongly entangled layers but i would like to use basicentangler layers instead and for some reason i get some errors

> @qml.qnode(dev, interface="tf", grad_method="backprop")
> def qnode(inputs, weights):
>     for i in range(blocks):
>         qml.templates.AngleEmbedding(inputs, wires=range(n_qubits))
> #       qml.templates.BasicEntanglerLayers(weights, wires=range(n_qubits)) #BASIC ENTANGLER LAYERS , chan
>         qml.templates.StronglyEntanglingLayers(weights[i], wires=range(n_qubits)) #STRONGLY ENTANGLING LAYERS
>     return [qml.expval(qml.PauliZ(i)) for i in range(n_qubits)]
> weights_shape = (blocks, layers, n_qubits, 3) 

weight_shapes = {"weights": weights_shape}

qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=n_qubits)

This is the working code with strongly entangling layers in which every “block” must be initialized with random weights (which hopefully works as i describe it). Can you let me know what changes to make so it works with basic entangler layers ? thanks.

Hi @NikSchet,

It seems like the issue might be the shape of the weights that you’re using. Are you getting something similar to the following error?

ValueError: Weights tensor must be 2-dimensional; got shape (3, 2, 3)

If so, you’d need to change the weights-shape to be (number_of_layers, n_qubits) instead of the the one you’re currently using, which I believe would work for StronglyEntanglingLayers. Try running your code without the final dimension of 3 and let me know if it works.

The change is due to the difference between how many parameters the different entangling-layer templates are using. StronglyEntanglingLayers uses rotation gates with 3 parameters each, while BasicEntanglerLayers only has single parameter rotation gates.

You can read more about it in the docs here, for StronglyEntanglingLayers, and here, for BasicEntanglerLayers.

1 Like

That works thanks! (I removed the 3)

1 Like