Quantum Clustering¶
quanta.layer3.clustering ¶
quanta.layer3.clustering — Quantum Clustering via Swap Test.
Implements quantum-enhanced clustering using the swap test circuit to compute data point similarities. The swap test measures |⟨ψ|φ⟩|² between two quantum states with a single measurement.
Pipeline
- Encode data points as quantum states (amplitude encoding)
- Compute pairwise distances via swap test circuits
- Run k-means assignment using quantum distance matrix
- Iterate until convergence
Classical k-means: O(nkd) per iteration (n points, k clusters, d dimensions) Quantum advantage: Distance computation O(log d) vs O(d) per pair
Example
from quanta.layer3.clustering import quantum_cluster data = [[1, 2], [1.5, 1.8], [5, 8], [8, 8], [1, 0.6], [9, 11]] result = quantum_cluster(data, k=2) print(result.labels) [0, 0, 1, 1, 0, 1]
ClusterResult
dataclass
¶
Result of quantum clustering.
Attributes:
| Name | Type | Description |
|---|---|---|
labels |
list[int]
|
Cluster assignment for each data point. |
centroids |
list[list[float]]
|
Final centroid positions. |
k |
int
|
Number of clusters. |
iterations |
int
|
Convergence iterations used. |
inertia |
float
|
Sum of squared distances to centroids. |
distance_matrix |
list[list[float]]
|
Quantum-computed pairwise distances. |
Source code in quanta/layer3/clustering.py
quantum_cluster ¶
quantum_cluster(
data: list[list[float]] | ndarray,
k: int = 2,
max_iterations: int = 20,
shots: int = 1024,
seed: int | None = None,
) -> ClusterResult
Quantum k-means clustering using swap test distances.
Uses quantum swap test circuits to compute pairwise distances between data points, then assigns clusters via nearest centroid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[list[float]] | ndarray
|
List of data points (n_points × n_features). |
required |
k
|
int
|
Number of clusters. |
2
|
max_iterations
|
int
|
Maximum iteration count. |
20
|
shots
|
int
|
Measurement shots per distance computation. |
1024
|
seed
|
int | None
|
Random seed. |
None
|
Returns:
| Type | Description |
|---|---|
ClusterResult
|
ClusterResult with labels, centroids, and metrics. |
Example
data = [[1, 2], [1.5, 1.8], [5, 8], [8, 8], [1, 0.6], [9, 11]] result = quantum_cluster(data, k=2, seed=42) print(result.labels)
Source code in quanta/layer3/clustering.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
quantum_distance ¶
quantum_distance(
point_a: list[float] | ndarray,
point_b: list[float] | ndarray,
shots: int = 1024,
seed: int | None = None,
) -> float
Computes quantum distance between two data points via swap test.
The swap test circuit
- Prepare ancilla |0⟩, encode |ψ⟩ and |φ⟩
- H on ancilla
- Controlled-SWAP between |ψ⟩ and |φ⟩ registers
- H on ancilla
- Measure ancilla: P(|0⟩) = (1 + |⟨ψ|φ⟩|²) / 2
Distance = √(1 - |⟨ψ|φ⟩|²)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point_a
|
list[float] | ndarray
|
First data point. |
required |
point_b
|
list[float] | ndarray
|
Second data point. |
required |
shots
|
int
|
Measurement shots. |
1024
|
seed
|
int | None
|
Random seed. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Quantum distance in [0, 1]. |
Source code in quanta/layer3/clustering.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |