Skip to content

Psi4 becomes a backend: Psi4Backend runs the SCFs and builds the integrals, the SAPT core reads its arrays - #14

Merged
btyrcha merged 16 commits into
mainfrom
feature/psi4-backend
Nov 29, 2026
Merged

btyrcha merged 16 commits into
mainfrom
feature/psi4-backend

Conversation

@btyrcha

@btyrcha btyrcha commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner
  • Backend contract. New prop_sapt/backends/base.py, with no Psi4 import:
    • MonomerReference holds a monomer's orbitals, orbital energies, occupation, nuclear potential and SCF energy.
    • The Backend base class holds the dimer's overlap, DF factor Qpq, nuclear repulsion and fragments.
    • Its operations: J/K builds, orbital-Hessian products, CPSCF J/K objects and dipole integrals, plus clean_scratch and timer hooks.
    • The docstrings state the conventions another backend must match: AO order, orbitals as the columns of C, the sign of V and of the Hessian, and the DF factorization.
  • Psi4Backend. New prop_sapt/backends/psi4_backend.py runs the monomer SCFs and builds the integrals, in the same order as before.
    • psi4_energy, jk_memory_doubles, changed_psi4_options and the Psi4 output handler move there.
    • helper_SAPT and sinfinity take a backend and read only its data, so helper_SAPT_DF.py no longer imports Psi4.
    • compute_jk stays in the helper, with the fold-in and the zero-fill, so the finite-field "matched integrals" mode still patches it.
  • Dimer. Dimer(...) keeps its signature. It builds a Psi4Backend and keeps it as dimer.backend. wfnA, wfnB, dimer_wfn, mints, jk and the other Psi4 attributes return the backend's objects and are now read-only: assigning to them raises AttributeError.
    nao_aux is now naux, the number of functions of the SAPT fitting basis (the rows of Qpq): nao_aux counted Cartesian functions also where the basis is spherical (285 against 222 for He–H2 in aug-cc-pVQZ-RI).
  • Jobs.
    • The induced dipole takes its integrals from the backend.
    • calc_sapt_energy, calc_property and the runner clean Psi4's scratch files through the backend.
    • calc_sapt_energy is timed through backend.timer() (Psi4's tstart/tstop) and now writes the tstop it was missing.
    • Dimer.__init__ is wrapped whole in Psi4Backend.timer(), as on main, so Psi4's module time still covers the SAPT setup. The backend doesn't time itself.
  • Import guard. tests/test_imports.py checks that only a listed set of Psi4-facing modules imports psi4: the backend, molecule, the analysis modules, the runner, the CLI, artifacts and finite field. The SAPT core is not on the list, which shrinks in the next steps. The test checks imports only: the density driver still uses the dimer's Psi4 objects for cube files and for the deprecated .npy wavefunction dump.
  • Docs. A Backends section in the API reference, a note on dimer.backend in the usage guide, and a changelog entry.

Smaller changes:

  • Psi4Backend rejects an RKS reference without a functional. Dimer always passes the default functional, so users won't hit this.
  • compute_jk checks the shapes of all pairs before building any J/K.
  • nvirt_B comes from monomer B's own orbitals; the value is the same as before.
  • The backend rejects monomers A and B with different numbers of orbitals, which helper_SAPT and the densities assume are equal.
  • Psi4Backend.compute_jk clears the J/K object's inputs also when a build fails. Before, they stayed for the next build, which returned their J and K matrices too; main has the same bug.
  • With a backend other than Psi4's, get_psi4_molecule(), get_psi4_basisset() and the methods built on them raise the same clear error as the Psi4 attributes.

Out of scope (steps 1.4 and 1.5): the NumPy test backend. The analysis modules (evaluate, partition, cubes, forces, checks), artifacts, the TREXIO export, finite field and the runner's Psi4 setup still use the dimer's Psi4 objects. make_cube keeps its own tstart/tstop.

Tested:

  • New tests:
    • the contract, with a pure NumPy stub backend: validation and the default hooks;
    • Psi4Backend: its data, the dipoles, the compatibility attributes, sinfinity rebuilt from a dimer's backend, compute_jk against J_A/K_B, and the SCF JK restored after a failed CPSCF-type product;
    • compute_jk skipping empty pairs;
    • compute_jk folding the tensor into either side, against the J/K of the explicit density (a wrong right-side fold fails it);
    • a failed J/K build leaving no inputs behind (fails without the fix), monomers with different numbers of orbitals, what naux counts, and the Psi4 getters of a dimer with another backend.
  • The source scans in test_log.py now expect the backend module.
  • The full suite passes locally at 221de25, and the docs build with -W.
  • A single-threaded He–H2 run at 58bc434 gives results bitwise equal to main on all 80 arrays; the later commits change no computed array. It covers the SAPT energy with both responses, the dipole, the densities and the intermediates. A single-threaded main reproduces itself bitwise.
  • The output file differs from main's only in four ways:
    • the helper banner comes after the SCF output;
    • the timings;
    • a new "finished the Psi4 calculations" line;
    • the new tstop.

🤖 Generated with Claude Code

btyrcha and others added 16 commits November 23, 2026 15:13
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…PT consumes its data

The dimer keeps the backend as `backend`, and its Psi4 objects (`wfnA`, `wfnB`,
`mints`, `dimer_wfn`, `jk`, ...) are read-only attributes delegating to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d operations

helper_SAPT keeps the tensor fold-in and the zero matrices of empty pairs, and
no longer imports Psi4.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s come from the backend

calc_sapt_energy now also stops Psi4's timer it starts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
prop_sapt.log keeps the logger and installs the handler at import, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
nao_aux counted the Cartesian functions of the fitting basis on main and its
(spherical) functions on this branch; the new name says which it is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ZeFQETDeuKdGz7YP8cz9M
…t density

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ZeFQETDeuKdGz7YP8cz9M
The backend timed only itself, so Psi4's module time left out the SAPT setup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ZeFQETDeuKdGz7YP8cz9M
The import test's docstring says what it checks: imports only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ZeFQETDeuKdGz7YP8cz9M
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ZeFQETDeuKdGz7YP8cz9M
Also drops two claims that were wrong: that the backend's handler is the only
code writing to Psi4's output (cubes and forces print basis sets there), and
that the default Backend.timer() is None.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ZeFQETDeuKdGz7YP8cz9M
@btyrcha
btyrcha requested a balanced review from Copilot and removed request for Copilot November 26, 2026 07:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@btyrcha
btyrcha requested an adaptive review from Copilot November 26, 2026 09:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@btyrcha
btyrcha merged commit 54d5c11 into main Nov 29, 2026
4 checks passed
@btyrcha
btyrcha deleted the feature/psi4-backend branch November 29, 2026 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants