Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions docs/notebooks/jordan-wigner.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "5b07e10e-fa76-44f5-a05e-3afb7e77f535",
"metadata": {},
"source": [
"# Jordan-Wigner Transformation for LiH\n",
"\n",
"This script constructs the molecular Hamiltonian of lithium hydride (LiH) using PySCF, performs a Hartree-Fock and CASCI calculation to extract the one- and two-electron integrals, and then maps the resulting fermionic Hamiltonian to a qubit (spin) Hamiltonian via the Jordan-Wigner transformation. The transformation is implemented explicitly using Qiskit's `SparsePauliOp` representation of Pauli strings, allowing for efficient quantum simulation on qubit-based hardware. The result is a sparse spin Hamiltonian suitable for variational algorithms or quantum eigensolvers, and the script outputs both the operator and the number of nonzero Pauli terms involved."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4a218043-9684-4d1c-b796-ce1ec6cf9deb",
"metadata": {},
"outputs": [],
"source": [
"import itertools\n",
"\n",
"import numpy as np\n",
"import pyscf\n",
"from pyscf import ao2mo, gto, mcscf\n",
"from qiskit.quantum_info import SparsePauliOp\n",
"\n",
"# Build LiH molecule using PySCF\n",
"mol = gto.Mole()\n",
"mol.build(\n",
" verbose=0,\n",
" atom=[[\"Li\", (0, 0, 0)], [\"H\", (0, 0, 1.59)]],\n",
" basis=\"sto-6g\",\n",
" spin=0,\n",
" charge=0,\n",
" symmetry=\"Coov\",\n",
")\n",
"\n",
"# Run Hartree-Fock\n",
"scf = pyscf.scf.RHF(mol)\n",
"hf_energy = scf.kernel()\n",
"\n",
"# Define active space and get molecular integrals\n",
"casci = mcscf.CASCI(scf, ncas=3, nelecas=(1, 1))\n",
"cas_space_symmetry = {\"A1\": 3}\n",
"mo = mcscf.sort_mo_by_irrep(casci, scf.mo_coeff, cas_space_symmetry)\n",
"casci.kernel(mo)\n",
"h1e, ecore = casci.get_h1eff()\n",
"h2e = ao2mo.restore(1, casci.get_h2eff(), casci.ncas)\n",
"\n",
"# If you want to visualize the orbitals, run these lines\n",
"# import pyscf.tools\n",
"# for i in range(1, 1 + casci.ncas):\n",
"# pyscf.tools.cubegen.orbital(\n",
"# mol, \"casscf_%d_%.1f.cube\" % (i, scf.mo_occ[i]), casci.mo_coeff[:, i]\n",
"# )\n",
"\n",
"\n",
"def _qubit_create(qubit: int, num_qubits: int):\n",
" \"\"\"Creation operator under Jordan-Wigner transformation.\"\"\"\n",
" qubits = list(range(qubit + 1))\n",
" return SparsePauliOp.from_sparse_list(\n",
" [(\"Z\" * qubit + \"X\", qubits, 0.5), (\"Z\" * qubit + \"Y\", qubits, -0.5j)],\n",
" num_qubits=num_qubits,\n",
" )\n",
"\n",
"\n",
"def _qubit_destroy(qubit: int, num_qubits: int):\n",
" \"\"\"Destruction operator under Jordan-Wigner transformation.\"\"\"\n",
" qubits = list(range(qubit + 1))\n",
" return SparsePauliOp.from_sparse_list(\n",
" [(\"Z\" * qubit + \"X\", qubits, 0.5), (\"Z\" * qubit + \"Y\", qubits, 0.5j)],\n",
" num_qubits=num_qubits,\n",
" )\n",
"\n",
"\n",
"def jordan_wigner(\n",
" h1e: np.ndarray, h2e: np.ndarray, ecore: float, tol: float = 1e-8\n",
") -> SparsePauliOp:\n",
" \"\"\"Apply Jordan-Wigner transformation to a molecular Hamiltonian.\"\"\"\n",
" norb, _ = h1e.shape\n",
" create_ops = [_qubit_create(i, 2 * norb) for i in range(2 * norb)]\n",
" destroy_ops = [_qubit_destroy(i, 2 * norb) for i in range(2 * norb)]\n",
" op = SparsePauliOp.from_sparse_list([(\"\", [], ecore)], num_qubits=2 * norb)\n",
" for p, r in itertools.product(range(norb), repeat=2):\n",
" coeff = h1e[p, r]\n",
" for sigma in range(2):\n",
" op += coeff * create_ops[norb * sigma + p] @ destroy_ops[norb * sigma + r]\n",
" for p, r, q, s in itertools.product(range(norb), repeat=4):\n",
" coeff = 0.5 * h2e[p, r, q, s]\n",
" for sigma, tau in itertools.product(range(2), repeat=2):\n",
" op += (\n",
" coeff\n",
" * create_ops[norb * sigma + p]\n",
" @ create_ops[norb * tau + q]\n",
" @ destroy_ops[norb * tau + s]\n",
" @ destroy_ops[norb * sigma + r]\n",
" )\n",
" return op.simplify(atol=tol)\n",
"\n",
"\n",
"hamiltonian = jordan_wigner(h1e, h2e, ecore)\n",
"\n",
"print(hamiltonian)\n",
"print(\"Number of Pauli terms:\", len(hamiltonian))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c7068e88-5bc3-48e8-b3a3-732eb45a8f4a",
"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.12.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading