WorksComputer Science
HamFormer: A Hamiltonian Flow-based SE(3)-Transformer for Conformation Optimization
Abstract
The Transformer architecture plays a central role in modern deep learning, and its SE(3)-equivariant variants have been widely applied to molecular conformation modeling and optimization. Given its importance, many methods attempt to interpret and improve the Transformer through the lens of dynamics. However, they mainly focus on a single attention head rather than the full Transformer architecture and seldom consider SE(3)-equivariance in their proposed models. In this study, we propose an improved SE(3)-Transformer architecture, called HamFormer, which revisits the design of SE(3)-Transformer through the lens of Hamiltonian flow. In particular, we define a parametrized Hamiltonian flow in the phase space of position and velocity, corresponding to optimizing an energy functional that combines an optimal transport-based potential with a damped momentum. The Lie-Trotter splitting framework of the Hamiltonian flow leads to the proposed HamFormer architecture, whose feedforward computation achieves the optimization step of the proposed energy functional. Furthermore, we combine HamFormer with Equivariant Graph Neural Network (EGNN), build a new model for molecular conformation optimization. Extensive experiments on molecular ground-state conformation prediction show that HamFormer provides a strong and physically interpretable surrogate for classic SE(3)-Transformer and generally outperforms state-of-the-art competitors.
Introduction
Transformers (Vaswani et al., [27]) have become the dominant architecture for modeling interacting elements, from tokens in language to atoms in molecules and materials. For the elements in three-dimensional space, whose underlying physics is invariant under rotations and translations, -Transformers (Thomas et al., [26]; Fuchs et al., [8]) build this symmetry into attention itself, coupling invariant scalar features with equivariant coordinates so that predictions transform consistently with their inputs. This geometric inductive bias underpins a series of practical successes across the molecular sciences, including protein structure prediction in AlphaFold (Jumper et al., [18]; Abramson et al., [1]), molecular representation learning in Uni-Mol (Zhou et al., [33]), equivariant force fields and materials modeling (Satorras et al., 2021; Liao and Smidt, [19]), and ground-state conformation prediction (Wang et al., [28]). As the Transformer and its -equivariant variants are entrusted with physically consequential predictions, understanding and improving what their layers compute has become a question of practical rather than merely theoretical significance.
Considerable efforts have gone into understanding the mechanism behind the Transformer, such as interpreting its self-attention layer through interacting particle dynamics (Geshkovski et al., [11]; Sander et al., [23]; Wang et al., [28]) and revisiting its residual architecture as ODE flows (Chen et al., [3]). Building on these interpretations, some recent methods improve the Transformer architecture through the lens of dynamics. For example, Sinkformer (Sander et al., [22]) applies doubly stochastic attention maps based on the Sinkhorn-scaling algorithm (Cuturi, [5]) and, accordingly, interprets the improved Transformer as Wasserstein gradient flows. Recent YurriFormer (Zimin et al., [34]) and SympFormer (Stein et al., [25]) introduce high-order dynamical information into the Transformer and design their models as the Nesterov-accelerated gradient descent algorithm. However, extending these dynamical interpretations to molecular refinement requires considering both feature evolution and molecular geometry. Beyond the attention operator, the architecture combines multiple attention heads, tokenwise FFNs, and pair relations that inform equivariant coordinate updates. The challenge is to connect these components within a common dynamical framework that guides their interaction while preserving -equivariance.
In this paper, we propose HamFormer, which revisits the -Transformer from the viewpoint of parametric Hamiltonian flows and improves the architecture accordingly. In principle, HamFormer is built based on coupled interacting particle dynamics, which capture the evolution of a distribution in the phase space of position and momentum. To model the dynamics, we define a Hamiltonian flow in phase space generated by an energy functional consisting of an optimal transport-based potential and a learned kinetic term. We parameterize the energy functional and discretize the resulting dynamics in the Lie–Trotter splitting framework (Hairer et al., [15]), with momentum retention applied before the interaction update. Given a set of particles (e.g., atomic embeddings of a molecule) and their initial momentum and pairwise relations, the discrete implementation of splitting leads to the proposed HamFormer architecture, whose feedforward computation updates the positions, momentum, and relations of the particles following the reference kick–drift structure.
Notably, HamFormer augments the atom features and pair relations used in -equivariant molecular architectures ([8, 33, 28]) with an explicit latent momentum state, allowing interactions from earlier layers to influence subsequent feature updates. In addition, compared with existing accelerated and momentum-based Transformer architectures ([34, 25, 10]), HamFormer combines an optimal transport-based potential with multi-head feature updates and -equivariant coordinate refinement.
As shown in Figure 1, combining HamFormer with Equivariant Graph Neural Network (EGNN) leads to a new model for molecular conformation optimization. We evaluate the proposed model on the task of ground-state conformation prediction. Experimental results demonstrate that HamFormer provides a strong and physically interpretable surrogate for classic -Transformer, whose feature updates follow a Hamiltonian reference structure and coordinate refinement preserves -equivariance. Accordingly, the HamFormer-based model outperforms state-of-the-art ground-state conformation predictors in both accuracy and efficiency.

Figure 1. An illustration of the proposed molecule model based on HamFormer.
Related Work and Preliminaries
Transformer and Its -Equivariant Version. The Transformer uses attention to aggregate information across a sequence or a set of samples. Typically, given an input , where indicates the sequence length or sample-set size and is the input feature dimension, an attention head first obtains the value, query, and key matrices by linear maps to a head dimension , i.e., , , and , with . It then aggregates using weights determined by and as , where is the attention map parametrized by and . The vanilla Transformer (Vaswani et al. [27]) implements this map based on the softmax operation, i.e., , where is applied to each row, giving nonnegative attention weights that sum to one. The multi-head attention layer applies a group of linear maps, i.e., , to construct attention heads with a common head dimension and concatenates their outputs, with the output projection omitted, so that , where , denotes column-wise concatenation, and , , and for . A complete Transformer block combines multi-head attention with a token-wise feedforward network (FFN), residual connections, and layer normalization.
Beyond sequence modeling, attention architectures support geometric prediction. For molecular coordinates , a coordinate predictor is SE(3)-equivariant if for every rotation and translation , with invariant atom and pair attributes held fixed. SE(3)-Transformer and Equiformer combine attention with equivariant representations for three-dimensional data and atomistic learning ([8, 19]).
Improved Transformer Architectures. Transformer layers can be interpreted as time discretizations of differential equations or iterations of optimization algorithms. The multi-particle formulation of Lu et al. [20] associates attention and FFNs with interacting flows, while optimization-based analyses view layers as iterative updates ([32]). Broader accounts connect Transformer dynamics to interacting-particle systems, gradient flows, and mean-field limits ([12]).
Sinkformers replace softmax attention with Sinkhorn normalization, which approximately enforces both row and column sums ([23]). Entropy-regularized optimal transport provides the associated optimization problem ([5]). Under suitable assumptions, the resulting continuous-depth dynamics form a Wasserstein gradient flow. WGFormer applies transport-normalized attention to molecular conformation refinement and evolves pair relations across layers ([28]). Their gradient-flow analyses use a single attention head, although the implemented architectures can contain multiple heads.
MomentumNets introduce persistent momentum-like states in residual networks ([22]), and Hamiltonian neural networks learn energies governing coupled state–momentum dynamics ([13]). Energy Transformer derives attention and tokenwise updates from a designed energy ([16]). More recently, YuriiFormer interprets attention and MLP updates through Lie–Trotter splitting of a composite objective ([34]), while SympFormer derives accelerated attention from inertial dynamics on density spaces ([25]). Connecting these dynamical descriptions to molecular refinement requires accounting for both invariant feature processing and equivariant coordinate updates.
Proposed HamFormer Architecture
In HamFormer, a parametrized Hamiltonian reference flow couples invariant atom features with latent momentum. The potential force updates momentum, and the kinetic gradient advances the features. Its Lie–Trotter splitting determines the feedforward update structure for molecular conformation optimization. Evolving multi-head relations condition these interactions and provide invariant edge information to EGNN for equivariant coordinate refinement.
Hamiltonian Flow
Hamiltonian dynamics are specified by an energy and a canonical state–momentum pairing (Evans [7]). On a periodic domain of dimension , let denote position, a smooth probability density, and its conjugate potential, with velocity field . Let be a potential-energy functional with first variation . For particles, collect positions and momentum as rows of , and let be the empirical approximation of , where denotes the Dirac measure at . The Wasserstein Hamiltonian [4] and its particle counterpart are
where the two terms of define the kinetic energy and potential energy , respectively. The particle expression uses total-energy normalization and assumes is well defined and smooth in .
Writing for the state at time , define the momentum along a characteristic trajectory by and the phase-space Hamiltonian by . With the time-dependent additive constant in chosen appropriately, the density and potential satisfy the continuity and Hamilton–Jacobi equations, whose characteristic trajectories obey Hamilton’s equations, i.e.,
where the right-hand system is evaluated along , with probability mass preserved for smooth periodic solutions and conserved when is time-independent (see also, Appendix § 6.§ 1 for more details.).
More generally, for smooth, time-independent kinetic and potential energies , the potential subflow changes momentum at fixed positions, while the kinetic subflow advances positions at fixed momentum. Their Lie–Trotter composition gives first-order symplectic Euler (see also, Wu et al., [29], § 4). For a step size , let approximate the states at . The particle dynamics and their discretization are
with the second-order Störmer–Verlet scheme obtained by symmetrizing this kick–drift splitting (Hairer et al., [15]).
Generalizing SE(3)-Transformer to Parametrized Hamiltonian Flows
A Parametric Instantiation of Hamiltonian. With denoting the model parameters, we retain the canonical structure of (1), replacing the quadratic kinetic energy by a learned density and the potential by a transport value. The rows of now represent invariant atom features and latent momentum. Let be a probability measure over with finite second moments, and let be its marginal on . The fixed auxiliary label preserves pair identity as features evolve and is used only in this distributional representation. For each head , a bounded nonnegative measurable kernel specifies the relations, with fixed during the reference dynamics.
The potential is the optimal value of a coupling problem, whose optimizer supplies the interaction weights. Let project features to head dimension . With denoting couplings with both marginals and KL denoting relative entropy, define
where for and . The constants and stabilize the logarithm and control entropic regularization, respectively. This is entropy-regularized optimal transport with cost , see also Cuturi, [5].
Extending the empirical representation above, let for distinct labels , with labeled feature marginal . Set and . Couplings in have masses , where the transport matrix belongs to . For a score matrix , the finite transport value is
Writing for the projected feature matrix, the distributional and particle Hamiltonians are
where denote the kinetic and potential energy functionals, and the two terms of the particle Hamiltonian define and . The particle Hamiltonian equals up to a constant independent of , as in the total-energy normalization of (1), with the exact relation derived in Appendix § 6.§ 2. The projected features are , with matrix logarithms and scalar offsets applied entrywise. The kinetic density is for learned and , where denotes entrywise leaky-ReLU with negative slope . The slope ensures that , matching the kinetic drift. We take , where is leaky-ReLU, applied entrywise.
Lie–Trotter realization. Fix and , and let be the unique optimizer of (4) for head . The envelope identity , obtained from Danskin’s theorem [6], together with , gives
where the prefactor in the potential accounts for score normalization and the two feature arguments. Symmetric relations imply symmetric optimal plans, reducing the force to . Substitution into (2) gives the reference kick–drift step, with momentum retention incorporated by applying before the kick, where and recovers the conservative splitting. Appendices § 6.§ 3 and § 6.§ 4 derive the gradients and splitting, respectively.
Implementation. HamFormer uses finite Sinkhorn iterations for transport, normalized learned maps for the kick and drift, and a tokenwise residual feed-forward network (FFN). Its stages alternate with EGNN stages, which use the final relations as invariant edge information for SE(3)-equivariant coordinate refinement, as shown in Figure 2.
Figure 2. HamFormer architecture.
Let be the input coordinates and . For each head, let embed the unordered atom-pair type into a scalar. Write for alternating row and column scaling iterations on , targeting marginals . Assuming , initialize
that rescales the target row and column sums to one, and we use iterations to approximate these constraints. At layer , use layer-indexed parameters and let , , and denote LayerNorm on the input features, interaction message, and drift. The relation update is
with the updated relations supplying the current interaction weights and conditioning the next layer. At , the kernel divides the exponentiated affinity entrywise by . The initialization and updates depend only on invariant attributes, distances, and features, so the relations remain invariant under rigid motions.
With learned retention and post-drift features , the state update is
where . Only the interaction branch of the momentum update is normalized. The coefficient is shared within each layer and allows signed retention, with at initialization. Also, it has no effect in the first layer because the initial momentum is . Learned maps and normalizations replace explicit step sizes.
For hidden width , let and have compatible biases . With input LayerNorm , activation and output dropout , and hidden activation , define , where .
Before output normalization, the tied drift equals the reference kinetic gradient. With fixed relations and exact symmetric transport plans, the interaction message equals the reference force with respect to normalized features. Training differentiates through the relation updates, normalization, and Sinkhorn iterations using the supervised loss (see also, Appendix § 6.6 for more details).
The combined gradient of the multi-head OT potential plays the interaction role of multi-head attention in an SE(3)-Transformer: it supplies the force that updates latent momentum. The tied tokenwise feedforward map realizes the kinetic gradient and advances features using the updated momentum. Together, these operations form the reference kick–drift structure. For fixed parameters and relations, quadratic kinetic energy gives , introducing second-order feature dynamics in contrast to the first-order flows in the single-head analyses of Sinkformers and WGFormer ([23, 28]). The learned kinetic energy generalizes the momentum-to-velocity relation. The residual FFN and EGNN extend this reference through learned feature corrections and equivariant coordinate updates. This provides a physics-aware organization of the complete architecture, connecting attention, momentum, feedforward processing, and molecular geometry.
Equivariant coordinate refinement. The initial relations depend only on atom-pair types and distances, and HamFormer preserves invariance of the features, momentum, and relations under rigid motions. Each EGNN stage ((Satorras et al., 2021)) uses these invariant quantities to construct coordinate displacements. At EGNN layer , learned pair messages produce a residual feature update using atom features, current and stage-entry squared distances, and final HamFormer relations. A separate MLP uses the updated features and the same distances and relations to produce invariant scalar weights . The stage-entry distances and relations remain fixed throughout the stage. Coordinates are updated by , where stabilizes the denominator and scalar normalization factors are absorbed into . The scalar coefficients are invariant, while relative vectors rotate and are unchanged by translation, giving an SE(3)-equivariant update. Appendix § 6.§ 5 proves equivariance of the complete pipeline.
EGNN leaves momentum unchanged and passes its feature output directly to the next HamFormer stage. Figure 1 shows the complete architecture.
Physical interpretation. Compared with the first-order transport-flow interpretations of Sinkformers and WGFormer [23, 28], HamFormer introduces an independent momentum state between attention and feature motion. The combined gradient of the multi-head transport potential supplies the momentum kick, while the tied feedforward map supplies the kinetic drift. For fixed parameters and relations, quadratic kinetic energy gives , so the interaction force determines acceleration. The learned kinetic energy generalizes the momentum-to-velocity relation. For ground-state conformation prediction, momentum carries earlier interaction effects across refinement steps, while evolving relations retain atom-pair information. Transport couples the atom features across the molecule, and EGNN uses the resulting features and relations to produce equivariant coordinate displacements (Satorras et al., 2021). Table 1 summarizes these roles. The mechanical interpretation concerns latent features and momentum, with molecular coordinates updated by EGNN.
| Component | Form | Physics | State | Molecular Role |
| OT Attention | Force | Atom Interactions | ||
| Kinetic Map | Velocity | Feature Motion | ||
| Retention | Signed Carry | Refinement Memory | ||
| Relations | Coupling | Pair Information | ||
| EGNN | Direction | Coordinate Refinement |
Table 1. Physical roles of HamFormer components.
| Model | Reference dynamics | Interaction | Momentum parametrization | Pair memory | SE(3) |
| Sinkformers (2022) | Wasserstein PDE: | OT | – | × | × |
| WGFormer (2025) | Wasserstein PDE: | OT | – | ✓ | ✓ |
| YuriiFormer (2026) | Nesterov: | Softmax | Learned coefficients | × | × |
| SympFormer (2026) | Hamiltonian PDE: | Softmax | Metric, damping | × | × |
| HamFormer (ours) | Hamiltonian PDE: | OT | Kinetic map, signed retention | ✓ | ✓ |
Table 2. Reference dynamics under the respective theoretical assumptions and features of the reported architectures. OT denotes entropic optimal transport, and pair memory denotes relations retained across layers. The SympFormer row reports its softmax variant.
Table 2 compares the reference dynamics and architectural features. YuriiFormer introduces momentum through a Nesterov template, while SympFormer uses an attention-dependent kinetic metric. HamFormer combines a separable reference Hamiltonian at fixed relations with entropic transport interactions, persistent pair relations, and SE(3)-equivariant coordinate refinement.
In the Nesterov template, , and control lookahead, retention, and step size for the reference objective . In the Hamiltonian rows, primes denote first variations, and is evaluated at fixed parameters and relations. The canonical Poisson bracket describes transport in feature–momentum space.
With feature dimensions, head count, and Sinkhorn iterations fixed, each dense HamFormer layer requires time and storage, as detailed in Remark A.4. In practice, we use three Sinkhorn iterations and reuse each head’s projected features for both score construction and transport aggregation. The practical layer retains the reference kick–drift order, with finite Sinkhorn iterations, normalized learned maps, signed retention, and a residual FFN. Appendix A.6 gives the complete pipeline, both algorithms, and the supervised training objective.
Experiments
To validate the effectiveness and physical interpretability of HamFormer, we evaluate our model on molecular ground-state conformation prediction following the established experimental protocol of WGFormer Wang et al. [28]. In addition to quantitative comparisons with existing 2D graph models and 3D equivariant architectures, we provide an empirical analysis of the latent dynamics learned by HamFormer, focusing on the layerwise evolution of phase-space energy and the role of the latent momentum state.
Experimental Setup
Datasets and splitting strategy. We evaluate our model on two standard molecular conformation benchmarks: QM9 and the larger-scale Molecule3D [31]. QM9 comprises approximately 134k small organic molecules with up to nine heavy atoms, with ground-state geometries calculated via density functional theory (DFT). Molecule3D contains substantially larger and more flexible drug-like molecules. Following the benchmark protocol in Wang et al. [28], we adopt the standard training, validation, and test splits for QM9, and evaluate on the standard random split for Molecule3D.
Baselines. Following Wang et al. [28], we compare HamFormer with two categories of baseline methods: (1) 2D molecular graph models, including GINE [17], GATv2 [2], GPS [21], and GTMGC [30], which generate 3D coordinates directly from 2D molecular graphs; and (2) 3D conformation refinement methods, including ConfOpt with TwoAtom and ThreeAtom energy functions [14], ConfOpt variants using SE(3)-Transformer [8] and EGNN (Satorras et al., 2021) backbones, and WGFormer [28].
Evaluation metrics. Following the established evaluation protocol in [31, 28], we evaluate predicted conformations using three complementary geometric metrics: (1) D-MAE (Distance Mean Absolute Error), (2) D-RMSE (Distance Root Mean Square Error), which measure the accuracy of internal molecular geometry via pairwise interatomic distances, and (3) C-RMSD (Coordinate Root Mean Square Deviation), which assesses global 3D structural alignment by computing the coordinate error against the ground truth after optimal rotation and translation. All metrics are reported in Ångströms (Å), where lower values indicate better predictive quality.
Main Results
Table 3 reports the quantitative comparison on Molecule3D and QM9. Compared to WGFormer [28], HamFormer achieves lower errors across all metrics on QM9 (e.g., test C-RMSD of 0.198 Å vs. 0.206 Å), while delivering improved D-MAE and C-RMSD on Molecule3D. HamFormer attains these results with 16.3M parameters, an approximate 79% reduction compared to WGFormer (78.9M). Compared to the first-order Wasserstein gradient flow in WGFormer, incorporating second-order phase-space dynamics provides a more parameter-efficient inductive bias for molecular conformation refinement.
| Dataset | Model | Validation | Test | ||||
| D-MAE ↓ | D-RMSE ↓ | C-RMSD ↓ | D-MAE ↓ | D-RMSE ↓ | C-RMSD ↓ | ||
| Molecule3D (Random) | GINE(2D) | 0.590 | 1.014 | 1.116 | 0.592 | 1.018 | 1.116 |
| GATv2(2D) | 0.563 | 0.983 | 1.082 | 0.564 | 0.986 | 1.083 | |
| GPS(2D) | 0.528 | 0.909 | 1.036 | 0.529 | 0.911 | 1.038 | |
| GTMGC(2D) | 0.432 | 0.719 | 0.712 | 0.433 | 0.721 | 0.713 | |
| SE(3)-Transformer | 0.466 | 0.712 | 0.800 | 0.467 | 0.774 | 0.802 | |
| EGNN | 0.461 | 0.704 | 0.798 | 0.462 | 0.766 | 0.799 | |
| ConfOpt-TwoAtom | 0.438 | 0.668 | 0.748 | 0.438 | 0.670 | 0.749 | |
| ConfOpt-ThreeAtom | 0.429 | 0.659 | 0.734 | 0.430 | 0.661 | 0.736 | |
| WGFormer | <u>0.391</u> | 0.649 | 0.662 | <u>0.392</u> | 0.652 | 0.664 | |
| HamFormer | 0.386 | <u>0.653</u> | 0.652 | 0.387 | <u>0.656</u> | 0.654 | |
| QM9 | GINE(2D) | 0.357 | 0.673 | 0.685 | 0.357 | 0.669 | 0.693 |
| GATv2(2D) | 0.339 | 0.663 | 0.661 | 0.339 | 0.659 | 0.666 | |
| GPS(2D) | 0.326 | 0.644 | 0.662 | 0.326 | 0.640 | 0.666 | |
| GTMGC(2D) | 0.262 | 0.468 | 0.362 | 0.264 | 0.470 | 0.367 | |
| SE(3)-Transformer | 0.254 | 0.451 | 0.296 | 0.256 | 0.455 | 0.303 | |
| EGNN | 0.248 | 0.442 | 0.257 | 0.251 | 0.449 | 0.265 | |
| ConfOpt-TwoAtom | 0.245 | 0.439 | 0.245 | 0.248 | 0.444 | 0.254 | |
| ConfOpt-ThreeAtom | 0.241 | 0.433 | 0.237 | 0.244 | 0.438 | 0.246 | |
| WGFormer | <u>0.223</u> | <u>0.416</u> | <u>0.198</u> | <u>0.227</u> | <u>0.422</u> | <u>0.206</u> | |
| HamFormer | 0.215 | 0.414 | 0.191 | 0.218 | 0.419 | 0.198 |
Table 3. Table 3. Performance comparison on Molecule3D and QM9 datasets. ↓ indicates that lower values are better. Bold indicates the best performance, and <u>underline</u> indicates the second best. Baseline results follow Wang et al. [28].
Ablation Studies and Further Analysis
Momentum Evolution over Layers. Figure 3 examines the six HamFormer layers separated into two alternating stages (H1–H3 for Stage 1 and H4–H6 for Stage 2). As expected in a dissipative physical system, the latent dynamics exhibit a clear two-stage relaxation behavior: 1) across the network as a whole, both the potential energy and total Hamiltonian follow an overall downward trajectory, achieving net energy dissipation toward a stable ground-state equilibrium; 2) the latent kinetic energy remains relatively suppressed in Stage 1, but experiences a prominent transient surge at the onset of Stage 2 (layer H4, marked by the dashed line) as stored potential energy is rapidly unleashed, followed by smooth dissipation across the remaining layers.
Figure 3. Layer-wise evolution of the latent kinetic energy, potential energy, and total Hamiltonian evaluated on 1,000 random molecules from the Molecule3D test set. Solid curves denote the mean, and shaded regions represent one standard deviation across molecules. The dashed line marks the transition between Stage 1 (layers H1–H3) and Stage 2 (layers H4–H6).
Architectural and Momentum Ablations. Figure 4 (left) systematically investigates the necessity of individual components on the Molecule3D random test set: (1) Inference-time momentum retention: Zeroing out the learned momentum at inference () induces pronounced performance degradation across all metrics, with test C-RMSD rising from 0.654 Å to 0.714 Å and D-MAE increasing from 0.387 Å to 0.420 Å. This demonstrates that preserving momentum across layers acts as an indispensable dynamical memory, effectively preventing error accumulation during iterative refinement. (2) Hamiltonian block ablation: Removing HamFormer and using pure EGNN, which eliminates phase-space feature updates and relies solely
Figure 4. Qualitative conformation visualizations and quantitative ablation studies on Molecule3D. Left: Quantitative ablation on the test set comparing the full HamFormer with zeroed inference momentum () and the EGNN baseline. Right: Visual comparison of predicted 3D molecular geometries against DFT references for RDKit, WGFormer, and HamFormer.
on local message-passing—causes notable performance drops (e.g., test C-RMSD rises to 0.697 Å). This confirms that our phase-space Hamiltonian flow provides an essential non-local inductive bias for capturing global molecular geometry.
Conformation Visualizations. Figure 4 (right) presents visual comparisons of predicted 3D molecular conformations aligned against ground-truth reference structures for representative molecules. Compared WGFormer, the conformations generated by HamFormer consistently align more closely with the true reference geometries, exhibiting fewer steric clashes and more accurate bond lengths and angles.
Conclusion and Future Work
We presented HamFormer, which brings Hamiltonian-inspired latent dynamics into SE(3)-equivariant conformation refinement through the joint evolution of features, momentum, and transport-based relations. Its integration with EGNN yields improved refinement on QM9 and Molecule3D, suggesting that structured latent dynamics can complement geometric message passing. These findings motivate further study of the balance between representational capacity and computational efficiency, particularly as molecular size and structural complexity increase. Extending this framework beyond small molecules remains a promising direction for assessing its scalability and broader applicability.
Theoretical Details
Section A.1 derives the density equations, their characteristics, and conservation properties. Sections A.2 and A.3 establish the particle representation of the learned Hamiltonian and compute its force and drift. Section A.4 derives the Lie–Trotter updates and examines momentum retention, and Section A.5 proves SE(3)-equivariance of the complete pipeline. Section A.6 presents the molecular refinement algorithms and supervised objective. Finally, Section A.7 details Sinkhorn scaling, rotational alignment, and computational complexity. Continuum calculations assume sufficient regularity to justify differentiation and integration by parts.
Hamiltonian Dynamics on Density Space
Consider the Wasserstein Hamiltonian in (1) on a periodic domain ,
Let be a smooth density perturbation with , and let be a smooth perturbation of . Writing for a directional derivative, the two variations are
where periodicity removes the boundary term. Since density perturbations have zero integral, the first variation with respect to is determined up to a spatial constant.
The canonical pairing identifies the density derivative with the variation in , and the potential derivative with the negative variation in [4]. Choosing the time-dependent additive constant in gives
where the first equation transports probability with velocity , while the second determines the evolution of the conjugate potential.
Let satisfy , and define . The chain rule along this trajectory yields
Thus, with , the characteristic equations are
which recover Hamilton’s equations through the classical Hamilton–Jacobi construction [7 Sec. 3.3.1]. Integrating the continuity equation over gives
If has no explicit time dependence, differentiating the Hamiltonian and integrating by parts also gives
The conserved quantity is the total functional . The characteristic Hamiltonian can still depend on time through .
Entropic Interaction and Particle Representation
The learned Hamiltonian in (5) combines a parametrized kinetic energy density with an entropic transport potential. We first verify that this energy is finite and then derive its particle representation.
For the interaction term, the inequality
and the finite second moments of the fixed marginals bound the affinity integral uniformly over admissible couplings. The logarithmic relation term is bounded because is bounded and . Relative entropy is nonnegative, and the product coupling is admissible with zero relative entropy. These observations give finite upper and lower bounds for the interaction value. The kinetic density has at most quadratic growth, so its integral is finite as well.
For distinct fixed atom identifiers , consider the empirical measures
The identifiers distinguish atoms with identical features. For a coupling of with itself, let denote the mass assigned to the ordered atom pair , where
so that has unit row and column sums, whereas is a probability coupling. With and , the sampled affinity is
with finite transport value from (4). The following identity accounts for the difference between the distributional and matrix entropy conventions.
Lemma A.1 (Particle normalization). For and its corresponding relation tensor,
where the additive correction is independent of and .
Proof. The product measure assigns mass to each ordered atom pair. Using and , a coupling with masses satisfies
Consequently,
The kinetic term satisfies
and summing the interaction identity over heads with weights proves the result.
This is an exact evaluation on empirical measures and does not require a limit as . After total-energy normalization, the additive constant has no effect on the feature or momentum gradients.
Gradients of the Reference Hamiltonian
For matrices of the same size, write . The entropy term makes the finite coupling problem strictly concave in its optimization variable.
Lemma A.2 (Transport gradient). For finite and , the problem in (4) has a unique positive optimizer , and
If is symmetric, then is symmetric.
Proof. The admissible set is nonempty and compact, and the objective is continuous under . A maximizer therefore exists, and strict concavity of the entropy term gives uniqueness.
If an optimizer has a zero entry, interpolate it with . For a small interpolation weight , the entropy gain at a zero entry has order , while the remaining changes have order . The objective therefore increases for sufficiently small , contradicting optimality.
At a fixed coupling, the derivative of the objective with respect to is . Danskin’s theorem and uniqueness give the stated gradient [6]. For symmetric , transposition preserves feasibility and the objective. Uniqueness then implies .
Fix and . For head , the score differential is
where . Applying Lemma A.2 gives
Summing over heads yields
The factor cancels the score normalization and averages the contributions from the two feature arguments. If each is symmetric, then and its optimal plan are symmetric, so the force reduces to
These derivatives use the exact optimizer and hold relations fixed.
For the kinetic term, the primitive of leaky-ReLU is
so . Under the row convention for ,
Its differential is
and hence
The transpose appears because both linear maps arise from differentiating the same scalar energy. This gradient converts momentum into feature velocity. Momentum and velocity coincide for , but need not coincide for the learned energy.
The primitive is continuously differentiable with a Lipschitz derivative, but is only piecewise twice differentiable. Calculations requiring a classical Hessian therefore apply within regions where no kinetic preactivation crosses zero.
Lie–Trotter Splitting and the Implemented Layer
For fixed parameters and relations, write . Hamilton’s equations are
Along a differentiable solution,
so the autonomous reference flow conserves its Hamiltonian.
The potential subflow changes momentum at fixed features, while the kinetic subflow advances features at fixed momentum. Their exact maps over a step are
Applying first and second gives , which expands to
This is the momentum-first symplectic Euler step in (2). For sufficiently smooth energies, Lie–Trotter composition has first-order accuracy, while the symmetric composition gives the second-order Störmer–Verlet method ([15]).
Symplecticity follows from the subflow Jacobians. After vectorizing and and suppressing evaluation points, these have the block forms
Symmetry of the Hessians gives for , and composition preserves this identity.2 The resulting numerical maps need not conserve the original Hamiltonian exactly.
Momentum retention can be introduced through the linear decay subflow , with . Its exact solution is . Applying decay before the kick gives
with recovering the conservative splitting. Applying decay after the kick would also scale the newly added force.
For the corresponding continuous damped system, the energy balance is
For , this becomes , and the feature dynamics satisfy
For a learned kinetic energy with a bias, the sign of is not automatic. Linear momentum decay therefore requires an additional condition to imply energy dissipation.
These calculations concern the reference system with fixed parameters and relations. The learned update in (8) follows the same momentum-first order, with the implementation differences described in the main text. Algorithm 1 gives the complete layer computation.
SE(3)-Equivariance of the Complete Pipeline
The Hamiltonian states and are invariant latent features, while molecular coordinates are updated separately. We hold atom attributes, pair types, and validity masks fixed under rigid motions. During training, we compare transformations using the same feature dropout masks.
Lemma A.3 (SE(3)-equivariance). The complete coordinate predictor , including centering and centroid restoration, satisfies
on valid atoms for every and .
Proof. Under , pairwise distances satisfy
Relation initialization uses only pair types and these distances, so the initialized relations are unchanged. Atom embeddings and zero initial momentum are unchanged as well. Every HamFormer operation acts on
invariant quantities, so induction over its layers gives identical features, momentum, and relations for the original and transformed inputs. This argument holds for any fixed number of Sinkhorn iterations.
At EGNN layer , the current and stage-entry distances remain invariant. The atom features and relation vectors are also invariant, so the pair messages, updated features, and scalar coordinate weights are unchanged. Writing , the coordinate update gives
Thus the coordinate update is equivariant, as in the EGNN construction (Satorras et al., 2021). Its feature output remains invariant and can enter the next HamFormer stage. Passing momentum through EGNN unchanged preserves its invariance.
For mask and , the input centroid is . It transforms as and . The centered network output therefore rotates by . Restoring the input centroid gives , which proves the claim. □
Predicted pairwise distances are consequently invariant. This argument depends on the transformation properties of the network operations and does not require an exact Hamiltonian integrator or exact transport marginals.
HamFormer for Molecular Conformation Optimization
Given atom types , pair types , and input coordinates , the atom embedding serves as the encoder and the final EGNN as the SE(3)-equivariant coordinate decoder. The network operates on centered coordinates and restores the input centroid to obtain , with pairwise distances . Algorithms 1 and 2 specify the layer update and pipeline.
Both algorithms update states in place. For a valid-atom mask , let . Masked Sinkhorn targets marginals , and geometric statistics use only valid atoms. Let denote the number of HamFormer–EGNN stage pairs. For , the stage maps and apply their layers in order. Each EGNN stage records its incoming coordinates to compute the fixed stage-entry distances. The map InitRelation implements (7).
Algorithm 1 HamFormer layer
▷ Require: , , , , parameters , iterations
for do
end for
▷ return , ,
Algorithm 2 Conformation refinement
▷ Require: , , ,
, ,
for do
end for
and
▷ return ,
For ground-state conformation prediction [28], we minimize distance and aligned coordinate errors relative to target distances and coordinates :
where the subscript denotes centering over the valid atoms, contains all ordered valid atom pairs, and weights the coordinate loss. The first term measures absolute distance errors, while the second measures squared coordinate errors after optimal rotation. During training, this rotation is computed from detached predictions and held fixed during backpropagation. Section A.7 details rotational alignment and masked Sinkhorn scaling. All network parameters are trained jointly, with gradients propagated through the relation updates, normalization, and Sinkhorn iterations.
Transport Computation, Training, and Complexity
For the positive optimizer of (4), introduce row and column multipliers and . Stationarity gives
so the optimizer has the form , where and . Sinkhorn scaling alternately adjusts and to satisfy the row and column constraints [5, 9].
The implementation targets probability marginals . Write for the resulting matrix, with invalid rows and columns excluded from normalization. On the valid block, the scaling updates are
The relation update returns , targeting unit row and column sums on valid atoms. With finitely many iterations, both sets of constraints need not hold exactly. Alternating row and column normalization also need not preserve symmetry at intermediate iterations.
For the relation-conditioned score in (6),
where and the power act entrywise. At , the exponentiated feature affinity is divided entrywise by before normalization.
For the coordinate term in (10), both coordinate matrices are restricted to valid atoms and centered before alignment. Expanding the coordinate error gives
The first two terms are independent of . Let be a singular value decomposition with singular values in decreasing order. A minimizing proper rotation is
where the determinant correction enforces . At points where the minimized coordinate loss is differentiable, for example when the minimizing rotation is unique, the envelope identity gives
Computing the exact optimal rotation from detached predictions and treating it as fixed therefore yields the gradient of the minimized value at such points. Degenerate configurations can have multiple minimizing rotations, so differentiability is not asserted everywhere.
Remark A.4 (Computational complexity). Consider one dense HamFormer layer with valid atoms, feature width , head widths , FFN width , and Sinkhorn iterations. Head projections cost . Forming scores and aggregating transport messages cost , and Sinkhorn scaling costs . The kinetic map and FFN cost and , respectively. The total time complexity is therefore , which reduces to when .
The relation tensor requires storage. Including features, projected heads, and FFN activations gives forward activation storage . Training requires additional saved intermediates depending on the differentiation and recomputation strategy. For dense padded tensors, replace by unless padded entries are excluded from computation. EGNN cost scales with the number of evaluated atom pairs at fixed MLP widths, and the complete network cost is obtained by summing over its layers.
Experimental Details
Datasets
We use the QM9 splits and the Molecule3D random split datasets, following the benchmark protocol used by WGFormer. The processed QM9 validation and test sets contain 9,900 and 10,697 molecules, respectively. The corresponding Molecule3D sets contain 779,918 and 779,915 molecules.
Evaluation
Following WGFormer, we use the following metrics to evaluate the quality of the predicted molecular structures.
D-MAE and D-RMSE: The mean absolute error and root mean square error between the predicted pairwise distances and ground-truth distances , both computed from the corresponding Cartesian coordinates:
C-RMSD: The coordinate root mean square deviation between the predicted coordinates and ground-truth coordinates after optimal rigid alignment:
For this metric, hydrogen atoms are removed, counts the retained atoms, and is understood to use the symmetry-equivalent atom ordering that minimizes the aligned RMSD.
Hyperparameters
Table 4 summarizes the full-model training settings used on the QM9 and Molecule3D random datasets, including the peak learning rate, scheduled epochs, warmup fraction, evaluation checkpoint epoch, number of training GPUs, batch size per GPU, global batch size, gradient accumulation steps, and random seed.
| Setting | QM9 | Molecule3D random |
| Peak learning rate | ||
| Scheduled epochs | 500 | 40 |
| Warmup fraction | 0.06 | 0.05 |
| Evaluated checkpoint epoch | 294 | 40 |
| Training GPUs | 8 | 2 |
| Batch size per GPU | 32 | 64 |
| Global batch size | 256 | 128 |
| Gradient accumulation | 1 | 1 |
| Random seed | 1 | 1 |
Table 4. Full-model training settings.
Ablation Study on Model Components
To further validate the effectiveness of our proposed design, we perform module ablation studies on the validation and test sets of both QM9 and Molecule3D random split. As shown in Table 5, removing the HamFormer feature block and the P&T branch leads to a prominent performance degradation across all metrics. This significant drop clearly demonstrates that the introduced components are essential for maintaining the model’s high representation capacity and prediction accuracy.
| Variant | Feature block | P&T branch | QM9 D-MAE | QM9 D-RMSE | QM9 C-RMSD | Molecule3D random D-MAE | Molecule3D random D-RMSE | Molecule3D random C-RMSD |
| Full model | HamFormer | ✓ | 0.218 | 0.419 | 0.198 | 0.387 | 0.656 | 0.654 |
| EGNN-only | × | × | 0.233 | 0.427 | 0.239 | 0.399 | 0.666 | 0.697 |
Table 5. Module ablation on the test sets. “Feature block” denotes the module applied before each EGNN stage.
Inference-Time Momentum-Retention Ablation
To further validate the importance of the inference-time momentum-retention mechanism, we investigate the effect of the momentum factor on the Molecule3D (random split) dataset. As shown in Table 6, setting the retention factor to zero () results in a substantial performance drop across both validation and test sets compared to using the learned momentum parameter (Learned ). This prominent degradation strongly confirms the crucial role and importance of the momentum-retention mechanism during inference.
| Inference setting | Validation D-MAE | Validation D-RMSE | Validation C-RMSD | Test D-MAE | Test D-RMSE | Test C-RMSD |
| Learned | 0.386 | 0.653 | 0.652 | 0.387 | 0.656 | 0.654 |
| 0.418 | 0.684 | 0.712 | 0.420 | 0.716 | 0.714 |
Table 6. Inference-time momentum-retention ablation on Molecule3D random.
Layer-wise Energy Evolution on the QM9 Dataset
Figure 5 presents the layer-wise evolution of the latent kinetic energy, potential energy, and total Hamiltonian on the QM9 test set. The energy profiles exhibit clear changes across HamFormer layers, particularly around the transition from Stage 1 to Stage 2. This observation is consistent with the corresponding results on Molecule3D and further suggests that the Hamiltonian dynamics are progressively transformed throughout the network.
Figure 5. Layer-wise evolution of the latent kinetic energy, potential energy, and total Hamiltonian evaluated on 1,000 randomly selected molecules from the QM9 test set.
Additional Visualizations

Figure 6. The visualizations for the ground-state conformations predicted by RDKit, WGFormer, and HamFormer.
References
- [1]Josh Abramson, Jonas Adler, Jack Dunger, Richard Evans, Tim Green, Alexander Pritzel, Olaf Ronneberger, Lindsay Willmore, Andrew J. Ballard, Joshua Bambrick, et al. Accurate structure prediction of biomolecular interactions with AlphaFold 3. Nature, 630(8016):493–500, 2024.DOI
- [2]Shaked Brody, Uri Alon, and Eran Yahav. How Attentive are Graph Attention Networks? In International Conference on Learning Representations, 2022.arxiv.org/abs/2105.14491
- [3]Ricky T.Q. Chen, Yulia Rubanova, Jesse Bettencourt, and David Duvenaud. Neural ordinary differential equations. In Proceedings of the 32nd International Conference on Neural Information Processing Systems, pages 6572–6583, 2018.arxiv.org/abs/1806.07366
- [4]Shui-Nee Chow, Wuchen Li, and Haomin Zhou. Wasserstein Hamiltonian flows. Journal of Differential Equations, 268 (3):1205–1219, 2020.arxiv.org/abs/1903.01088
- [5]Marco Cuturi. Sinkhorn distances: lightspeed computation of optimal transport. In Proceedings of the 27th International Conference on Neural Information Processing Systems-Volume 2, pages 2292–2300, 2013.
- [6]John M. Danskin. The Theory of Max-Min, with Applications. SIAM Journal on Applied Mathematics, 14(4):641–664, 1966.DOI
- [7]Lawrence C. Evans. Partial differential equations, volume 19. American Mathematical Society, 2022.
- [8]Fabian B. Fuchs, Daniel E. Worrall, Volker Fischer, and Max Welling. SE(3)-Transformers: 3D Roto-Translation Equivariant Attention Networks. In Advances in Neural Information Processing Systems, volume 33, pages 1970–1981, 2020.arxiv.org/abs/2006.10503
- [9]Peyré Gabriel and Cuturi Marco. Computational optimal transport: With applications to data science. Foundations and trends® in machine learning, 11(5-6):355–607, 2019.DOI
- [10]Jingchu Gai, Nai-Chieh Huang, and Jiayun Wu. Momentum Streams for Optimizer-Inspired Transformers. arXiv preprint arXiv:2605.24425, 2026.arxiv.org/abs/2605.24425
- [11]Borjan Geshkovski, Cyril Letrouit, Yury Polyanskiy, and Philippe Rigollet. The emergence of clusters in self-attention dynamics. Advances in Neural Information Processing Systems, 36:57026–57037, 2023.DOI
- [12]Borjan Geshkovski, Cyril Letrouit, Yury Polyanskiy, and Philippe Rigollet. A mathematical perspective on Transformers. Bulletin (New Series) of the American Mathematical Society, 62(3):427, 2025.arxiv.org/abs/2312.10794
- [13]Samuel Greydanus, Misko Dzamba, and Jason Yosinski. Hamiltonian Neural Networks. In Advances in Neural Information Processing Systems, volume 32, 2019.arxiv.org/abs/1906.01563
- [14]Jiaqi Guan, Wesley Wei Qian, Qiang Liu, Wei-Ying Ma, Jianzhu Ma, and Jian Peng. Energy-inspired molecular conformation optimization. In International Conference on Learning Representations, 2022.
- [15]Ernst Hairer, Marlis Hochbruck, Arieh Iserles, and Christian Lubich. Geometric numerical integration. Oberwolfach Reports, 3(1):805–882, 2006.
- [16]Benjamin Hoover, Yuchen Liang, Bao Pham, Rameswar Panda, Hendrik Strobel, Duen Horng Chau, Mohammed Zaki, and Dmitry Krotov. Energy Transformer. volume 36, pages 27532–27559, 2023.arxiv.org/abs/2302.07253
- [17]Weihua Hu, Bowen Liu, Joseph Gomes, Marinka Zitnik, Percy Liang, Vijay Pande, and Jure Leskovec. Strategies for Pre-training Graph Neural Networks. 2019.arxiv.org/abs/1905.12265
- [18]John Jumper, Richard Evans, Alexander Pritzel, Tim Green, Michael Figurnov, Olaf Ronneberger, Kathryn Tunyasu-vunakool, Russ Bates, Augustin Žídek, Anna Potapenko, et al. Highly accurate protein structure prediction with AlphaFold. Nature, 596(7873):583–589, 2021.DOI
- [19]Yi-Lun Liao and Tess Smidt. Equiformer: Equivariant Graph Attention Transformer for 3D Atomistic Graphs. In International Conference on Learning Representations, 2023.arxiv.org/abs/2206.11990
- [20]Yiping Lu, Zhuohan Li, Di He, Zhiqing Sun, Bin Dong, Tao Qin, Liwei Wang, and Tie-Yan Liu. Understanding and improving transformer from a multi-particle dynamic system point of view. arXiv preprint arXiv:1906.02762, 2019.arxiv.org/abs/1906.02762
- [21]Ladislav Rampášek, Mikhail Galkin, Vijay Prakash Dwivedi, Anh Tuan Luu, Guy Wolf, and Dominique Beaini. Recipe for a general, powerful, scalable graph transformer. In Proceedings of the 36th International Conference on Neural Information Processing Systems, pages 14501–14515, 2022.arxiv.org/abs/2205.12454
- [22]Michael E. Sander, Pierre Ablin, Mathieu Blondel, and Gabriel Peyré. Momentum residual neural networks. In Proceedings of the 38th International Conference on Machine Learning, volume 139 of Proceedings of Machine Learning Research, pages 9276–9287, 2021.arxiv.org/abs/2102.07870
- [23]Michael E. Sander, Pierre Ablin, Mathieu Blondel, and Gabriel Peyré. Sinkformers: Transformers with doubly stochastic attention. In International Conference on Artificial Intelligence and Statistics, pages 3515–3530. PMLR, 2022.arxiv.org/abs/2110.11773
- [25]Viktor Stein, Wuchen Li, and Gabriele Steidl. SympFormer: Accelerated attention blocks via inertial dynamics on density manifolds. arXiv preprint arXiv:2603.16535, 2026.arxiv.org/abs/2603.16535
- [26]Nathaniel Thomas, Tess Smidt, Steven Kearnes, Lusann Yang, Li Li, Kai Kohlhoff, and Patrick Riley. Tensor field networks: Rotation-and translation-equivariant neural networks for 3D point clouds. arXiv preprint arXiv:1802.08219, 2018.arxiv.org/abs/1802.08219
- [27]Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, and Illia Polosukhin. Attention is All you Need. In Advances in Neural Information Processing Systems, volume 30, 2017.arxiv.org/abs/1706.03762
- [28]Fanmeng Wang, Minjie Cheng, and Hongteng Xu. WGFormer: An SE(3)-transformer driven by Wasserstein gradient flows for molecular ground-state conformation prediction. In Proceedings of the 42nd International Conference on Machine Learning, volume 267 of Proceedings of Machine Learning Research, pages 62652–62671. PMLR, 13–19 Jul 2025.arxiv.org/abs/2410.09795
- [29]Hao Wu, Shu Liu, Xiaojing Ye, and Haomin Zhou. Parameterized Wasserstein Hamiltonian flow. SIAM Journal on Numerical Analysis, 63(1):360–395, 2025.arxiv.org/abs/2306.00191
- [30]Guikun Xu, Yongquan Jiang, PengChuan Lei, Yan Yang, and Jim Chen. GTMGC: Using graph Transformer to predict molecule’s ground-state conformation. In International Conference on Learning Representations, volume 2024, pages 47712–47731, 2024.
- [31]Zhao Xu, Youzhi Luo, Xuan Zhang, Xinyi Xu, Yaochen Xie, Meng Liu, Kaleb Dickerson, Cheng Deng, Maho Nakata, and Shuiwang Ji. Molecule3D: A Benchmark for Predicting 3D Geometries from Molecular Graphs. arXiv preprint arXiv:2110.01717, 2021.arxiv.org/abs/2110.01717
- [32]Yongyi Yang and David P. Wipf. Transformers from an Optimization Perspective. volume 35, pages 36958–36971, 2022.arxiv.org/abs/2205.13891
- [33]Gengmo Zhou, Zhifeng Gao, Qiankun Ding, Hang Zheng, Hongteng Xu, Zhewei Wei, Linfeng Zhang, and Guolin Ke. Uni-Mol: A Universal 3D Molecular Representation Learning Framework. In The Eleventh International Conference on Learning Representations, 2023.DOI
- [34]Aleksandr Zimin, Yury Polyanskiy, and Philippe Rigollet. YuriiFormer: A suite of Nesterov-Accelerated Transformers. arXiv preprint arXiv:2601.23236, 2026.arxiv.org/abs/2601.23236



