8000 Migrate molecular LASCI and LASSCF to PySCF-Forge by MatthewRHermes · Pull Request #230 · pyscf/pyscf-forge · GitHub
[go: up one dir, main page]

Skip to content

Migrate molecular LASCI and LASSCF to PySCF-Forge - #230

Closed
MatthewRHermes wants to merge 1 commit into
pyscf:masterfrom
MatthewRHermes:lasscf
Closed

MatthewRHermes wants to merge 1 commit into
pyscf:masterfrom
MatthewRHermes:lasscf

Conversation

@MatthewRHermes
Copy link
Copy Markdown
8000 Collaborator

Migration of LASSCF from mrh to PySCF-Forge

This is one of a pair of PRs describing the transfer of molecular LASCI and LASSCF code from mrh to PySCF-Forge. The other is EDIT LINK AFTER POSTING. When the task is complete, the PySCF-Forge PR will be retargeted at origin/master and its description will be rewritten. The PR into mrh will be accepted and merged only after the PR into PySCF-Forge is accepted and merged. The goal is to open the PR into PySCF-Forge by the end of November 2026 and have the migration completed by the end of December 2026.

Scope

Only the synchronous algorithm of LASSCF (lasscf_sync_o0.py) and the RDM algorithm (lasscf_rdm.py) are being transferred, and the gpu4mrh extension along with all other LASSCF implementation variants, the PBC reimplementation, and all LASSI features will remain in mrh. After migration, mrh will import the transferred code from PySCF-Forge; it will not retain copies. Effort will be made to make this modification invisible to users.

Checklist

  1. Prepare local workspace
  2. Refactor within mrh to prepare for migration
  3. Migrate code without editing
  4. Migrate and edit unittests
  5. Refactor migrated code
  6. Add example files

Code transfer list

Module and method renames happen in step 5. mrh will keep aliases for the old names.

Transferred in their entirety

  • mrh/my_pyscf/mcscf/productstate.py -> pyscf/mcscf/las/productstate.py
  • mrh/my_pyscf/mcscf/lasscf_guess.py -> pyscf/mcscf/las/lasscf_guess.py
  • mrh/my_pyscf/mcscf/chkfile.py -> pyscf/mcscf/las/chkfile.py
  • mrh/my_pyscf/mcscf/lasscf_rdm.py -> pyscf/mcscf/las/lasscf_rdm.py
  • mrh/my_pyscf/mcpdft/laspdft.py -> pyscf/mcpdft/laspdft.py (LAS-PDFT only. LASSI-PDFT was split out into mrh/my_pyscf/mcpdft/lassipdft.py, which stays in mrh.)
  • mrh/my_pyscf/grad/lasscf.py -> pyscf/grad/lasscf.py

Transferred except for gpu4mrh code

mrh keeps a hook file of the same name for each of these. The hook defines an mrh subclass that adds the GPU code back by overriding __init__, _get_h2eff_df, HessianOperator._init_df_ and fast_veffa.

  • mrh/my_pyscf/mcscf/lasci.py -> pyscf/mcscf/las/lasci.py. The LASCINoSymm.lassi() convenience method also stays in mrh.
  • mrh/my_pyscf/mcscf/las_ao2mo.py -> pyscf/mcscf/las/las_ao2mo.py. get_h2eff_gpu and get_h2eff_gpu_v2 stay in mrh.
  • mrh/my_pyscf/mcscf/lasscf_sync_o0.py -> pyscf/mcscf/las/lasscf.py

Transferred partially

  • mrh/my_pyscf/mcscf/__init__.py -> pyscf/mcscf/las/__init__.py: _DFLASCI only. mrh re-exports the same class object, so isinstance checks keep working.
  • mrh/my_pyscf/mcscf/addons.py -> pyscf/mcscf/las/addons.py: everything except debug_lasscf_hessian_ and lasscf_hessian_debugger, which stay in mrh. This includes ci_outer_product and its helpers, which moved here from mrh/my_pyscf/lassi/op_o0.py in step 2.
  • mrh/my_pyscf/df/sparse_df.py (sparsedf_array) and mrh/lib/sparsedf.c -> somewhere under pyscf/lib/ (destination not yet decided). Marked for step 5 refactoring.
  • mrh/util/la.py: the matrix_svd_control_options closure -> a private Forge module, e.g. pyscf/mcscf/las/_la.py. Marked for step 5 refactoring.

Not transferred

  • gpu4mrh, laspscf.py, lasscf_async, lasscf_sync_o1.py, the PBC LAS code, and all of LASSI, including LASSI-PDFT
  • The LAS-PDFT constructors in mrh/my_pyscf/mcpdft/__init__.py, to be revisited in step 5

Development workflow

I am performing the transfer and refactor using Claude Code running Opus 5.5. The file MIGRATION.md, which explained to Claude the goals and strategy of this transfer, is reproduced in its entirety below.

MIGRATION.md

Goal

It is imperative to migrate the core features of LASSCF to PySCF-Forge as soon as possible. I have done this sort of thing in the past with MC-PDFT, which once lived here, then moved to PySCF-Forge, then finally to PySCF core. Throughout this project, there are "hook" files that import something from PySCF and print a warning about a migration. The idea is to avoid disrupting anyone's current workflow while prompting them to update for the new home of the method.

"Core features of LASSCF" means (in the folder my_pyscf/mcscf unless otherwise indicated)

  • lasci.py, excluding all gpu4mrh features (see below)
  • las_ao2mo.py, excluding all gpu4mrh features
  • lasscf_sync_o0.py, excluding all gpu4mrh features (file should be renamed in PySCF-Forge)
  • productstate.py
  • lasscf_guess.py
  • chkfile.py
  • lasscf_rdm.py, so that maintainers of lassqd_code (see ~/gits/lassqd_code) can eventually remove dependence on mrh
  • my_pyscf/mcpdft/laspdft.py; same reason as above
  • my_pyscf/grad/lasscf.py
  • code from addons.py implicated in the above
  • code from __init__.py implicated in the above
  • corresponding unit tests and example scripts (with appropriate edits for reasonableness and clarity)

Most files will live in a new folder pyscf/mcscf/las in PySCF-Forge. The PDFT and gradient code are exceptions: they will live respectively in pyscf/mcpdft/laspdft.py and pyscf/grad/lasscf.py.

laspscf.py and lasscf_async will not be migrated at this time. gpu4mrh will also not be migrated, which potentially creates difficulty. We have three potentially competing imperatives:

  1. Avoid disrupting existing workflows
  2. Host LASCI and the synchronous LASSCF method, as such, exclusively on PySCF-Forge (minimal or no duplicative code on mrh)
  3. Host gpu4mrh exclusively on mrh
    I hope that clever subclassing and/or monkeypatching is enough to satisfy all three imperatives simultaneously, but I'm not sure it will be.

Unit tests and example scripts can be copied (or written from scratch) - they can live in both packages for a time. All other code that is migrated should remain only in Forge. Small "hook" files remaining in mrh/my_pyscf/mcscf can define mrh-variant subclasses, but this should only be for the purpose of maintaining features not transfered and should be as minimal as possible otherwise.

Workspace

PySCF-Forge LASSCF migration worktree: /home/herme068/gits/pyscf_ext/pyscf-forge_las-migration
PySCF-Forge LASSCF migration branch: lasscf
mrh LASSCF migration worktree: /home/herme068/gits/mrh_las-migration
mrh LASSCF migration branch: las-migration

These worktrees will be turned into PRs simultaneously against PySCF-Forge origin and mrh origin. When executing test files and running example scripts in either of these worktrees on this machine, PYTHONPATH should prefer both worktrees over the corresponding standard installations, either via some kind of command a la pyscf-here or by modifying the environment.

Steps

This is a plan; it is subject to change. Open draft PRs against mrhfork/master for PySCF-Forge and origin/dev for mrh, which I will review between each of numbered steps 2-6 below. At each review point, all tests in mrh should always pass, and all examples should produce unchanged results (to within the maximum of the relevant convergence tolerance). Tests and examples should never be removed from mrh at any point, and only modified inasmuch as refactor/cleanup in steps 2 or 5 below require it. Commits should be tagged as each step is completed. In steps 3-5, commits to the two worktree branches should be made simultaneously, in parallel, with matching tags.

  1. Prepare the workspace and environment modifications or bashrc commands. -COMPLETED
  2. Refactor within mrh, if necessary.
    • Look for code in the target feature files that exists only to enable features not migrated (e.g.
      lasscf_async or lassi), and for any dependencies from mrh not included in the scope list.
    • If you find any, present it to the me. For each discrete feature, I will decide to either mark it as excluded
      from the migration, or refactor within mrh to move it out of that layer.
    • Refactor gpu code in mrh behind overridable methods so that mrh can subclass the eventual Forge classes
      instead of monkeypatching them, if possible.
    • Before moving on to step 3, mrh_las-migration should be merged into dev.
  3. Migrate code other than tests and examples.
    • All code in lasci.py, etc. other than gpu4mrh code and other functionalities marked in step 2 above, will
      live exclusively in the Forge worktree.
    • Import hooks, gpu4mrh monkeypatching code, and other excluded functionalities remain in mrh.
    • All mrh tests should still pass, and results of examples should be unchanged.
    • Do not rename any files or methods or "clean" any code at this stage except as necessary to accomplish the
      migration (ignore lint, flake, etc.).
  4. Migrate (copy) LAS unittests to PySCF-Forge worktree.
    • Review the current test battery implicated by the migrated code in mrh.
    • Use _slow marker liberally.
    • Omit extremely expensive tests (i.e., anything that takes nearly as long as the worst tests in lassi).
    • Assess gaps in test coverage and write new tests in Forge.
    • Do not delete any tests from mrh, which should always pass forever through the end of this entire migration.
  5. Clean up PySCF-Forge LAS code
    • lint, flake, etc. to the standards of both PySCF core and PySCF-Forge.
    • rename modules and methods in a sensible way (e.g., lasscf_sync_o0 should just become lasscf)
    • mrh should retain old module and method name aliasing for backwards compatibility
    • pyscf.mcscf.las should expose a sensible, intuitive method constructor: pyscf.mcscf.las.LASSCF
    • Fill in docstring gaps.
    • Delete dead code and useless comments.
    • Common-sense performance and readability optimizations without changing signatures or behaviors.
    • Especially ensure that no refactor affects behavior that lassqd_code depends on:
      • _capture_hamiltonians: We should write an API for this, to give lassqd_code developers the opportunity to retire the
        hack.
      • replay_orbital_step: Common sense says max_cycle_rdmjk = 0; max_cycle_macro = 1; conv_tol_grad = 0 should always
        accomplish "take exactly zero RDM steps and one orbital step", so there is no need to write a new API. However, their
        implementation doesn't set conv_tol_grad, and depends instead on default logic for RDM jk convergence after zero
        cycles. I will ask them to firm this up on their end. In the mean time, we have to avoid breaking it.
  6. Write PySCF-Forge example scripts
    • Review existing example scripts in mrh.
    • In the style of PySCF core, compose no more than 5 example scripts covering
      • LASCI
      • LASSCF
      • the use of localize_init_guess
      • control of fragment-specific S and M quantum numbers

Notes

  • Whatever's in the debug folder in mrh does not need to be maintained. It represents stuff that I keep around
    just in case it happens to be useful, but on which I do not depend.

@MatthewRHermes
Copy link
Copy Markdown
Collaborator Author

oops wrong repo sry

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.

1 participant

0