Hello, whenever I try to run my program i faced an issue in training model as mentioned below. As I am new to Pennylane and QML I am having a hard time understanding what the error is. The problem I have implemented is a regression or parameter estimation problem and I am not very sure if this circuit can be used for regression. I went through the photonic architecture based function fitting demo in pennylane but that specific architecture is kind of slow for my requirement and I feel like the one below has more flexibility in terms of developing the circuits (I may be wrong). And in the former i can’t give (900,1000) as input and the only way i can do it is using Amplitude Embedding hence I resorted to this. Please go through this and let me know what can be done. Thank you.

- I have attached the code below. I initiate inputs to 10 qubits and require only 5 outputs to estimate 5 parameters, hence i have used separate CNOT gates to entangle neighboring qubits.
- I have provided random x_train and y_train inputs here but of the same size as the actual data. If required, I can provide the actual dataset as well.
**Note: I am new to QML and pennlane and I am running the whole code using Google Colab and all the packages are up-to-date.**

```
n_qubits = 10
layers = 2
dev = qml.device("lightning.qubit", wires=n_qubits)
@qml.qnode(dev)
def qnn(weights, inputs):
qml.AmplitudeEmbedding(inputs, wires=range(n_qubits),pad_with=0.5)
qml.StronglyEntanglingLayers(weights, wires=range(n_qubits))
for i in range(n_qubits-1):
if i%2==0:
qml.CNOT(wires=[i,i+1])
# qml.BasicEntanglerLayers(weights, wires=range(n_qubits))
# qml.RandomLayers(weights,wires=range(n_qubits))
return [qml.expval(qml.PauliZ(i)) for i in range(n_qubits)[1::2]]
var_init = pnp.random.randn(layers, n_qubits, 3,requires_grad=True)
def mse(observed, predictions):
loss = 0
for l, p in zip(observed, predictions):
loss = loss + (l - p) ** 2
loss = loss / len(observed)
return loss
def cost(var, features, observed):
preds = [qnn(var,x) for x in features]
return mse(observed, preds)
x_train = np.random.randn(900,1000)
y_train = np.random.randn(900,5)
opt = qml.AdamOptimizer(0.1, beta1=0.9, beta2=0.999)
var = var_init
for it in range(100):
(var, _, _), _cost = opt.step_and_cost(cost, var, x_train, y_train)
print("Iter: {:5d} | Cost: {:0.7f} ".format(it, _cost))
```

This is the error I get when I try to train my model:

```
TypeError Traceback (most recent call last)
<ipython-input-45-100808a25dcc> in <cell line: 4>()
3 var = var_init
4 for it in range(100):
----> 5 (var, _, _), _cost = opt.step_and_cost(cost, var, x_train, y_train)
6 print("Iter: {:5d} | Cost: {:0.7f} ".format(it, _cost))
4 frames
/usr/local/lib/python3.10/dist-packages/pennylane/_grad.py in _grad_with_forward(fun, x)
137
138 if not vspace(ans).size == 1:
--> 139 raise TypeError(
140 "Grad only applies to real scalar-output functions. "
141 "Try jacobian, elementwise_grad or holomorphic_grad."
TypeError: Grad only applies to real scalar-output functions. Try jacobian, elementwise_grad or holomorphic_grad.
```

The output of `qml.about()`

.

Name: PennyLane

Version: 0.33.0

Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.

Home-page: GitHub - PennyLaneAI/pennylane: PennyLane is a cross-platform Python library for differentiable programming of quantum computers. Train a quantum computer the same way as a neural network.

Author:

Author-email:

License: Apache License 2.0

Location: /usr/local/lib/python3.10/dist-packages

Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions

Required-by: PennyLane-Lightning

Platform info: Linux-5.15.120±x86_64-with-glibc2.35

Python version: 3.10.12

Numpy version: 1.23.5

Scipy version: 1.11.3