PyTorch Quantum Layer & Continuous Resonance¶
quanta.torch.layer ¶
quanta.torch.layer — Variational Quantum Circuit Layer for PyTorch.
Provides QuantumLayer(nn.Module), an autograd-differentiable quantum circuit layer supporting arbitrary variational ansatzes, flexible Pauli observable readouts, and exact analytical gradients via the Parameter-Shift Rule.
ObservableParser ¶
Compiles diverse user observable inputs into canonical ParsedObservables.
Source code in quanta/torch/layer.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 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 | |
parse_all
classmethod
¶
Parse observable specifications into canonical form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observables
|
Sequence[Any] | None
|
None, list of strings, list of tuples, or nested lists. |
required |
num_qubits
|
int
|
Number of qubits in the circuit. |
required |
Returns:
| Type | Description |
|---|---|
list[ParsedObservable]
|
List of ParsedObservable instances. |
Source code in quanta/torch/layer.py
ParsedObservable
dataclass
¶
A quantum observable composed of one or more Pauli terms.
Attributes:
| Name | Type | Description |
|---|---|---|
terms |
tuple[PauliTerm, ...]
|
Tuple of Pauli terms summing to the observable H = Σ c_i P_i. |
name |
str
|
Human-readable display label. |
Source code in quanta/torch/layer.py
PauliTerm
dataclass
¶
A single Pauli tensor product with a scalar coefficient.
Attributes:
| Name | Type | Description |
|---|---|---|
pauli_string |
str
|
String of length num_qubits, e.g. "ZII", "IXY". |
coeff |
float
|
Real coefficient. |
Source code in quanta/torch/layer.py
QuantumLayer ¶
Bases: Module
Variational Quantum Circuit Layer for PyTorch.
Accepts classical input tensors and evaluates expectation values of specified quantum observables across parameterized quantum states. Differentiable via the analytical Parameter-Shift Rule for both variational parameters and inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_qubits
|
int
|
Number of simulated qubits (>= 1). |
required |
circuit_fn
|
Callable[..., Any] | Any | str
|
Ansatz preset name ('hardware_efficient', 'strongly_entangling', 'reuploading', 'real_amplitudes'), or a custom Callable. |
'hardware_efficient'
|
num_layers
|
int
|
Depth of the variational circuit (>= 1). |
1
|
observables
|
Sequence[Any] | None
|
Observables to measure. Defaults to [Z0, Z1, ..., Z_{N-1}]. |
None
|
diff_method
|
str
|
Differentiation method ('parameter-shift' or 'finite-diff'). |
'parameter-shift'
|
device
|
str | device | None
|
Torch device for execution and parameters. |
None
|
dtype
|
dtype
|
Floating-point precision (torch.float32 or torch.float64). |
float32
|
init_method
|
str
|
Weight initialization scheme ('uniform', 'normal', 'zeros'). |
'uniform'
|
encoding
|
str
|
Feature encoding method ('angle'). |
'angle'
|
num_params
|
int | None
|
Optional explicit parameter count override for custom callables. |
None
|
Examples:
>>> import torch
>>> import torch.nn as nn
>>> from quanta.torch import QuantumLayer
>>>
>>> layer = QuantumLayer(num_qubits=4, num_layers=2)
>>> x = torch.randn(8, 4)
>>> y = layer(x)
>>> y.shape
torch.Size([8, 4])
>>>
>>> # Sequential model integration
>>> model = nn.Sequential(
... nn.Linear(2, 4),
... QuantumLayer(num_qubits=4, num_layers=1),
... nn.Linear(4, 1),
... )
Source code in quanta/torch/layer.py
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 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 | |
forward ¶
Executes forward simulation and returns observable expectations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input feature tensor of shape (B, in_features), (in_features,), or (*batch, in_features). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Expectation tensor of shape (B, out_features), (out_features,), |
Tensor
|
or (*batch, out_features). |
Source code in quanta/torch/layer.py
reset_parameters ¶
Initialize variational parameters.
Source code in quanta/torch/layer.py
get_ansatz_param_count ¶
Compute total parameter count for built-in ansatz presets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ansatz
|
str
|
Name of ansatz preset. |
required |
num_qubits
|
int
|
Number of qubits. |
required |
num_layers
|
int
|
Number of variational layers. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Integer parameter count. |
Source code in quanta/torch/layer.py
quanta.torch.continuous ¶
quanta.torch.continuous — Continuous Resonant Quantum Neural Network Layer.
Pillar 2 of Quanta SDK (Milestone 3): Brain-inspired continuous-time quantum resonance, non-local quantum coherence, and simultaneous non-sequential state evolution grounded in foundational physics (Einstein-Podolsky-Rosen non-locality, continuous-time quantum walks, many-body spin networks).
Evolves quantum statevectors continuously under a parameterized network Hamiltonian
H(x, θ) = H_XY(J) + H_Z(x, h, W) + H_X(ω) |ψ(t)⟩ = exp(-i H(x, θ) t) |ψ0⟩
with simultaneous multi-observable expectation readout across all nodes in O(2^N) time. Features exact analytical autograd gradients via Ehrenfest's theorem (for duration t) and Daleckii-Krein matrix spectral Fréchet derivatives (for J, h, W, ω, x).
ContinuousResonantLayer ¶
Bases: Module
Continuous-Time Resonant Quantum Neural Network Layer.
Evolves quantum statevectors under an interacting many-body graph Hamiltonian
H(x, θ) = H_XY(J) + H_Z(x, h, W) + H_X(ω) |ψ(t)⟩ = exp(-i H(x, θ) t) |ψ0⟩
with simultaneous multi-observable expectation readout across all nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int
|
Number of qubits/nodes in the network (>= 1). |
required |
in_features
|
int
|
Dimension of input feature vector x (>= 1). |
required |
coupling_graph
|
Tensor | Sequence[tuple[int, int]] | str
|
Interaction graph topology: - Preset string: 'complete', 'ring', 'line', 'star', 'none'. - Adjacency matrix: torch.Tensor of shape (num_nodes, num_nodes). - Custom edge list: Sequence of (u, v) tuples. |
'complete'
|
observable_types
|
tuple[str, ...]
|
Tuple of observables to measure across all nodes. Supported: ('Z', 'X'), ('Z',), ('Z', 'X', 'Y'). Output feature dimension is num_nodes * len(observable_types). |
('Z', 'X')
|
initial_state
|
str | Tensor
|
Reference initial quantum state: - Preset mode: 'zero' (|0...0>), 'plus' (|+...+>), 'ghz'. - Custom tensor: Normalized statevector of shape (2^num_nodes,). |
'zero'
|
learnable_time
|
bool
|
Whether evolution duration t is a trainable parameter. If True, registered as nn.Parameter; if False, registered as buffer. |
True
|
initial_time
|
float
|
Initial evolution duration t0 (> 0.0). Defaults to 1.0. |
1.0
|
device
|
str | device | None
|
Target execution device ('cpu', 'mps', or torch.device). Defaults to MPS if available, otherwise CPU. |
None
|
dtype
|
dtype
|
Real scalar floating-point precision (torch.float32 or torch.float64). Note: Apple Silicon MPS only supports torch.float32. |
float32
|
init_method
|
str
|
Parameter initialization strategy ('default', 'uniform', 'normal', 'zeros'). |
'default'
|
diff_method
|
str
|
Differentiation strategy ('exact', 'spectral', 'autograd', 'finite-diff'). |
'exact'
|
Examples:
>>> import torch
>>> import torch.nn as nn
>>> from quanta.torch import ContinuousResonantLayer
>>>
>>> layer = ContinuousResonantLayer(num_nodes=4, in_features=3, coupling_graph="ring")
>>> x = torch.randn(8, 3)
>>> y = layer(x)
>>> y.shape
torch.Size([8, 8])
>>>
>>> # Seamless Sequential integration
>>> model = nn.Sequential(
... nn.Linear(2, 4),
... ContinuousResonantLayer(num_nodes=4, in_features=4, coupling_graph="complete"),
... nn.Linear(8, 1),
... )
Source code in quanta/torch/continuous.py
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 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 | |
forward ¶
Executes continuous Hamiltonian evolution and returns expectation readouts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input feature tensor of shape (B, in_features), (in_features,), or (*batch, in_features). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Expectation tensor of shape (B, out_features), (out_features,), |
Tensor
|
or (*batch, out_features). |
Source code in quanta/torch/continuous.py
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 | |
reset_parameters ¶
Initializes variational parameters based on physical resonance criteria.
Source code in quanta/torch/continuous.py
GraphTopologyParser ¶
Parses diverse user graph inputs into canonical sorted edge lists.
Supports presets ('complete', 'ring', 'line', 'star', 'none'), adjacency matrices, and explicit edge sequences with strict validation.
Source code in quanta/torch/continuous.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
parse
classmethod
¶
Parses and validates graph topologies into canonical sorted edge lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Tensor | Sequence[tuple[int, int]] | str
|
Graph topology preset name, adjacency matrix, or edge sequence. |
required |
num_nodes
|
int
|
Total number of nodes/qubits (>= 1). |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[int, int]]
|
Canonical list of sorted (u, v) edge tuples with 0 <= u < v < num_nodes. |
Raises:
| Type | Description |
|---|---|
ValueError
|
On invalid node count, out-of-bounds index, self-loop, or non-symmetric adjacency matrix. |
TypeError
|
On unsupported input type. |