Spaces:
Sleeping
Sleeping
Claude Fable 5.1
Initial commit: spinor library with SU(2) rotations, autograd and nn modules
51b20c8 |
Download SPEC.md from SoulFireMage/spinor-lib: direct link, hf CLI and curl.
- Browser
- Download file 3.57 kB
-
https://huggingface.co/spaces/SoulFireMage/spinor-lib/resolve/main/SPEC.md
- Command line
-
hf download hf://spaces/SoulFireMage/spinor-lib/SPEC.md
-
curl -L -o SPEC.md https://huggingface.co/spaces/SoulFireMage/spinor-lib/resolve/main/SPEC.md
3.57 kB
| 1. Project Structure | |
| Organize the library into logical modules to maintain separation of concerns. | |
| spinor_lib/ | |
| __init__.py (Exports public API) | |
| core.py (Core Spinor class definition) | |
| ops.py (Mathematical operations) | |
| transformations.py (Rotation/SU(2) operations) | |
| utils.py (Helper functions for validation and device handling) | |
| tests/ (Unit tests) | |
| 2. Core Data Structure (core.py) | |
| Define a Spinor class that wraps a backend tensor (PyTorch/NumPy). | |
| Attributes: | |
| data: The underlying tensor. Shape should be [..., N] where N is the spinor component count (e.g., 2 for 2-component spinors). | |
| dtype: Complex or real. | |
| device: CPU/GPU. | |
| Constructor: | |
| Accept tensors, lists, or shapes. | |
| Validate that the last dimension matches the expected spinor dimension (e.g., 2). | |
| Magic Methods: Implement __repr__, __str__, __getitem__, __len__. | |
| 3. Basic Operations (ops.py) | |
| Implement algebraic operations that respect the spinor vector space structure. | |
| Arithmetic: | |
| add(s1, s2): Element-wise addition. | |
| sub(s1, s2): Element-wise subtraction. | |
| mul(s, scalar): Scalar multiplication. | |
| Products: | |
| inner_product(s1, s2): Compute conjugate transpose dot product (s1† · s2). Output scalar or tensor depending on batch. | |
| outer_product(s1, s2): Tensor product of two spinors. | |
| Norms & Projections: | |
| norm(s): L2 norm (sqrt of inner product with self). | |
| normalize(s): Normalize spinor to unit length. | |
| conjugate(s): Complex conjugate. | |
| 4. Transformations (transformations.py) | |
| Implement operations that rotate or transform the spinor state. | |
| Rotation Matrices: | |
| Create functions to generate SU(2) rotation matrices given an axis and angle (or axis-angle vector). | |
| Ensure matrices are compatible with backend broadcasting. | |
| Application: | |
| rotate(spinor, matrix): Apply matrix multiplication. | |
| global_phase(spinor, angle): Multiply by exp(i * angle). | |
| Tensor Contractions: | |
| contract(s1, s2, indices): Allow specific index contractions for higher-order operations. | |
| 5. Neural Network Integration | |
| Ensure the library plays well with standard DL workflows. | |
| Autograd Compatibility: All operations must preserve the computation graph. Avoid detaching tensors unless explicitly requested. | |
| Batching: The first dimension(s) of the input tensor are treated as batch dimensions. Operations must broadcast correctly over batches. | |
| Device Management: Ensure tensors remain on the same device (CPU/GPU) after operations. Provide a .to(device) method wrapper. | |
| 6. Implementation Steps for LLM | |
| Backend Setup: Choose PyTorch as the default backend. Create a utility to detect the backend. | |
| Class Skeleton: Create the Spinor class in core.py with validation logic for the last dimension. | |
| Basic Ops: Implement add, mul, inner_product in ops.py. Write tests comparing results to raw tensor operations. | |
| Transformations: Implement SU(2) rotation matrix generators in transformations.py. Verify unitarity (R† R = I). | |
| Integration: Create a SpinorModule (e.g., a simple Linear layer acting on spinors) to demonstrate NN usage. | |
| Testing: Write unit tests for: | |
| Gradient propagation (via torch.autograd.grad). | |
| Dimension consistency. | |
| Transformation invariance (e.g., norm preservation under rotation). | |
| 7. API Examples (Expected Usage) | |
| import torch | |
| from spinor_lib import Spinor, inner_product, rotate | |
| # Create a batch of spinors | |
| data = torch.randn(32, 2, dtype=torch.complex64) # Batch=32, Components=2 | |
| s = Spinor(data) | |
| # Operation | |
| norm = inner_product(s, s) | |
| # Transformation | |
| rot_matrix = generate_su2_rotation(axis=[0, 0, 1], angle=torch.pi/4) | |
| s_rotated = rotate(s, rot_matrix) | |
| # Use in gradient descent | |
| loss = s_rotated.norm()**2 | |
| loss.backward() |