/
orbparams.py
276 lines (231 loc) · 8.96 KB
/
orbparams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""
Derived from: https://github.com/diffqc/dqc/blob/master/dqc/hamilton/orbparams.py
"""
from typing import List
import torch
__all__ = ["BaseOrbParams", "QROrbParams", "MatExpOrbParams"]
class BaseOrbParams(object):
"""Class that provides free-parameterization of orthogonal orbitals.
Examples
--------
>>> import torch
>>> from deepchem.utils.dft_utils import BaseOrbParams
>>> class MyOrbParams(BaseOrbParams):
... @staticmethod
... def params2orb(params, coeffs, with_penalty):
... return params, coeffs
... @staticmethod
... def orb2params(orb):
... return orb, torch.tensor([0], dtype=orb.dtype, device=orb.device)
>>> params = torch.randn(3, 4, 5)
>>> coeffs = torch.randn(3, 4, 5)
>>> with_penalty = 0.1
>>> orb, penalty = MyOrbParams.params2orb(params, coeffs, with_penalty)
>>> params2, coeffs2 = MyOrbParams.orb2params(orb)
>>> torch.allclose(params, params2)
True
"""
@staticmethod
def params2orb( # type: ignore[empty-body]
params: torch.Tensor,
coeffs: torch.Tensor,
with_penalty: float = 0.0) -> List[torch.Tensor]:
"""
Convert the parameters & coefficients to the orthogonal orbitals.
``params`` is the tensor to be optimized in variational method, while
``coeffs`` is a tensor that is needed to get the orbital, but it is not
optimized in the variational method.
Parameters
----------
params: torch.Tensor
The free parameters to be optimized.
coeffs: torch.Tensor
The coefficients to get the orthogonal orbitals.
with_penalty: float (default 0.0)
If not 0.0, return the penalty term for the free parameters.
Returns
-------
orb: torch.Tensor
The orthogonal orbitals.
penalty: torch.Tensor
The penalty term for the free parameters. If ``with_penalty`` is 0.0,
this is not returned.
"""
pass
@staticmethod
def orb2params( # type: ignore[empty-body]
orb: torch.Tensor) -> List[torch.Tensor]:
"""
Get the free parameters from the orthogonal orbitals. Returns ``params``
and ``coeffs`` described in ``params2orb``.
Parameters
----------
orb: torch.Tensor
The orthogonal orbitals.
Returns
-------
params: torch.Tensor
The free parameters to be optimized.
coeffs: torch.Tensor
The coefficients to get the orthogonal orbitals.
"""
pass
class QROrbParams(BaseOrbParams):
"""
Orthogonal orbital parameterization using QR decomposition.
The orthogonal orbital is represented by:
P = QR
Where Q is the parameters defining the rotation of the orthogonal tensor,
and R is the coefficients tensor.
Examples
--------
>>> import torch
>>> from deepchem.utils.dft_utils import QROrbParams
>>> params = torch.randn(3, 3)
>>> coeffs = torch.randn(4, 3)
>>> with_penalty = 0.1
>>> orb, penalty = QROrbParams.params2orb(params, coeffs, with_penalty)
>>> params2, coeffs2 = QROrbParams.orb2params(orb)
"""
@staticmethod
def params2orb(params: torch.Tensor,
coeffs: torch.Tensor,
with_penalty: float = 0.0) -> List[torch.Tensor]:
"""
Convert the parameters & coefficients to the orthogonal orbitals.
``params`` is the tensor to be optimized in variational method, while
``coeffs`` is a tensor that is needed to get the orbital, but it is not
optimized in the variational method.
Parameters
----------
params: torch.Tensor
The free parameters to be optimized.
coeffs: torch.Tensor
The coefficients to get the orthogonal orbitals.
with_penalty: float (default 0.0)
If not 0.0, return the penalty term for the free parameters.
Returns
-------
orb: torch.Tensor
The orthogonal orbitals.
penalty: torch.Tensor
The penalty term for the free parameters. If ``with_penalty`` is 0.0,
this is not returned.
"""
orb, _ = torch.linalg.qr(params)
if with_penalty == 0.0:
return [orb]
else:
# QR decomposition's solution is not unique in a way that every column
# can be multiplied by -1 and it still a solution
# So, to remove the non-uniqueness, we will make the sign of the sum
# positive.
s1 = torch.sign(orb.sum(dim=-2, keepdim=True)) # (*BD, 1, norb)
s2 = torch.sign(params.sum(dim=-2, keepdim=True))
penalty = torch.mean((orb * s1 - params * s2)**2) * with_penalty
return [orb, penalty]
@staticmethod
def orb2params(orb: torch.Tensor) -> List[torch.Tensor]:
"""
Get the free parameters from the orthogonal orbitals. Returns ``params``
and ``coeffs`` described in ``params2orb``.
Parameters
----------
orb: torch.Tensor
The orthogonal orbitals.
Returns
-------
params: torch.Tensor
The free parameters to be optimized.
coeffs: torch.Tensor
The coefficients to get the orthogonal orbitals.
"""
coeffs = torch.tensor([0], dtype=orb.dtype, device=orb.device)
return [orb, coeffs]
class MatExpOrbParams(BaseOrbParams):
"""
Orthogonal orbital parameterization using matrix exponential.
The orthogonal orbital is represented by:
P = matrix_exp(Q) @ C
where C is an orthogonal coefficient tensor, and Q is the parameters defining
the rotation of the orthogonal tensor.
Examples
--------
>>> from deepchem.utils.dft_utils import MatExpOrbParams
>>> params = torch.randn(3, 3)
>>> coeffs = torch.randn(4, 3)
>>> with_penalty = 0.1
>>> orb, penalty = MatExpOrbParams.params2orb(params, coeffs, with_penalty)
>>> params2, coeffs2 = MatExpOrbParams.orb2params(orb)
"""
@staticmethod
def params2orb(params: torch.Tensor,
coeffs: torch.Tensor,
with_penalty: float = 0.0) -> List[torch.Tensor]:
"""
Convert the parameters & coefficients to the orthogonal orbitals.
``params`` is the tensor to be optimized in variational method, while
``coeffs`` is a tensor that is needed to get the orbital, but it is not
optimized in the variational method.
Parameters
----------
params: torch.Tensor
The free parameters to be optimized. (*, nparams)
coeffs: torch.Tensor
The coefficients to get the orthogonal orbitals. (*, nao, norb)
with_penalty: float (default 0.0)
If not 0.0, return the penalty term for the free parameters.
Returns
-------
orb: torch.Tensor
The orthogonal orbitals.
penalty: torch.Tensor
The penalty term for the free parameters. If ``with_penalty`` is 0.0,
this is not returned.
"""
nao = coeffs.shape[-2]
norb = coeffs.shape[-1] # noqa: F841
nparams = params.shape[-1]
bshape = params.shape[:-1]
# construct the rotation parameters
triu_idxs = torch.triu_indices(nao, nao, offset=1)[..., :nparams]
rotmat = torch.zeros((*bshape, nao, nao),
dtype=params.dtype,
device=params.device)
rotmat[..., triu_idxs[0], triu_idxs[1]] = params
rotmat = rotmat - rotmat.transpose(-2, -1).conj()
# calculate the orthogonal orbital
ortho_orb = torch.matrix_exp(rotmat) @ coeffs
if with_penalty != 0.0:
penalty = torch.zeros((1,),
dtype=params.dtype,
device=params.device)
return [ortho_orb, penalty]
else:
return [ortho_orb]
@staticmethod
def orb2params(orb: torch.Tensor) -> List[torch.Tensor]:
"""
Get the free parameters from the orthogonal orbitals. Returns ``params``
and ``coeffs`` described in ``params2orb``.
Parameters
----------
orb: torch.Tensor
The orthogonal orbitals.
Returns
-------
params: torch.Tensor
The free parameters to be optimized.
coeffs: torch.Tensor
The coefficients to get the orthogonal orbitals.
"""
# orb: (*, nao, norb)
nao = orb.shape[-2]
norb = orb.shape[-1]
nparams = norb * (nao - norb) + norb * (norb - 1) // 2
# the orbital becomes the coefficients while params is all zeros (no rotation)
coeffs = orb
params = torch.zeros((*orb.shape[:-2], nparams),
dtype=orb.dtype,
device=orb.device)
return [params, coeffs]