• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

adc-connect / adcc / 16384966787

03 Jul 2025 11:29AM UTC coverage: 73.883% (+0.3%) from 73.602%
16384966787

push

github

web-flow
Restructure ExcitedStates etc for IP/EA Integration (#195)

* allow timer customization in cached_member_function

* add option to exclude the args in the timer task

* timer description if nothing was recorded

* basic restructuring of ElectronicTransition, ExcitedStates and State2States

* basic restructuring of Excitation

* add _module member variable to dispatch to the corresponding working equations

* basic restructuring for excited state properties

* enable excitation for s2s

* port dataframe export

* add state_dm

* port the remaining properties - tests passing

* * cache the MO integrals instead of the AO integrals
* remove unnecessary property decorated returns of callbacks
* implement available integrals directly in the backends

* raise Exception directly in the backends for PE and PCM

* add to_qcvars back in

* split plot_spectrum in common function on base class and adc type dependent function on child classes

* invert order of unit conversion and broadening for plot_spectrum

* add input options for lower and upper bounds of the broadened spectrum

* remove broadening min/max and allow variable width_units

* refactor the ExcitedStates.describe method

* determine the column width automatically

* move describe_amplitudes to ElectronicStates and adapt for IP/EA

* store operators in tuple on IsrMatrix

* make flake happy and remove the cache for now since it is not used anyway

* reintroduce the cache for available backends and cache them lazily

* explicitly install setupstools

* enable libtensorlight download for macos arm64

* naively update CI to macos-latest

* install llvm and use arm64 compilers

* explicitly only build for the current platform

* only set architecture for macos

* generic block_orders for secular and isr matrix

* move init complete back to secular and isr matrix

* raise more explicit exception for not implemented methods

* patch coverage not required for status check

* add token for github api request... (continued)

1176 of 1884 branches covered (62.42%)

Branch coverage included in aggregate %.

700 of 965 new or added lines in 21 files covered. (72.54%)

6 existing lines in 6 files now uncovered.

7376 of 9691 relevant lines covered (76.11%)

175662.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.02
/adcc/IsrMatrix.py
1
#!/usr/bin/env python3
2
## vi: tabstop=4 shiftwidth=4 softtabstop=4 expandtab
3
## ---------------------------------------------------------------------
4
##
5
## Copyright (C) 2022 by the adcc authors
6
##
7
## This file is part of adcc.
8
##
9
## adcc is free software: you can redistribute it and/or modify
10
## it under the terms of the GNU General Public License as published
11
## by the Free Software Foundation, either version 3 of the License, or
12
## (at your option) any later version.
13
##
14
## adcc is distributed in the hope that it will be useful,
15
## but WITHOUT ANY WARRANTY; without even the implied warranty of
16
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
## GNU General Public License for more details.
18
##
19
## You should have received a copy of the GNU General Public License
20
## along with adcc. If not, see <http://www.gnu.org/licenses/>.
21
##
22
## ---------------------------------------------------------------------
23
import libadcc
2✔
24

25
from .AdcMatrix import AdcMatrixlike
2✔
26
from .AdcMethod import AdcMethod
2✔
27
from .adc_pp import bmatrix as ppbmatrix
2✔
28
from .AmplitudeVector import AmplitudeVector
2✔
29
from .LazyMp import LazyMp
2✔
30
from .OneParticleOperator import OneParticleOperator
2✔
31
from .timings import Timer, timed_member_call
2✔
32

33

34
class IsrMatrix(AdcMatrixlike):
2✔
35

36
    def __init__(self, method, hf_or_mp, operator, block_orders=None):
2✔
37
        """
38
        Initialise an ISR matrix of a given one-particle operator
39
        for the provided ADC method.
40

41
        Parameters
42
        ----------
43
        method : str or adcc.AdcMethod
44
            Method to use.
45
        hf_or_mp : adcc.ReferenceState or adcc.LazyMp
46
            HF reference or MP ground state.
47
        operator : adcc.OneParticleOperator or list of adcc.OneParticleOperator
48
                    objects
49
            One-particle matrix elements associated with a one-particle operator.
50
        block_orders : optional
51
            The order of perturbation theory to employ for each matrix block.
52
            If not set, defaults according to the selected ADC method are chosen.
53
        """
54
        if isinstance(hf_or_mp, (libadcc.ReferenceState,
2!
55
                                 libadcc.HartreeFockSolution_i)):
56
            hf_or_mp = LazyMp(hf_or_mp)
×
57
        if not isinstance(hf_or_mp, LazyMp):
2!
58
            raise TypeError("hf_or_mp is not a valid object. It needs to be "
×
59
                            "either a LazyMp, a ReferenceState or a "
60
                            "HartreeFockSolution_i.")
61

62
        if not isinstance(method, AdcMethod):
2!
63
            method = AdcMethod(method)
2✔
64

65
        if isinstance(operator, (list, tuple)):
2✔
66
            self.operator = tuple(operator)
2✔
67
        else:
68
            self.operator = (operator,)
2✔
69
        if not all(isinstance(op, OneParticleOperator) for op in self.operator):
2!
70
            raise TypeError("operator is not a valid object. It needs to be "
×
71
                            "either an OneParticleOperator or a list of "
72
                            "OneParticleOperator objects.")
73

74
        self.timer = Timer()
2✔
75
        self.method = method
2✔
76
        self.ground_state = hf_or_mp
2✔
77
        self.reference_state = hf_or_mp.reference_state
2✔
78
        self.mospaces = hf_or_mp.reference_state.mospaces
2✔
79
        self.is_core_valence_separated = method.is_core_valence_separated
2✔
80
        self.ndim = 2
2✔
81
        self.extra_terms = []
2✔
82

83
        self.block_orders = self._default_block_orders(self.method)
2✔
84
        if block_orders is None:
2!
85
            # only implemented through PP-ADC(2)
86
            if method.adc_type != "pp" or method.name.endswith("adc2x") \
2!
87
                    or method.level > 2:
NEW
88
                raise NotImplementedError("The B-matrix is not implemented "
×
89
                                          f"for method {method.name}.")
90
        else:
NEW
91
            self.block_orders.update(block_orders)
×
92
        self._validate_block_orders(
2✔
93
            block_orders=self.block_orders, method=self.method,
94
            allow_missing_diagonal_blocks=True
95
        )
96

97
        # Build the blocks
98
        with self.timer.record("build"):
2✔
99
            variant = None
2✔
100
            if self.is_core_valence_separated:
2✔
101
                variant = "cvs"
2✔
102
            blocks = [{
2✔
103
                block: ppbmatrix.block(self.ground_state, op,
104
                                       block.split("_"), order=order,
105
                                       variant=variant)
106
                for block, order in self.block_orders.items() if order is not None
107
            } for op in self.operator]
108
            self.blocks = [{
2✔
109
                b: bl[b].apply for b in bl
110
            } for bl in blocks]
111

112
    @timed_member_call()
2✔
113
    def matvec(self, v):
2✔
114
        """
115
        Compute the matrix-vector product of the ISR one-particle
116
        operator and return the result.
117

118
        If a list of OneParticleOperator objects was passed to the class
119
        instantiation operator, a list of AmplitudeVector objects is returned.
120
        """
121
        ret = [
2✔
122
            sum(block(v) for block in bl_ph.values())
123
            for bl_ph in self.blocks
124
        ]
125
        if len(ret) == 1:
2✔
126
            return ret[0]
2✔
127
        else:
128
            return ret
2✔
129

130
    def rmatvec(self, v):
2✔
131
        # Hermitian operators
132
        if all(op.is_symmetric for op in self.operator):
2✔
133
            return self.matvec(v)
2✔
134
        else:
135
            diffv = [op.ov + op.vo.transpose((1, 0)) for op in self.operator]
2✔
136
            # anti-Hermitian operators
137
            if all(dv.dot(dv) < 1e-12 for dv in diffv):
2!
138
                return [
2✔
139
                    AmplitudeVector(ph=-1.0 * mv.ph, pphh=-1.0 * mv.pphh)
140
                    for mv in self.matvec(v)
141
                ]
142
            # operators without any symmetry
143
            else:
144
                return NotImplemented
×
145

146
    def __matmul__(self, other):
2✔
147
        """
148
        If the matrix-vector product is to be calculated with multiple vectors,
149
        a list of AmplitudeVector objects is returned.
150

151
        Consequently, applying the ISR matrix with multiple operators to multiple
152
        vectors results in an N_vectors x N_operators 2D list of AmplitudeVector
153
        objects.
154
        """
155
        if isinstance(other, AmplitudeVector):
2✔
156
            return self.matvec(other)
2✔
157
        if isinstance(other, list):
2!
158
            if all(isinstance(elem, AmplitudeVector) for elem in other):
2!
159
                return [self.matvec(ov) for ov in other]
2✔
160
        return NotImplemented
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc