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) 
@qml.qnode(dev)
def quantum_neural_net(pars,cm):
    mean_vector=np.array([[0.0],
                                              [0.0],
                                              [0.0],
                                             [0.0]])
    cm=cm.reshape((4,4))
    qml.GaussianState(mean_vector,cm,wires=range(2))
    CVNeuralNetLayers(*pars,wires=range(2))
    return qml.expval(qml.X(0))
#random pick a column with 16 elements in training dataset 
cm_test=xtr[89]
result=quantum_neural_net(init_pars,cm_test))
ValueError: The input matrix is not square

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

> import strawberryfields as sf
cm_test=xtr[89].reshape((4,4))
result=sf.ops.Gaussian(cm_test)
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:

Thanks.
It works now.

Awesome - thanks for letting us know!

1 Like