Skip to content

Commit

Permalink
add conversion section to fermion operator guide
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsung committed Jan 29, 2025
1 parent 7465bfe commit 5014886
Showing 1 changed file with 158 additions and 41 deletions.
199 changes: 158 additions & 41 deletions docs/how-to-guides/fermion-operator.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
"data": {
"text/plain": [
"FermionOperator({\n",
" (cre_a(3), des_a(0)): -0.25,\n",
" (cre_b(1), des_b(5), cre_a(4)): 1+1j,\n",
" (cre_a(0), des_a(3)): 0.5,\n",
" (cre_b(1), des_b(5), cre_a(4)): 1+1j\n",
" (cre_a(3), des_a(0)): -0.25\n",
"})"
]
},
Expand Down Expand Up @@ -76,7 +76,7 @@
{
"data": {
"text/plain": [
"'FermionOperator({((True, False, 3), (False, False, 0)): -0.25+0j, ((True, False, 0), (False, False, 3)): 0.5+0j, ((True, True, 1), (False, True, 5), (True, False, 4)): 1+1j})'"
"'FermionOperator({((True, True, 1), (False, True, 5), (True, False, 4)): 1+1j, ((True, False, 0), (False, False, 3)): 0.5+0j, ((True, False, 3), (False, False, 0)): -0.25+0j})'"
]
},
"execution_count": 2,
Expand All @@ -88,6 +88,123 @@
"repr(op1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Converting other objects to FermionOperators\n",
"\n",
"Some objects, such as instances of [MolecularHamiltonian](https://qiskit-community.github.io/ffsim/api/ffsim.html#ffsim.MolecularHamiltonian), can be converted to FermionOperators using the `ffsim.fermion_operator` function. This function works on any object that has a `_fermion_operator_` method, so if you make your own class, you can implement this method to tell `ffsim.fermion_operator` how to convert your class to a FermionOperator.\n",
"\n",
"The following code cell demonstrates converting a MolecularHamiltonian to a FermionOperator."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"converged SCF energy = -1.07358293078636\n",
"FermionOperator({\n",
" (cre_b(1), cre_a(1), des_a(1), des_b(0)): -0.0000000000000003137575408652635,\n",
" (cre_b(0), cre_b(1), des_b(0), des_b(0)): 0.00000000000000009140142649458241,\n",
" (cre_b(0), cre_a(1), des_a(0), des_b(0)): 0.00000000000000009140142649458241,\n",
" (cre_a(0), des_a(0)): -1.114600166529875,\n",
" (cre_a(0), cre_b(0), des_b(0), des_a(0)): 0.3132200956766947,\n",
" (cre_b(1), cre_b(0), des_b(1), des_b(1)): -0.0000000000000002916800864863112,\n",
" (cre_a(1), cre_b(1), des_b(1), des_a(0)): -0.0000000000000003137575408652635,\n",
" (cre_a(0), cre_a(0), des_a(0), des_a(1)): 0.00000000000000011329216430280904,\n",
" (cre_a(0), cre_a(0), des_a(0), des_a(0)): 0.3132200956766947,\n",
" (cre_a(1), cre_a(1), des_a(1), des_a(0)): -0.0000000000000003137575408652635,\n",
" (cre_b(0), cre_b(0), des_b(0), des_b(0)): 0.3132200956766947,\n",
" (cre_a(1), des_a(1)): -0.5952339032326008,\n",
" (cre_b(1), cre_a(0), des_a(1), des_b(0)): 0.0985617249345788,\n",
" (cre_a(0), cre_a(0), des_a(1), des_a(0)): 0.00000000000000009140142649458241,\n",
" (cre_a(0), cre_a(1), des_a(1), des_a(0)): 0.3110432387826118,\n",
" (cre_b(0), cre_a(0), des_a(1), des_b(0)): 0.00000000000000009140142649458241,\n",
" (cre_b(0), cre_a(0), des_a(0), des_b(1)): 0.00000000000000011329216430280904,\n",
" (cre_a(0), cre_a(0), des_a(1), des_a(1)): 0.0985617249345788,\n",
" (cre_b(1), cre_b(0), des_b(0), des_b(0)): 0.00000000000000011329216430280904,\n",
" (cre_a(1), cre_a(1), des_a(0), des_a(0)): 0.0985617249345788,\n",
" (cre_a(1), cre_b(0), des_b(0), des_a(1)): 0.3110432387826118,\n",
" (cre_a(1), cre_a(0), des_a(1), des_a(0)): 0.0985617249345788,\n",
" (cre_a(1), cre_a(0), des_a(1), des_a(1)): -0.0000000000000002916800864863112,\n",
" (cre_a(1), cre_a(1), des_a(0), des_a(1)): -0.0000000000000002916800864863112,\n",
" (cre_b(1), cre_a(0), des_a(0), des_b(0)): 0.00000000000000011329216430280904,\n",
" (): 0.52917721092,\n",
" (cre_a(0), cre_b(1), des_b(0), des_a(1)): 0.0985617249345788,\n",
" (cre_b(1), des_b(1)): -0.5952339032326008,\n",
" (cre_b(0), cre_a(1), des_a(0), des_b(1)): 0.0985617249345788,\n",
" (cre_b(1), cre_b(1), des_b(0), des_b(0)): 0.0985617249345788,\n",
" (cre_b(0), des_b(0)): -1.114600166529875,\n",
" (cre_b(0), cre_b(0), des_b(1), des_b(1)): 0.0985617249345788,\n",
" (cre_b(1), cre_b(1), des_b(0), des_b(1)): -0.0000000000000002916800864863112,\n",
" (cre_b(0), cre_b(0), des_b(1), des_b(0)): 0.00000000000000009140142649458241,\n",
" (cre_b(1), cre_a(1), des_a(0), des_b(1)): -0.0000000000000002916800864863112,\n",
" (cre_a(0), cre_a(1), des_a(0), des_a(1)): 0.0985617249345788,\n",
" (cre_b(0), cre_b(1), des_b(0), des_b(1)): 0.0985617249345788,\n",
" (cre_a(1), cre_b(0), des_b(1), des_a(1)): -0.0000000000000002916800864863112,\n",
" (cre_a(1), des_a(0)): 0.0000000000000000405213163257498,\n",
" (cre_b(1), cre_b(0), des_b(1), des_b(0)): 0.0985617249345788,\n",
" (cre_b(1), cre_b(0), des_b(0), des_b(1)): 0.3110432387826118,\n",
" (cre_a(0), cre_b(1), des_b(0), des_a(0)): 0.00000000000000009140142649458241,\n",
" (cre_a(1), cre_b(1), des_b(1), des_a(1)): 0.3267544183944909,\n",
" (cre_a(0), cre_b(1), des_b(1), des_a(1)): -0.0000000000000003137575408652635,\n",
" (cre_b(0), cre_b(1), des_b(1), des_b(1)): -0.0000000000000003137575408652635,\n",
" (cre_b(0), cre_a(1), des_a(1), des_b(0)): 0.3110432387826118,\n",
" (cre_b(1), cre_b(1), des_b(1), des_b(0)): -0.0000000000000003137575408652635,\n",
" (cre_b(1), cre_a(0), des_a(1), des_b(1)): -0.0000000000000002916800864863112,\n",
" (cre_a(0), des_a(1)): -0.0000000000000000038874896604712055,\n",
" (cre_b(0), cre_b(1), des_b(1), des_b(0)): 0.3110432387826118,\n",
" (cre_b(1), cre_a(1), des_a(1), des_b(1)): 0.3267544183944909,\n",
" (cre_a(0), cre_b(0), des_b(1), des_a(0)): 0.00000000000000009140142649458241,\n",
" (cre_a(0), cre_b(1), des_b(1), des_a(0)): 0.3110432387826118,\n",
" (cre_b(0), cre_b(0), des_b(0), des_b(1)): 0.00000000000000011329216430280904,\n",
" (cre_a(0), cre_a(1), des_a(1), des_a(1)): -0.0000000000000003137575408652635,\n",
" (cre_b(1), cre_b(1), des_b(1), des_b(1)): 0.3267544183944909,\n",
" (cre_b(0), cre_a(0), des_a(0), des_b(0)): 0.3132200956766947,\n",
" (cre_b(1), des_b(0)): 0.0000000000000000405213163257498,\n",
" (cre_a(0), cre_b(0), des_b(0), des_a(1)): 0.00000000000000011329216430280904,\n",
" (cre_a(1), cre_a(0), des_a(0), des_a(0)): 0.00000000000000011329216430280904,\n",
" (cre_a(0), cre_b(0), des_b(1), des_a(1)): 0.0985617249345788,\n",
" (cre_a(1), cre_b(0), des_b(1), des_a(0)): 0.0985617249345788,\n",
" (cre_a(0), cre_a(1), des_a(0), des_a(0)): 0.00000000000000009140142649458241,\n",
" (cre_b(0), cre_a(1), des_a(1), des_b(1)): -0.0000000000000003137575408652635,\n",
" (cre_a(1), cre_b(1), des_b(0), des_a(0)): 0.0985617249345788,\n",
" (cre_a(1), cre_b(0), des_b(0), des_a(0)): 0.00000000000000011329216430280904,\n",
" (cre_a(1), cre_b(1), des_b(0), des_a(1)): -0.0000000000000002916800864863112,\n",
" (cre_a(1), cre_a(0), des_a(0), des_a(1)): 0.3110432387826118,\n",
" (cre_a(1), cre_a(1), des_a(1), des_a(1)): 0.3267544183944909,\n",
" (cre_b(0), des_b(1)): -0.0000000000000000038874896604712055,\n",
" (cre_b(1), cre_a(0), des_a(0), des_b(1)): 0.3110432387826118,\n",
" (cre_b(0), cre_a(0), des_a(1), des_b(1)): 0.0985617249345788,\n",
" (cre_b(1), cre_a(1), des_a(0), des_b(0)): 0.0985617249345788\n",
"})\n"
]
}
],
"source": [
"import pyscf\n",
"\n",
"# Build H2 molecule\n",
"mol = pyscf.gto.Mole()\n",
"mol.build(atom=[[\"H\", (0, 0, 0)], [\"H\", (1.0, 0, 0)]], basis=\"sto-6g\", symmetry=\"Dooh\")\n",
"\n",
"# Get molecular data and Hamiltonian\n",
"scf = pyscf.scf.RHF(mol).run()\n",
"mol_data = ffsim.MolecularData.from_scf(scf)\n",
"mol_hamiltonian = mol_data.hamiltonian\n",
"\n",
"# Convert MolecularHamiltonian to FermionOperator\n",
"ferm_op = ffsim.fermion_operator(mol_hamiltonian)\n",
"\n",
"print(ferm_op)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -99,28 +216,28 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FermionOperator({\n",
" (cre_b(1), des_b(5), cre_a(4), des_a(3), des_b(3)): -0.25-0.25j,\n",
" (cre_a(0), des_a(3), cre_b(2)): 0+0.5j,\n",
" (cre_a(0), des_a(3)): 1,\n",
" (cre_b(1), des_b(5), cre_a(4)): 2+2j,\n",
" (cre_a(3), des_a(0), cre_b(2)): 0-0.25j,\n",
" (cre_a(0), des_a(3), des_a(3), des_b(3)): -0.125,\n",
" (cre_b(1), des_b(5), cre_a(4), des_a(3), des_b(3)): -0.25-0.25j,\n",
" (cre_a(3), des_a(0), des_a(3), des_b(3)): 0.0625,\n",
" (des_a(3), des_b(3)): 0.0625,\n",
" (cre_b(1), des_b(5), cre_a(4)): 2+2j,\n",
" (cre_a(0), des_a(3)): 1,\n",
" (cre_a(3), des_a(0)): -0.5,\n",
" (cre_a(3), des_a(0), cre_b(2)): 0-0.25j,\n",
" (des_a(3), des_b(3)): 0.0625,\n",
" (cre_b(1), des_b(5), cre_a(4), cre_b(2)): -1+1j,\n",
" (cre_b(2)): 0-0.25j\n",
"})"
]
},
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -152,28 +269,28 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FermionOperator({\n",
" (cre_b(1), des_b(5), cre_a(4), des_a(3), des_b(3)): -1+1j,\n",
" (cre_a(0), des_a(3), cre_b(2)): 2,\n",
" (cre_a(0), des_a(3)): 0-6j,\n",
" (cre_b(1), des_b(5), cre_a(4)): 12-12j,\n",
" (cre_a(3), des_a(0), cre_b(2)): -1,\n",
" (cre_a(0), des_a(3), des_a(3), des_b(3)): 0+0.5j,\n",
" (cre_b(1), des_b(5), cre_a(4), des_a(3), des_b(3)): -1+1j,\n",
" (cre_a(3), des_a(0), des_a(3), des_b(3)): 0-0.25j,\n",
" (des_a(3), des_b(3)): 0-1.25j,\n",
" (cre_b(1), des_b(5), cre_a(4)): 12-12j,\n",
" (cre_a(0), des_a(3)): 0-6j,\n",
" (cre_a(3), des_a(0)): 0+3j,\n",
" (cre_a(3), des_a(0), cre_b(2)): -1,\n",
" (des_a(3), des_b(3)): 0-1.25j,\n",
" (cre_b(1), des_b(5), cre_a(4), cre_b(2)): 4+4j,\n",
" (cre_b(2)): -5\n",
"})"
]
},
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -197,27 +314,27 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FermionOperator({\n",
" (cre_b(2), cre_b(1), cre_a(4), des_b(5)): 4+4j,\n",
" (cre_b(1), cre_a(4), des_b(5), des_b(3), des_a(3)): -1+1j,\n",
" (cre_b(1), cre_a(4), des_b(5)): -12+12j,\n",
" (cre_b(2), cre_a(0), des_a(3)): 2,\n",
" (cre_b(2), cre_a(3), des_a(0)): -1,\n",
" (cre_b(2)): -5,\n",
" (cre_a(3), des_a(0)): 0+3j,\n",
" (des_b(3), des_a(3)): 0+1.25j,\n",
" (cre_b(2), cre_a(0), des_a(3)): 2,\n",
" (cre_b(1), cre_a(4), des_b(5)): -12+12j,\n",
" (cre_a(0), des_a(3)): 0-6j,\n",
" (cre_a(3), des_b(3), des_a(3), des_a(0)): 0+0.25j\n",
" (cre_a(3), des_b(3), des_a(3), des_a(0)): 0+0.25j,\n",
" (des_b(3), des_a(3)): 0+1.25j,\n",
" (cre_b(2), cre_b(1), cre_a(4), des_b(5)): 4+4j,\n",
" (cre_b(2)): -5,\n",
" (cre_b(1), cre_a(4), des_b(5), des_b(3), des_a(3)): -1+1j,\n",
" (cre_b(2), cre_a(3), des_a(0)): -1\n",
"})"
]
},
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -237,7 +354,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -263,7 +380,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -299,20 +416,20 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0. +0.j , 0. +0.j ,\n",
" 0. +0.j , 0. +0.j ,\n",
" 0.15021897+0.02023638j, 0. +0.j ,\n",
" 0. +0.j , 0. +0.j ,\n",
" 0. +0.j ])"
"array([ 0. +0.j , 0. +0.j ,\n",
" 0. +0.j , 0. +0.j ,\n",
" -0.03767916+0.03776255j, 0. +0.j ,\n",
" 0. +0.j , 0. +0.j ,\n",
" 0. +0.j ])"
]
},
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -331,16 +448,16 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-0.5+1.21984743e-18j])"
"array([-0.5+5.28582186e-18j])"
]
},
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -355,7 +472,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "ffsim-Z17aaLzy",
"display_name": "ffsim",
"language": "python",
"name": "python3"
},
Expand All @@ -369,7 +486,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.13.1"
},
"orig_nbformat": 4
},
Expand Down

0 comments on commit 5014886

Please sign in to comment.