LCOV - code coverage report
Current view: top level - tests - test_mapping.py (source / functions) Hit Total Coverage
Test: coverage.info Lines: 17 17 100.0 %
Date: 2025-04-29 15:59:54 Functions: 1 2 50.0 %

          Line data    Source code
       1             : # SPDX-FileCopyrightText: 2024 Pairinteraction Developers
       2             : # SPDX-License-Identifier: LGPL-3.0-or-later
       3             : 
       4             : """Test the mapping between kets and states."""
       5             : 
       6           1 : import numpy as np
       7           1 : from scipy.optimize import linear_sum_assignment
       8             : 
       9           1 : import pairinteraction.real as pi
      10             : 
      11             : 
      12           1 : def test_mapping() -> None:
      13             :     """Test generation of a mapping."""
      14             :     # Get the eigenbasis of the Hamiltonian describing an atom in an electric field
      15           1 :     basis = pi.BasisAtom("Rb", n=(58, 62), l=(0, 2))
      16           1 :     system = pi.SystemAtom(basis).set_electric_field([0, 0, 2.5], unit="V/cm")
      17           1 :     system.diagonalize(diagonalizer="eigen", sort_by_energy=True)
      18           1 :     eigenbasis = system.get_eigenbasis()
      19             : 
      20           1 :     assert eigenbasis.number_of_states == eigenbasis.number_of_kets
      21             : 
      22             :     # Obtain the mapping
      23           1 :     state_indices = [eigenbasis.get_corresponding_state_index(ket) for ket in eigenbasis.kets]
      24             : 
      25             :     # Calculate the mapping from the coefficient matrix using scipy
      26           1 :     coefficient_matrix = np.square(np.abs(eigenbasis.get_coefficients().todense()))
      27           1 :     rows, cols = linear_sum_assignment(-coefficient_matrix)
      28             : 
      29           1 :     sorter = np.argsort(rows)
      30           1 :     rows = rows[sorter]
      31           1 :     cols = cols[sorter]
      32             : 
      33             :     # Because we have chosen the electric field to be weak enough to avoid strong mixing of states,
      34             :     # the mapping obtained by pairinteraction's heuristic should be the same as the optimal mapping
      35             :     # obtained by scipy's linear_sum_assignment
      36           1 :     np.testing.assert_array_equal(rows, np.arange(eigenbasis.number_of_kets))
      37           1 :     np.testing.assert_array_equal(cols, state_indices)  # TODO

Generated by: LCOV version 1.16