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)
まずはいつも通りたまプラーザの家賃でやってみます。
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)
早速回路の準備です。今回はデータ入力が4なので、4量子ビット用意します。
Copy
N = 4
初期パラメータは今回の量子回路のために24個準備します。
Copy
np.random.seed = 1
params = [np.random.rand() for _ in range(N*6)]
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>
最後にテスト用のデータを使って予測をしてみます。
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)