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 generate_hamiltonian() to generate the electronic Hamiltonian in a single call. For example,

h, nr_qubits = qml.qchem.generate_hamiltonian(


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

  • nr_qubits is the number of qubits operators needed to represent it.

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

read_structure(filepath[, outpath])

Reads the molecular structure from a file and creates a list containing the symbol and Cartesian coordinates of the atomic species.

meanfield_data(mol_name, geometry, charge, …)

Launches the meanfield (Hartree-Fock) electronic structure calculation.

active_space(mol_name, hf_data[, …])

Builds the active space by partitioning the set of Hartree-Fock molecular orbitals.

decompose_hamiltonian(mol_name, hf_data[, …])

Decomposes the electronic 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:

>>> geometry = qml.qchem.read_structure('h2o.SDF')
>>> print(geometry)
[['H', (-0.0211, -0.002, 0.0)], ['O', (0.8345, 0.4519, 0.0)], ['H', (1.4769, -0.273, 0.0)]]

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_data() 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, its net charge, the spin multiplicity and the atomic basis functions.

geometry = qml.qchem.read_structure('h2o.SDF')
hf_data = qml.qchem.meanfield_data(

The output variable hf_data stores the path to the directory containing the file 'water.hd5' with 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 doubly-occupied, active, and external orbitals. Within this approximation, a certain number of active electrons can populate the active orbitals.

d_occ_indices, active_indices = qml.qchem.active_space(

Once we have defined the active space, decompose_hamiltonian() calls OpenFermion 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 = qml.qchem.decompose_hamiltonian(

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 VQECost to automatically create the required PennyLane QNodes and define the cost function:

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.VQECost(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.