Skip to main content

PyTHC

logo

Python-based implementations of Tensor Hypercontraction (THC). Preprint paper available at: https://arxiv.org/abs/2608.17885. The full code can be found at https://github.com/QuantumCorrelators/pythc.

Description

This project implements Tensor Hypercontraction for use with Electron Repulsion Integrals (ERIs). It is built in pure Python on top of:

Installation

The project is not yet published to PyPI, so you will need to install it directly from Git.

Installing into a virtual environment

On Debian-based distributions, ensure that the following packages are installed and up to date: python3, python3-pip, python3-venv, gcc, gfortran, libopenblas-dev. (Tested on Ubuntu 24.04/26.04).

To create a new virtual environment, run:

python3 -m venv .venv

Activate the environment with:

source .venv/bin/activate

Now install the project using:

pip install pythc

Installing with Conda / Mamba

To install the library in a new environment using Conda or Mamba:

mamba create -n my_env python=3.12
mamba activate my_env
pip install pythc 

Installing into an existing uv project

This is the easiest way to install the project. You can create a new uv project using uv init my-project-name.

If you already have a project set up using the uv package manager, running:

uv add pythc 

will add the package to your pyproject.toml.

Developer Instructions

If you want to contribute to PyTHC, you can clone the repository and install the dependencies either via mamba

mamba create -n pythc python=3.12
mamba activate pythc
git clone git@github.com:QuantumCorrelators/pythc.git
cd pythc
pip install . 

or via uv

git clone git@github.com:QuantumCorrelators/pythc.git
cd pythc
uv sync

given uv is installed on your system.

Basic Usage

Detailed examples can be found in src/pythc/examples.

All THC calculations require a PySCF Molecule object as well as a completed mean-field calculation:

from pyscf import gto, scf

mol = gto.M('path/to/your/mol', basis='cc-pvdz')
mf = scf.RHF(mol)
mf.kernel()

From those objects, we can build the THC representation by importing the appropriate THC class and grid builder:

from pythc.thc.ls_ri_becke import LS_RI_Becke
from pythc.grid import BeckeGrid

grid_builder = BeckeGrid(mol)
thc_builder = LS_RI_Becke(mol=mol, grid=grid_builder, auxbasis='cc-pvdz-ri')
thc_eri = thc_builder.build(mode='ao')

Supported modes are ao for the AO-ERI $(\mu \nu|\lambda \sigma)$ and ov for the occupied-virtual block if the MO-ERI $(i j|a b)$. The THC representation must be built in mode='ao' for the HF-SCF calculation.

Once the THC ERI is built, we can retrieve the $X_\mu^P$ and $Z^{P Q}$ matrices using:

X, Z = thc_eri.get_X_Z()

or construct the full 4-dimensional quantity:

$$ (\mu \nu | \lambda \sigma) = \sum_{P Q} X_\mu^P X_\nu^P Z^{P Q} X_\lambda^Q X_\sigma^Q $$

full_eri = thc_eri.get_full()

The matrices can also be saved to HDF5 format:

thc_eri.save('path/to/place/eri.hdf5')

Alternatively, you can load an existing ERI from disk:

from pythc.thc.thc_base import ThcEri

thc_eri = ThcEri.from_file("path/to/place/eri.hdf5")

With this THC ERI interface, you can implement improved quantum chemistry algorithms. This project currently implements:

  1. The HF-SCF algorithm
  2. Møller-Plesset Perturbation Theory of 2nd order
  3. Random Phase Approximation (RPA)

The HF-SCF implementations inherit from PySCF's RHF/UHF classes. We build in ao mode so the THC does not need to be rebuilt between SCF iterations:

from pythc.grid import BeckeGrid
from pythc.thc.ls_ri_becke import LS_RI_Becke
from pythc.methods.hf import THC_RHF

auxbasis = 'cc-pvdz-ri'
grid_builder = BeckeGrid(mol)
thc_builder = LS_RI_Becke(mol=mol, grid=grid_builder, auxbasis=auxbasis)
thc_eri = thc_builder.build(mode='ao')

mf = THC_RHF(mol,
             thc_eri,
             auxbasis,
             verbose=4,
             thc_threshold=0.1,
             min_exact_cycles=1)
mf.kernel()

The usage of THC during SCF can be configured using thc_threshold and min_exact_cycles. The thc_threshold parameter specifies the energy difference threshold (in Hartree) below which the solver switches from exact RI/DF cycles to THC, provided at least min_exact_cycles iterations have passed. If the parameter thc_only=True is provided, the SCF will be converged exclusively using the THC-ERI (details).

To calculate a Laplace-transformed MP2 energy contribution with 10 integration points, build the THC in ov mode:

from pythc.grid import BeckeGrid
from pythc.thc.ls_ri_becke import LS_RI_Becke
from pythc.methods.mp2 import LaplaceRMP2

grid_builder = BeckeGrid(mol)
thc_builder = LS_RI_Becke(mol=mol, mo_coeff=mf.mo_coeff, grid=grid_builder,
                          auxbasis='cc-pvdz-ri')
thc_eri = thc_builder.build(mode='ov')

mp2_e_thc = LaplaceRMP2(mol, mf, thc_eri, n_laplace=10).kernel()

The THC representation must be built in mode='ov' for the MP2 calculation.

Metadata

Release files for pythc 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pythc 0.0.3
File Size Uploaded
pythc-0.0.3.tar.gz 6.5 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for pythc 0.0.3
File Interpreter ABI Platform
pythc-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 6.7 MB

Release files / pythc-0.0.3.tar.gz

Download URL pythc-0.0.3.tar.gz
Size 6.5 MB
Tags Source
SHA-256 checksum
How to use checksums
82b7281a80ba47703106fed5389c14bf332892abf1eb73a9e65bb4c9c0ca0ad1
BLAKE2b-256 checksum
How to use checksums
ff84af6d66ec1b87b580afbbc949d16008190e1c27587205adfc5c950d9d3f56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / pythc-0.0.3-py3-none-any.whl

Download URL pythc-0.0.3-py3-none-any.whl
Size 160.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
72cf85d9746ca34f1fcf5a39c1772b2ea7557659ec01d604b6b17303a14e2ccc
BLAKE2b-256 checksum
How to use checksums
046e1a1e436f0b505f84a90003de86dafe3007feea59d6fee00d8e90c1af4d79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page