CVNeuralNetLayers parameters shape error

Hi everyone,

I found a confused problem about parameter shape when I use CVNeuralNetLayers:

dev = qml.device('strawberryfields.fock', wires=2, cutoff_dim=10)
def circuit(pars):
    CVNeuralNetLayers(*pars, wires=range(2))
@qml.qnode(dev)
def quantum_neural_net(var, x1=None,x2=None):
    qml.Displacement(x1, 0.0, wires=0)
    qml.Displacement(x2, 0.0, wires=1)
    circuit(var)
    return qml.expval(qml.X(0))

init_pars=cvqnn_layers_all(n_layers=2, n_wires=2, seed= None)

result = [quantum_neural_net(init_pars, x1=xlo[0],x2=xlo[1]) for xlo in X]
#X is dataset with shape (# of samples, 2)

ValueError: could not broadcast input array from shape (2,1) into shape (2)

However, I tried number of wires 1,2,4 all return similar error. Only use 3 wires, it would work. I read the CVNeuralNetLayers documentation, I found the M =K when wires are 3.

Hi @Alice_Wong,

Could you please provide the full version of your code with all the parameters defined?

Thanks.

Thanks for reply.

import pennylane as qml
from pennylane import numpy as np
from pennylane.optimize import AdamOptimizer
from pennylane.templates.layers import CVNeuralNetLayers
from pennylane.init import cvqnn_layers_all
from pennylane.templates.embeddings import DisplacementEmbedding
import pickle as pkl
with open('./delta_New.txt', 'rb') as f1:
        X=pkl.load(f1)
with open('./discord_New.txt', 'rb') as f2:
        Y=pkl.load(f2)
X = X[:, 0:4]
print(X.shape) 
Out:(784,4)
print(Y.shape)
Out:(784,)

dev = qml.device('strawberryfields.fock', wires=4, cutoff_dim=10)
def circuit(pars):
    CVNeuralNetLayers(*pars, wires=range(4))

@qml.qnode(dev)
def quantum_neural_net(var, x1=None,x2=None,x3=None,x4=None):

    qml.Displacement(x1, 0.0, wires=0)
    qml.Displacement(x2, 0.0, wires=1)
    qml.Displacement(x3, 0.0, wires=2)
    qml.Displacement(x4, 0.0, wires=3)
    
    circuit(var)
    return qml.expval(qml.X(0))


init_pars=cvqnn_layers_all(n_layers=2, n_wires=4, seed= None)
result = [quantum_neural_net(init_pars, x1=xlo[0],x2=xlo[1] x3=xlo[2],x4=xlo[3]) for xlo in X]

ValueError: could not broadcast input array from shape (2,6) into shape (2)

The dataset could be switch to the .txt document in http://discuss.pennylane.ai/t/typeerror-unhashable-type-numpy-ndarray/1012

Hey @Alice_Wong,

this error is indeed strange. I reduced it to a minimum working example, since there is no need to load any data or use nested functions to reproduce the error:

import pennylane as qml
from pennylane.templates.layers import CVNeuralNetLayers
from pennylane.init import cvqnn_layers_all

dev = qml.device('strawberryfields.fock', wires=4, cutoff_dim=10)

@qml.qnode(dev)
def quantum_neural_net(var):
    CVNeuralNetLayers(*var, wires=range(4))
    return qml.expval(qml.X(0))

init_pars = cvqnn_layers_all(n_layers=2, n_wires=4, seed=None)
result = quantum_neural_net(init_pars)

I’ll investigate and get back to you!

I understand now what is happening and how to fix it :slight_smile: It is a problem of ragged arrays.

You are feeding a list of tensors of different shapes to the QNode. Inside, the QNode tries to turn this into a trainable numpy array. This is currently the default behaviour to maintain backwards compatibility to when positional arguments were differentiable by default - we will soon get rid of it!

But even without this upcoming improvement, we are going more and more towards a design where all trainable arguments are represented by tensors, and it is good practice to start thinking this way. Of course, feeding all 11 weight tensors can be very unwieldy for the CVNeuralNetLayer template!

An elegant solution is to use star notation. This code should run fine:

import pennylane as qml
from pennylane.templates.layers import CVNeuralNetLayers
from pennylane.init import cvqnn_layers_all

dev = qml.device('strawberryfields.fock', wires=4, cutoff_dim=10)

@qml.qnode(dev)
def quantum_neural_net(*var):
    CVNeuralNetLayers(*var, wires=range(4))
    return qml.expval(qml.X(0))

init_pars = cvqnn_layers_all(n_layers=2, n_wires=4, seed=None)
result = quantum_neural_net(*init_pars)

Here your quantum_neural_net is a function of 11 arguments which are fed by unpacking the list in each signature.

Does this work for you?

1 Like