Quantum Chemistry¶
PennyLane provides the qchem
module to perform quantum chemistry simulations. It
contains a differentiable HartreeFock solver and the functionality to construct a
fullydifferentiable molecular Hamiltonian that can be used as input to quantum algorithms
such as the variational quantum eigensolver (VQE) algorithm. The qchem
module
also provides tools for building other observables such as molecular dipole moment, spin
and particle number observables. The theoretical foundation of the quantum chemistry functionality
in PennyLane is explained in our white paper.
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,
import pennylane as qml
from pennylane import numpy as np
symbols = ["H", "H"]
geometry = np.array([[0., 0., 0.66140414], [0., 0., 0.66140414]])
molecule = qml.qchem.Molecule(symbols, geometry)
hamiltonian, qubits = qml.qchem.molecular_hamiltonian(molecule)
where:
hamiltonian
is the qubit Hamiltonian of the molecule represented as a PennyLane Hamiltonian andqubits
is the number of qubits needed to perform the quantum simulation.
The molecular_hamiltonian()
function can also be used to construct the molecular Hamiltonian
with external backends that use the
OpenFermionPySCF plugin or the
electronic structure package PySCF, which requires separate
installation. These backends are nondifferentiable and can be selected by setting
method='openfermion'
or method='pyscf'
in molecular_hamiltonian
.
Furthermore, the net charge, the spin multiplicity, the atomic basis functions, the mapping method and the active space can also be specified for each backend.
molecule = qml.qchem.Molecule(
symbols,
geometry,
charge=0,
mult=1,
basis_name='sto3g')
hamiltonian, qubits = qml.qchem.molecular_hamiltonian(
molecule,
mapping='jordan_wigner',
active_electrons=2,
active_orbitals=2
)
Importing the Hamiltonian and ansatz state¶
If the electronic Hamiltonian is built independently using
OpenFermion tools, it can be readily converted
to a PennyLane observable using the import_operator()
function. There is also
capability to import wavefunctions (states) that have been precomputed by traditional quantum chemistry methods
from PySCF, which could be used to for example provide a better
starting point to a quantum algorithm. State import can be accomplished using the import_state()
utility function.
Importing molecular structure data¶
The atomic structure of a molecule can be either defined as an array or imported from an external
file using the read_structure()
function:
symbols, geometry = qml.qchem.read_structure('h2.xyz')
VQE simulations¶
The Variational Quantum Eigensolver (VQE) is a hybrid quantumclassical 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.
PennyLane supports treating Hamiltonians just like any other observable, and the
expectation value of a Hamiltonian can be calculated using qml.expval
:
dev = qml.device('default.qubit', wires=4)
symbols = ["H", "H"]
geometry = np.array([[0., 0., 0.66140414], [0., 0., 0.66140414]])
molecule = qml.qchem.Molecule(symbols, geometry)
hamiltonian, qubits = qml.qchem.molecular_hamiltonian(molecule)
@qml.qnode(dev)
def circuit(params):
qml.BasisState(np.array([1, 1, 0, 0]), wires=[0, 1, 2, 3])
qml.DoubleExcitation(params, wires=[0, 1, 2, 3])
return qml.expval(hamiltonian)
params = np.array(0.20885146442480412, requires_grad=True)
circuit(params)
tensor(1.13618912, requires_grad=True)
The circuit parameter can be optimized using the interface of choice.
Note
For more details on VQE and the quantum chemistry functionality available in
qchem
, check out the PennyLane quantum chemistry tutorials.
Quantum chemistry functions and classes¶
PennyLane supports the following quantum chemistry functions and classes.
Molecular integrals and matrices¶
Return a function that computes the nuclear attraction integral for two contracted Gaussian functions. 

Return a function that computes the electronnuclear attraction matrix for a given set of basis functions. 

Compute the normalization constant for a contracted Gaussian function. 

Return a function that computes the core matrix for a given set of basis functions. 

Return a function that computes the dipole moment integrals over the molecular orbitals. 

Return a function that computes the one and twoelectron integrals in the molecular orbital basis. 

Compute the electronelectron repulsion integral between four primitive Gaussian functions. 

Compute Hermite Gaussian expansion coefficients recursively for two Gaussian functions. 

Compute the kinetic integral for two primitive Gaussian functions. 

Compute the onedimensional multipole moment integral for two primitive Gaussian functions. 

Compute overlap integral for two primitive Gaussian functions. 

Compute the Hermite moment integral recursively. 

Return a function that computes the kinetic integral for two contracted Gaussian functions. 

Return a function that computes the kinetic matrix for a given set of basis functions. 

Compute the molecular density matrix. 

Return a function that computes the multipole moment integral for two contracted Gaussians. 

Return a function that computes the multipole moment matrix for a set of basis functions. 

Compute nuclear attraction integral between primitive Gaussian functions. 

Return a function that computes the overlap integral for two contracted Gaussian functions. 

Return a function that computes the overlap matrix for a given set of basis functions. 

Compute the normalization constant for a primitive Gaussian function. 

Return a function that computes the electronelectron repulsion integral for four contracted Gaussian functions. 

Return a function that computes the electron repulsion tensor for a given set of basis functions. 
Differentiable HartreeFock¶
Return a function that computes the HartreeFock energy. 

Return a function that computes the nuclearrepulsion energy. 

Return a function that performs the selfconsistentfield calculations. 
HartreeFock with external packages¶
Decomposes the molecular Hamiltonian into a linear combination of Pauli operators using OpenFermion tools. 

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

Generates the FermionOperator representing a given oneparticle operator required to build manybody qubit observables. 

Generates the FermionOperator representing a given twoparticle operator required to build manybody qubit observables. 
Differentiable observables¶
Return a function that computes the qubit Hamiltonian. 

Return a function that computes the qubit dipole moment observable. 

Return a function that builds the fermionic dipole moment observable. 

Return a function that computes the fermionic Hamiltonian. 

Create a fermionic observable from molecular orbital integrals. 

Convert a fermionic operator to a qubit operator using the JordanWigner mapping. 

Generate the qubit Hamiltonian of a molecule. 

Convert a fermionic observable to a PennyLane qubit observable. 
Other observables¶
Computes the electric dipole moment operator in the Pauli basis. 

Builds the fermionic manybody observable whose expectation value can be measured in PennyLane. 

Compute the particle number observable \(\hat{N}=\sum_\alpha \hat{n}_\alpha\) in the Pauli basis. 

Compute the total spin observable \(\hat{S}^2\). 

Computes the total spin projection observable \(\hat{S}_z\). 
Qubit tapering¶
Compute a Clifford operator from a set of generators and PauliX operators. 

Get the optimal sector which contains the ground state. 

Generate the single qubit PauliX operators \(\sigma^{x}_{i}\) for each symmetry \(\tau_j\), such that it anticommutes with \(\tau_j\) and commutes with all others symmetries \(\tau_{k\neq j}\). 

Compute the generators \(\{\tau_1, \ldots, \tau_k\}\) for a Hamiltonian over the binary field \(\mathbb{Z}_2\). 

Transform a Hamiltonian with a Clifford operator and then taper qubits. 

Transform a HartreeFock state with a Clifford operator and then taper qubits. 

Transform a gate operation with a Clifford operator and then taper qubits. 
Utility functions¶
Build the active space for a given number of active electrons and active orbitals. 

Generate single and double excitations from a HartreeFock reference state. 

Map the indices representing the single and double excitations generated with the function 

Return the doublefactorized form of a twoelectron integral tensor in spatial basis. 

Decompose a unitary into a sequence of Givens rotation gates with phase shifts and a diagonal phase matrix. 

Generate the HartreeFock statevector with respect to a chosen basis. 

Convert an external operator to a PennyLane operator. 

Convert an external wavefunction to a state vector. 

Obtain symbols and geometry of a compound from the PubChem Database. 

Read 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). 
Molecule class and basis functions¶
Generate default basis set parameters for an atom. 

Create a basis function object. 

Create a molecule object that stores molecular information and default basis set parameters. 

Generates default basis set parameters for a molecule. 