4
4
from sklearn .datasets import make_blobs
5
5
from sklearn .preprocessing import MinMaxScaler
6
6
7
+
7
8
def generate_data (n_samples = 100 , n_features = 2 , n_clusters = 2 ):
8
- data , labels = make_blobs (n_samples = n_samples , centers = n_clusters , n_features = n_features , random_state = 42 )
9
+ data , labels = make_blobs (
10
+ n_samples = n_samples , centers = n_clusters , n_features = n_features , random_state = 42
11
+ )
9
12
return MinMaxScaler ().fit_transform (data ), labels
10
13
14
+
11
15
def quantum_distance (point1 , point2 ):
12
16
"""
13
17
Quantum circuit explanation:
@@ -19,40 +23,44 @@ def quantum_distance(point1, point2):
19
23
qubit = cirq .LineQubit (0 )
20
24
diff = np .clip (np .linalg .norm (point1 - point2 ), 0 , 1 )
21
25
theta = 2 * np .arcsin (diff )
22
-
23
- circuit = cirq .Circuit (
24
- cirq .ry (theta )(qubit ),
25
- cirq .measure (qubit , key = 'result' )
26
- )
27
-
26
+
27
+ circuit = cirq .Circuit (cirq .ry (theta )(qubit ), cirq .measure (qubit , key = "result" ))
28
+
28
29
result = cirq .Simulator ().run (circuit , repetitions = 1000 )
29
- return result .histogram (key = 'result' ).get (1 , 0 ) / 1000
30
+ return result .histogram (key = "result" ).get (1 , 0 ) / 1000
31
+
30
32
31
33
def initialize_centroids (data , k ):
32
34
return data [np .random .choice (len (data ), k , replace = False )]
33
35
36
+
34
37
def assign_clusters (data , centroids ):
35
38
clusters = [[] for _ in range (len (centroids ))]
36
39
for point in data :
37
- closest = min (range (len (centroids )), key = lambda i : quantum_distance (point , centroids [i ]))
40
+ closest = min (
41
+ range (len (centroids )), key = lambda i : quantum_distance (point , centroids [i ])
42
+ )
38
43
clusters [closest ].append (point )
39
44
return clusters
40
45
46
+
41
47
def recompute_centroids (clusters ):
42
48
return np .array ([np .mean (cluster , axis = 0 ) for cluster in clusters if cluster ])
43
49
50
+
44
51
def quantum_kmeans (data , k , max_iters = 10 ):
45
52
centroids = initialize_centroids (data , k )
46
-
53
+
47
54
for _ in range (max_iters ):
48
55
clusters = assign_clusters (data , centroids )
49
56
new_centroids = recompute_centroids (clusters )
50
57
if np .allclose (new_centroids , centroids ):
51
58
break
52
59
centroids = new_centroids
53
-
60
+
54
61
return centroids , clusters
55
62
63
+
56
64
# Main execution
57
65
n_samples , n_clusters = 10 , 2
58
66
data , labels = generate_data (n_samples , n_clusters = n_clusters )
@@ -68,12 +76,20 @@ def quantum_kmeans(data, k, max_iters=10):
68
76
plt .subplot (122 )
69
77
for i , cluster in enumerate (final_clusters ):
70
78
cluster = np .array (cluster )
71
- plt .scatter (cluster [:, 0 ], cluster [:, 1 ], label = f'Cluster { i + 1 } ' )
72
- plt .scatter (final_centroids [:, 0 ], final_centroids [:, 1 ], color = 'red' , marker = 'x' , s = 200 , linewidths = 3 , label = 'Centroids' )
79
+ plt .scatter (cluster [:, 0 ], cluster [:, 1 ], label = f"Cluster { i + 1 } " )
80
+ plt .scatter (
81
+ final_centroids [:, 0 ],
82
+ final_centroids [:, 1 ],
83
+ color = "red" ,
84
+ marker = "x" ,
85
+ s = 200 ,
86
+ linewidths = 3 ,
87
+ label = "Centroids" ,
88
+ )
73
89
plt .title ("Quantum k-Means Clustering with Cirq" )
74
90
plt .legend ()
75
91
76
92
plt .tight_layout ()
77
93
plt .show ()
78
94
79
- print (f"Final Centroids:\n { final_centroids } " )
95
+ print (f"Final Centroids:\n { final_centroids } " )
0 commit comments