# TypeError: unhashable type: 'numpy.ndarray'

**URL:** <https://discuss.pennylane.ai/t/typeerror-unhashable-type-numpy-ndarray/1012>\
**Category:** PennyLane Help\
**Created:** [April 30, 2021, 4:30pm UTC](https://discuss.pennylane.ai/t/typeerror-unhashable-type-numpy-ndarray/1012 "2021-04-30T16:30:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alice\_Wong](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/alice_wong/32/505_2.png) [@Alice\_Wong](https://discuss.pennylane.ai/u/Alice_Wong)\
**Post date:** [April 30, 2021, 4:30pm UTC](https://discuss.pennylane.ai/t/typeerror-unhashable-type-numpy-ndarray/1012/1 "2021-04-30T16:30:36Z")

</div>

Hi guys,

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

```auto
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](https://pennylane.ai/qml/demos/tutorial_variational_classifier.html)

```auto
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’ "

```auto
init_pars=cvqnn_layers_all(n_layers=2, n_wires=3, seed= None)
opt = AdamOptimizer(0.01, beta1=0.9, beta2=0.999)
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:

```auto
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 :

```auto
1 [tensor(0.69090221, requires_grad=True)]
2 [tensor(0.69090221, requires_grad=True)]
3 [tensor(0.69090221, requires_grad=True)]
4 [tensor(0.69090221, requires_grad=True)]
5 [tensor(0.69090221, requires_grad=True)]
6 [tensor(0.69090221, requires_grad=True)]
7 [tensor(0.69090221, requires_grad=True)]
8 [tensor(0.69090221, requires_grad=True)]
9 [tensor(0.69090221, requires_grad=True)]
10 [tensor(0.69090221, requires_grad=True)]
```

---

<div class="post-metadata">

**Author:** ![sjahangiri](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/sjahangiri/32/1443_2.png) [@sjahangiri](https://discuss.pennylane.ai/u/sjahangiri)\
**Post date:** [April 30, 2021, 10:30pm UTC](https://discuss.pennylane.ai/t/typeerror-unhashable-type-numpy-ndarray/1012/2 "2021-04-30T22:30:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Alice\_Wong](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/alice_wong/32/505_2.png) [@Alice\_Wong](https://discuss.pennylane.ai/u/Alice_Wong)\
**Post date:** [May 1, 2021, 12:19am UTC](https://discuss.pennylane.ai/t/typeerror-unhashable-type-numpy-ndarray/1012/3 "2021-05-01T00:19:18Z")

</div>

Thanks for your reply!

The document is the dataset I used.  
[gaussian\_classification.txt](https://discuss.pennylane.ai/uploads/short-url/eCX1ho4BxVjEHv4boCZ5pffYcHE.txt) (104.3 KB)  
Here is the full code which non-working with TypeError:

```auto
import pennylane as qml
from pennylane import numpy as np
from pennylane.templates.layers import CVNeuralNetLayers
from pennylane.init import cvqnn_layers_all
from pennylane.optimize import AdamOptimizer

```

```auto
import pickle as pkl
with open('./gaussian_classification.txt', 'rb') as f1:
        data=pkl.load(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:]]

```

```auto
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))

```

```auto
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)

```

```auto
init_pars=cvqnn_layers_all(n_layers=2, n_wires=3, seed= None)
opt = AdamOptimizer(0.01, beta1=0.9, beta2=0.999)
 
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))
```

---

<div class="post-metadata">

**Author:** ![sjahangiri](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/sjahangiri/32/1443_2.png) [@sjahangiri](https://discuss.pennylane.ai/u/sjahangiri)\
**Post date:** [May 1, 2021, 10:44pm UTC](https://discuss.pennylane.ai/t/typeerror-unhashable-type-numpy-ndarray/1012/4 "2021-05-01T22:44:46Z")

</div>

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.
