Good morning PennyLane’s team, I would like to discuss with you a problem on image classification over MNIST dataset. I am using JAX to optimize the process, and I have realized a “data-reuploading” quantum circuit, which has 2 times the feature map encoding the same features (I downsampled the MNIST from 784 to 64 features (8,8)) and 2 different variational quantum layers which “learn” different parameters. I return the qml.expval(qml.PauliZ(0)) and subsequently I have made the cross_entropy loss function simply using:

```
@jax.jit
def binary_crossentropy(X, y, theta):
ytrue = jnp.array(y)
ypred = mapping(qnn(X, theta)) #qnn is the model I made
epsilon = 1e-8 # to avoid log overflow (to -infty)
ypred_bounded = jnp.clip(ypred, epsilon, 1 - epsilon) # to bound the value of ypred
loss = ytrue*jnp.log10(ypred_bounded) + (1-ytrue)*jnp.log10(1-ypred_bounded)
return -jnp.mean(loss)
```

and the function mapping is defined as

```
def mapping(y):
return (y+1)/2
```

since outcomes are in the range {-1,1} and should be remapped into the set {0,1} to feed the cross_entropy. I also upload the accuracy which I calculate over the training process

```
def calculate_accuracy(theta, X, y):
y_pred = mapping(qnn(X, theta)) # I will pass x of training set
thresholded_y_pred = jnp.where(y_pred <= 0.5, 0, 1) ### threshold to 0.5 because the values are in the range {0,1}
accuracy = jnp.mean(thresholded_y_pred == y)
return accuracy
```

I would like to know if it’s correct. I mean, I have made the training and everything works fine I suppose (got train accuracy 0.93) and accuracy on the test set after model trained on 500 epochs is :

```
ypred = mapping(qnn(X_test, opt_params)) # again I should remap the values to confine them into {0,1}
```

0.89, so little overfitting, but I could play with hyperparameters on a validation set (varying number of layers, gates_per_layers, entangling scheme…), but the question is much more related to Physics. In Qiskit you make the measurement and the compute the accuracy by using the probabilities that should be calculated as it follows (approximately):

```
job = execute(qc, backend, shots=1024)
result = job.result()
counts = result.get_counts()
probs = []
for key in counts.keys():
probs.append(counts[key]/shots)
```

or something like that. But I am using the expval, not the shots and counts and this mentioned scheme. Am I right anyway? Could you explain me if I should make changes in my code? Can I use the expval anyway for the classification? Is what I have done correct?

Last question (sorry for bothering you), how to set up a multiclass classification based on the provieded functions? (Suppose to have 4 classes, I should measure 2 qubits, change the loss and then?).

Thanks in advance guys, I need your help!!

#qml #quantumcomputing #pennylane