# Internship lecture 5 : Machine Learning using Ising Model

#### Yuichiro Minato 13 days ago

This will be the fifth technical guidance for interns in 2022. So far we have looked at classical machine learning, optimization, quantum combinatorial optimization, and quantum machine learning. The last part will be machine learning with quantum optimization.

The two models we will be performing are clustering and linear regression calculations. These models can be implemented using the Ising model. For optimization problems, the problem can be mapped directly or a model can be created to solve the problem indirectly.

# Clustering

Clustering is the problem of dividing the nodes into specified groups according to conditions, and in this case we will consider the problem of dividing the nodes into specified number of groups according to the distance between them. In this case, we will use the Ising model, which requires as many qubits as the number of nodes * number of clusters.

## QUBO

The formulation is as follows. This time, clustering can be set up by making copies of nodes into multiple clusters, with 1 for nodes belonging to a cluster and 0 for nodes not belonging to a cluster. There are two terms. This one is the cost function that minimizes the sum of the distances in the clusters.

$H_A = \sum d_{ij} q_i q_j\ \ \ \ (q_i \in \{0, 1\})$

Also, the constraint condition is that one node must be the only one in all clusters.

$H_B = \sum(\sum_{i}^n q_i - 1)^2$

Here, quantum entanglement can be used to eliminate the second constraint. In that case, the following is used as the second Hamiltonian. This is an expression that exchanges the values of |01> and|10>.

$H_{XY} = (X_0 X_1 + Y_0 Y_1)/2 = \begin{pmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 \end{pmatrix}$
.css-wy3l36{position:absolute;top:0px;right:0px;}.css-u40cf7{display:-webkit-inline-box;display:-webkit-inline-flex;display:-ms-inline-flexbox;display:inline-flex;-webkit-appearance:none;-moz-appearance:none;-ms-appearance:none;appearance:none;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:center;-ms-flex-pack:center;-webkit-justify-content:center;justify-content:center;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;position:relative;white-space:nowrap;vertical-align:middle;outline:2px solid transparent;outline-offset:2px;line-height:1.2;border-radius:var(--chakra-radii-md);font-weight:var(--chakra-fontWeights-semibold);transition-property:var(--chakra-transition-property-common);transition-duration:var(--chakra-transition-duration-normal);height:var(--chakra-sizes-8);min-width:var(--chakra-sizes-8);font-size:var(--chakra-fontSizes-sm);-webkit-padding-start:var(--chakra-space-3);padding-inline-start:var(--chakra-space-3);-webkit-padding-end:var(--chakra-space-3);padding-inline-end:var(--chakra-space-3);background:var(--chakra-colors-teal-500);color:var(--chakra-colors-white);}.css-u40cf7:focus-visible,.css-u40cf7[data-focus-visible]{box-shadow:var(--chakra-shadows-outline);}.css-u40cf7[disabled],.css-u40cf7[aria-disabled=true],.css-u40cf7[data-disabled]{opacity:0.4;cursor:not-allowed;box-shadow:var(--chakra-shadows-none);}.css-u40cf7:hover,.css-u40cf7[data-hover]{background:var(--chakra-colors-teal-600);}.css-u40cf7:hover[disabled],.css-u40cf7[data-hover][disabled],.css-u40cf7:hover[aria-disabled=true],.css-u40cf7[data-hover][aria-disabled=true],.css-u40cf7:hover[data-disabled],.css-u40cf7[data-hover][data-disabled]{background:var(--chakra-colors-teal-500);}.css-u40cf7:active,.css-u40cf7[data-active]{background:var(--chakra-colors-teal-700);}.css-rwh0nv{display:inline-block;font-family:var(--chakra-fonts-mono);font-size:var(--chakra-fontSizes-sm);-webkit-padding-start:0.2em;padding-inline-start:0.2em;-webkit-padding-end:0.2em;padding-inline-end:0.2em;border-radius:var(--chakra-radii-sm);background:grey.100;color:grey.800;}import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

from blueqat import Circuit
from blueqat.utils import qaoa
from blueqat.pauli import X,Y,Z
from blueqat.pauli import qubo_bit as q
import networkx as nx
import matplotlib.pyplot as plt

n = 10
m = 30
seed = 15

options = {'node_size': 100}

G = nx.gnm_random_graph(n, m, seed = seed)
nx.draw(G, **options)
.css-4m8w8z{display:inline-block;font-family:var(--chakra-fonts-mono);font-size:var(--chakra-fontSizes-sm);-webkit-padding-start:0.2em;padding-inline-start:0.2em;-webkit-padding-end:0.2em;padding-inline-end:0.2em;border-radius:var(--chakra-radii-sm);background:var(--chakra-colors-gray-100);color:var(--chakra-colors-gray-800);}<Figure size 432x288 with 1 Axes>
hamiltonian = sum(q(e[0])*q(e[1]) + q(e[0]+n)*q(e[1]+n)  for e in G.edges)
step = 1

#mixer and init state
mixer = 0.0
init = Circuit()
for i in range(n):
mixer += 0.5*X[i]*X[i+n] + 0.5*Y[i]*Y[i+n]
init.h[i].cx[i,i+n].x[i]

result = qaoa(hamiltonian, step, init, mixer)
b = result.circuit.run(shots=10)
sample = b.most_common(1)[0][0]
print("sample:"+ str(sample))

nx.draw(G, **options, node_color=[int(s) for s in list(sample[:n])])

# Linear regression calculation

Next is the linear regression problem. This one has a construction method in the paper.

Reference: https://arxiv.org/abs/2008.02355

First, consider a vector Y of objective variables, a matrix X of explanatory variables and a vector of weights w. We consider the model to minimize the error between the explanatory variable X and Y predicted from the weights w.

$min_wE(x) = ||Xw - Y||^2$

now this will be

$min_wE(x) = w^TX^TXw -2w^TX^TY + Y^TY$

Here $Y^TY$ is constant variable so can be removed from optimization

$min_wE(x) = w^TX^TXw -2w^TX^TY$

And we solve it. Here we have a problem that QUBO is using 01 variables. So we use $w = P \hat w$ and encode continuous variables.

$min_wE(x) = \hat w^TP^TX^TX P \hat w -2 \hat w^TP^TX^TY$

Finally with $A=P^TX^TXP$ and $b=P^TX^TY$, we get,

$min_w E(x) = \hat w^T A \hat w + \hat w^T b$

We just have to minimize this equation. First to make w we set variable K

import numpy as np

K = 2 # bit number for weight
d = 3 # Number of features

p = [2 ** (-i-1) for i in range(K)]
I = np.eye(d)
P = np.kron(I, p)
w = np.array([0.25, 0.75, 0.5])
X = np.random.rand(100, 3)

y = X @ w + np.random.normal(scale = 0.05, size = X.shape[0])
A = np.dot(np.dot(P.T, X.T), np.dot(X, P))
b = -2 * np.dot(np.dot(P.T, X.T), y)
QUBO = A + np.diag(b)
QUBO = np.triu(QUBO + QUBO.T) - np.eye(QUBO.shape[0]) * QUBO
from blueqat.utils import qaoa
from blueqat.pauli import from_qubo

step = 2
hamiltonian = from_qubo(QUBO)

result = qaoa(hamiltonian, step)
b = result.circuit.run(shots=100)
sample = b.most_common(1)[0][0]
print(sample)
011111

w_qa = P @ [int(i) for i in list(sample)]

print("Predicted weight:", w_qa)
print("True weight:", w)
Predicted weight: [0.25 0.75 0.75]

True weight: [0.25 0.75 0.5 ]


Occasionally we get result

## Yuichiro Minato

@yuichiro_minato2

blueqat CEO/CTO