cpp_hf — Hartree–Fock on k‑grids (C++ reimplementation of jax_hf)
cpp_hf is a C++ reimplementation of jax_hf:
two Hartree–Fock solvers on uniform 2D k‑meshes that share the same kernel,
the same public API, and pass the same regression tests, with the JAX
dependency replaced by a single pybind11 extension built on FFTW + Eigen.
- Direct minimisation (default) — preconditioned Riemannian CG on Stiefel × capped simplex, eigen‑free inner loop, Cayley retraction, one Fock build per iteration.
- Reference SCF (baseline / fallback) — standard Roothaan iteration with linear mixing.
Both solvers run in double precision throughout (complex128 / float64).
Install
Requires FFTW (double precision: fftw3) and a C++17 compiler.
# macOS
brew install fftw eigen
pip install -e .
# Linux
sudo apt install libfftw3-dev libeigen3-dev
pip install -e .
Minimal example
import numpy as np
import cpp_hf
# Build a HartreeFockKernel: precomputes the FFT of the interaction kernel,
# the Hartree matrix, etc., ready for the solver.
kernel = cpp_hf.HartreeFockKernel(
weights=weights, # (nk1, nk2) k-point weights
hamiltonian=hamiltonian, # (nk1, nk2, nb, nb) single-particle Hamiltonian
coulomb_q=coulomb_q, # (nk1, nk2, 1, 1) scalar or (nk1, nk2, nb, nb) layer-resolved
T=0.1,
include_hartree=False, # set True for Hartree; also pass reference_density + hartree_matrix
include_exchange=True,
)
# Solve (direct minimisation, default)
result = cpp_hf.solve(kernel, P0=np.zeros_like(hamiltonian), n_electrons=N)
print(result.energy, result.converged, result.n_iter)
# result.density, result.fock, result.Q, result.p, result.mu, result.history
# Or use SCF as a fallback baseline
result_scf = cpp_hf.solve_scf(kernel, P0=np.zeros_like(hamiltonian), n_electrons=N)
Architecture
| Layer | Where it lives | What it does |
|---|---|---|
| C++ core | cpp/include/cpp_hf/*.hpp + cpp/cpp_hf_native.cpp |
FFT‑based exchange (selfenergy_fft), batched Hermitian eigendecomposition, contact‑term Fock construction, SCF main loop, direct‑minimisation main loop (preconditioned Riemannian CG with spectral Cayley line search), k‑grid resampling — all in double precision. |
| Native extension | cpp_hf._native (compiled .so inside the package) |
pybind11 wrapper exposing the C++ entry points; the GIL is released for the duration of every solver call. |
| Python surface | src/cpp_hf/*.py |
Public dataclasses (SolverConfig, SCFConfig, SolveResult, SCFResult, ContinuationResult), the HartreeFockKernel constructor (input validation + kernel precomputation), the symmetry projector framework (passed to the C++ solver as a Python callback), and the continuation driver (composes two C++ solver calls + resample_kgrid). |
The Python surface is intentionally thin: it validates inputs, packs them
into the dict shape the C++ binding expects, then hands off. Every
production code path runs in C++; importing the package without the
compiled extension raises a clear RuntimeError from native_required().
Public API
| Name | Purpose |
|---|---|
HartreeFockKernel |
Problem definition + precomputed arrays |
solve (alias solve_direct_minimization), SolverConfig, SolveResult |
Primary solver |
solve_scf, SCFConfig, SCFResult |
Reference SCF solver |
build_fock, hf_energy, free_energy, occupation_entropy |
HF objective building blocks |
solve_continuation, ContinuationResult, resample_kgrid |
Coarse → fine multigrid driver + k‑grid resampler |
cpp_hf.symmetry.make_project_fn |
Symmetry projector builder (unitary / spatial / time‑reversal) |
cpp_hf.linalg.eigh |
Block‑diagonal Hermitian eigh with optional structure check |
The API mirrors jax_hf exactly so that scripts written against jax_hf
work against cpp_hf by changing only the import line.
Coarse → fine continuation
from cpp_hf import HartreeFockKernel, SolverConfig, SCFConfig, solve_continuation
coarse = HartreeFockKernel(weights_c, h_c, Vq_c, T=0.1)
fine = HartreeFockKernel(weights_f, h_f, Vq_f, T=0.1)
result = solve_continuation(
coarse, fine, P0_coarse=np.zeros_like(h_c),
n_electrons_coarse=N, n_electrons_fine=N,
coarse_config=SCFConfig(max_iter=50, mixing=0.5),
fine_config=SolverConfig(max_iter=200, tol_E=1e-8),
)
# result.coarse, result.fine, result.P0_fine
Tests
pip install -e .
python -m pytest tests/
The suite is a port of jax_hf/tests/; it covers the chemical‑potential
solver, block‑diagonal eigh and self‑energy, the symmetry projectors,
direct minimisation (basic convergence, multi‑k, contact terms, edge
cases, Cayley spectral identities), SCF, continuation, and package‑import
contracts. The bilayer regression test (test_bilayer_regression.py,
ported from jax_hf) is not included by default since it requires
contimod.
License
GPLv2+ — see LICENSE.
Acknowledgments
- FFTW — GPLv2+ (www.fftw.org)
- Eigen — MPL2
- pybind11 — BSD‑style
Release files for cpp-hf 1.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| cpp_hf-1.1.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl | CPython 3.13 | CPython 3.13 | Linux glibc 2.28+ x86-64, Linux glibc 2.24+ x86-64 | Details |
| cpp_hf-1.1.1-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl | CPython 3.13 | CPython 3.13 | Linux glibc 2.24+ ARM64, Linux glibc 2.28+ ARM64 | Details |
| cpp_hf-1.1.1-cp313-cp313-macosx_14_0_x86_64.whl | CPython 3.13 | CPython 3.13 | macOS 14.0+ x86-64 | Details |
| cpp_hf-1.1.1-cp313-cp313-macosx_14_0_arm64.whl | CPython 3.13 | CPython 3.13 | macOS 14.0+ ARM64 | Details |
| cpp_hf-1.1.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl | CPython 3.12 | CPython 3.12 | Linux glibc 2.28+ x86-64, Linux glibc 2.24+ x86-64 | Details |
| cpp_hf-1.1.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl | CPython 3.12 | CPython 3.12 | Linux glibc 2.24+ ARM64, Linux glibc 2.28+ ARM64 | Details |
| cpp_hf-1.1.1-cp312-cp312-macosx_14_0_x86_64.whl | CPython 3.12 | CPython 3.12 | macOS 14.0+ x86-64 | Details |
| cpp_hf-1.1.1-cp312-cp312-macosx_14_0_arm64.whl | CPython 3.12 | CPython 3.12 | macOS 14.0+ ARM64 | Details |
| cpp_hf-1.1.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl | CPython 3.11 | CPython 3.11 | Linux glibc 2.28+ x86-64, Linux glibc 2.24+ x86-64 | Details |
| cpp_hf-1.1.1-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl | CPython 3.11 | CPython 3.11 | Linux glibc 2.28+ ARM64, Linux glibc 2.24+ ARM64 | Details |
| cpp_hf-1.1.1-cp311-cp311-macosx_14_0_x86_64.whl | CPython 3.11 | CPython 3.11 | macOS 14.0+ x86-64 | Details |
| cpp_hf-1.1.1-cp311-cp311-macosx_14_0_arm64.whl | CPython 3.11 | CPython 3.11 | macOS 14.0+ ARM64 | Details |
Total release size: 38.0 MB
Release files / cpp_hf-1.1.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | cpp_hf-1.1.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 3.1 MB |
| Tags | CPython 3.13 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
39266ea2165863e9ea537fd2895aea2c5013bb21a11d536b569e33e63a93dff1
|
|
BLAKE2b-256 checksum How to use checksums |
37081cca846800a59499f2406e1d5e51749062b4ffca643a45c34928641a14b2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.
Transparency logRelease files / cpp_hf-1.1.1-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
| Download URL | cpp_hf-1.1.1-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 2.9 MB |
| Tags | CPython 3.13 Linux glibc 2.24+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
9e440ccf3703db3b4589ef211c8b8bb4457d7670e570605c95a24af803931017
|
|
BLAKE2b-256 checksum How to use checksums |
22f8766d36b9770bc86b361ecba2a4e2b894c8aea92225e0c71a24b9ecca939f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.
Transparency logRelease files / cpp_hf-1.1.1-cp313-cp313-macosx_14_0_x86_64.whl
| Download URL | cpp_hf-1.1.1-cp313-cp313-macosx_14_0_x86_64.whl |
|---|---|
| Size | 3.6 MB |
| Tags | CPython 3.13 macOS 14.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
42472cfa5bdffec3c67cbbb0cbfffd7a9c0e46e7d49b220f14ab3f668be4374a
|
|
BLAKE2b-256 checksum How to use checksums |
a93336b0bff7b3ed3e1558e66916565519b64afd96644f8f101d10a54c94c6e7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.
Transparency logRelease files / cpp_hf-1.1.1-cp313-cp313-macosx_14_0_arm64.whl
| Download URL | cpp_hf-1.1.1-cp313-cp313-macosx_14_0_arm64.whl |
|---|---|
| Size | 3.0 MB |
| Tags | CPython 3.13 macOS 14.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
f2e63c89541e464f7a74835a91dd5e31f8b0f427cbd9d4ffa7e30b583303946f
|
|
BLAKE2b-256 checksum How to use checksums |
c63b7e97b0de69774b9907ef152f41365a9e8e9c8f2b9de03633501755a6fa0c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.
Transparency logRelease files / cpp_hf-1.1.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | cpp_hf-1.1.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 3.1 MB |
| Tags | CPython 3.12 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
3f9a628c1840432833c3119c978a1393bcd8db7625e1be54bfacbe7551cf37f4
|
|
BLAKE2b-256 checksum How to use checksums |
5b1c5261217d9d417830a8af0592f7065948e2d8d2a94402f9bce37d3a1d33fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.
Transparency logRelease files / cpp_hf-1.1.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
| Download URL | cpp_hf-1.1.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 2.9 MB |
| Tags | CPython 3.12 Linux glibc 2.24+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
e9bb68d51f439c727f99397568e35e8d455df20c17d590b4212d613318a47ab9
|
|
BLAKE2b-256 checksum How to use checksums |
ff45c53d4f01220056e1a7a8524b5ffa57f7b2b0b3c0488fc2726fa36045a82a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.
Transparency logRelease files / cpp_hf-1.1.1-cp312-cp312-macosx_14_0_x86_64.whl
| Download URL | cpp_hf-1.1.1-cp312-cp312-macosx_14_0_x86_64.whl |
|---|---|
| Size | 3.6 MB |
| Tags | CPython 3.12 macOS 14.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
a46e1c6c3392597b2ea8d67f1cfc9f7046cb82215fb8036b38c46b50e2fdcada
|
|
BLAKE2b-256 checksum How to use checksums |
d6fc1ffef8c7026c7798f0fcca41a60d29bd1749b67056086fea1a65c3611673
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.
Transparency logRelease files / cpp_hf-1.1.1-cp312-cp312-macosx_14_0_arm64.whl
| Download URL | cpp_hf-1.1.1-cp312-cp312-macosx_14_0_arm64.whl |
|---|---|
| Size | 3.0 MB |
| Tags | CPython 3.12 macOS 14.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
c35343b0d22e611050000dedd5afcc68da70ba7a39cd2f92609a714ba2205f59
|
|
BLAKE2b-256 checksum How to use checksums |
c2fbc470ab90c7f74b601aa45319c7f9707ed3fef3ec96285406886b5d8b5c84
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.
Transparency logRelease files / cpp_hf-1.1.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | cpp_hf-1.1.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 3.1 MB |
| Tags | CPython 3.11 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
02ef165d86cf94a92a35445d16e0ad5a215cfd64a29be8a69be427c24f65d39e
|
|
BLAKE2b-256 checksum How to use checksums |
0822b6699e8cab6000a5eb7a56914f4ae617e9beeed66f0a8ac8a1f7986c7479
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.
Transparency logRelease files / cpp_hf-1.1.1-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
| Download URL | cpp_hf-1.1.1-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 2.9 MB |
| Tags | CPython 3.11 Linux glibc 2.24+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
afa4f41dde13f967979d8327b5416c0bbeb5df37f548fe7f7525dfc1b0eb7f84
|
|
BLAKE2b-256 checksum How to use checksums |
621d77ca8ce5b9988e8f5af77964a295794c3b70f979f73d58c6ff96af30edc1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.
Transparency logRelease files / cpp_hf-1.1.1-cp311-cp311-macosx_14_0_x86_64.whl
| Download URL | cpp_hf-1.1.1-cp311-cp311-macosx_14_0_x86_64.whl |
|---|---|
| Size | 3.6 MB |
| Tags | CPython 3.11 macOS 14.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
ec1004ddb0dc0dea24fe957a1191142a4b4a2a7b877b75f418dae1a2dc2f7e3e
|
|
BLAKE2b-256 checksum How to use checksums |
44a0411c9ac43b40941d1f230a4ca29eec32cd0d68bb7fc0efe25dea3f4bd1c4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.
Transparency logRelease files / cpp_hf-1.1.1-cp311-cp311-macosx_14_0_arm64.whl
| Download URL | cpp_hf-1.1.1-cp311-cp311-macosx_14_0_arm64.whl |
|---|---|
| Size | 3.0 MB |
| Tags | CPython 3.11 macOS 14.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
ca7e3f1b0519d09f74d0b77ad09742811dc37018f87771ace1094db6dafedb55
|
|
BLAKE2b-256 checksum How to use checksums |
b86d553435eb6d9163a7a79da3803518a3d585a66da8ea7db55c681ad15e41a4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.
Transparency log