Quantum Chemistry

PennyLane provides a quantum chemistry module qchem to perform quantum simulations of the electronic structure of molecules. qchem contains tools to construct the electronic Hamiltonian of molecules, and uses PennyLane to implement the Variational Quantum Eigensolver (VQE) algorithm.



To access the qchem module, the PennyLane-QChem plugin must be installed separately:

pip install pennylane-qchem

Building the electronic Hamiltonian

The qchem module provides access to a driver function molecular_hamiltonian() to generate the electronic Hamiltonian in a single call. For example,

from pennylane import qchem
import numpy as np

symbols, coordinates = (['H', 'H'], np.array([0., 0., -0.66140414, 0., 0., 0.66140414]))
h, qubits = qchem.molecular_hamiltonian(


  • h is the qubit Hamiltonian of the molecule represented as a PennyLane Hamiltonian, and

  • qubits is the number of qubits needed to perform the quantum simulation.

Internally, molecular_hamiltonian() calls the following functions in order to generate the qubit Hamiltonian:

read_structure(filepath[, outpath])

Reads the structure of the polyatomic system from a file and returns a list with the symbols of the atoms in the molecule and a 1D array with their positions \([x_1, y_1, z_1, x_2, y_2, z_2, \dots]\) in atomic units (Bohr radius = 1).

meanfield(symbols, coordinates[, name, …])

Generates a file from which the mean field electronic structure of the molecule can be retrieved.

active_space(electrons, orbitals[, mult, …])

Builds the active space for a given number of active electrons and active orbitals.

decompose(hf_file[, mapping, core, active])

Decomposes the molecular Hamiltonian into a linear combination of Pauli operators using OpenFermion tools.

For more fine-grained control, these functions may be called independently as required.

Importing molecular structure data

The atomic structure of a molecule can be imported from an external file using the read_structure() function:

>>> symbols, coordinates = qchem.read_structure('h2.xyz')
>>> print(symbols, coordinates)
['H', 'H'] [0.    0.   -0.66140414    0.    0.    0.66140414]

The geometry of the molecule is returned as a list containing the symbol and the Cartesian coordinates of each atomic species.


The xyz format is supported out of the box. Additionally, if Open Babel is installed, any format recognized by Open Babel is also supported.

See the read_structure() function for more details.

Solving the Hartree-Fock equations

The meanfield() function uses the OpenFermion-PySCF and OpenFermion-Psi4 plugins to solve the Hartree-Fock equations for the molecule using the electronic structure packages PySCF and Psi4, respectively.

For this, it is required to specify a string to label the molecule. Furthermore, the net charge, the spin multiplicity and the atomic basis functions can also be specified.

symbols, coordinates = qchem.read_structure('h2o.xyz')
hf_file = qchem.meanfield(

The output hf_file is the absolute path to the file containing the Hartree-Fock electronic structure of the water molecule.

Mapping the Hamiltonian to the Pauli basis

The function active_space() is used to create an active space by classifying the Hartree-Fock molecular orbitals as core, active, and external orbitals. Within this approximation, a certain number of active electrons can populate the active orbitals.

from openfermion import MolecularData
water = MolecularData(filename=hf_file)
core, active = qchem.active_space(

Once we have defined the active space, decompose() uses OpenFermion functionalities to generate the second-quantized fermionic Hamiltonian and map it to a linear combination of Pauli operators via the Jordan-Wigner or Bravyi-Kitaev transformation. For example,

qubit_hamiltonian = qchem.decompose(

Here, qubit_hamiltonian is an instance of the QubitOperator class of OpenFermion.

VQE simulations

The Variational Quantum Eigensolver (VQE) is a hybrid quantum-classical computational scheme, where a quantum computer is used to prepare the trial wave function of a molecule and to measure the expectation value of the electronic Hamiltonian, while a classical optimizer is used to find its ground state.

We can use ExpvalCost to automatically create the required PennyLane QNodes and define the cost function:

import pennylane as qml

dev = qml.device('default.qubit', wires=4)

def circuit(params, wires):
    qml.BasisState(np.array([1, 1, 0, 0]), wires=wires)
    for i in wires:
        qml.Rot(*params[i], wires=i)
    qml.CNOT(wires=[2, 3])
    qml.CNOT(wires=[2, 0])
    qml.CNOT(wires=[3, 1])

cost = qml.ExpvalCost(circuit, hamiltonian, dev, interface="torch")
params = torch.rand([4, 3])

The rotation angles can be optimized using the machine learning interface of choice until the energy difference between two consecutive iterations has converged to near zero.


For more details on VQE and the quantum chemistry functionality available in qml.qchem, check out the PennyLane quantum chemistry tutorials.