Skip to content

Commit

Permalink
molecular Hamiltonian MPO construction for a spin orbital basis avail…
Browse files Browse the repository at this point in the history
…able via Python interface
  • Loading branch information
cmendl committed Sep 4, 2024
1 parent 7fd9cd7 commit 4fc41ef
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 139 deletions.
160 changes: 60 additions & 100 deletions examples/chemistry/water_molecule.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"sys.path.append(\"../../build/\")\n",
"import chemtensor\n",
"\n",
"# pyscf (https://pyscf.org/) can define a molecular basis, compute overlap integrals and run other computational methods for comparison.\n",
"# pyscf (https://pyscf.org/) defines a molecular orbital basis, computes overlap integrals and runs other computational methods for comparison.\n",
"import pyscf"
]
},
Expand Down Expand Up @@ -62,7 +62,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"converged SCF energy = -74.9307084821\n"
"converged SCF energy = -74.9307084820999\n"
]
}
],
Expand All @@ -80,13 +80,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"E(CCSD) = -74.97016403895398 E_corr = -0.03945555685402184\n"
"E(CCSD) = -74.97016403895393 E_corr = -0.03945555685402184\n"
]
},
{
"data": {
"text/plain": [
"-74.97016403895398"
"-74.97016403895393"
]
},
"execution_count": 4,
Expand All @@ -95,7 +95,6 @@
}
],
"source": [
"\n",
"# run coupled-cluster with single and double excitations (CCSD), for comparison\n",
"ccsd = pyscf.cc.CCSD(hf).run()\n",
"ccsd.e_tot"
Expand Down Expand Up @@ -135,40 +134,6 @@
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def spatial_to_spin_overlap_integrals(h1, h2):\n",
" \"\"\"\n",
" Enlarge the single- and two-particle electron overlap integral tensors\n",
" from an orbital basis without spin to a spin-orbital basis.\n",
" \"\"\"\n",
" h1 = np.asarray(h1)\n",
" h2 = np.asarray(h2)\n",
"\n",
" n = h1.shape[0]\n",
" assert h1.shape == (n, n)\n",
" assert h2.shape == (n, n, n, n)\n",
"\n",
" # single-particle integrals\n",
" h1_so = np.kron(np.eye(2), h1)\n",
"\n",
" # two-particle integrals\n",
" tmp = np.zeros((2*n, 2*n, n, n))\n",
" for i in range(n):\n",
" for j in range(n):\n",
" tmp[:, :, i, j] = np.kron(np.eye(2), h2[:,:, i, j])\n",
" h2_so = np.zeros((2*n, 2*n, 2*n, 2*n))\n",
" for i in range(2*n):\n",
" for j in range(2*n):\n",
" h2_so[i, j, :, :] = np.kron(np.eye(2), tmp[i, j, :, :])\n",
"\n",
" return h1_so, h2_so"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand All @@ -186,7 +151,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -208,34 +173,13 @@
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(14, 14)\n",
"(14, 14, 14, 14)\n"
]
}
],
"source": [
"# extend to spin-orbitals\n",
"h1_so, eri_so = spatial_to_spin_overlap_integrals(h1_mo, eri_mo)\n",
"print(h1_so.shape)\n",
"print(eri_so.shape)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# convert to physicists' convention\n",
"tkin = h1_so\n",
"vint = np.transpose(eri_so, (0, 2, 1, 3))"
"tkin = h1_mo\n",
"vint = np.transpose(eri_mo, (0, 2, 1, 3))"
]
},
{
Expand All @@ -247,44 +191,67 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 4, 16, 31, 38, 43, 52, 51, 52, 43, 38, 31, 16, 4, 1]"
"[1, 36, 58, 96, 96, 58, 36, 1]"
]
},
"execution_count": 11,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hamiltonian = chemtensor.construct_molecular_hamiltonian_mpo(tkin, vint, optimize=True)\n",
"hamiltonian = chemtensor.construct_spin_molecular_hamiltonian_mpo(tkin, vint)\n",
"# virtual bond dimensions\n",
"hamiltonian.bond_dims"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(0, 0), (1, -1), (1, 1), (2, 0)]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# local physical quantum numbers (number of electrons and spin)\n",
"[chemtensor.decode_quantum_number_pair(qnum) for qnum in hamiltonian.qsite]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# number of electrons (determines quantum number sector)\n",
"pnum = 10"
"# overall quantum number sector of quantum state (number of electrons and spin)\n",
"q_pnum = 10\n",
"q_spin = 0\n",
"qnum_sector = chemtensor.encode_quantum_number_pair(q_pnum, q_spin)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# run two-site DMRG\n",
"psi, en_sweeps, entropy = chemtensor.dmrg(hamiltonian, num_sweeps=6, maxiter_lanczos=25, tol_split=1e-9, qnum_sector=pnum)"
"psi, en_sweeps, entropy = chemtensor.dmrg(hamiltonian, num_sweeps=6, maxiter_lanczos=25, tol_split=1e-9, qnum_sector=qnum_sector)"
]
},
{
Expand All @@ -296,16 +263,16 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 4, 7, 10, 14, 19, 19, 20, 16, 11, 7, 4, 2, 1]"
"[1, 4, 13, 27, 19, 16, 4, 1]"
]
},
"execution_count": 14,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -317,21 +284,21 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[-84.88903443027432,\n",
" -84.8890344313337,\n",
" -84.88903443132047,\n",
" -84.88903443131844,\n",
" -84.88903443131815,\n",
" -84.88903443131794]"
"[-84.88903443537285,\n",
" -84.88903443537973,\n",
" -84.88903443537977,\n",
" -84.88903443537987,\n",
" -84.88903443537993,\n",
" -84.8890344353799]"
]
},
"execution_count": 15,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -343,16 +310,16 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-74.97027263097696"
"-74.97027263503892"
]
},
"execution_count": 16,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -365,16 +332,16 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.00010859202298263426"
"0.0001085960849991352"
]
},
"execution_count": 17,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -386,16 +353,16 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5.8616635101316206e-08"
"5.455467544379644e-08"
]
},
"execution_count": 18,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -404,13 +371,6 @@
"# difference to FCI energy\n",
"en_dmrg - en_fci"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
4 changes: 2 additions & 2 deletions examples/python/chemtensor_python.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
],
"source": [
"# local physical quantum numbers (particle number and spin)\n",
"[chemtensor.fermi_hubbard_decode_quantum_numbers(qnum) for qnum in hamiltonian.qsite]"
"[chemtensor.decode_quantum_number_pair(qnum) for qnum in hamiltonian.qsite]"
]
},
{
Expand Down Expand Up @@ -154,7 +154,7 @@
"# overall quantum number sector of quantum state (particle number and spin)\n",
"q_pnum = 7\n",
"q_spin = 1\n",
"qnum_sector = chemtensor.fermi_hubbard_encode_quantum_numbers(q_pnum, q_spin)"
"qnum_sector = chemtensor.encode_quantum_number_pair(q_pnum, q_spin)"
]
},
{
Expand Down
Loading

0 comments on commit 4fc41ef

Please sign in to comment.