Requirement already satisfied: scikit-learn in /opt/conda/lib/python3.10/site-packages (1.1.2)
Requirement already satisfied: numpy>=1.17.3 in /opt/conda/lib/python3.10/site-packages (from scikit-learn) (1.21.0)
Requirement already satisfied: joblib>=1.0.0 in /opt/conda/lib/python3.10/site-packages (from scikit-learn) (1.2.0)
Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/conda/lib/python3.10/site-packages (from scikit-learn) (3.1.0)
Requirement already satisfied: scipy>=1.3.2 in /opt/conda/lib/python3.10/site-packages (from scikit-learn) (1.8.1)
Copy
from blueqat import Circuit
import numpy as np
from sklearn.svm import SVC
from sklearn.datasets import load_iris
from sklearn.datasets import make_moons
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
def print_Zbasis_expression(statevector):
nqubit = int(np.log2(np.size(statevector)))
for i in range(2**nqubit):
print('+({:.2f})*|{number:0{width}b}>'.format(statevector[i],number=i,width=nqubit),end='')
def myplot_histogram(sv,n,figsize=(20,10),fontsize=24):
probs = np.abs(sv)**2
## z方向に射影測定した時に得られる可能性があるビット列
z_basis = [format(i,"b").zfill(n) for i in range(probs.size)]
plt.figure(figsize=figsize)
plt.xlabel("states",fontsize=fontsize)
plt.ylabel("probability(%)",fontsize=fontsize)
plt.xticks(fontsize=fontsize)
plt.yticks(fontsize=fontsize)
plt.bar(z_basis, probs*100)
plt.show()
関数の定義
データxの埋め込み回路 ϕ(x)
Copy
def data_encoding(data):
circuit = Circuit(2)
for i,x in enumerate(data):
circuit.rx(x)[i]
return circuit
Copy
data = np.array([[0.1,0.2],[0.5,0.6],[0.3,0.4],[-0.2,0.6]])
data_encoding(data[0]).run(backend="draw") # show a circuit
def kernel_matrix(A, B):
"""Compute the matrix whose entries are the kernel
evaluated on pairwise data from sets A and B."""
return np.array([[kernel(a, b) for b in B] for a in A])
Copy
mat = kernel_matrix(data,data)
fig, ax = plt.subplots()
ax.imshow(mat)
plt.show()
<Figure size 432x288 with 1 Axes>
よくわからないが、対称行列であることはわかります。
サンプルデータセットでやってみる
Iris data set (2値分類)
Copy
np.random.seed(42)
X, y = load_iris(return_X_y=True)
# pick inputs and labels from the first two classes only,
# corresponding to the first 100 samples
X = X[0:100,0:2]
y = y[0:100]
# scaling the inputs is important since the embedding we use is periodic
scaler = StandardScaler().fit(X)
X_scaled = scaler.transform(X)
# scaling the labels to -1, 1 is important for the SVM and the
# definition of a hinge loss
y_scaled = 2 * (y - 0.5)
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_scaled)
kernel行列の計算
Copy
mat = kernel_matrix(X_train,X_train)
fig, ax = plt.subplots()
ax.imshow(mat)
plt.show()
X, y = make_moons(n_samples=200, shuffle=True)
# pick inputs and labels from the first two classes only,
# corresponding to the first 100 samples
X = X[0:100,0:2]
y = y[0:100]
# scaling the inputs is important since the embedding we use is periodic
scaler = StandardScaler().fit(X)
X_scaled = scaler.transform(X)
# scaling the labels to -1, 1 is important for the SVM and the
# definition of a hinge loss
y_scaled = 2 * (y - 0.5)
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_scaled)