A novel neural network architecture for predicting quantum Hamiltonian matrices from molecular geometries using SO(3)-equivariant graph neural networks with Inner Product Attention mechanism.
main keeps the original QHformer inner-product attention implementation. Active architecture work is split across two development branches:
| Branch | Role | Technical updates |
|---|---|---|
qhformer-v2 |
Hybrid-attention baseline | Replaces the legacy single-head attention stack with Multi-head attention over irrep multiplicities, adds CSA/HCA alternating layers, uses invariant sparse edge selection, compresses HCA K/V to low angular order, identity-initializes NormGate and attention residuals, adds half-edge Hamiltonian assembly, and includes MD17/QH9 training utilities plus equivariance diagnostics. |
qhformer_v2_sparsity |
Current SO(2)/sparsity branch | Builds on qhformer-v2 and adds optional attention_operator="so2" K/V projections through a DeePTB-style edge-frame SO(2) operator, packed per-` |
Use qhformer-v2 for the tensor-product hybrid-attention baseline. Use qhformer_v2_sparsity for the latest SO(2) K/V operator and sparsity-performance experiments.
QHformer introduces Inner Product Attention that preserves complete irreducible representations throughout the attention computation, unlike traditional methods that compress Query/Key to scalars.
Mathematical Foundation:
Theorem (Rotation Invariance):
For features
This guarantees that attention scores are invariant to molecular rotations.
Input: Molecular Geometry (π«_i, z_i)
β
Node Embedding β h_i^(0)
β
GNN Layers (Γnum_gnn_layers)
ββ 1. Query Projection
ββ 2. Key TP with Edge SH
ββ 3. Inner Product Attention
ββ 4. Value TP + Aggregation
ββ 5. Feed-forward Network
β
Hamiltonian Block β H β β^(n_orb Γ n_orb)
β
Output: Hamiltonian Matrix
QHformer/
βββ models/
β βββ __init__.py # Package initialization
β βββ inner_product_attention.py # Inner Product Attention layer
β βββ qhformer.py # Main QHformer model
βββ training/
β βββ train_qhformer.py # Training script
β βββ monitor_training.sh # Training monitor script
βββ utils/
β βββ data_utils.py # Data loading utilities
β βββ ori_dataset.py # Original dataset wrapper
βββ requirements.txt # Python dependencies
βββ README.md # This file
- Python >= 3.8
- PyTorch >= 1.12
- e3nn >= 0.5.0
- PyTorch Geometric
pip install -r requirements.txtOr manually:
pip install torch torchvision torchaudio
pip install e3nn pytorch-scatter pytorch-sparse
pip install torch-geometric
pip install numpy scipy matplotlibBasic training:
cd training
python train_qhformer.pyWith custom hyperparameters:
python train_qhformer.py \
--dataset_path /path/to/md17_water.npz \
--hidden_size 256 \
--num_gnn_layers 5 \
--learning_rate 1e-4 \
--epochs 15000from models.qhformer import QHformer
# Initialize model
model = QHformer(
in_node_features=1, # Atomic number
sh_lmax=4, # Max spherical harmonic degree
hidden_size=256, # Hidden dimension
bottle_hidden_size=64, # Bottleneck dimension
num_gnn_layers=5, # Number of GNN layers
max_radius=12.0, # Cutoff radius (Γ
)
radius_embed_dim=64, # Radius embedding dimension
attention_temperature=1.0, # Attention temperature
)
# Forward pass
outputs = model(batch_data)
hamiltonian = outputs['hamiltonian'] # Shape: (batch, n_orb, n_orb)| Parameter | Default | Description |
|---|---|---|
in_node_features |
1 | Input node feature dimension (atomic number) |
sh_lmax |
4 | Maximum degree of spherical harmonics |
hidden_size |
256 | Hidden feature dimension |
bottle_hidden_size |
64 | Bottleneck dimension |
num_gnn_layers |
5 | Number of GNN layers |
max_radius |
12.0 | Maximum neighbor radius (Γ ) |
radius_embed_dim |
64 | Radial basis embedding dimension |
attention_temperature |
1.0 | Temperature for attention softmax |
A function
For Hamiltonian prediction:
For irreps
The tensor product of two irreps decomposes as a direct sum:
Example:
Using Clebsch-Gordan coefficients
Selection Rules:
-
$M = m_1 + m_2$ (magnetic quantum number conservation) -
$|l_1 - l_2| \leq L \leq l_1 + l_2$ (triangle inequality) -
$l_1 + l_2 + L$ is even (for real spherical harmonics)
training_config = {
# Architecture
'hidden_size': 256,
'bottle_hidden_size': 64,
'num_gnn_layers': 5,
'sh_lmax': 4,
'max_radius': 12.0,
# Training
'learning_rate': 1e-4,
'batch_size': 256,
'epochs': 15000,
'warmup_epochs': 1000,
'weight_decay': 1e-4,
'gradient_clipping': 0.5,
# Learning rate schedule
'min_lr': 1e-6,
'scheduler': 'cosine_annealing',
}The model expects molecular data in the following format:
{
'pos': Tensor[N, 3], # Atomic positions (Γ
)
'z': Tensor[N], # Atomic numbers
'hamiltonian': Tensor[M, M], # Hamiltonian matrix
'edge_index': Tensor[2, E], # Graph connectivity
}-
Maximum Information Preservation
- Query and Key maintain full irreps
- No information loss before attention
- All (l, m) components participate
-
Mathematical Elegance
- Built-in rotation invariance
- Guaranteed by inner product structure
- No handcrafted equivariance enforcement
-
Computational Efficiency
- Direct inner product computation
- No complex tensor decomposition
- Fewer parameters than projection-based methods
-
Strong Expressiveness
- Upper bound on representational capacity
- Theoretical foundation in representation theory
- Optimal for high-precision tasks
| Metric | Value |
|---|---|
| Molecule | HβO |
| Orbitals | 24 (def2-SVP) |
| Hamiltonian Size | 24 Γ 24 |
| Training Samples | 3,999 |
| Validation Samples | 1,000 |
| Total Epochs | 15,000 |
| Best Val MAE |
|
| Final Val MAE |
|
| Training Time | ~5 days (CUDA) |
| Equivariance | Verified β |
The model achieved excellent convergence with no overfitting:
-
Rapid initial convergence: MAE dropped from 0.25 to
$10^{-4}$ Hartree in first 100 epochs - Stable optimization: Consistent improvement over 15,000 epochs
- No overfitting: Training and validation MAE track closely
-
Best performance:
$1.2 \times 10^{-5}$ Hartree MAE on validation set
The model achieves high-accuracy Hamiltonian prediction:
Prediction errors are concentrated around zero with minimal outliers:
Issue: CUDA out of memory
- Solution: Reduce
batch_sizeorhidden_size
Issue: Poor convergence
- Solution: Lower
learning_rateto 1e-5, increasewarmup_epochs
Issue: Equivariance violation
- Solution: Check tensor product implementation, verify Clebsch-Gordan coefficients
- QHNet: Divel-DiNISR/QHNet - Original Hamiltonian prediction network
- e3nn: e3nn documentation - Equivariant neural networks
- Equiformer: EquiformerV2 - SO(2) convolution attention
- Clebsch-Gordan: CG coefficients - Angular momentum coupling
Yuan Jiao (η¦ζΊ)
- GitHub: STOKES-DOT
- Email: jiaoyuan24@mails.ucas.ac.cn
- ORCID: 0009-0006-9418-5545
- Institution: University of Chinese Academy of Sciences (UCAS)
MIT License - see LICENSE file for details
- Developed for quantum chemistry research at UCAS
- Built upon e3nn and PyTorch Geometric
- Inspired by advances in equivariant deep learning
If you find QHformer useful for your research, please cite:
@software{jiao2026qhformer,
title={QHformer: SO(3)-Equivariant Hamiltonian Prediction with Inner Product Attention},
author={Jiao, Yuan},
year={2026},
url={https://github.com/STOKES-DOT/QHformer},
institution={University of Chinese Academy of Sciences}
}Made with β€οΈ for the computational chemistry community


