-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
123 lines (91 loc) · 3.54 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from dataset import Kmeans_dataset
from KMeans_code.delta_KMeans import DeltaKMeans
from KMeans_code.KMeans_file import ClassicKMeans
from QMeans_code.my_qmeans import QMeans
from config import IBM_QUANTUM_API_TOKEN, IONQ_API_TOKEN
from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit_ionq import IonQProvider
from plotly.subplots import make_subplots
from plotly.offline import plot
import warnings
warnings.filterwarnings("ignore", category=RuntimeWarning)
# QiskitRuntimeService.save_account(channel="ibm_cloud", token=IBM_QUANTUM_API_TOKEN, instance='ibm-q/open/main')
if __name__ == "__main__":
print("[INFO]: Starting\n")
# ======================== Data ======================== #
source = "varied"
n_clusters = 4
n_features = 2
n_samples = 80
random_state = 999
cluster_std = 0.045
test_size = 0.15
delta_init = 0.0005
service = "IBM"
if service == "IBM":
print("Using IBM Quantum Service")
service = QiskitRuntimeService()
backend = service.backend("simulator_mps")
else:
print("Using IonQ Quantum Service")
provider = IonQProvider(IONQ_API_TOKEN)
backend = provider.get_backend("ionq_simulator")
shots = 2048
data = Kmeans_dataset(
source=source,
n_samples=n_samples,
n_features=n_features,
n_clusters=n_clusters,
cluster_std=cluster_std,
random_state=random_state,
test_size=test_size,
)
X_train, X_test, y_train, y_test = data.get_dataset()
data.plot_dataset(show_split=False)
# ======================== Model ======================== #
# Classic KMeans
classic_kmeans = ClassicKMeans(n_clusters=n_clusters, random_state=random_state)
# Delta KMeans
delta_kmeans = DeltaKMeans(
n_clusters=n_clusters, random_state=random_state, delta_init=delta_init
)
# QMeans
qmeans = QMeans(
n_clusters=n_clusters,
random_state=random_state,
init_method="kmeans++",
backend=backend,
)
# ======================== Training ======================== #
print("Start training Classic KMeans...")
classic_kmeans.fit(X_train, X_test, y_test)
print("Start training Delta KMeans...")
delta_kmeans.fit(X_train, X_test, y_test)
print("Start training QMeans...")
qmeans.fit(X_train, X_test, y_test, shots=shots)
print("Training done.")
# ======================== Subplots results ======================== #
classic_kmeans_fig = classic_kmeans.plot_data_with_labels(X_train, return_fig=True)
delta_kmeans_fig = delta_kmeans.plot_data_with_labels(X_train, return_fig=True)
qmeans_fig = qmeans.plot_data_with_labels(X_train, return_fig=True)
fig = make_subplots(
rows=1,
cols=3,
subplot_titles=("KMeans", "Delta KMeans", "QMeans"),
shared_yaxes=True,
)
fig.add_trace(classic_kmeans_fig.data[0], row=1, col=1)
fig.add_trace(classic_kmeans_fig.data[1], row=1, col=1)
fig.add_trace(delta_kmeans_fig.data[0], row=1, col=2)
fig.add_trace(delta_kmeans_fig.data[1], row=1, col=2)
fig.add_trace(qmeans_fig.data[0], row=1, col=3)
fig.add_trace(qmeans_fig.data[1], row=1, col=3)
fig.update_layout(
showlegend=False,
template="plotly_white",
)
# Ratio 1:1 des subplots
fig.update_yaxes(scaleanchor="x", scaleratio=1, row=1, col=1)
fig.update_yaxes(scaleanchor="x", scaleratio=1, row=1, col=2)
fig.update_yaxes(scaleanchor="x", scaleratio=1, row=1, col=3)
plot(fig, filename="images/comparaisons.html")