6
6
7
7
8
8
def generate_data (n_samples = 100 , n_features = 2 , n_clusters = 2 ):
9
- 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
+ )
10
12
return MinMaxScaler ().fit_transform (data ), labels
11
13
14
+
12
15
def quantum_distance (point1 , point2 ):
13
16
"""
14
17
Computes the quantum distance between two points.
@@ -25,14 +28,12 @@ def quantum_distance(point1, point2):
25
28
qubit = cirq .LineQubit (0 )
26
29
diff = np .clip (np .linalg .norm (point1 - point2 ), 0 , 1 )
27
30
theta = 2 * np .arcsin (diff )
28
-
29
- circuit = cirq .Circuit (
30
- cirq .ry (theta )(qubit ),
31
- cirq .measure (qubit , key = 'result' )
32
- )
33
-
31
+
32
+ circuit = cirq .Circuit (cirq .ry (theta )(qubit ), cirq .measure (qubit , key = "result" ))
33
+
34
34
result = cirq .Simulator ().run (circuit , repetitions = 1000 )
35
- return result .histogram (key = 'result' ).get (1 , 0 ) / 1000
35
+ return result .histogram (key = "result" ).get (1 , 0 ) / 1000
36
+
36
37
37
38
def initialize_centroids (data : np .ndarray , k : int ) -> np .ndarray :
38
39
"""
@@ -48,28 +49,34 @@ def initialize_centroids(data: np.ndarray, k: int) -> np.ndarray:
48
49
"""
49
50
return data [np .random .choice (len (data ), k , replace = False )]
50
51
52
+
51
53
def assign_clusters (data , centroids ):
52
54
clusters = [[] for _ in range (len (centroids ))]
53
55
for point in data :
54
- closest = min (range (len (centroids )), key = lambda i : quantum_distance (point , centroids [i ]))
56
+ closest = min (
57
+ range (len (centroids )), key = lambda i : quantum_distance (point , centroids [i ])
58
+ )
55
59
clusters [closest ].append (point )
56
60
return clusters
57
61
62
+
58
63
def recompute_centroids (clusters ):
59
64
return np .array ([np .mean (cluster , axis = 0 ) for cluster in clusters if cluster ])
60
65
66
+
61
67
def quantum_kmeans (data , k , max_iters = 10 ):
62
68
centroids = initialize_centroids (data , k )
63
-
69
+
64
70
for _ in range (max_iters ):
65
71
clusters = assign_clusters (data , centroids )
66
72
new_centroids = recompute_centroids (clusters )
67
73
if np .allclose (new_centroids , centroids ):
68
74
break
69
75
centroids = new_centroids
70
-
76
+
71
77
return centroids , clusters
72
78
79
+
73
80
# Main execution
74
81
n_samples , n_clusters = 10 , 2
75
82
data , labels = generate_data (n_samples , n_clusters = n_clusters )
@@ -85,12 +92,20 @@ def quantum_kmeans(data, k, max_iters=10):
85
92
plt .subplot (122 )
86
93
for i , cluster in enumerate (final_clusters ):
87
94
cluster = np .array (cluster )
88
- plt .scatter (cluster [:, 0 ], cluster [:, 1 ], label = f'Cluster { i + 1 } ' )
89
- plt .scatter (final_centroids [:, 0 ], final_centroids [:, 1 ], color = 'red' , marker = 'x' , s = 200 , linewidths = 3 , label = 'Centroids' )
95
+ plt .scatter (cluster [:, 0 ], cluster [:, 1 ], label = f"Cluster { i + 1 } " )
96
+ plt .scatter (
97
+ final_centroids [:, 0 ],
98
+ final_centroids [:, 1 ],
99
+ color = "red" ,
100
+ marker = "x" ,
101
+ s = 200 ,
102
+ linewidths = 3 ,
103
+ label = "Centroids" ,
104
+ )
90
105
plt .title ("Quantum k-Means Clustering with Cirq" )
91
106
plt .legend ()
92
107
93
108
plt .tight_layout ()
94
109
plt .show ()
95
110
96
- print (f"Final Centroids:\n { final_centroids } " )
111
+ print (f"Final Centroids:\n { final_centroids } " )
0 commit comments