 # TypeError: unhashable type: 'numpy.ndarray'

Hi guys,

I am working on classification problem with Gaussian state as input features.

``````dev = qml.device('strawberryfields.fock', wires=3, 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(cm, mean_vector, wires=range(2))

CVNeuralNetLayers(*pars,wires=range(3))
return qml.expval(qml.X(0))
``````

The definitions of mean square error function and cost function I used are same as https://pennylane.ai/qml/demos/tutorial_variational_classifier.html

``````def square_loss(labels, predictions):
loss = 0
for l, p in zip(labels, predictions):
loss = loss + (l - p) ** 2

loss = loss / len(labels)
return loss

def cost(weights, cov_matrix, labels):
predictions = [quantum_neural_net(weights, f) for f in cov_matrix]
return square_loss(labels, predictions)
``````

I use AdamOptimizer for optimization but return error "unhashable type: ‘numpy.ndarray’ "

``````init_pars=cvqnn_layers_all(n_layers=2, n_wires=3, seed= None)
var = init_pars
for i in range(10):
var = opt.step(lambda v: cost(v, xtr,ytr), var)
#xtr,ytr are training dataset and training labels, respectively .
predictions_train = [np.sign(quantum_neural_net(var,cm)) for cm in xtr]
print(i + 1, cost(var, xtr, ytr))
``````

As some solutions I found, I rewrited the cost function as:

``````def cost(weights, cov_matrix, labels):
predictions=np.zeros((len(labels),1), dtype=object)
for i in range(len(labels)):
predictions[i,0]= quantum_neural_net(weights, cov_matrix[i,:])
return square_loss(labels, predictions)
``````

It can run now, but with warning " the output seems independent with input" and unexpected outputs :

``````1 [tensor(0.69090221, requires_grad=True)]

Hi @Alice_Wong and welcome to the forum!

I could not reproduce the errors you reported since the code you shared is incomplete. Could you please share minimal working/non-working examples so I can have a deeper look at the code and the errors? Thanks.

The document is the dataset I used.
gaussian_classification.txt (104.3 KB)
Here is the full code which non-working with TypeError:

``````import pennylane as qml
from pennylane import numpy as np
from pennylane.templates.layers import CVNeuralNetLayers
from pennylane.init import cvqnn_layers_all
``````
``````import pickle as pkl
with open('./gaussian_classification.txt', 'rb') as f1:
XX=data[:,0:16]
YY=data[:,16]

# shift label from {0, 1} to {-1, 1}
YY = YY* 2 - np.ones((len(YY)))

#15%data from original dataset
np.random.seed(0)
num_data = len(YY)
num_train = int(0.15 * num_data)
index = np.random.permutation(range(num_data))
xtr = XX[index[:num_train]]
ytr = YY[index[:num_train]]
xt = XX[index[num_train:]]
yt = YY[index[num_train:]]

``````
``````dev = qml.device('strawberryfields.fock', wires=3, 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(cm, mean_vector, wires=range(2))

CVNeuralNetLayers(*pars,wires=range(3))
return qml.expval(qml.X(0))
``````
``````def square_loss(labels, predictions):
loss = 0
for l, p in zip(labels, predictions):
loss = loss + (l - p) ** 2

loss = loss / len(labels)
return loss
def cost(weights, cov_matrix, labels):
predictions = [quantum_neural_net(weights, f) for f in cov_matrix]
return square_loss(labels, predictions)
``````
``````init_pars=cvqnn_layers_all(n_layers=2, n_wires=3, seed= None)

var = init_pars

for it in range(10):
var = opt.step(lambda v: cost(v, xtr,ytr), var)
predictions_train = [np.sign(quantum_neural_net(var,cm)) for cm in xtr]
print(it + 1, cost(var, xtr, ytr))``````

Thanks @Alice_Wong for the code. The vector of means created in the `quantum_neural_net` function should be an array containing the mean position and momentum quadrature of each mode. You currently have an array of lists which I believe is the reason for getting that error. Please define `mean_vector` as

`mean_vector = np.array([0.0, 0.0, 0.0, 0.0])`

to get correct outputs. Please let me know if you have any other questions.