Gristmill is a package built upon the drudge computer algebra system for automatic optimization and code generation of tensor computations. While designed for quantum chemistry and many-body theory, it is suitable for any scientific computing problem involving tensors.
The optimizer utilizes advanced algorithms to efficiently parenthesize and factorize tensor computations, reducing the floating-point operation (FLOP) count. For example, a matrix chain product
can be parenthesized as
or
depending on which has fewer FLOPs given the shapes of the matrices. General tensor contractions are supported, with minimal overhead relative to specialized dynamic programming algorithms for matrix chain products. As an example, when evaluating the ladder term in coupled cluster doubles (CCD) residuals
Gristmill can automatically generate a two-step contraction scheme:
Gristmill's algorithm can efficiently handle complicated contractions with, e.g., twenty factors.
When evaluating sums of multiple contractions, Gristmill factors each term to reduce computational cost. For example, the coupled cluster singles and doubles (CCSD) correlation energy
can be automatically simplified into
which has lower FLOP count.
Additionally, Gristmill includes optimization heuristics such as common symmetrization, which ensures intermediates that are equivalent by symmetry are computed only once using the canonicalization capability in drudge.
The code generator in Gristmill is an orthogonal component to the optimizer. Both optimized and unoptimized computations can be fed into the code generator to yield naive Fortran and C code (with optional OpenMP parallelization) as well as Python code using NumPy and Julia code using OMEinsum.jl.
Gristmill can be installed directly from the GitHub repository using uv (recommended)
uv pip install git+https://github.com/DrudgeCAS/gristmill.gitor pip
pip install git+https://github.com/DrudgeCAS/gristmill.gitNote: Native Windows builds are currently not supported. Please use WSL (Windows Subsystem for Linux) to install and run Gristmill on Windows for now.
Please refer to the documentation at
https://drudgecas.github.io/drudge/.
Additional examples can be found in the ./docs/examples directory.
If you use Drudge and Gristmill in your work, please cite their GitHub repositories and Jinmo Zhao's Ph.D. thesis:
1. The Drudge GitHub repository
@misc{DrudgeCAS,
author = {Jinmo Zhao and Guo P. Chen and Gaurav Harsha and Matthew Wholey and Thomas M. Henderson and Gustavo E. Scuseria},
title = {Drudge: A symbolic algebra system for tensorial and noncommutative algebras},
publisher = {GitHub},
year = {2016--2025},
url = {https://github.com/DrudgeCAS/drudge},
note = {GitHub repository}
}2. The Gristmill GitHub repository
@misc{Gristmill,
author = {Jinmo Zhao and Guo P. Chen and Gaurav Harsha and Thomas M. Henderson and Gustavo E. Scuseria},
title = {Gristmill: A tensor contraction optimizer and code generator based on Drudge},
publisher = {GitHub},
year = {2016--2025},
url = {https://github.com/DrudgeCAS/gristmill},
note = {GitHub repository}
}3. Jinmo Zhao’s Ph.D. thesis
@phdthesis{Zhao2018Drudge,
author = {Jinmo Zhao},
title = {Symbolic Solution for Computational Quantum Many-Body Theory Development},
school = {Rice University},
year = {2018},
month = {April},
address = {Houston, Texas, USA},
type = {PhD thesis},
url = {https://www.proquest.com/openview/61a9a86c07dbb6e5270bdeb1c84384db/1?pq-origsite=gscholar&cbl=18750&diss=y}
}Link: Symbolic Solution for Computational Quantum Many-Body Theory Development — Jinmo Zhao (2018)
You may also use the CITATION.cff file provided in this
repository, which is compatible with citation managers such as Zotero and
Mendeley.
Gristmill was originally developed by Jinmo Zhao during his Ph.D. at Rice University, under the supervision of Prof. Gustavo E. Scuseria. The project was supported as part of the Center for the Computational Design of Functional Layered Materials, an Energy Frontier Research Center funded by the U.S. Department of Energy, Office of Science, Basic Energy Sciences under Award DE-SC0012575. The package is currently maintained by Guo P. Chen, Gaurav Harsha, and members of the Scueria group.