Prepare Gaussianstate

I want to feed two-qubit gaussian states with covariance matrix into CVNeuralNetLayers. However, it returned error that " The input matrix is not square".

> dev = qml.device('strawberryfields.fock', wires=2, cutoff_dim=10) 
def quantum_neural_net(pars,cm):
    return qml.expval(qml.X(0))
#random pick a column with 16 elements in training dataset 
ValueError: The input matrix is not square

I directly used sf.ops.Gaussian(cm_test) to check, which worked.

> import strawberryfields as sf
print("returned gaussian state",result)
Out: returned gaussian state Gaussian([[ 68.4229   0.      68.5705   0.    ]
 [  0.      68.4229   0.     -68.5705]
 [ 68.5705   0.      68.9229   0.    ]
 [  0.     -68.5705   0.      68.9229]], [0. 0. 0. 0.])

Hi @Alice_Wong,

Welcome to the forum!

One thing to note is the required shape of mean_vector. The documentation specifies that it should be an array of length 2N (4 in this case), while the array in the code snippet is shape (4, 1). However I tested even with the correct shape (mean_vector = np.array([0,0,0,0])) and received a different error which will require some looking into.

I’ve created a bug report in the PennyLane-SF plugin repository. Feel free to check that out for the extra details, and follow the progress on that issue.

Thanks for uncovering this!

Quick update: we believe the source of the error is in the order of the parameters in GaussianState. In addition to changing the shape of mean_vector, try swapping the order of mean_vector and cm in the qml.GaussianState function. Let us know if it works :slight_smile:

It works now.

Awesome - thanks for letting us know!

1 Like