|
| 1 | +# Copyright 2023-2026 The PySCFAD Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import jax |
| 16 | +from pyscfad import numpy as np |
| 17 | + |
| 18 | +def make_mp2_rdm1_ie(Lia, Ljb, eia, ejb): |
| 19 | + naux1, nocc1, nvir1 = Lia.shape |
| 20 | + naux2, nocc2, nvir2 = Ljb.shape |
| 21 | + assert naux1 == naux2 |
| 22 | + assert nvir1 == nvir2 |
| 23 | + naux = naux1 |
| 24 | + nvir = nvir1 |
| 25 | + |
| 26 | + @jax.checkpoint |
| 27 | + def fn(carry, x): |
| 28 | + dmvv, dmoo = carry |
| 29 | + La, ea = x |
| 30 | + buf = np.dot(La.T, Ljb.reshape(naux,-1)).reshape(nvir, nocc2, nvir) |
| 31 | + t2i = buf / (ea[:,None,None] + ejb[None,:,:]) |
| 32 | + dmvv += np.dot(t2i.reshape(nvir, -1), t2i.reshape(nvir, -1).T) |
| 33 | + dmvv -= .5 * np.einsum('ajc,cjb->ab', t2i, t2i) |
| 34 | + dmvv += np.dot(t2i.reshape(-1,nvir).T, t2i.reshape(-1,nvir)) |
| 35 | + dmvv -= .5 * np.einsum('cja,bjc->ab', t2i, t2i) |
| 36 | + |
| 37 | + dmoo += np.einsum('aib,ajb->ij', t2i, t2i) |
| 38 | + dmoo -= .5 * np.einsum('aib,bja->ij', t2i, t2i) |
| 39 | + dmoo += np.einsum('bia,bja->ij', t2i, t2i) |
| 40 | + dmoo -= .5 * np.einsum('bia,ajb->ij', t2i, t2i) |
| 41 | + return (dmvv, dmoo), None |
| 42 | + |
| 43 | + (dmvv, dmoo), _ = jax.lax.scan(fn, (np.zeros((nvir, nvir)), np.zeros((nocc2,nocc2))), |
| 44 | + (Lia.transpose(1,0,2), eia)) |
| 45 | + return dmvv, dmoo |
0 commit comments