qml.qchem¶
Overview¶
The quantum chemistry module provides the functionality to perform Hartree-Fock calculations and construct observables such as molecular Hamiltonians as well as dipole moment, spin and particle number observables. It also includes functionalities to convert to and from Openfermion’s QubitOperator and FermionOperator.
This submodule provides the functionality to perform quantum chemistry calculations.
Functions¶
|
Build the active space for a given number of active electrons and active orbitals. |
|
Generate default basis set parameters for an atom. |
|
Return a function that computes the nuclear attraction integral for two contracted Gaussian functions. |
|
Return a function that computes the electron-nuclear attraction matrix for a given set of basis functions. |
|
Return the grouped coefficients and observables of a molecular Hamiltonian and the basis rotation unitaries obtained with the basis rotation grouping method. |
|
Compute a Clifford operator from a set of generators and Pauli-X operators. |
|
Compute the normalization constant for a contracted Gaussian function. |
|
Return a function that computes the core matrix for a given set of basis functions. |
|
Decomposes the molecular Hamiltonian into a linear combination of Pauli operators using OpenFermion tools. |
|
Return a function that computes the qubit Hamiltonian. |
|
Return a function that computes the dipole moment integrals over the molecular orbitals. |
|
Return a function that computes the qubit dipole moment observable. |
|
Computes the electric dipole moment operator in the Pauli basis. |
|
Return a function that computes the one- and two-electron integrals in the molecular orbital basis. |
|
Compute the electron-electron repulsion integral between four primitive Gaussian functions. |
|
Generate single and double excitations from a Hartree-Fock reference state. |
|
Map the indices representing the single and double excitations generated with the function |
|
Compute Hermite Gaussian expansion coefficients recursively for two Gaussian functions. |
|
Return the double-factorized form of a two-electron integral tensor in spatial basis. |
|
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 OpenFermion FermionOperator to PennyLane |
|
Compute the kinetic integral for two primitive Gaussian functions. |
|
Compute the one-dimensional multipole moment integral for two primitive Gaussian functions. |
|
Compute overlap integral for two primitive Gaussian functions. |
|
Decompose a unitary into a sequence of Givens rotation gates with phase shifts and a diagonal phase matrix. |
|
Compute the Hermite moment integral recursively. |
|
Return a function that computes the Hartree-Fock energy. |
|
Generate the Hartree-Fock statevector with respect to a chosen basis. |
|
Convert an external wavefunction to a state vector. |
|
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. |
|
Extracts basis set data from the Basis Set Exchange library. |
|
Generates a file from which the mean field electronic structure of the molecule can be retrieved. |
|
Generates default basis set parameters for a molecule. |
|
Obtain symbols and geometry of a compound from the PubChem Database. |
|
Compute the molecular density matrix. |
|
Generate the dipole moment operator for a molecule in the Pauli basis. |
|
Generate the qubit Hamiltonian of a molecule. |
|
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 nuclear-repulsion energy. |
|
Builds the fermionic many-body observable whose expectation value can be measured in PennyLane. |
|
Generates the FermionOperator representing a given one-particle operator required to build many-body qubit observables. |
|
Get the optimal sector which contains the ground state. |
|
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 particle number observable \(\hat{N}=\sum_\alpha \hat{n}_\alpha\) in the Pauli basis. |
|
Compute the normalization constant for a primitive Gaussian function. |
|
Convert a fermionic observable to a PennyLane qubit observable. |
|
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). |
|
Return a function that computes the electron-electron repulsion integral for four contracted Gaussian functions. |
|
Return a function that computes the electron repulsion tensor for a given set of basis functions. |
|
Return a function that performs the self-consistent-field calculations. |
|
Compute the total spin observable \(\hat{S}^2\). |
|
Computes the total spin projection observable \(\hat{S}_z\). |
|
Transform a Hartree-Fock state with a Clifford operator and then taper qubits. |
|
Transform a gate operation with a Clifford operator and then taper qubits. |
|
Convert a PennyLane operator to OpenFermion QubitOperator or FermionOperator. |
|
Generates the FermionOperator representing a given two-particle operator required to build many-body qubit observables. |
Classes¶
|
Create a basis function object. |
|
Create a molecule object that stores molecular information and default basis set parameters. |
Differentiable Hartree-Fock¶
The differentiable Hartree-Fock (HF) solver allows performing differentiable HF calculations and computing exact gradients with respect to molecular geometry, basis set, and circuit parameters simultaneously using the techniques of automatic differentiation available in Autograd. This makes the solver more versatile and robust compared to non-differentiable tools that mainly rely on numerical approaches for computing gradients, which can lead to inaccuracies and instability. Additionally, optimizing the basis set parameters allows reaching lower ground-state energies without increasing the size of the basis set. Overall, the solver allows users to execute end-to-end differentiable algorithms for quantum chemistry.
The differentiable HF solver computes the integrals over basis functions, constructs the relevant matrices, and performs self-consistent-field iterations to obtain a set of optimized molecular orbital coefficients. These coefficients and the computed integrals over basis functions are used to construct the one- and two-body electron integrals in the molecular orbital basis, which can be used to generate differentiable second-quantized Hamiltonians and dipole moments in the fermionic and qubit basis.
The following code shows the construction of the Hamiltonian for the hydrogen molecule where the geometry of the molecule and the basis set parameters are all differentiable.
import pennylane as qml
from pennylane import numpy as np
symbols = ["H", "H"]
# This initial geometry is suboptimal and will be optimized by the algorithm
geometry = np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 2.0]], requires_grad=True)
# The exponents and contraction coefficients of the Gaussian basis functions
alpha = np.array([[3.42525091, 0.62391373, 0.1688554],
[3.42525091, 0.62391373, 0.1688554]], requires_grad = True)
coeff = np.array([[0.15432897, 0.53532814, 0.44463454],
[0.15432897, 0.53532814, 0.44463454]], requires_grad = True)
We then construct the Hamiltonian.
args = [geometry, alpha, coeff] # initial values of the differentiable parameters
molecule = qml.qchem.Molecule(symbols, geometry, alpha=alpha, coeff=coeff)
hamiltonian, qubits = qml.qchem.molecular_hamiltonian(molecule, args=args)
>>> print(hamiltonian)
-0.3596823592263396 * I(0) + 0.1308241430372839 * Z(0) + -0.11496335836158356 * Z(2)+ 0.10316898251630022 * (Z(0) @ Z(2)) + 0.1308241430372839 * Z(1) + 0.15405495855812648 * (Z(0) @ Z(1)) + 0.050130617426473734 * (Y(0) @ X(1) @ X(2) @ Y(3)) + -0.050130617426473734 * (Y(0) @ Y(1) @ X(2) @ X(3)) + -0.050130617426473734 * (X(0) @ X(1) @ Y(2) @ Y(3)) + 0.050130617426473734 * (X(0) @ Y(1) @ Y(2) @ X(3)) + -0.11496335836158356 * Z(3) + 0.15329959994277395 * (Z(0) @ Z(3)) + 0.10316898251630022 * (Z(1) @ Z(3)) + 0.15329959994277395 * (Z(1) @ Z(2)) + 0.16096866639866414 * (Z(2) @ Z(3))
The generated Hamiltonian can be used in a circuit where the molecular geometry, the basis set parameters, and the circuit parameters are optimized simultaneously. Further information about molecular geometry optimization with PennyLane is provided in this paper and this demo.
dev = qml.device("default.qubit", wires=4)
hf_state = np.array([1, 1, 0, 0])
params = [np.array([0.0], requires_grad=True)] # initial values of the circuit parameters
def generate_circuit(mol):
@qml.qnode(dev)
def circuit(*args):
qml.BasisState(hf_state, wires=[0, 1, 2, 3])
qml.DoubleExcitation(*args[0][0], wires=[0, 1, 2, 3])
return qml.expval(qml.qchem.molecular_hamiltonian(mol, args=args[1:])[0])
return circuit
Now that the circuit is defined, we can create a geometry and parameter optimization loop. For convenience, we create a molecule object that stores the molecular parameters.
for n in range(21):
mol = qml.qchem.Molecule(symbols, geometry, alpha=alpha, coeff=coeff)
args = [params, geometry, alpha, coeff] # initial values of the differentiable parameters
# compute gradients with respect to the circuit parameters and update the parameters
g_params = qml.grad(generate_circuit(mol), argnum = 0)(*args)
params = params - 0.25 * g_params[0]
# compute gradients with respect to the nuclear coordinates and update geometry
g_coor = qml.grad(generate_circuit(mol), argnum = 1)(*args)
geometry = geometry - 0.5 * g_coor
# compute gradients with respect to the Gaussian exponents and update the exponents
g_alpha = qml.grad(generate_circuit(mol), argnum = 2)(*args)
alpha = alpha - 0.25 * g_alpha
# compute gradients with respect to the Gaussian contraction coefficients and update them
g_coeff = qml.grad(generate_circuit(mol), argnum = 3)(*args)
coeff = coeff - 0.25 * g_coeff
if n%5 == 0:
print(f'Step: {n}, Energy: {generate_circuit(mol)(*args)}, Maximum Absolute Force: {abs(g_coor).max()}')
Running this optimization, we get the following output in atomic units:
Step: 0, Energy: -1.0491709019856188, Maximum Absolute Force: 0.1580194718925249
Step: 5, Energy: -1.1349862621177522, Maximum Absolute Force: 0.037660768852544046
Step: 10, Energy: -1.1399960666483346, Maximum Absolute Force: 0.005175323916673413
Step: 15, Energy: -1.140321384816611, Maximum Absolute Force: 0.0004138319900744425
Step: 20, Energy: -1.1403680839339787, Maximum Absolute Force: 8.223248376348913e-06
Note that the computed energy is lower than the Full-CI energy, -1.1373060483 Ha, obtained without optimizing the basis set parameters.
The components of the HF solver can also be differentiated individually. For instance, the overlap integral can be differentiated with respect to the basis set parameters as follows
symbols = ["H", "H"]
geometry = np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 2.0]], requires_grad=False)
alpha = np.array([[3.42525091, 0.62391373, 0.1688554],
[3.42525091, 0.62391373, 0.1688554]], requires_grad = True)
coeff = np.array([[0.15432897, 0.53532814, 0.44463454],
[0.15432897, 0.53532814, 0.44463454]], requires_grad = True)
mol = qml.qchem.Molecule(symbols, geometry, alpha=alpha, coeff=coeff)
args = [alpha, coeff]
a = mol.basis_set[0]
b = mol.basis_set[1]
g_alpha = qml.grad(qml.qchem.overlap_integral(a, b), argnum = 0)(*args)
g_coeff = qml.grad(qml.qchem.overlap_integral(a, b), argnum = 1)(*args)
>>> print(g_alpha)
[[ 0.00169332 -0.14826928 -0.37296693]
[ 0.00169332 -0.14826928 -0.37296693]]
OpenFermion-PySCF backend¶
The molecular_hamiltonian()
function can also be used to construct the molecular Hamiltonian
with non-differentiable backends that use the
OpenFermion-PySCF plugin or the
electronic structure package PySCF. The non-differentiable
backends can be selected by setting method='openfermion'
or method='pyscf'
in molecular_hamiltonian
:
import pennylane as qml
from pennylane import numpy as np
symbols = ["H", "H"]
geometry = np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 2.0]])
molecule = qml.qchem.Molecule(symbols, geometry, charge=0, mult=1, basis_name='sto-3g')
hamiltonian, qubits = qml.qchem.molecular_hamiltonian(molecule, method='pyscf')
The non-differentiable backends require either OpenFermion-PySCF
or PySCF
to be installed by
the user with
pip install openfermionpyscf
pip install pyscf