arXiv:2610.39687physics.chem-phOct 2026

Manuscript Offprint

arXiv:2610.39687

Transformer-inspired architectures make machine-learned electron densities accurate and transferable enough to bypass self-consistency

Jeheon Woo

Abstract

We introduce machine learning models that accurately predict the electron density, the central variable of density functional theory. They are built on Transformer-inspired architectures, which require little computation per parameter and can therefore be enlarged at low cost. The proposed models, SwinUNETR-V2 and its attention-free counterpart NeXtUNETR, achieve state-of-the-art accuracy on QM9, with normalized mean absolute errors in the density averaging 0.075% and 0.046%, respectively. The predicted densities are accurate enough to bypass the self-consistent iteration: a single Kohn–Sham step reproduces the self-consistent electronic structure. For NeXtUNETR, the median errors are 0.036 meV in the total energy, 0.65 meV Å −1^{-1} in the forces and 1.8 meV in the HOMO–LUMO gap. The forces not only recover the self-consistent minima in geometry optimization but also successfully drive ab initio molecular dynamics. Fine-tuned on small datasets, both networks also transfer accurately to systems containing elements absent from QM9 or larger than its molecules.

Kohn–Sham density functional theory 1,2^{1,2} is the most widely used electronic-structure method in chemistry and materials science, and the electron density is its central variable: it determines all ground-state properties 1^{1}, and the Kohn–Sham Hamiltonian is built from it. Finding the density is the costly part: the self-consistent-field (SCF) iteration diagonalizes a Hamiltonian built from a trial density and updates the density until the input and output densities agree. Because this iteration is repeated at every step of quantum-chemical simulations such as geometry optimization and molecular dynamics, it dominates their cost. Machine learning offers two ways around it. Machine-learned interatomic potentials (MLIPs) avoid the electronic problem by mapping atomic positions directly to energies and forces 3−5^{3-5}, and foundation models now span most of the periodic table 6^{6}, but they return no electronic structure. Learning the density 7,8^{7,8}, or the density matrix or Hamiltonian 9,10^{9,10}, retains it: from a predicted density, a single Kohn–Sham step returns energies, forces and orbitals together, and their quality is set by that of the density.

The density at a point is determined mainly by the atoms nearby, a property known as the nearsightedness of electronic matter 11^{11}, so a model can learn it from local environments and apply it to larger systems. A useful model must be accurate, because density errors propagate into every derived property, and transferable, because a model trained on small molecules must also serve larger, charged or differently composed systems. Most density models describe a molecule by its atoms: kernel methods and graph neural networks, mostly rotationally equivariant, expand the density in atom-centered or floating basis functions or predict it point by point 12−16^{12-16}. Others work on the real-space grid of the calculation, where the density forms a three-dimensional image, and refine a superposition of atomic densities (SAD), alone 8^{8} or together with grid-projected atomic fingerprints 17^{17}, with convolutional neural networks (CNNs). Li et al. cast this refinement as image super-resolution 8^{8}: their residual network (here SR-ResNet), trained on QM9 (ref. 18), was more accurate than earlier models and transferred to unseen geometries and elements after fine-tuning on small datasets.

If density prediction is an imaging problem, advances in computer vision may carry over to it. The Transformer 19^{19}, developed for natural language processing, was applied to images by the Vision Transformer 20^{20}, and hierarchical variants such as the Swin Transformer 21^{21}, with self-attention in local windows and a hierarchy of resolutions, reach or exceed the accuracy of CNNs such as ResNet 22^{22} on major vision benchmarks. In three dimensions, SwinUNETR and SwinUNETR-V2 (refs. 23,24) are widely used for volumetric medical image segmentation, where the convolutional 3D U-Net 25^{25} had long been the standard. Transformers have also reshaped CNNs: ConvNeXt 26^{26} reached the accuracy of Swin by adopting its macro-design, with large-kernel depthwise convolutions in place of attention, and 3D UX-Net and MedNeXt carried this design to volumetric images 27,28^{27,28}. We call both kinds of networks Transformer-inspired: they share the hierarchical macro-design and block recipe of the Swin Transformer, with or without attention. To our knowledge, density refinement on real-space grids treated as images has so far used only CNNs 8,17^{8,17}, and whether Transformer-inspired architectures make densities more accurate and transferable has not been tested.

An accurate density can replace the SCF iteration altogether. The Kohn–Sham Hamiltonian built from the predicted density is diagonalized once, and this single Kohn–Sham step gives the orbitals, a new density and the energy. Because the Kohn–Sham energy is stationary at the self-consistent density, the error of this one-step energy is second order in the density error 29,30^{29,30}. Forces need more care. The predicted density is not exactly self-consistent, so, as for other non-self-consistent energies 31,32^{31,32}, the exact gradient of the one-step energy retains a term, omitted by standard Kohn–Sham forces, from the response of the density to the nuclear positions. For a learned density, the response must be propagated through the network; we call the added term the non-self-consistency correction (NSCC). Non-self-consistent properties of learned densities have so far been tested on fixed structures 8,14,17,33,34^{8,14,17,33,34}, and SCF-free dynamics has used system-specific models of the density 7,35^{7,35} or density matrix 10,36^{10,36}. Whether a transferable density model makes one Kohn–Sham step accurate enough for geometry optimization and energy-conserving molecular dynamics has remained open.

Here we show that Transformer-inspired architectures make learned densities accurate and transferable enough to bypass the SCF iteration. We adapt two U-shaped networks to refine the SAD guess: SwinUNETR-V2, with windowed self-attention, and NeXtUNETR, an attention-free counterpart with the same hierarchical design. Trained on QM9, they reach normalized mean absolute errors (NMAEs) in the density averaging 0.075% and 0.046%, respectively, below those of any published model trained on the same data. At equal training data, NeXtUNETR is the most accurate network at every size tested, and in the same U-shaped network, ConvNeXt blocks are markedly more accurate than Swin blocks. One Kohn–Sham step from the NeXtUNETR densities gives median errors of 0.036 meV in the total energy and 0.65 meV Å −1^{-1} in the forces, and both networks give frontier orbital energies within a few meV. With or without the NSCC, the forces relax 200 molecules from nearby starting structures, 97–98.5% of them to within 0.1 Å of the SCF minima. The NSCC, which also lowers the force error for most molecules, makes molecular dynamics energy-conserving for a molecule with more heavy atoms than any training molecule. Both networks also transfer: without fine-tuning, SwinUNETR-V2 is the most accurate model on a Guess density

Network

Predicted density

One-step Kohn–Sham

Properties

Figure

ρSAD\rho_{SAD} on a 3D grid

U-shaped encoder-decoder

ρML\rho_{ML}

no SCF loop

one diagonalization

E,F=−∂E/∂R,εi,etc.E, \mathbf{F} = -\partial E/\partial \mathbf{R}, \varepsilon_i, \text{etc.}

b

Figure

C

d

Fig. 1 | One-step Kohn–Sham calculation from predicted densities, and the architectures compared. a, Schematic workflow. A U-shaped network refines the superposition of atomic densities (SAD) on the real-space grid (left) into the predicted density ρML\rho_{ML}. Its encoder blocks mix features within 7×7×77 \times 7 \times 7-voxel regions (enlarged): SwinUNETR-V2 by windowed attention, NeXtUNETR by a depthwise convolution. One diagonalization of the Kohn–Sham Hamiltonian H[ρML]H[\rho_{ML}], without a self-consistent-field (SCF) loop, gives the energy, forces and orbital energies (right: the one-step energy surface). b, Density NMAE against parameter count for three families trained on the same 8,000 QM9 molecules (QM9-subset-8k). c, Floating-point operations per molecule against parameter count. d, Density NMAE against network forward time per molecule (one NVIDIA H200 NVL, batch size 1, fp32). b–d, Means over 500 randomly sampled QM9 test molecules; one training run per model; labels give the parameter count; counting and timing in Methods. Extended Data Fig. 1 shows the details of the architectures.

charged fragments with unseen elements and on water clusters, and fine-tuned on about a hundred structures, both outperform SR-ResNet on these fragments. Architectural advances made for images thus carry over to electronic structure.

Results

References (66)