OpenFermion

raw JSON →
1.7.1 verified Mon Apr 27 auth: no python

OpenFermion is an open-source library for quantum chemistry simulation on quantum computers. Version 1.7.1 (requires Python >=3.10) provides tools for mapping fermionic Hamiltonians to qubit operators, variational quantum eigensolver (VQE) workflows, and interfaces to quantum computing SDKs. Released periodically, approximately quarterly.

pip install openfermion
error AttributeError: 'MolecularData' object has no attribute 'n_qubits'
cause MolecularData was not loaded. Accessing attributes before `.load()` fails.
fix
Add molecule.load() after constructing the MolecularData object.
error ImportError: cannot import name 'InteractionOperator' from 'openfermion'
cause In versions >=1.0, the import path changed. InteractionOperator is still available but must be imported from `openfermion.ops` only in older versions.
fix
Use from openfermion import InteractionOperator (v1.0+) or from openfermion.ops import InteractionOperator (v0.x). Check your version.
error ModuleNotFoundError: No module named 'openfermionpyscf'
cause openfermionpyscf is a separate plugin package for PySCF integration. Not included by default.
fix
Install with pip install openfermionpyscf and import as import openfermionpyscf.
breaking In v1.0, many top-level imports changed from submodules (e.g., `openfermion.ops`, `openfermion.utils`) to direct `openfermion`. Code using old import paths will break.
fix Replace e.g. `from openfermion.ops import InteractionOperator` with `from openfermion import InteractionOperator`.
deprecated `get_ground_state` function is deprecated. Use `eigensolver` from `openfermion` or `scipy.sparse.linalg.eigsh`.
fix Use `from openfermion import eigensolver` or `scipy.sparse.linalg.eigsh`.
gotcha When using `MolecularData`, the molecule must be loaded with `.load()` before accessing properties. Forgetting this raises an `AttributeError`.
fix Always call `molecule.load()` after creating `MolecularData` object.
gotcha The Jordan-Wigner transform of a large Hamiltonian can produce a very dense operator; consider using Bravyi-Kitaev or other transforms for efficiency.
fix Use `of.bravyi_kitaev(hamiltonian)` or `of.symmetric_jordan_wigner(hamiltonian)` to reduce qubit count or operator weight.

Basic workflow: load molecular data, obtain fermionic Hamiltonian, map to qubit operator, and inspect sparse matrix representation.

import openfermion as of

# Create a simple hydrogen molecule (minimal basis)
geometry = [('H', (0.0, 0.0, 0.0)), ('H', (0.0, 0.0, 0.7414))]
molecule = of.MolecularData(geometry, basis='sto-3g', multiplicity=1, charge=0)
molecule.load()

# Get the fermionic Hamiltonian
hamiltonian = molecule.get_molecular_hamiltonian()
print(hamiltonian)

# Map to qubit Hamiltonian (Jordan-Wigner)
qubit_ham = of.jordan_wigner(hamiltonian)
print(qubit_ham)

# Get sparse matrix
sparse_mat = of.get_sparse_operator(qubit_ham)
print(sparse_mat.shape)