Multidimensional regression with a variational quantum circuit

# Introduction
In this tutorial, I show how to use a variational quantum circuit to fit the simple multivariate function,
$f(x_{1},x_{2}) = C_{0}(\sin(ax_{1}) + \sin(bx_{2}))$
where $C_{0}$ is a normalisation factor.

Fitting $f(x)$ with a quantum computer can be done in an iterative way[^1].

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/878664/31f4cbff-9b22-ccb7-8d73-251fcc3ae7a0.png)

Here, the parameterized quantum circuit is an analog of a neural network.
We train the parameterized quantum circuit (a.k.a. Ansatz) to output $f(x)$.

The Ansatz is as follows[^1]:
```python
@qml.qnode(dev)
def quantum_neural_network(params, x):
    layers=len(params[:,0,0])-1
    n_wires=len(params[0,:,0])
    n_params_rot=len(params[0,0,:])
    for i in range(layers):
      W(params[i,:,:].reshape(1,n_wires,n_params_rot))
      S(x)
    W(params[-1,:,:].reshape(1,n_wires,n_params_rot))

    return qml.expval(qml.PauliZ(wires=0)@qml.PauliZ(wires=1))
```

$W$ is a parameterized sub circuit to be trained and $S$ is a data embedding sub circuit.

I can illustrate this,
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/878664/7bdb5413-7bd8-d573-3d36-24a8cac94f2f.png)

The fourfold data reloading structure is used.
In this case, according to the Fourier expression theory of variational circuits [^1],
We can expect this circuit to mimic functions including sine waves up to the fourth harmonic, such as $\sin(x),\sin(2x),\sin(3x),\sin(4x)$.

For example, I show the fit curve of $f(x_{1},x_{2}) = \frac{1}{2}(\sin(2x_{1}) + \sin(2x_{2}))$ and the accuracy (R2 score)[^1][^2].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/878664/5c251e9d-d7fb-7225-fa64-39da8689275f.png)

# Investigation of curve fitting (modification of the demo)

I said,
>*we can expect that this circuit can mimic functions including up to fourth harmonic sine waves such as $\sin(x),\sin(2x),\sin(3x),\sin(4x)$.*

Is this statement is correct?
To confirm this, I conducted the curve fitting of $\sin(x),\sin(2x),...\sin(3x),\sin(9x)$....

```python
import pandas as pd
df = pd.DataFrame()
for a in range(1,10):
    for b in range(1,10):
        def target_function(x):
            f=1/2*( jnp.sin(a*x[0]) + jnp.sin(b*x[1]) )
            return f
            
        x_train=pnp.stack((x1_mesh.flatten(), x2_mesh.flatten()), axis=1)
        y_train = target_function([x1_mesh,x2_mesh]).reshape(-1,1)
        
        
        best_params=optimization_jit(params, x_train, jnp.array(y_train), print_training=False)
        y_predictions=evaluate(best_params,x_train)
        from sklearn.metrics import r2_score
        r2 = round(float(r2_score(y_train, y_predictions)),3)
        df_tmp = pd.DataFrame([{"a":a, "b":b, "r2":r2}])
        df = pd.concat((df,df_tmp))
```

Plot the results.

```python
plt.subplot(1,2,1)
plt.xlabel("Degree,b")
plt.ylabel("R2 score")
plt.title(r"$f(x)=(1/2)(\sin(ax)+\sin(bx))$")
for n in range(1,6):
    df_sub = df.query("b==@n")
    plt.plot(df_sub["a"],df_sub["r2"],'o-',label="b=%d" % (n,))
plt.ylim([0,1])
plt.legend()

plt.subplot(1,2,2)
plt.xlabel("Degree,a")
plt.ylabel("R2 score")
plt.title(r"$f(x)=(1/2)(\sin(ax)+\sin(bx))$")
for n in range(1,6):
    df_sub = df.query("a==@n")
    plt.plot(df_sub["b"],df_sub["r2"],'o-',label="a=%d" % (n,))
plt.ylim([0,1])
plt.legend()
plt.tight_layout()
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/878664/b0ddb901-ed28-0222-fa83-1543f00ffd3a.png)

The higher the degree of the sine wave, the lower the R2 score.
Since the R2 score depends not only on the expressiveness of the quantum kernel, but also on the optimization process, the trend is noisy.
We can see that sinusoidal waves with $ a \leq 3$ and $ b \leq 3$ are well fitted by our ansatz.
The sinusoids with $ a \ge 4$ or $ b \ge 4$ are difficult to be fitted.
According to Fourier theory, the $ a = 4$ or $ b = 4$ should be well fitted by our quantum kernel, but the R2 score is bad.
I believe that the degradation is caused by the poor convergence of the numerical optimizer.

# Future work
We have extended the input dimension from one to two, $f(x_{1})\to f(x_{1}, x_{2})$.
However, the output dimension was limited to one.
For example, $\mathbb{C} \to \mathbb{C}$ was not supported, even though the use of complex variables is a key to many realistic problems such as telecommunications.

# Conclusion
The accuracy of function fitting by a quantum kernel depends not only on the ansatz, but also on the numerical optimization process.
Don't confuse them.

[^1]:https://pennylane.ai/qml/demos/tutorial_qnn_multivariate_regression

[^2]:The demo requires an old jax, e.g. jax==0.4.30