This is the fourth internship lecture. This time we will leave QAOA and do quantum machine learning.
Machine learning on NISQ quantum computation is
The quantum circuit is described by a gate with 1 qubit (bias?) and 2 qubits (weight?).
Find the expectation value of the Hamiltonian from the measurement results of the quantum circuit and put it into the loss function.
Vary the angles and optimize using derivatives.
That's the three steps of the process. We will immediately implement this using the knowledge from the intern lecture so far.
This time we will use a model where the data is input as continuous values.
Model
This time as a model, we bring the NN circuit layer first and use the circuit to input the data after. The data is put in as continuous values as the angles of the quantum gates.
The first three are one qubit, the next loop is two qubits. Then RX is the data input; data goes into abcd. After that comes the data adjustment layer, and finally the measurement.
Copy
!pip install sklearn
Requirement already satisfied: sklearn in /opt/conda/lib/python3.10/site-packages (0.0)
Requirement already satisfied: scikit-learn in /opt/conda/lib/python3.10/site-packages (from sklearn) (1.1.2)
Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/conda/lib/python3.10/site-packages (from scikit-learn->sklearn) (3.1.0)
Requirement already satisfied: scipy>=1.3.2 in /opt/conda/lib/python3.10/site-packages (from scikit-learn->sklearn) (1.8.1)
Requirement already satisfied: numpy>=1.17.3 in /opt/conda/lib/python3.10/site-packages (from scikit-learn->sklearn) (1.21.0)
Requirement already satisfied: joblib>=1.0.0 in /opt/conda/lib/python3.10/site-packages (from scikit-learn->sklearn) (1.1.0)
I'll start with rent in Tama Plaza as usual.
Copy
from blueqat import Circuit
from blueqat.pauli import Z
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
#data
data = np.array([[8,3,83.74,18,22],[5,2,75.72,19,19],[7,2,31.47,46,7.8],[18,2,46.62,36,8],[8,3,41.02,49,8],[8,1,70.43,25,15.8],[8,1,70.43,25,15.8],[12,1,48.02,3,12.5],[10,4,58.57,36,11.8]])
df = pd.DataFrame(data,columns=['walk','floor','area','age','rent'])
#rent data
y = df["rent"]
#others
X = df.drop(columns=["rent"], axis=1)
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=1)
Prepare a quantum circuit. In this case, the data input is 4, so prepare 4 qubits.
Copy
N = 4
We will prepare 24 initial parameters for this quantum circuit.
Copy
np.random.seed = 1
params = [np.random.rand() for _ in range(N*6)]
Next, the measurement is expected to be performed with a shorter circuit and less quantum entanglement this time, so the results of the measurement of all the qubits are used.
Copy
hamiltonian = 1*Z[0]*Z[1]*Z[2]*Z[3]
Prepare the error for numerical differentiation and set the learning rate.
Copy
h = 1e-6
e = 0.1
We turn the learning loop. Learning with differentiation.
Copy
arr_loss = []
for k in range(100):
for item, answer in zip(X_train.values, y_train.values):
circuit = Circuit()
for i in range(N):
circuit.rx(params[i*3+0])[i].ry(params[i*3+1])[i].rz(params[i*3+2])[i]
circuit.cz[0,1].cz[1,2].cz[2,3].cz[3,0]
for i in range(N):
circuit.rx(np.pi*2*item[i]/100)[i]
for i in range(N):
circuit.rx(params[i*3+N*3])[i].ry(params[i*3+N*3+1])[i].rz(params[i*3+N*3+2])[i]
expt = circuit.run(hamiltonian = hamiltonian)
loss = (expt - answer/100)**2
grad_temp = np.zeros(N*6)
for j in range(N*6):
params_temp = params[:]
angle_temp = params_temp[j]
params_temp[j] += h
circuit = Circuit()
for i in range(N):
circuit.rx(params_temp[i*3+0])[i].ry(params_temp[i*3+1])[i].rz(params_temp[i*3+2])[i]
circuit.cz[0,1].cz[1,2].cz[2,3].cz[3,0]
for i in range(N):
circuit.rx(np.pi*2*item[i]/100)[i]
for i in range(N):
circuit.rx(params_temp[i*3+N*3])[i].ry(params_temp[i*3+N*3+1])[i].rz(params_temp[i*3+N*3+2])[i]
expt_temp = circuit.run(hamiltonian = hamiltonian)
loss_temp = (expt_temp - answer/100)**2
grad_temp[j] = (loss_temp - loss)/h
params -= grad_temp*e
arr_loss.append(loss)
print(loss)
import matplotlib.pyplot as plt
plt.plot(arr_loss)
plt.show()
<Figure size 432x288 with 1 Axes>
Finally, we will use the data for testing to make a prediction.
Copy
X_test = [9,5,58.3,34]
circuit = Circuit()
for i in range(N):
circuit.rx(params[i*3+0])[i].ry(params[i*3+1])[i].rz(params[i*3+2])[i]
circuit.cz[0,1].cz[1,2].cz[2,3].cz[3,0]
for i in range(N):
circuit.rx(np.pi*2*X_test[i]/100)[i]
for i in range(N):
circuit.rx(params[i*3+N*3])[i].ry(params[i*3+N*3+1])[i].rz(params[i*3+N*3+2])[i]
expt = circuit.run(hamiltonian = hamiltonian)
print(expt*100)
12.649119097171557
126,000 yen. In reality, it is better to stack multiple layers of quantum circuits, but this time we tried to do it in a single layer. That's all.