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.0-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.0-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.0-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.0-cp313-cp313-macosx_14_0_arm64.whl (3.0 MB view details)

Uploaded CPython 3.13macOS 14.0+ ARM64

cpp_hf-1.1.0-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.0-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.0-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.0-cp312-cp312-macosx_14_0_arm64.whl (3.0 MB view details)

Uploaded CPython 3.12macOS 14.0+ ARM64

cpp_hf-1.1.0-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.0-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.0-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.0-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.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 45f663cdf5e02ed9eebebd728d818246c280d80795dd9a914feff8430a6f3f76
MD5 e1279068766308e08c23607564eab5c5
BLAKE2b-256 f22713ff849669623a6355a5090ffa268935c3678fdadaa7c1b682f41b6c1c33

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.0-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.0-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.0-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 09ec4c3475844d419a414a3f94ad4834dcdf07976edbd598ad3e9b9806af1ccd
MD5 d19968ae94effee097fbbe5ae14bf303
BLAKE2b-256 bed5ed922378df75a424958c91ce7adb52b3f2cb54460776761df5f070142089

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.0-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.0-cp313-cp313-macosx_14_0_x86_64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.0-cp313-cp313-macosx_14_0_x86_64.whl
Algorithm Hash digest
SHA256 2d295530cd406e9fa7e61b95f98c0b031cf0ffd9ac89f66b1abbe576aa3e78a2
MD5 f3d5c542ef2ea3b811148bd9362720ee
BLAKE2b-256 69de99049b4baf268cc1b7b66d212e47921005a691793d6088f740b47c4fe321

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.0-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.0-cp313-cp313-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.0-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 a39d8cbe5700680b2925e38c370c625487b8804c3139be902db45a918b9c8512
MD5 f27b427854e88f0a86ff37b403025374
BLAKE2b-256 bbcaa04e54f92ddbc00a0a56f4a77447671ff17611e2355cc716068aa513279a

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.0-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.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 884849ff247385b99ac027aa67467bafa90a82f7d8e6dfa0800665df670cf723
MD5 99f9f52ddae030671c00b0680e8a6ed0
BLAKE2b-256 4ef27901a4626521a486377425b71795267de26da5676a9621de323581e1d004

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.0-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.0-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.0-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 eef7156e97d0c8abed4619ee197f13f38bd01ade4bca631e433f7adf1cc48e6f
MD5 e02c75bf38fa1c514c098736890132c8
BLAKE2b-256 a1e1987de4d09e41f990afe77c3cee12d0603086a1119f40da80e3207bec5de3

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.0-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.0-cp312-cp312-macosx_14_0_x86_64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.0-cp312-cp312-macosx_14_0_x86_64.whl
Algorithm Hash digest
SHA256 d8422e586b55137fa663d1d0766fc661bf9b74733029b829c7bac4332c9fd819
MD5 5417be2f1a3d2c4358f3df0315d26afc
BLAKE2b-256 61bd7486a7286e9d84d9b1577376c36b27bd645060df108178042208e4faecc9

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.0-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.0-cp312-cp312-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.0-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 e7d950908ed0da638f873e035a6edb44b2738ec6da15649127b014d1cd3e0020
MD5 4c06b01c4345b983203fad1ed7e5071f
BLAKE2b-256 eafaeb6e14383ae4ff9ef0dc879a85e61d41545e7d5a13718abd0c98a9c41769

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.0-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.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1be57bfdc25b56677e12b2fddc90445d903b63018b37e6256566f104f0fbbe43
MD5 9e9da3aca6e0c258cbf49ffb6b2752a1
BLAKE2b-256 e234107bed1c703578ecd3af82850d6fdacf6ed999ccdcdc189dd02116ebff7e

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.0-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.0-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.0-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 f938d800240028597afb22f2f5f150837b8d17bd6f2a78df2bf6ed73ee88beaf
MD5 f9c0afc1c11dd4a3f8e8e6ae36a52405
BLAKE2b-256 89d8c6912784b15492f4f6b1e98b54157a47081425d6b6640e25633d9c513052

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.0-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.0-cp311-cp311-macosx_14_0_x86_64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.0-cp311-cp311-macosx_14_0_x86_64.whl
Algorithm Hash digest
SHA256 ac5c2816489360fa529e3f85b5330dbee3126518b297a2504996886ab484bbad
MD5 e1a2485ec751f19fe060b7e0f8c52889
BLAKE2b-256 dfb60508a6372fcfe2dae4138c53711a9b7aafe7e81da7ac61151d9882d7b5aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.0-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.0-cp311-cp311-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for cpp_hf-1.1.0-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 a07dfe730fe35f47bb1f9f2173366648e4cb6575406429a21efeda6b5ca24e24
MD5 fde0693501e4909ed3b8b3d1318d56e3
BLAKE2b-256 a0f8e3535480d88cc1601a266a2d19a949101825b409b8862d2b177cded19c9c

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpp_hf-1.1.0-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