|
| 1 | +# This code is a Qiskit project. |
| 2 | +# |
| 3 | +# (C) Copyright IBM 2026. |
| 4 | +# |
| 5 | +# This code is licensed under the Apache License, Version 2.0. You may |
| 6 | +# obtain a copy of this license in the LICENSE.txt file in the root directory |
| 7 | +# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. |
| 8 | +# |
| 9 | +# Any modifications or derivative works of this code must retain this |
| 10 | +# copyright notice, and modified files need to carry a notice indicating |
| 11 | +# that they have been altered from the originals. |
| 12 | + |
| 13 | +"""Check SBD against the reference energies published with the upstream test data. |
| 14 | +
|
| 15 | +Each molecule directory under ``vendor/sbd-upstream/data`` carries a README with a |
| 16 | +table of determinant-selection thresholds and the electronic energy that a |
| 17 | +diagonalization over the corresponding determinants should produce. Those tables are |
| 18 | +the closest thing to ground truth available here: the determinants are fixed, so the |
| 19 | +answer is deterministic and independent of sampling, and the energies were obtained |
| 20 | +by filtering a full CI calculation. |
| 21 | +
|
| 22 | +The cases are the rows of those tables. Only the cheapest of them run by default; |
| 23 | +the rest are marked ``slow`` because they take minutes to hours and, at the far end, |
| 24 | +more memory than a workstation has. |
| 25 | +""" |
| 26 | + |
| 27 | +from __future__ import annotations |
| 28 | + |
| 29 | +import pytest |
| 30 | + |
| 31 | +import sbd |
| 32 | + |
| 33 | +# (molecule, alpha determinant file, expected electronic energy, is_slow) |
| 34 | +# |
| 35 | +# Transcribed from the tables in vendor/sbd-upstream/data/<molecule>/README.md. The |
| 36 | +# rows beyond the first of each molecule are marked slow: the determinant count grows |
| 37 | +# by roughly an order of magnitude per row, and compute time is reported upstream to |
| 38 | +# scale as (determinants)**1.23. The largest rows of each table are omitted entirely, |
| 39 | +# needing hundreds of gigabytes. |
| 40 | +REFERENCE_ENERGIES = [ |
| 41 | + # H2O, cc-pvdz, 24 orbitals, 10 electrons. FCI: -76.24377680 |
| 42 | + ("h2o", "h2o-1em3-alpha.txt", -76.23594663, False), |
| 43 | + ("h2o", "h2o-1em4-alpha.txt", -76.24295848, True), |
| 44 | + ("h2o", "h2o-1em5-alpha.txt", -76.24373504, True), |
| 45 | + # N2, 6-31g, 18 orbitals, 14 electrons. FCI: -109.04874199 |
| 46 | + ("n2", "1em3-alpha.txt", -109.04162110, False), |
| 47 | + ("n2", "3em4-alpha.txt", -109.04697304, True), |
| 48 | + ("n2", "1em4-alpha.txt", -109.04835269, True), |
| 49 | + ("n2", "3em5-alpha.txt", -109.04864315, True), |
| 50 | + ("n2", "1em5-alpha.txt", -109.04871934, True), |
| 51 | +] |
| 52 | + |
| 53 | + |
| 54 | +def _case_id(case) -> str: |
| 55 | + molecule, det_file, _, _ = case |
| 56 | + # e.g. "h2o-1em3": the threshold is the informative part of the file name. |
| 57 | + threshold = det_file.replace(f"{molecule}-", "").replace("-alpha.txt", "") |
| 58 | + return f"{molecule}-{threshold}" |
| 59 | + |
| 60 | + |
| 61 | +# SBD packs a determinant into words of ``bit_length`` bits, and the resulting word |
| 62 | +# count is a process-wide constant: ``det_vector::_elem_size`` is an inline static that |
| 63 | +# throws "det_vector: elem_size mismatch" if a later diagonalization needs a different |
| 64 | +# one. Two molecules can therefore share a process only if they agree on it. |
| 65 | +# |
| 66 | +# The word count is ``ceil(2 * norb / bit_length)``, so a ``bit_length`` of 64 keeps it |
| 67 | +# at 1 for every reference molecule up to 32 orbitals: h2o (24), n2 (18) and nh3 (29). |
| 68 | +# The two larger ones, c2h2 (38) and c4h4 (44), would need 2 words and so cannot share |
| 69 | +# a process with these; adding them means a separate module, or forking per test. |
| 70 | +# |
| 71 | +# ``bit_length`` does not affect the result. Verified across 8, 20, 32, 48 and 64, |
| 72 | +# which span word counts 6 down to 1: the h2o energy was identical to ten digits. |
| 73 | +BIT_LENGTH = 64 |
| 74 | + |
| 75 | + |
| 76 | +def _diagonalize(backend, fcidump, det_file, **overrides): |
| 77 | + """Diagonalize over the determinants in ``det_file`` and return the energy.""" |
| 78 | + sbd_data = backend.TPB_SBD() |
| 79 | + # A tolerance well below the precision the reference energies are quoted to, so |
| 80 | + # that a disagreement means a wrong answer rather than an unconverged one. |
| 81 | + sbd_data.eps = 1e-10 |
| 82 | + sbd_data.max_it = 200 |
| 83 | + sbd_data.bit_length = BIT_LENGTH |
| 84 | + for name, value in overrides.items(): |
| 85 | + setattr(sbd_data, name, value) |
| 86 | + results = sbd.tpb_diag_from_files(str(fcidump), str(det_file), sbd_data) |
| 87 | + return results["energy"] |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.parametrize( |
| 91 | + "molecule,det_file,expected", |
| 92 | + [ |
| 93 | + pytest.param( |
| 94 | + molecule, |
| 95 | + det_file, |
| 96 | + expected, |
| 97 | + marks=pytest.mark.slow if is_slow else (), |
| 98 | + id=_case_id((molecule, det_file, expected, is_slow)), |
| 99 | + ) |
| 100 | + for molecule, det_file, expected, is_slow in REFERENCE_ENERGIES |
| 101 | + ], |
| 102 | +) |
| 103 | +def test_reference_energy(data_dir, backend, molecule, det_file, expected): |
| 104 | + """The energy matches the value published for these determinants. |
| 105 | +
|
| 106 | + The reference energies are quoted to eight decimal places, so they are compared to |
| 107 | + that precision rather than to the solver's own convergence tolerance. |
| 108 | + """ |
| 109 | + molecule_dir = data_dir / molecule |
| 110 | + energy = _diagonalize( |
| 111 | + backend, molecule_dir / "fcidump.txt", molecule_dir / det_file |
| 112 | + ) |
| 113 | + assert energy == pytest.approx(expected, abs=1e-8) |
| 114 | + |
| 115 | + |
| 116 | +@pytest.mark.mpi |
| 117 | +def test_energy_does_not_depend_on_process_count(data_dir, backend): |
| 118 | + """Splitting the determinants across processes does not change the answer. |
| 119 | +
|
| 120 | + Run under ``mpirun``, this diagonalizes the same subspace over however many |
| 121 | + processes were launched and compares against the published energy. A result that |
| 122 | + depends on the process count would mean the distribution itself is wrong, which is |
| 123 | + the failure this guards against; it is also why the comparison is against the |
| 124 | + reference rather than against another run. |
| 125 | + """ |
| 126 | + from mpi4py import MPI |
| 127 | + |
| 128 | + comm = MPI.COMM_WORLD |
| 129 | + molecule_dir = data_dir / "h2o" |
| 130 | + energy = _diagonalize( |
| 131 | + backend, |
| 132 | + molecule_dir / "fcidump.txt", |
| 133 | + molecule_dir / "h2o-1em3-alpha.txt", |
| 134 | + adet_comm_size=comm.Get_size(), |
| 135 | + ) |
| 136 | + |
| 137 | + # Only rank 0 receives the energy; the others are given a placeholder. |
| 138 | + if comm.Get_rank() == 0: |
| 139 | + assert energy == pytest.approx(-76.23594663, abs=1e-8) |
0 commit comments