Skip to content

Commit

Permalink
Update all guides to calculate integrals using pyscf (#109) (#111)
Browse files Browse the repository at this point in the history
* Update all guides

* style

(cherry picked from commit 40b276c)

Co-authored-by: Caleb Johnson <[email protected]>
  • Loading branch information
mergify[bot] and caleb-johnson authored Dec 10, 2024
1 parent ba7f2ef commit bc2704f
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 45 deletions.
48 changes: 36 additions & 12 deletions docs/how_tos/choose_subspace_dimension.ipynb

Large diffs are not rendered by default.

42 changes: 33 additions & 9 deletions docs/how_tos/integrate_dice_solver.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
"source": [
"%%capture\n",
"\n",
"import os\n",
"\n",
"import numpy as np\n",
"from pyscf import ao2mo, tools\n",
"import pyscf\n",
"import pyscf.cc\n",
"import pyscf.mcscf\n",
"from qiskit_addon_dice_solver import solve_fermion\n",
"from qiskit_addon_sqd.configuration_recovery import recover_configurations\n",
"from qiskit_addon_sqd.counts import counts_to_arrays, generate_counts_uniform\n",
Expand All @@ -39,12 +40,35 @@
"open_shell = False\n",
"spin_sq = 0\n",
"\n",
"# Read in molecule from disk\n",
"active_space_path = os.path.abspath(os.path.join(\"..\", \"molecules\", \"n2_fci.txt\"))\n",
"mf_as = tools.fcidump.to_scf(active_space_path)\n",
"hcore = mf_as.get_hcore()\n",
"eri = ao2mo.restore(1, mf_as._eri, num_orbitals)\n",
"nuclear_repulsion_energy = mf_as.mol.energy_nuc()\n",
"# Specify molecule properties\n",
"open_shell = False\n",
"spin_sq = 0\n",
"\n",
"# Build N2 molecule\n",
"mol = pyscf.gto.Mole()\n",
"mol.build(\n",
" atom=[[\"N\", (0, 0, 0)], [\"N\", (1.0, 0, 0)]],\n",
" basis=\"6-31g\",\n",
" symmetry=\"Dooh\",\n",
")\n",
"\n",
"# Define active space\n",
"n_frozen = 2\n",
"active_space = range(n_frozen, mol.nao_nr())\n",
"\n",
"# Get molecular integrals\n",
"scf = pyscf.scf.RHF(mol).run()\n",
"num_orbitals = len(active_space)\n",
"n_electrons = int(sum(scf.mo_occ[active_space]))\n",
"num_elec_a = (n_electrons + mol.spin) // 2\n",
"num_elec_b = (n_electrons - mol.spin) // 2\n",
"cas = pyscf.mcscf.CASCI(scf, num_orbitals, (num_elec_a, num_elec_b))\n",
"mo = cas.sort_mo(active_space, base=0)\n",
"hcore, nuclear_repulsion_energy = cas.get_h1cas(mo)\n",
"eri = pyscf.ao2mo.restore(1, cas.get_h2cas(mo), num_orbitals)\n",
"\n",
"# Compute exact energy\n",
"exact_energy = cas.run().e_tot\n",
"\n",
"# Create a seed to control randomness throughout this workflow\n",
"rng = np.random.default_rng(24)\n",
Expand Down Expand Up @@ -133,7 +157,7 @@
}
],
"source": [
"print(\"Exact energy: -109.10288938\")\n",
"print(\"Exact energy: {exact_energy}\")\n",
"print(f\"Estimated energy: {np.min(e_hist[-1])}\")"
]
}
Expand Down
79 changes: 57 additions & 22 deletions docs/how_tos/use_oo_to_optimize_hamiltonian_basis.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tutorials/01_chemistry_hamiltonian.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"\\hat{a}_{r\\sigma}\n",
"$$\n",
"\n",
"$\\hat{a}^\\dagger_{p\\sigma}$/$\\hat{a}_{p\\sigma}$ are the fermionic creation/annihalation operators associated to the $p$-th basis set element and the spin $\\sigma$. $h_{pr}$ and $(pr|qs)$ are the one- and two-body electronic integrals. These are loaded from an ``fcidump`` file with standard chemistry software.\n",
"$\\hat{a}^\\dagger_{p\\sigma}$/$\\hat{a}_{p\\sigma}$ are the fermionic creation/annihalation operators associated to the $p$-th basis set element and the spin $\\sigma$. $h_{pr}$ and $(pr|qs)$ are the one- and two-body electronic integrals.\n",
"\n",
"The SQD workflow with self-consistent configuration recovery is depicted in the following diagram.\n",
"\n",
Expand All @@ -60,7 +60,7 @@
"\n",
"In this tutorial, we will approximate the ground state energy of an $N_2$ molecule. First, we will specify the molecule and its properties. Next, we will create a [local unitary cluster Jastrow (LUCJ)](https://pubs.rsc.org/en/content/articlelanding/2023/sc/d3sc02516k) ansatz (quantum circuit) to generate samples from a quantum computer for ground state energy estimation.\n",
"\n",
"First, we will specify the molecule and its properties"
"First, we will specify the molecule and its properties."
]
},
{
Expand Down

0 comments on commit bc2704f

Please sign in to comment.