Quantum Machine Learning¶
quanta.layer3.qml ¶
quanta.layer3.qml -- Quantum Machine Learning (QML) module.
Provides quantum-enhanced classification and regression using parameterized quantum circuits (PQC) with classical optimizers.
Architecture
- QuantumFeatureMap: Encodes classical data into quantum states
- QuantumKernel: Quantum kernel for SVM-style classification
- QuantumClassifier: End-to-end variational quantum classifier
Supported feature maps
- ZZFeatureMap: Entangling feature map using ZZ interactions
- AngleEncoding: Simple RY rotation encoding
- AmplitudeEncoding: Log-depth amplitude encoding
Example
from quanta.layer3.qml import QuantumClassifier clf = QuantumClassifier(n_qubits=4, n_layers=2, feature_map="angle") clf.fit(X_train, y_train, epochs=50) predictions = clf.predict(X_test) print(f"Accuracy: {clf.score(X_test, y_test):.2%}")
Theory
Variational quantum classifiers use: 1. Feature map U_φ(x): Encodes input x into quantum state 2. Variational ansatz V(θ): Parameterized unitary 3. Measurement: Extracts class probabilities
Total unitary: |ψ⟩ = V(θ) · U_φ(x) · |0⟩ Loss: cross-entropy between measured probabilities and labels
QMLResult
dataclass
¶
Result of quantum machine learning training.
Attributes:
| Name | Type | Description |
|---|---|---|
accuracy |
float
|
Training accuracy. |
loss_history |
list[float]
|
Loss values during training. |
predictions |
ndarray
|
Last predictions made. |
n_qubits |
int
|
Number of qubits used. |
n_params |
int
|
Number of trainable parameters. |
Source code in quanta/layer3/qml.py
QuantumClassifier ¶
Variational Quantum Classifier (VQC).
End-to-end quantum classification pipeline
- Feature map encodes classical data → quantum state
- Variational ansatz parameterizes the classifier
- Measurement gives class probabilities
- Classical optimizer updates parameters
Example
clf = QuantumClassifier(n_qubits=4, n_layers=2) clf.fit(X_train, y_train, epochs=30) acc = clf.score(X_test, y_test) print(f"Accuracy: {acc:.2%}")
Source code in quanta/layer3/qml.py
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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | |
__init__ ¶
__init__(
n_qubits: int = 4,
n_layers: int = 2,
feature_map: str = "angle",
learning_rate: float = 0.1,
seed: int | None = None,
) -> None
Initialize quantum classifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits. |
4
|
n_layers
|
int
|
Variational ansatz depth. |
2
|
feature_map
|
str
|
"angle", "zz", or "amplitude". |
'angle'
|
learning_rate
|
float
|
Gradient descent step size. |
0.1
|
seed
|
int | None
|
Random seed. |
None
|
Source code in quanta/layer3/qml.py
fit ¶
Train the quantum classifier.
Uses parameter-shift rule for quantum-native gradients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Training data (n_samples, n_features). |
required |
y
|
ndarray
|
Binary labels (0 or 1). |
required |
epochs
|
int
|
Number of training epochs. |
30
|
Returns:
| Type | Description |
|---|---|
QMLResult
|
QMLResult with accuracy and loss history. |
Source code in quanta/layer3/qml.py
predict ¶
Predict class labels for samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Data matrix (n_samples, n_features). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of predicted labels (0 or 1). |
Source code in quanta/layer3/qml.py
predict_proba ¶
Predict class probabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Data matrix (n_samples, n_features). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of (P(class=0), P(class=1)) per sample. |
Source code in quanta/layer3/qml.py
score ¶
Compute accuracy on test data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Test data. |
required |
y
|
ndarray
|
True labels. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Accuracy as a float. |
Source code in quanta/layer3/qml.py
QuantumKernel ¶
Quantum kernel for kernel-based classification.
Computes kernel matrix K(x_i, x_j) = |⟨φ(x_i)|φ(x_j)⟩|² using the ZZ feature map for quantum advantage.
Example
kernel = QuantumKernel(n_qubits=4, feature_map="zz") K = kernel.matrix(X_train)
Use with any kernel SVM¶
Source code in quanta/layer3/qml.py
__init__ ¶
Initialize quantum kernel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_qubits
|
int
|
Number of qubits. |
4
|
feature_map
|
str
|
"zz" or "angle". |
'zz'
|
reps
|
int
|
Feature map repetitions. |
2
|
Source code in quanta/layer3/qml.py
evaluate ¶
Compute kernel value K(x1, x2) = |⟨φ(x1)|φ(x2)⟩|².
matrix ¶
Compute kernel matrix for dataset X.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Data matrix (n_samples, n_features). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
(n_samples, n_samples) kernel matrix. |
Source code in quanta/layer3/qml.py
amplitude_encoding ¶
Amplitude encoding: embed data directly into state amplitudes.
Encodes 2^n features into n qubits. Input is normalized to unit vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sim
|
SimulatorBackend
|
Simulator to apply encoding. |
required |
x
|
ndarray
|
Feature vector (length must be ≤ 2^n_qubits). |
required |
Source code in quanta/layer3/qml.py
angle_encoding ¶
Encode features via RY rotations.
Each feature x_i → RY(π · x_i) on qubit i. Simple and effective for small feature spaces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sim
|
SimulatorBackend
|
Simulator to apply encoding. |
required |
x
|
ndarray
|
Feature vector (values in [0, 1]). |
required |
qubits
|
tuple[int, ...] | None
|
Which qubits to use (default: first len(x)). |
None
|
Source code in quanta/layer3/qml.py
zz_feature_map ¶
zz_feature_map(
sim: SimulatorBackend,
x: ndarray,
qubits: tuple[int, ...] | None = None,
reps: int = 2,
) -> None
ZZ entangling feature map.
Encodes features with entanglement
- H on all qubits
- RZ(x_i) on each qubit
- RZZ(x_i * x_j) on adjacent pairs
Repeated reps times for expressiveness.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sim
|
SimulatorBackend
|
Simulator to apply encoding. |
required |
x
|
ndarray
|
Feature vector. |
required |
qubits
|
tuple[int, ...] | None
|
Which qubits to use. |
None
|
reps
|
int
|
Number of repetitions. |
2
|