Skip to content

feat: CAS active-space support for 40-58 qubit molecules (Issue #21 Phase 1)#22

Merged
George930502 merged 13 commits into
QuantumNoLab:mainfrom
thc1006:feat/cas-active-space
Mar 31, 2026
Merged

feat: CAS active-space support for 40-58 qubit molecules (Issue #21 Phase 1)#22
George930502 merged 13 commits into
QuantumNoLab:mainfrom
thc1006:feat/cas-active-space

Conversation

@thc1006
Copy link
Copy Markdown
Member

@thc1006 thc1006 commented Mar 30, 2026

Summary

Closes Phase 1 of #21. Adds CASSCF/CASCI support to compute_molecular_integrals and 12 new CAS molecules to the registry, enabling 40-58 qubit systems.

Key changes:

  • compute_molecular_integrals(..., cas=(nelecas, ncas), casci=False) — new params
  • _compute_cas_integrals() helper — CASSCF/CASCI with auto-fallback at >50M configs
  • nuclear_repulsion = e_core (frozen-core), NOT mol.energy_nuc() — verified: N₂ CAS(10,8) e_core=-77.38 vs E_nuc=23.57
  • Cr₂ factory with fix_spin_(ss=0) to enforce singlet (prevents septet convergence)
  • Benzene CAS(6,15) via CASCI (ncas=15 auto-triggers CASCI)
  • joblib cache bypassed for CAS integrals (CASSCF is non-deterministic)
  • 12 new registry entries: N₂-CAS ×5, Cr₂ ×6, Benzene ×1

Files changed (4):

  • src/qvartools/hamiltonians/integrals.pycas/casci params + _compute_cas_integrals
  • src/qvartools/molecules/registry.py — 3 factory functions + 12 registry entries
  • tests/test_hamiltonians/test_cas_integrals.py — 19 tests (new)
  • tests/test_hamiltonians/test_cas_factories.py — 10 tests (new)

Test plan

  • 19 CAS integral tests (shapes, e_core, electron counts, CASCI fallback, cache bypass)
  • 10 factory/registry tests (completeness, is_cas flag, N₂-CAS creation)
  • ruff check + ruff format clean
  • 354 existing tests pass (no regression)
  • N₂ CAS(10,12) end-to-end: 24Q Hamiltonian created successfully (89s)

Remaining Phases (separate PRs)

  • Phase 2: Sparse diag path (remove 10K limit) — unblocks >10K basis experiments
  • Phase 3: CAS-aware FCI + pipeline scripts — unblocks experiment scripts
  • Phase 4: Parameter auto-scaling — accuracy tuning

…uantumNoLab#21 Phase 1)

Add CASSCF/CASCI support to compute_molecular_integrals via `cas` and
`casci` parameters. When `cas=(nelecas, ncas)` is provided, runs CASSCF
(or CASCI for ncas>=15 / >50M configs) and returns active-space integrals
with nuclear_repulsion=e_core (frozen-core energy, NOT mol.energy_nuc()).

New molecule factories:
- _make_n2_cas: N₂ CAS with auto-CASCI for large active spaces
- _make_cr2: Cr₂ with fix_spin_(ss=0) for singlet ground state
- _make_benzene: Benzene CAS(6,15)

Registry additions (12 new entries, 24→24 total):
- N₂-CAS(10,12/15/17/20/26): 24-52 qubits
- Cr₂ + Cr₂-CAS(12,18/20/26/28/29): 24-58 qubits
- Benzene CAS(6,15): 30 qubits

Cache: CAS integrals bypass joblib cache (CASSCF non-deterministic).

TDD: 21 new tests (19 integrals + 2 registry completeness).
All verified: N₂ CAS(10,8) e_core=-77.38 Ha (not E_nuc=23.57 Ha).
Copilot AI review requested due to automatic review settings March 30, 2026 01:20
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds CAS active-space integral generation and registers new large-qubit benchmark molecules, enabling 40–58 qubit workflows via PySCF CASSCF/CASCI integration.

Changes:

  • Extend compute_molecular_integrals / cached_compute_molecular_integrals with cas=(nelecas, ncas) and casci to produce active-space integrals (with auto-CASCI switching for large spaces).
  • Add CAS-capable molecule factories + 12 CAS registry entries (N₂-CAS variants, Cr₂ variants, Benzene).
  • Add new PySCF-marked slow tests for CAS integrals and CAS molecule factories/registry entries.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 12 comments.

File Description
src/qvartools/hamiltonians/integrals.py Introduces CAS/CASCI codepath and cache-bypass behavior for CAS in the cached helper.
src/qvartools/molecules/registry.py Adds CAS geometries/factories and registers N₂/Cr₂/Benzene CAS entries.
tests/test_hamiltonians/test_cas_integrals.py Adds CAS integral tests (shapes, e_core semantics, CASCI fallback, cache behavior).
tests/test_hamiltonians/test_cas_factories.py Adds tests for new CAS factories and registry completeness.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_hamiltonians/test_cas_factories.py
Comment thread src/qvartools/hamiltonians/integrals.py
Comment thread src/qvartools/hamiltonians/integrals.py
Comment thread src/qvartools/hamiltonians/integrals.py
Comment thread tests/test_hamiltonians/test_cas_factories.py Outdated
Comment thread tests/test_hamiltonians/test_cas_integrals.py Outdated
Comment thread src/qvartools/molecules/registry.py
Comment thread src/qvartools/hamiltonians/integrals.py
Comment thread src/qvartools/molecules/registry.py
Comment thread src/qvartools/molecules/registry.py
thc1006 added 3 commits March 30, 2026 09:25
- Fix test_is_cas_flag_present: lowercase key comparison (was "CAS"→"cas")
- Fix test_valid_config_count: use integrals fixture, not hardcoded value
- Fix typo: TestCacheBypasForCAS → TestCacheBypassForCAS
- Update docstring: RHF/CASSCF non-convergence warns, not raises
- Add CAS electron/orbital validation in _compute_cas_integrals
- Use shared _FCI_CONFIG_LIMIT constant in _make_cr2 (was hardcoded 50M)
- Add ROHF non-convergence warning in _make_cr2
- Add comment explaining N₂ CAS 1.10 Å vs full-space 1.0977 Å
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 5 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/qvartools/molecules/registry.py:34

  • registry.py imports _FCI_CONFIG_LIMIT from qvartools.hamiltonians.integrals. Since the name is underscore-prefixed (private), this creates a fragile cross-module dependency and makes refactors harder. Consider exporting a public constant (e.g. FCI_CONFIG_LIMIT) or moving the threshold into a shared config module, or avoid importing it here by delegating the CASCI decision entirely to compute_molecular_integrals.
from qvartools.hamiltonians.integrals import _FCI_CONFIG_LIMIT
from qvartools.hamiltonians.molecular import (
    MolecularHamiltonian,
    compute_molecular_integrals,
)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/qvartools/hamiltonians/integrals.py Outdated
thc1006 added 3 commits March 30, 2026 09:35
Previously deferred 3 items — all now fixed:

#1: Cr₂ singlet test: assert e_core in [-2040, -2030] Ha range
    (septet would give different e_core, catches missing fix_spin_)
QuantumNoLab#4: get_integral_cache() now returns CAS-aware wrapper that bypasses
    joblib when cas is not None (was a real cache-escape bug)
QuantumNoLab#7: Add test_cache_actually_bypassed_for_cas using mock.patch to
    verify compute_molecular_integrals is called directly

Also from new comment:
QuantumNoLab#13: Remove dead tuple/list branch for nelecas in _compute_cas_integrals
     (public API is tuple[int, int], nelecas is always int)
…mNoLab#22 audit)

- Issue 7: Replace module-level pytest.importorskip("pyscf") with
  importlib.util.find_spec check so TestCASParameterSignature runs
  without PySCF installed
- Issue 8: Add Raises section to _compute_cas_integrals docstring
- Issue 9: Add full NumPy-style docstrings (Parameters, Returns, Raises)
  to _make_n2_cas, _make_cr2, _make_benzene factory functions
- Issue 10: Update CHANGELOG.md [Unreleased] > Added with CAS entries
- Issue 11: Update AGENTS.md Section 4 molecule table from 12 to 24
  molecules (12 full-space + 12 CAS active-space)
Merge with upstream/main brought in _MOLECULE_INFO_REGISTRY consistency
check (from PR QuantumNoLab#18). Our 12 CAS molecules were in MOLECULE_REGISTRY but
missing from _MOLECULE_INFO_REGISTRY, causing import-time RuntimeError.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/qvartools/molecules/registry.py
Comment thread src/qvartools/hamiltonians/integrals.py Outdated
Comment thread tests/test_hamiltonians/test_cas_integrals.py
@thc1006 thc1006 requested a review from George930502 March 30, 2026 10:53
@George930502
Copy link
Copy Markdown
Collaborator

Code review

Found 2 issues:

  1. fix_spin_(ss=0) is only applied on the CASSCF branch, not the CASCI branch. For Cr2 entries with ncas >= 15 (cr2-cas(12,18), cr2-cas(12,20), cr2-cas(12,26), cr2-cas(12,28), cr2-cas(12,29)), the CASCI path is taken and spin enforcement is skipped entirely. The function's own docstring warns: "Without fix_spin_, CASSCF converges to the septet (S=3) instead of the singlet ground state." (bug due to conditional spin enforcement logic)

https://github.com/George930502/qvartools/blob/a96803ae306444443db32a8bbf89073df967e9c8/src/qvartools/molecules/registry.py#L527-L535

  1. 12 new CAS molecules added but README.md molecule table not updated. AGENTS.md Section 9 "Adding a New Molecule" step 4 says: "Update README.md molecule table"

https://github.com/George930502/qvartools/blob/a96803ae306444443db32a8bbf89073df967e9c8/AGENTS.md#L647-L653

🤖 Generated with Claude Code

- If this code review was useful, please react with 👍. Otherwise, react with 👎.

Copy link
Copy Markdown
Collaborator

@George930502 George930502 left a comment

Choose a reason for hiding this comment

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

Code review: 2 inline issues found.

Comment thread src/qvartools/molecules/registry.py
Comment thread AGENTS.md
@George930502 George930502 merged commit f91101c into QuantumNoLab:main Mar 31, 2026
5 checks passed
@thc1006 thc1006 deleted the feat/cas-active-space branch March 31, 2026 09:27
thc1006 added a commit to thc1006/qvartools that referenced this pull request Mar 31, 2026
7 fixes from owner code reviews on PRs QuantumNoLab#20/QuantumNoLab#22/QuantumNoLab#23:

1. Cr₂ CASCI path now has fix_spin_(ss=0) — was missing, causing
   large CAS (ncas>=15) to converge to septet instead of singlet
2. README.md molecule table updated with all 24 molecules (12 full-space
   + 12 CAS active-space)
3. AGENTS.md Section 5 error handling: 10K → 50K for matrix_elements_fast
4. test_sparse_diag.py: add @pytest.mark.pyscf to 3 test classes that
   use h2_hamiltonian fixture (importorskip alone is insufficient)
5. Validation ordering: check shape/binary on raw initial_basis BEFORE
   .to(dtype=torch.long) cast (fail-fast, error shows original shape)
6. float("inf") → RuntimeError when all batches produce non-finite
   energies (convention per AGENTS.md: non-finite = RuntimeError)
7. ADR-004 TDD plan: note _MOLECULE_INFO_REGISTRY sync requirement
George930502 added a commit that referenced this pull request Mar 31, 2026
fix: address owner review findings across merged PRs #20/#22/#23
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.

3 participants