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
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 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | |
__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
QuantumRegressor ¶
Variational Quantum Regressor (VQR).
Continuous quantum regression pipeline
- Feature map encodes classical features into a quantum state.
- Variational ansatz parameterizes the circuit.
- Expectation value of Pauli-Z on readout qubit is measured.
- Trainable affine parameters (weight, bias) scale expectation in [-1, 1]
to continuous target range: y_hat = weight *
+ bias. - Parameter-shift gradients minimize Mean Squared Error (MSE) loss.
Example
reg = QuantumRegressor(n_qubits=4, n_layers=2) reg.fit(X_train, y_train, epochs=30) preds = reg.predict(X_test) print(f"R2 Score: {reg.score(X_test, y_test):.4f}")
Source code in quanta/layer3/qml.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 | |
__init__ ¶
__init__(
n_qubits: int = 4,
n_layers: int = 2,
feature_map: str = "angle",
learning_rate: float = 0.1,
optimizer: str = "adam",
seed: int | None = None,
) -> None
Initialize quantum regressor.
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
|
optimizer
|
str
|
"adam", "sgd", or "spsa". |
'adam'
|
seed
|
int | None
|
Random seed. |
None
|
Source code in quanta/layer3/qml.py
fit ¶
Train the quantum regressor using parameter-shift rule and MSE loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Training features (n_samples, n_features). |
required |
y
|
ndarray
|
Continuous target values. |
required |
epochs
|
int
|
Number of training epochs. |
30
|
Returns:
| Type | Description |
|---|---|
RegressionResult
|
RegressionResult with R^2, MSE, and loss history. |
Source code in quanta/layer3/qml.py
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 | |
predict ¶
Predict continuous targets for samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Data matrix (n_samples, n_features). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
1D array of predicted continuous values. |
Source code in quanta/layer3/qml.py
score ¶
Compute R^2 (coefficient of determination) regression score.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Test data. |
required |
y
|
ndarray
|
True continuous targets. |
required |
Returns:
| Type | Description |
|---|---|
float
|
R^2 score as a float (best possible is 1.0). |
Source code in quanta/layer3/qml.py
RegressionResult
dataclass
¶
Result of quantum machine learning regression training.
Attributes:
| Name | Type | Description |
|---|---|---|
r2 |
float
|
Coefficient of determination R^2 score. |
mse |
float
|
Mean squared error on training data. |
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
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
|