Skip to content

Instantly share code, notes, and snippets.

@ncrubin
Created August 20, 2021 19:34
Show Gist options
  • Save ncrubin/8cd2eae91f8431b216e89b4d08943bdc to your computer and use it in GitHub Desktop.
Save ncrubin/8cd2eae91f8431b216e89b4d08943bdc to your computer and use it in GitHub Desktop.
import os
import numpy as np
from itertools import product
import openfermion as of
def read_dr_li_data(ofile):
with open(ofile, 'r') as fid:
text = fid.read().strip().split('\n')
file_name = text[0]
nelectrons = int(text[1].split('=')[-1])
norb = int(text[2].split('=')[-1])
nsorb = 2 * norb
S2 = int(text[3].split('=')[-1])
L2 = int(text[4].split('=')[-1])
J2 = int(text[5].split('=')[-1])
Jz = int(text[6].split('=')[-1])
print(file_name)
print(nelectrons)
print(norb)
print("S2 = ", S2)
print("L2 = ", L2)
print("J2 = ", J2)
print("JZ = ", Jz)
tpdm = np.zeros((nsorb,) * 4, dtype=np.complex128)
for line in text[7:]:
tline = line.strip()
tline = tline.replace('(', '')
tline = tline.replace(')', '')
tline = tline.split(',')
i, j, k, l= [int(xx) for xx in tline[:4]]
real_val, imag_val = float(tline[4]), float(tline[5])
print(i, j, k, l, complex(real_val, imag_val))
# print(tline)
tpdm[i, j, k, l] = float(real_val) + 1j * float(imag_val)
return file_name, nelectrons, norb, S2, L2, J2, Jz, tpdm
if __name__ == "__main__":
print(os.getcwd())
file_name, nelectrons, norb, S2, L2, J2, Jz, tpdm = \
read_dr_li_data('c_sto-3g_tpdm/c_sto-3g_0_0_0_0.out.dat')
nsorb = 2 * norb
nholes = nsorb - nelectrons
print(np.einsum('ijij', tpdm), nelectrons * (nelectrons - 1))
tpdm_of = tpdm.transpose((0, 1, 3, 2))
print(np.einsum('ijji', tpdm_of), nelectrons * (nelectrons - 1))
for i, j, k, l in product(range(nsorb), repeat=4):
assert np.isclose(tpdm_of[i, j, k, l], -tpdm_of[i, j, l, k])
assert np.isclose(tpdm_of[j, i, k, l], -tpdm_of[i, j, k, l])
assert np.isclose(tpdm_of[i, j, k, l], tpdm_of[l, k, j, i].conj())
# if (not np.isclose(tpdm_of[i, j, k, l].real, tpdm_of[l, k, j, i].real)
# and abs(tpdm_of[i, j, k, l]) > 1.0E-4):
# print(i, j, k, l, tpdm_of[i, j, k, l], tpdm_of[l, k, j, i])
qmat = tpdm_of.transpose((0, 1, 3, 2)).reshape((nsorb**2, nsorb**2))
print(np.linalg.norm(qmat - qmat.conj().T))
opdm = of.map_two_pdm_to_one_pdm(tpdm_of, nelectrons)
assert of.is_hermitian(opdm)
assert np.isclose(opdm.trace(), nelectrons)
oqdm = of.map_one_pdm_to_one_hole_dm(opdm)
assert of.is_hermitian(oqdm)
assert np.isclose(oqdm.trace(), nholes)
tqdm = of.map_two_pdm_to_two_hole_dm(tpdm_of, opdm)
assert np.isclose(np.einsum('ijji', tqdm), nholes * (nholes - 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment