{ "cells": [ { "cell_type": "markdown", "id": "a4d9978c-e611-429e-991c-8a550d9b123d", "metadata": {}, "source": [ "# Loading Molecular data\n", "\n", "The most common usecase for `FermionicOperator` is for representing molecules from Quantum Chemistry. When using standard classical quantum chemistry software, molecules are often stored in FCIDump format, or as matrices representing one- and two-body integrals. " ] }, { "cell_type": "code", "execution_count": 10, "id": "0228dcfd-f005-4065-a39e-fd52712c6f67", "metadata": {}, "outputs": [], "source": [ "import pyscf\n", "import pyscf.mcscf\n", "\n", "import fulqrum as fq\n", "from fulqrum.convert import integrals_to_fq_fermionic_op" ] }, { "cell_type": "markdown", "id": "e8b020a4-dc6a-417b-a352-9ffed161c212", "metadata": {}, "source": [ "## Loading an operator from an FCIDump file\n", "\n", "FCIDump is a common format for dealing with molecules with real-valued coefficients. Fulqrum can directly load molecules in this format, evaluating only those terms that are unique: " ] }, { "cell_type": "code", "execution_count": 6, "id": "ac656bed-12f6-445a-b336-51676aa23ead", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Parsing ./data/fcidump_Fe4S4_MO.txt\n" ] } ], "source": [ "fop = fq.FermionicOperator.from_fcidump(\"./data/fcidump_Fe4S4_MO.txt\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "532b117d-97e9-4a83-83dd-2790ce7640d3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2476008" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fop.size()" ] }, { "cell_type": "markdown", "id": "c2f5e982-22f3-49df-855a-913033ba30a1", "metadata": {}, "source": [ "Importantly, because only unique terms are present in the final operator, the Extended Jordan-Wigner transformation necessary to bring the operator into a `QubitOperator` is much faster." ] }, { "cell_type": "markdown", "id": "cc8f5f0c-27cd-4ce2-b6cf-fd9949e71038", "metadata": {}, "source": [ "## Loading a molecule from integrals\n", "\n", "It is also possible to load molecules from the integrals generated by programs such as [PySCF]([https://pyscf.org/). This is done using the `integrals_to_fq_fermionic_op` routine. For example, generating the integrals from PySCF looks like:" ] }, { "cell_type": "code", "execution_count": 16, "id": "d2894305-a225-4f89-9caa-2a3e80146061", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "converged SCF energy = -108.835236570774\n" ] } ], "source": [ "# 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", ")\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) # hcore: one-body integrals\n", "eri = pyscf.ao2mo.restore(1, cas.get_h2cas(mo), num_orbitals) # eri: two-body integrals" ] }, { "cell_type": "markdown", "id": "92bcc248-c8e6-4abe-bd67-8511e1c15588", "metadata": {}, "source": [ "From which we can load an operator using the one- and two-body integrals, along with a constant term, if any" ] }, { "cell_type": "code", "execution_count": 17, "id": "6354a211-d409-4097-89e0-18ecd089ec3b", "metadata": {}, "outputs": [], "source": [ "fop2 = integrals_to_fq_fermionic_op(one_body_integrals=hcore, two_body_integrals=eri)" ] }, { "cell_type": "code", "execution_count": 18, "id": "682a4250-9fd4-4723-b649-dee44cae1b75", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "21520" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fop2.size()" ] }, { "cell_type": "code", "execution_count": null, "id": "88eeb56d-0af8-49f9-8a1f-26f5c1188276", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.4" } }, "nbformat": 4, "nbformat_minor": 5 }