Skip to main content

Hartree-Fock solvers on 2D k-meshes — C++ reimplementation of jax_hf

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

cpp_hf-1.1.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

cpp_hf-1.1.1-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (2.9 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

cpp_hf-1.1.1-cp313-cp313-macosx_14_0_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.13macOS 14.0+ x86-64

cpp_hf-1.1.1-cp313-cp313-macosx_14_0_arm64.whl (3.0 MB view details)

Uploaded CPython 3.13macOS 14.0+ ARM64

cpp_hf-1.1.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

cpp_hf-1.1.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (2.9 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

cpp_hf-1.1.1-cp312-cp312-macosx_14_0_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.12macOS 14.0+ x86-64

cpp_hf-1.1.1-cp312-cp312-macosx_14_0_arm64.whl (3.0 MB view details)

Uploaded CPython 3.12macOS 14.0+ ARM64

cpp_hf-1.1.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

cpp_hf-1.1.1-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (2.9 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

cpp_hf-1.1.1-cp311-cp311-macosx_14_0_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.11macOS 14.0+ x86-64

cpp_hf-1.1.1-cp311-cp311-macosx_14_0_arm64.whl (3.0 MB view details)

Uploaded CPython 3.11macOS 14.0+ ARM64

File details

Details for the file cpp_hf-1.1.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 39266ea2165863e9ea537fd2895aea2c5013bb21a11d536b569e33e63a93dff1
MD5 bd6aaf9c0d85129a4a9e44121b1e4a50
BLAKE2b-256 37081cca846800a59499f2406e1d5e51749062b4ffca643a45c34928641a14b2

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: wheels.yml on skilledwolf/cpp_hf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpp_hf-1.1.1-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.1-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 9e440ccf3703db3b4589ef211c8b8bb4457d7670e570605c95a24af803931017
MD5 57b48d081daa7a1eaa6594777c79c7da
BLAKE2b-256 22f8766d36b9770bc86b361ecba2a4e2b894c8aea92225e0c71a24b9ecca939f

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.1-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: wheels.yml on skilledwolf/cpp_hf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpp_hf-1.1.1-cp313-cp313-macosx_14_0_x86_64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.1-cp313-cp313-macosx_14_0_x86_64.whl
Algorithm Hash digest
SHA256 42472cfa5bdffec3c67cbbb0cbfffd7a9c0e46e7d49b220f14ab3f668be4374a
MD5 f4aa30a7e43c55122f008d376fe3bfaa
BLAKE2b-256 a93336b0bff7b3ed3e1558e66916565519b64afd96644f8f101d10a54c94c6e7

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.1-cp313-cp313-macosx_14_0_x86_64.whl:

Publisher: wheels.yml on skilledwolf/cpp_hf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpp_hf-1.1.1-cp313-cp313-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.1-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 f2e63c89541e464f7a74835a91dd5e31f8b0f427cbd9d4ffa7e30b583303946f
MD5 ff2d3d9ee2b05a0ec82323c8c79adf01
BLAKE2b-256 c63b7e97b0de69774b9907ef152f41365a9e8e9c8f2b9de03633501755a6fa0c

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.1-cp313-cp313-macosx_14_0_arm64.whl:

Publisher: wheels.yml on skilledwolf/cpp_hf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpp_hf-1.1.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3f9a628c1840432833c3119c978a1393bcd8db7625e1be54bfacbe7551cf37f4
MD5 ac38efbe3895f790b2a3a8e6f5c2fd8c
BLAKE2b-256 5b1c5261217d9d417830a8af0592f7065948e2d8d2a94402f9bce37d3a1d33fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: wheels.yml on skilledwolf/cpp_hf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpp_hf-1.1.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 e9bb68d51f439c727f99397568e35e8d455df20c17d590b4212d613318a47ab9
MD5 f81f5c22841c5ec96cc856708f7a91ee
BLAKE2b-256 ff45c53d4f01220056e1a7a8524b5ffa57f7b2b0b3c0488fc2726fa36045a82a

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: wheels.yml on skilledwolf/cpp_hf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpp_hf-1.1.1-cp312-cp312-macosx_14_0_x86_64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.1-cp312-cp312-macosx_14_0_x86_64.whl
Algorithm Hash digest
SHA256 a46e1c6c3392597b2ea8d67f1cfc9f7046cb82215fb8036b38c46b50e2fdcada
MD5 57df513d2e43c113c970509df923a47e
BLAKE2b-256 d6fc1ffef8c7026c7798f0fcca41a60d29bd1749b67056086fea1a65c3611673

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.1-cp312-cp312-macosx_14_0_x86_64.whl:

Publisher: wheels.yml on skilledwolf/cpp_hf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpp_hf-1.1.1-cp312-cp312-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.1-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 c35343b0d22e611050000dedd5afcc68da70ba7a39cd2f92609a714ba2205f59
MD5 f33e3d39425ec454b6fa2c83f02a5cf4
BLAKE2b-256 c2fbc470ab90c7f74b601aa45319c7f9707ed3fef3ec96285406886b5d8b5c84

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.1-cp312-cp312-macosx_14_0_arm64.whl:

Publisher: wheels.yml on skilledwolf/cpp_hf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpp_hf-1.1.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 02ef165d86cf94a92a35445d16e0ad5a215cfd64a29be8a69be427c24f65d39e
MD5 1ccee64747bc1f3beb3d73f51eb86902
BLAKE2b-256 0822b6699e8cab6000a5eb7a56914f4ae617e9beeed66f0a8ac8a1f7986c7479

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: wheels.yml on skilledwolf/cpp_hf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpp_hf-1.1.1-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.1-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 afa4f41dde13f967979d8327b5416c0bbeb5df37f548fe7f7525dfc1b0eb7f84
MD5 41b43fec3a1296e255763587653094f9
BLAKE2b-256 621d77ca8ce5b9988e8f5af77964a295794c3b70f979f73d58c6ff96af30edc1

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.1-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: wheels.yml on skilledwolf/cpp_hf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpp_hf-1.1.1-cp311-cp311-macosx_14_0_x86_64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.1-cp311-cp311-macosx_14_0_x86_64.whl
Algorithm Hash digest
SHA256 ec1004ddb0dc0dea24fe957a1191142a4b4a2a7b877b75f418dae1a2dc2f7e3e
MD5 0ad9804fb7511373f543061d77eebcd0
BLAKE2b-256 44a0411c9ac43b40941d1f230a4ca29eec32cd0d68bb7fc0efe25dea3f4bd1c4

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.1-cp311-cp311-macosx_14_0_x86_64.whl:

Publisher: wheels.yml on skilledwolf/cpp_hf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpp_hf-1.1.1-cp311-cp311-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.1-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 ca7e3f1b0519d09f74d0b77ad09742811dc37018f87771ace1094db6dafedb55
MD5 2ef293d443c050cecc673ec2339a7367
BLAKE2b-256 b86d553435eb6d9163a7a79da3803518a3d585a66da8ea7db55c681ad15e41a4

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.1-cp311-cp311-macosx_14_0_arm64.whl:

Publisher: wheels.yml on skilledwolf/cpp_hf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page