# Quantum Chemistry¶

PennyLane provides the qchem module to perform quantum chemistry simulations. It contains a differentiable Hartree-Fock solver and the functionality to construct a fully-differentiable 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]])
hamiltonian, qubits = qml.qchem.molecular_hamiltonian(symbols, geometry)


where:

• hamiltonian 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.

The molecular_hamiltonian() function can also be used to construct the molecular Hamiltonian with an external backend that uses the OpenFermion-PySCF plugin interfaced with the electronic structure package PySCF, which requires separate installation. This backend is non-differentiable and can be selected by setting method='pyscf' in molecular_hamiltonian(). Additionally, if the electronic Hamiltonian is built independently using OpenFermion tools, it can be readily converted to a PennyLane observable using the import_operator() function.

Furthermore, the net charge, the spin multiplicity, the atomic basis functions and the active space can also be specified for each backend.

hamiltonian, qubits = qml.qchem.molecular_hamiltonian(
symbols,
geometry,
charge=0,
mult=1,
basis='sto-3g',
method='pyscf',
active_electrons=2,
active_orbitals=2
)


### 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 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.

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]])
hamiltonian, qubits = qml.qchem.molecular_hamiltonian(symbols, geometry)

@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)

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¶

 attraction_integral Return a function that computes the nuclear attraction integral for two contracted Gaussian functions. attraction_matrix Return a function that computes the electron-nuclear attraction matrix for a given set of basis functions. contracted_norm Compute the normalization constant for a contracted Gaussian function. core_matrix Return a function that computes the core matrix for a given set of basis functions. dipole_integrals Return a function that computes the dipole moment integrals over the molecular orbitals. electron_integrals Return a function that computes the one- and two-electron integrals in the molecular orbital basis. electron_repulsion Compute the electron-electron repulsion integral between four primitive Gaussian functions. expansion Compute Hermite Gaussian expansion coefficients recursively for two Gaussian functions. gaussian_kinetic Compute the kinetic integral for two primitive Gaussian functions. gaussian_moment Compute the one-dimensional multipole moment integral for two primitive Gaussian functions. gaussian_overlap Compute overlap integral for two primitive Gaussian functions. hermite_moment Compute the Hermite moment integral recursively. kinetic_integral Return a function that computes the kinetic integral for two contracted Gaussian functions. kinetic_matrix Return a function that computes the kinetic matrix for a given set of basis functions. mol_density_matrix Compute the molecular density matrix. moment_integral Return a function that computes the multipole moment integral for two contracted Gaussians. moment_matrix Return a function that computes the multipole moment matrix for a set of basis functions. nuclear_attraction Compute nuclear attraction integral between primitive Gaussian functions. overlap_integral Return a function that computes the overlap integral for two contracted Gaussian functions. overlap_matrix Return a function that computes the overlap matrix for a given set of basis functions. primitive_norm Compute the normalization constant for a primitive Gaussian function. repulsion_integral Return a function that computes the electron-electron repulsion integral for four contracted Gaussian functions. repulsion_tensor Return a function that computes the electron repulsion tensor for a given set of basis functions.

### Differentiable Hartree-Fock¶

 hf_energy Return a function that computes the Hartree-Fock energy. nuclear_energy Return a function that computes the nuclear-repulsion energy. scf Return a function that performs the self-consistent-field calculations.

### Hartree-Fock with external packages¶

 decompose Decomposes the molecular Hamiltonian into a linear combination of Pauli operators using OpenFermion tools. meanfield Generates a file from which the mean field electronic structure of the molecule can be retrieved. one_particle Generates the FermionOperator representing a given one-particle operator required to build many-body qubit observables. two_particle Generates the FermionOperator representing a given two-particle operator required to build many-body qubit observables.

### Differentiable observables¶

 diff_hamiltonian Return a function that computes the qubit Hamiltonian. dipole_moment Return a function that computes the qubit dipole moment observable. fermionic_dipole Return a function that builds the fermionic dipole moment observable. fermionic_hamiltonian Return a function that computes the fermionic Hamiltonian. fermionic_observable Create a fermionic observable from molecular orbital integrals. jordan_wigner Convert a fermionic operator to a qubit operator using the Jordan-Wigner mapping. molecular_hamiltonian Generate the qubit Hamiltonian of a molecule. qubit_observable Convert a fermionic observable to a PennyLane qubit observable.

### Other observables¶

 dipole_of Computes the electric dipole moment operator in the Pauli basis. observable Builds the fermionic many-body observable whose expectation value can be measured in PennyLane. particle_number Compute the particle number observable $$\hat{N}=\sum_\alpha \hat{n}_\alpha$$ in the Pauli basis. spin2 Compute the total spin observable $$\hat{S}^2$$. spinz Computes the total spin projection observable $$\hat{S}_z$$.

### Qubit tapering¶

 clifford Compute a Clifford operator from a set of generators and Pauli-X operators. optimal_sector Get the optimal sector which contains the ground state. paulix_ops Generate the single qubit Pauli-X operators $$\sigma^{x}_{i}$$ for each symmetry $$\tau_j$$, such that it anti-commutes with $$\tau_j$$ and commutes with all others symmetries $$\tau_{k\neq j}$$. symmetry_generators Compute the generators $$\{\tau_1, \ldots, \tau_k\}$$ for a Hamiltonian over the binary field $$\mathbb{Z}_2$$. taper Transform a Hamiltonian with a Clifford operator and then taper qubits. taper_hf Transform a Hartree-Fock state with a Clifford operator and then taper qubits. taper_operation Transform a gate operation with a Clifford operator and then taper qubits.

### Utility functions¶

 active_space Build the active space for a given number of active electrons and active orbitals. excitations Generate single and double excitations from a Hartree-Fock reference state. excitations_to_wires Map the indices representing the single and double excitations generated with the function excitations() to the wires that the Unitary Coupled-Cluster (UCCSD) template will act on. factorize Return the double-factorized form of a two-electron integral tensor. hf_state Generate the occupation-number vector representing the Hartree-Fock state. import_operator Convert an external operator to a PennyLane operator. read_structure 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¶

 atom_basis_data Generate default basis set parameters for an atom. BasisFunction Create a basis function object. Molecule Create a molecule object that stores molecular information and default basis set parameters. mol_basis_data Generates default basis set parameters for a molecule.