In [ ]:
import qiskit 
import qiskit.quantum_info as qiskit_quantum_info
import numpy as np
from importlib import reload
import qmlmodel, iris
In [15]:
reload(qmlmodel)

x= np.random.rand(4)*np.pi*2
t= np.random.rand(4)*np.pi*2
qc = qmlmodel.make_circuit(x,t)
qc.draw(output='mpl')
Out[15]:
No description has been provided for this image
In [18]:
reload(qmlmodel)
reload(iris)

X_min, X_max = np.min(iris.X, axis=0), np.max(iris.X, axis=0)

X_train = qmlmodel.rescale_to_angle(iris.X_train, X_min, X_max)
In [17]:
np.set_printoptions(precision=3, suppress=True)

qmlmodel.predict_proba(qmlmodel.rescale_to_angle(iris.X_test,X_min,X_max),t).argmax(axis=1)
Out[17]:
array([2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
       2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1,
       1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1])
In [25]:
reload(qmlmodel)

X_min, X_max = np.min(iris.X, axis=0), np.max(iris.X, axis=0)

X_train_rescaled = qmlmodel.rescale_to_angle(iris.X_train, 
                                             X_min, X_max)
X_test_rescaled = qmlmodel.rescale_to_angle(iris.X_test, 
                                             X_min, X_max)
In [26]:
reload(qmlmodel)

t= np.random.rand(4)*np.pi*2

qmlmodel.loss(t, X_train_rescaled, iris.y_train)

qmlmodel.predict(X_test_rescaled,t)
Out[26]:
array([0, 2, 2, 1, 1, 2, 1, 0, 1, 0, 0, 1, 0, 0, 2, 0, 0, 1, 2, 1, 0, 2,
       0, 0, 1, 1, 1, 0, 0, 2, 0, 0, 0, 2, 1, 1, 2, 1, 1, 1, 0, 0, 2, 0,
       0, 1, 0, 0, 0, 0, 2, 2, 1, 0, 0, 0, 0, 1, 1, 1])
In [32]:
reload(qmlmodel)
reload(iris)
from sklearn.metrics import accuracy_score


X_train_rescaled = qmlmodel.rescale_to_angle(iris.X_train, 
                                             X_min, X_max)
X_test_rescaled = qmlmodel.rescale_to_angle(iris.X_test,
                                            X_min, X_max)
def objective_function(t):
    return qmlmodel.loss(t,X_train_rescaled,iris.y_train)
In [47]:
reload(iris)
reload(qmlmodel)
from scipy.optimize import minimize
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt


np.random.seed(42)
t0= np.random.rand(4)*np.pi*2


optimization_path = []
accuracy_path=[]

def objective_function_with_memory(t):
    z = objective_function(t)
    optimization_path.append(z)
    accuracy = accuracy_score(iris.y_train, qmlmodel.predict(X_train_rescaled, t))
    accuracy_path.append(accuracy*100)
    return z

result = minimize(objective_function_with_memory, 
                  t0, method='COBYLA',options={'maxiter': 20} )

print(result)

y_pred = qmlmodel.predict(X_train_rescaled, result.x)
accuracy = accuracy_score(iris.y_train, y_pred)
print( f" \nAccuracy on training set: {accuracy*100:.2f}%")

plt.plot(list(range(len(accuracy_path)))[10:], 
         accuracy_path[10:], marker='o', linestyle='-')
# plt.plot(list(range(len(optimization_path))), 
#          optimization_path, marker='o', linestyle='-')

plt.grid(True)
plt.show()
 message: Maximum number of function evaluations has been exceeded.
 success: False
  status: 2
     fun: 0.8255113758267565
       x: [ 5.419e+00  4.032e+00  7.738e+00  5.264e+00]
    nfev: 20
   maxcv: 0.0
 
Accuracy on training set: 91.11%
No description has been provided for this image
In [48]:
y_pred = qmlmodel.predict(X_test_rescaled, result.x)
accuracy = accuracy_score(iris.y_test, y_pred)
print( f" \nAccuracy on test set: {accuracy*100:.2f}%")
 
Accuracy on test set: 95.00%