A from-scratch variational quantum eigensolver for H₂ dissociation, built on Qiskit 2.0 without Qiskit Nature.
This repository implements a from-scratch Variational Quantum Eigensolver (VQE) for the H₂ molecule on Qiskit 2.0. The electronic Hamiltonian is expressed in second quantization using one-electron (
Goal: Compute the H₂ dissociation curve at 34 bond distances using a from-scratch VQE pipeline, and validate against exact diagonalization.
Design decisions:
- No Qiskit Nature dependency. Every component is built from scratch: Pauli algebra engine (symplectic bit encoding, vectorized phase computation), Jordan-Wigner mapper, Hartree-Fock solver, QWC measurement grouping.
- Analytic gradients via the parameter-shift rule instead of finite differences.
- Typed Python with mypy in CI, ruff for linting, and 70 pytest tests across 5 modules.
Pre-computed spin orbital integrals for H₂ at 34 bond distances are loaded from .npz files. Each contains one-electron (
def load_h2_spin_orbital_integral(data_path, filename):
"""Load a single H2 integral file (sequential npy streams)."""
file = os.path.join(data_path, filename)
with open(file, "rb") as f:
distance = np.load(f, allow_pickle=False)
one_body = np.load(f, allow_pickle=False)
two_body = np.load(f, allow_pickle=False)
nuc_eneg = np.load(f, allow_pickle=False)
return distance, one_body, two_body, nuc_enegThe Jordan-Wigner transformation converts each fermionic operator Operator representing the qubit Hamiltonian.
def build_qubit_hamiltonian(one_body, two_body, creation_ops, annihilation_ops):
"""Build qubit Hamiltonian from fermionic integrals using JW mapping."""
h1 = build_one_body_qubit_hamiltonian(one_body, creation_ops, annihilation_ops)
h2 = build_two_body_qubit_hamiltonian(two_body, creation_ops, annihilation_ops)
return (h1 + h2).combine().apply_threshold().sort()A particle-number-preserving ansatz starts from the Hartree-Fock state |0101⟩ and applies a CNOT staircase, reducing the double excitation to a single Ry rotation. The resulting state
def h2_ansatz_circuit():
"""Build a particle-number-preserving H2 ansatz with 1 parameter."""
varform = QuantumCircuit(4)
theta = Parameter('theta')
varform.x([1, 3]) # Prepare |0101⟩
varform.cx(1, 0) # CNOT staircase
varform.cx(2, 1)
varform.cx(3, 2)
varform.ry(theta, 3) # Parametric rotation
varform.cx(3, 2) # Reverse staircase
varform.cx(2, 1)
varform.cx(1, 0)
return varform# Clone and install
git clone https://github.com/IsolatedSingularity/Quantum-Chemistry-Eigensolver.git
cd Quantum-Chemistry-Eigensolver
pip install -e ".[dev]"
# Run the test suite
pytest| Command | Description |
|---|---|
qce-dissociation |
Run VQE across all H₂ bond distances and plot the dissociation curve |
qce-visualize |
Generate molecular orbital and VQE energy visualizations |
# Example: compute dissociation curve and save the plot
qce-dissociation --shots 10000 -o dissociation.png
# Generate all visualizations
qce-visualize --which allTo add a new molecule, generate spin-orbital integrals with PySCF (see usage/) and pass the data directory to qce-dissociation --data-path.
| Directory | Purpose |
|---|---|
quantum_chemistry/ |
Core library: Pauli algebra, Jordan-Wigner mapping, estimation, VQE |
quantum_chemistry/molecule/ |
Molecular integrals, Hartree-Fock solver (RHF + UHF), linear algebra |
tests/ |
70 pytest tests (unit, integration, end-to-end VQE) |
examples/ |
Step-by-step tutorials for mapping and estimation |
visualization/ |
Matplotlib visualizations and animations |
h2_data/ |
Pre-computed H₂ spin-orbital integrals (34 bond distances) |
Theoretical Background
Second Quantization and the Jordan-Wigner Mapping
The electronic Hamiltonian in second quantization is
where
The Z-string enforces fermionic antisymmetry. After substitution and simplification, the 4-qubit Hamiltonian reduces to a weighted sum of Pauli strings that can be measured on a quantum device.
Variational Quantum Eigensolver
VQE exploits the variational principle: for any trial state
The classical optimizer tunes
which requires only two additional circuit evaluations per parameter and is exact for gates of the form
Ansatz Design
The ansatz prepares the Hartree-Fock reference
spans the two-dimensional subspace of particle-number-conserving configurations and is exact for H₂ in a minimal basis.
Measurement Reduction
Each Pauli string in the Hamiltonian must be measured independently unless it commutes qubitwise with other strings. Two Pauli operators
Ground state energy at equilibrium: -1.137 Ha (within
Bonding and antibonding molecular orbitals of H₂, computed from one-electron integrals in the STO-3G basis.
Bond-length sweep from 0.3 to 1.95 Å showing the H₂ wavefunction transitioning from a compact bonding state to separated atoms.
VQE energy landscape across 8 sampled bond distances (left) and the full dissociation curve compared against exact diagonalization (right), colored with mako/cubehelix palette.
VQE potential energy surface showing E(theta, R) - E_min(R) across all 34 bond distances. White piecewise paths show discrete gradient-descent steps at 8 sampled geometries; arrows indicate descent direction. Right panel: energy error on a log scale at three geometries (compressed, equilibrium, stretched).
- Implement more sophisticated ansatz circuits, such as the Unitary Coupled Cluster (UCC) ansatz for better accuracy.
- Extend the implementation to handle larger molecules like LiH, BeH₂, or H₂O.
- Incorporate noise models to simulate realistic quantum hardware performance.
- Implement quantum subspace expansion techniques to improve accuracy of excited state calculations.
- Add support for calculating molecular properties beyond the ground state energy (dipole moments, forces, etc.).
Note
This project targets H₂ as a proof of concept. The single-parameter ansatz may not capture all relevant physics for larger molecules, and classical simulation does not account for hardware noise or decoherence.
Built by Jeffrey Morais, Quantum Software Lead at BTQ Technologies.
Inspired by workshop notebooks from Maxime Dion at the Institut Quantique.




