Quantum Chemistry with PennyLane

In quantum chemistry and materials science, the term electronic structure methods encompasses the approximations used to find the many-electron wave function of polyatomic systems. Electronic structure methods rely on the Born-Oppenheimer approximation [1], which allows to write the electronic Hamiltonian of the molecule as an operator which depends parametrically on the “frozen” nuclear positions.

Once the electronic problem is well defined, estimating the molecular properties with chemical accuracy requires wave-function-based electronic structure calculations. However, even if we have access to powerful high-performance computers, the application of post-Hartree-Fock electron correlation methods [2] becomes extremely challenging even for molecular systems with a few atoms.

Quantum computers offer a promising avenue for major breakthroughs in quantum chemistry. For example, a quantum computer consisting of 50 qubits could naturally encode the wave function of the water molecule, which on a classical computer would have to be obtained by diagonalizing a Hamiltonian matrix with dimensions on the order of \(\sim 10^{11}\). In particular, the Variational Quantum Eigensolver (VQE) [3] is a promising 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 adjust the quantum circuit parameters in order to find the lowest eigenvalue of the measured Hamiltonian.

The goal of this tutorial is to highlight PennyLane’s quantum chemistry functions and abilities to build the electronic Hamiltonian of any molecule starting with its geometry and continue all the way to obtaining the Hamiltonian represented in the basis of Pauli matrices. This tutorial can also be an opportunity to become more familiar with fundamental concepts in quantum chemistry.

Sit down, brew a hot drink, and let’s take a look!

Importing the molecular structure

The first step is to import PennyLane.

import pennylane as qml

In this example, we construct the electronic Hamiltonian of one of the most unique molecules: water. We begin by reading the positions of the oxygen and hydrogen atoms. The equilibrium geometry of water is read from the file h2o.xyz and stored in a list containing the symbol and the Cartesian coordinates of each atomic species:

geometry = qml.qchem.read_structure('h2o.xyz')
print("The total number of atoms is: {}".format(len(geometry)))
print(geometry)

Out:

The total number of atoms is: 3
[['H', (-0.0211, -0.002, 0.0)], ['O', (0.8345, 0.4519, 0.0)], ['H', (1.4769, -0.273, 0.0)]]

Note

The xyz format is supported out of the box. If Open Babel is installed, any format recognized by Open Babel is also supported by PennyLane, such as .mol and .sdf.

Please see the read_structure() and Open Babel documentation for more information on installing Open Babel.

Calling the function read_structure() also creates the file structure.xyz, which we can use to visualize our molecule using any molecule editor, e.g., Avogadro.

../_images/water_structure1.png

Solve the Hartree-Fock equations

The next step is to solve the Hartree-Fock (HF) equations for our molecule. The HF method is a mean field approximation, where each electron in the molecule is treated as an independent particle that moves under the influence of the nuclei Coulomb potential and a mean field generated by all other electrons. The Hartree-Fock approximation is typically the starting point for most electron correlation methods in quantum chemistry, such as Configuration Interaction (CI) and Coupled Cluster (CC) methods among others [2].

Before launching the HF calculation using the function meanfield_data(), we need to specify a string to label the molecule and the net charge of the molecule. In this example we choose 'water' as the string. On the other hand, although positively or negatively charged molecules can be simulated, we choose a neutral system.

name = 'water'
charge = 0

In the Hartree-Fock method the many-electron wave function is approximated by a Slater determinant [4] that results from occupying the lowest-energy molecular orbitals until all electrons in the molecule are accommodated. The way molecular orbitals are occupied matters as they determine the self-consistent field.

Let’s focus on our water molecule with a total number of ten electrons. For example, the Slater determinant resulting from occupying the first five lowest-energy molecular orbitals with two paired electrons in each orbital, one with spin-up and the other with spin-down, is said to be a closed-shell HF state with spin multiplicity one. Alternatively, if we define an occupation where the first four orbitals are doubly occupied and the next two are singly occupied by unpaired electrons with spin-up, this is said to be an open-shell HF state with multiplicity three.


../_images/hf_references.png

The take-home message in this context, is that the multiplicity, which we can set as \((N_\mathrm{unpaired}^e + 1)\) with \(N_\mathrm{unpaired}^e\) being the number of unpaired electrons, determines the occupation of the molecular orbitals in the HF calculations. In this tutorial we will consider a closed-shell reference state.

multiplicity = 1

Now we need to define the atomic basis set. Hartree-Fock molecular orbitals are typically represented as a Linear Combination of Atomic Orbitals (LCAO) which are further approximated by using Gaussian function. The Basis Set Exchange database is an excellent source of Gaussian-type orbitals, although many of these basis sets are already incorporated in modern quantum chemistry packages. In this example we choose the minimum basis set 'sto-3g' of Slater-type orbitals (STO) which provides the minimum number of atomic orbitals required to accommodate the electrons of the neutral atoms.

basis_set = 'sto-3g'

Finally, we can call the function meanfield_data() to launch the mean field calculation. At present, the quantum chemistry packages PySCF or Psi4 can be chosen to solve the Hartree-Fock equations. In this example, we choose 'pyscf', which is the default option, but the same results can be obtained using 'psi4'.

hf_data = qml.qchem.meanfield_data(name, geometry, charge,
                                   multiplicity, basis_set, qc_package='pyscf')

Once the calculation is completed, the string variable hf_data returned by the function stores the path to the directory containing the file 'water.hdf5' with the Hartree-Fock electronic structure of the water molecule.

import os
print(hf_data)
for file in os.listdir(hf_data):
    print(file)

Out:

./pyscf/sto-3g
water.hdf5

At this stage, we have a basis set of molecular orbitals. Next, we can use the function active_space() to define an active space. But, what is an active space?

Defining an active space

In general, post-Hartree-Fock electron correlation methods expand the molecule’s wave function around the Hartree-Fock solution, by adding Slater determinants, commonly referred to as configurations, that result from exciting the electrons from the occupied to the unoccupied HF orbitals. Despite the fact that there are different techniques to truncate this expansion, the number of configurations increases combinatorially with the number of electrons and basis functions and the task of finding the wave function expansion coefficients becomes numerically intractable should we want to include the full set of molecular orbitals.

In order to circumvent the combinatorial explosion, we can create an active space by classifying the molecular orbitals as doubly-occupied, active, and external orbitals:

  • Doubly-occupied orbitals are always occupied by two electrons.
  • Active orbitals can be occupied by zero, one, or two electrons.
  • The external orbitals are never occupied.

Within this approximation, a certain number of active electrons can populate the active orbitals from which we can generate a finite-size space of Slater determinants.

../_images/sketch_active_space.png

Note

The number of active spin-orbitals determines the number of qubits required to perform quantum simulations of the electronic structure of the molecule.

For the case of the water molecule described using a minimal basis set, we have a total of ten electrons occupying the first five out of seven molecular orbitals in the HF reference state. Let’s partition the HF orbitals to define an active space of four electrons in four active orbitals:

d_occ_indices, active_indices = qml.qchem.active_space(name, hf_data,
                                                       n_active_electrons=4,
                                                       n_active_orbitals=4)

Viewing the results:

print("List of doubly-occupied molecular orbitals: {:}".format(d_occ_indices))
print("List of active molecular orbitals: {:}".format(active_indices))
print("Number of qubits required for quantum simulation: {:}".format(2*len(active_indices)))

Out:

List of doubly-occupied molecular orbitals: [0, 1, 2]
List of active molecular orbitals: [3, 4, 5, 6]
Number of qubits required for quantum simulation: 8

Notice that calling the active_space() function without specifying an active space results in no doubly-occupied orbitals—all molecular orbitals are considered to be active.

no_d_occ, all_active = qml.qchem.active_space(name, hf_data)
print("List of doubly-occupied molecular orbitals: {:}".format(no_d_occ))
print("List of active molecular molecular orbitals: {:}".format(all_active))
print("Number of qubits required for quantum simulation: {:}".format(2*len(all_active)))

Out:

List of doubly-occupied molecular orbitals: []
List of active molecular molecular orbitals: [0, 1, 2, 3, 4, 5, 6]
Number of qubits required for quantum simulation: 14

Building the Hamiltonian

Once we have an active space defined to generate the correlated wave function of the molecule, the next step is to build the second-quantized fermionic Hamiltonian,

\[H = \sum_{p,q} h_{pq} c_p^\dagger c_q + \frac{1}{2} \sum_{p,q,r,s} h_{pqrs} c_p^\dagger c_q^\dagger c_r c_s,\]

and apply the Jordan-Wigner or Bravyi-Kitaev transformation [5] to map it to a linear combination of tensor products of Pauli operators

\[\sum_j C_j \prod_i \sigma_i^{(j)},\]

where \(C_j\) is a scalar coefficient and \(\sigma_i^{(j)}\) denotes the \(j\)-th Pauli matrix \(X\), \(Y\) or \(Z\) acting on the \(i\)-th qubit. To perform the fermionic-to-qubit transformation of the electronic Hamiltonian, the one-body and two-body Coulomb matrix elements \(h_{pq}\) and \(h_{pqrs}\) [2] describing the fermionic Hamiltonian are retrieved from the previously generated file './pyscf/sto-3g/water.hdf5'.

qubit_hamiltonian = qml.qchem.decompose_hamiltonian(
    name,
    hf_data,
    mapping='jordan_wigner',
    docc_mo_indices=d_occ_indices,
    active_mo_indices=active_indices
)
print("Electronic Hamiltonian of the water molecule represented in the Pauli basis")
print(qubit_hamiltonian)

Out:

Electronic Hamiltonian of the water molecule represented in the Pauli basis
(-73.13873231352534+0j) [] +
(-0.013873381748426065+0j) [X0 X1 Y2 Y3] +
(-0.01782514099578653+0j) [X0 X1 Y4 Y5] +
(-1.0358477602959626e-06+0j) [X0 X1 Y4 Z5 Z6 Y7] +
(-1.0358477602959626e-06+0j) [X0 X1 X5 X6] +
(-0.01736611899465138+0j) [X0 X1 Y6 Y7] +
(0.013873381748426065+0j) [X0 Y1 Y2 X3] +
(0.01782514099578653+0j) [X0 Y1 Y4 X5] +
(1.0358477602959626e-06+0j) [X0 Y1 Y4 Z5 Z6 X7] +
(-1.0358477602959626e-06+0j) [X0 Y1 Y5 X6] +
(0.01736611899465138+0j) [X0 Y1 Y6 X7] +
(0.00029219862611104617+0j) [X0 Z1 X2 X3 Z4 X5] +
(-3.328139350847763e-07+0j) [X0 Z1 X2 X3 Z4 Z5 Z6 X7] +
(0.00029219862611104617+0j) [X0 Z1 X2 Y3 Z4 Y5] +
(-3.328139350847763e-07+0j) [X0 Z1 X2 Y3 Z4 Z5 Z6 Y7] +
(0.04375263801066001+0j) [X0 Z1 Z2 Z3 X4] +
(3.3131455000775607e-06+0j) [X0 Z1 Z2 Z3 X4 X5 Z6 X7] +
(3.3131455000775607e-06+0j) [X0 Z1 Z2 Z3 X4 Y5 Z6 Y7] +
(0.01130727400884823+0j) [X0 Z1 Z2 Z3 X4 Z5] +
(0.025104957138844485+0j) [X0 Z1 Z2 Z3 X4 Z6] +
(0.010540425907671472+0j) [X0 Z1 Z2 Z3 X4 Z7] +
(-0.014564531231173019+0j) [X0 Z1 Z2 Z3 Z4 X5 Y6 Y7] +
(0.014564531231173019+0j) [X0 Z1 Z2 Z3 Z4 Y5 Y6 X7] +
(-5.105396548948974e-06+0j) [X0 Z1 Z2 Z3 Z4 Z5 X6] +
(4.183932559327114e-06+0j) [X0 Z1 Z2 Z3 Z4 Z5 X6 Z7] +
(-6.524373848131389e-06+0j) [X0 Z1 Z2 Z3 Z4 X6] +
(-3.2112283480538283e-06+0j) [X0 Z1 Z2 Z3 Z5 X6] +
(0.029812424517345823+0j) [X0 Z1 Z2 X4] +
(-3.277483194996477e-06+0j) [X0 Z1 Z2 Z4 Z5 X6] +
(0.03010462314345687+0j) [X0 Z1 Z3 X4] +
(-3.6102971300812533e-06+0j) [X0 Z1 Z3 Z4 Z5 X6] +
(0.030787505389143884+0j) [X0 Z2 Z3 X4] +
(-3.769659451494764e-06+0j) [X0 Z2 Z3 Z4 Z5 X6] +
(0.013873381748426065+0j) [Y0 X1 X2 Y3] +
(0.01782514099578653+0j) [Y0 X1 X4 Y5] +
(1.0358477602959626e-06+0j) [Y0 X1 X4 Z5 Z6 Y7] +
(-1.0358477602959626e-06+0j) [Y0 X1 X5 Y6] +
(0.01736611899465138+0j) [Y0 X1 X6 Y7] +
(-0.013873381748426065+0j) [Y0 Y1 X2 X3] +
(-0.01782514099578653+0j) [Y0 Y1 X4 X5] +
(-1.0358477602959626e-06+0j) [Y0 Y1 X4 Z5 Z6 X7] +
(-1.0358477602959626e-06+0j) [Y0 Y1 Y5 Y6] +
(-0.01736611899465138+0j) [Y0 Y1 X6 X7] +
(0.00029219862611104617+0j) [Y0 Z1 Y2 X3 Z4 X5] +
(-3.328139350847763e-07+0j) [Y0 Z1 Y2 X3 Z4 Z5 Z6 X7] +
(0.00029219862611104617+0j) [Y0 Z1 Y2 Y3 Z4 Y5] +
(-3.328139350847763e-07+0j) [Y0 Z1 Y2 Y3 Z4 Z5 Z6 Y7] +
(0.04375263801066001+0j) [Y0 Z1 Z2 Z3 Y4] +
(3.3131455000775607e-06+0j) [Y0 Z1 Z2 Z3 Y4 X5 Z6 X7] +
(3.3131455000775607e-06+0j) [Y0 Z1 Z2 Z3 Y4 Y5 Z6 Y7] +
(0.01130727400884823+0j) [Y0 Z1 Z2 Z3 Y4 Z5] +
(0.025104957138844485+0j) [Y0 Z1 Z2 Z3 Y4 Z6] +
(0.010540425907671472+0j) [Y0 Z1 Z2 Z3 Y4 Z7] +
(0.014564531231173019+0j) [Y0 Z1 Z2 Z3 Z4 X5 X6 Y7] +
(-0.014564531231173019+0j) [Y0 Z1 Z2 Z3 Z4 Y5 X6 X7] +
(-5.105396548948974e-06+0j) [Y0 Z1 Z2 Z3 Z4 Z5 Y6] +
(4.183932559327114e-06+0j) [Y0 Z1 Z2 Z3 Z4 Z5 Y6 Z7] +
(-6.524373848131389e-06+0j) [Y0 Z1 Z2 Z3 Z4 Y6] +
(-3.2112283480538283e-06+0j) [Y0 Z1 Z2 Z3 Z5 Y6] +
(0.029812424517345823+0j) [Y0 Z1 Z2 Y4] +
(-3.277483194996477e-06+0j) [Y0 Z1 Z2 Z4 Z5 Y6] +
(0.03010462314345687+0j) [Y0 Z1 Z3 Y4] +
(-3.6102971300812533e-06+0j) [Y0 Z1 Z3 Z4 Z5 Y6] +
(0.030787505389143884+0j) [Y0 Z2 Z3 Y4] +
(-3.769659451494764e-06+0j) [Y0 Z2 Z3 Z4 Z5 Y6] +
(0.2275726900545348+0j) [Z0] +
(0.030787505389143887+0j) [Z0 X1 Z2 Z3 Z4 X5] +
(-3.769659451494764e-06+0j) [Z0 X1 Z2 Z3 Z4 Z5 Z6 X7] +
(0.030787505389143887+0j) [Z0 Y1 Z2 Z3 Z4 Y5] +
(-3.769659451494764e-06+0j) [Z0 Y1 Z2 Z3 Z4 Z5 Z6 Y7] +
(0.19392534613270143+0j) [Z0 Z1] +
(0.16756653265461247+0j) [Z0 Z2] +
(0.18143991440303853+0j) [Z0 Z3] +
(-1.8551201214367044e-06+0j) [Z0 X4 Z5 X6] +
(-1.8551201214367044e-06+0j) [Z0 Y4 Z5 Y6] +
(0.11952438964682668+0j) [Z0 Z4] +
(-2.8909678817326668e-06+0j) [Z0 X5 Z6 X7] +
(-2.8909678817326668e-06+0j) [Z0 Y5 Z6 Y7] +
(0.13734953064261318+0j) [Z0 Z5] +
(0.13401715261963673+0j) [Z0 Z6] +
(0.1513832716142881+0j) [Z0 Z7] +
(-0.00029219862611104617+0j) [X1 X2 Y3 Y4] +
(3.328139350847763e-07+0j) [X1 X2 Y3 Z4 Z5 Y6] +
(0.00029219862611104617+0j) [X1 Y2 Y3 X4] +
(-3.328139350847763e-07+0j) [X1 Y2 Y3 Z4 Z5 X6] +
(-3.3131455000775607e-06+0j) [X1 Z2 Z3 X4 Y5 Y6] +
(-0.014564531231173017+0j) [X1 Z2 Z3 X4 X6 X7] +
(3.3131455000775607e-06+0j) [X1 Z2 Z3 Y4 Y5 X6] +
(-0.014564531231173017+0j) [X1 Z2 Z3 Y4 Y6 X7] +
(0.04375263801066002+0j) [X1 Z2 Z3 Z4 X5] +
(0.010540425907671472+0j) [X1 Z2 Z3 Z4 X5 Z6] +
(0.025104957138844485+0j) [X1 Z2 Z3 Z4 X5 Z7] +
(-5.105396548948978e-06+0j) [X1 Z2 Z3 Z4 Z5 Z6 X7] +
(4.183932559327114e-06+0j) [X1 Z2 Z3 Z4 Z5 X7] +
(-3.2112283480538283e-06+0j) [X1 Z2 Z3 Z4 Z6 X7] +
(0.01130727400884823+0j) [X1 Z2 Z3 X5] +
(-6.524373848131389e-06+0j) [X1 Z2 Z3 Z5 Z6 X7] +
(0.03010462314345687+0j) [X1 Z2 Z4 X5] +
(-3.6102971300812533e-06+0j) [X1 Z2 Z4 Z5 Z6 X7] +
(0.029812424517345823+0j) [X1 Z3 Z4 X5] +
(-3.277483194996477e-06+0j) [X1 Z3 Z4 Z5 Z6 X7] +
(0.00029219862611104617+0j) [Y1 X2 X3 Y4] +
(-3.328139350847763e-07+0j) [Y1 X2 X3 Z4 Z5 Y6] +
(-0.00029219862611104617+0j) [Y1 Y2 X3 X4] +
(3.328139350847763e-07+0j) [Y1 Y2 X3 Z4 Z5 X6] +
(3.3131455000775607e-06+0j) [Y1 Z2 Z3 X4 X5 Y6] +
(-0.014564531231173017+0j) [Y1 Z2 Z3 X4 X6 Y7] +
(-3.3131455000775607e-06+0j) [Y1 Z2 Z3 Y4 X5 X6] +
(-0.014564531231173017+0j) [Y1 Z2 Z3 Y4 Y6 Y7] +
(0.04375263801066002+0j) [Y1 Z2 Z3 Z4 Y5] +
(0.010540425907671472+0j) [Y1 Z2 Z3 Z4 Y5 Z6] +
(0.025104957138844485+0j) [Y1 Z2 Z3 Z4 Y5 Z7] +
(-5.105396548948978e-06+0j) [Y1 Z2 Z3 Z4 Z5 Z6 Y7] +
(4.183932559327114e-06+0j) [Y1 Z2 Z3 Z4 Z5 Y7] +
(-3.2112283480538283e-06+0j) [Y1 Z2 Z3 Z4 Z6 Y7] +
(0.01130727400884823+0j) [Y1 Z2 Z3 Y5] +
(-6.524373848131389e-06+0j) [Y1 Z2 Z3 Z5 Z6 Y7] +
(0.03010462314345687+0j) [Y1 Z2 Z4 Y5] +
(-3.6102971300812533e-06+0j) [Y1 Z2 Z4 Z5 Z6 Y7] +
(0.029812424517345823+0j) [Y1 Z3 Z4 Y5] +
(-3.277483194996477e-06+0j) [Y1 Z3 Z4 Z5 Z6 Y7] +
(0.22757269005453493+0j) [Z1] +
(0.18143991440303853+0j) [Z1 Z2] +
(0.16756653265461247+0j) [Z1 Z3] +
(-2.8909678817326668e-06+0j) [Z1 X4 Z5 X6] +
(-2.8909678817326668e-06+0j) [Z1 Y4 Z5 Y6] +
(0.13734953064261318+0j) [Z1 Z4] +
(-1.8551201214367044e-06+0j) [Z1 X5 Z6 X7] +
(-1.8551201214367044e-06+0j) [Z1 Y5 Z6 Y7] +
(0.11952438964682668+0j) [Z1 Z5] +
(0.1513832716142881+0j) [Z1 Z6] +
(0.13401715261963673+0j) [Z1 Z7] +
(-0.009560705729135961+0j) [X2 X3 Y4 Y5] +
(6.628614201334999e-07+0j) [X2 X3 Y4 Z5 Z6 Y7] +
(6.628614201334997e-07+0j) [X2 X3 X5 X6] +
(-0.006087822480561846+0j) [X2 X3 Y6 Y7] +
(0.009560705729135961+0j) [X2 Y3 Y4 X5] +
(-6.628614201334999e-07+0j) [X2 Y3 Y4 Z5 Z6 X7] +
(6.628614201334997e-07+0j) [X2 Y3 Y5 X6] +
(0.006087822480561846+0j) [X2 Y3 Y6 X7] +
(0.009560705729135961+0j) [Y2 X3 X4 Y5] +
(-6.628614201334999e-07+0j) [Y2 X3 X4 Z5 Z6 Y7] +
(6.628614201334997e-07+0j) [Y2 X3 X5 Y6] +
(0.006087822480561846+0j) [Y2 X3 X6 Y7] +
(-0.009560705729135961+0j) [Y2 Y3 X4 X5] +
(6.628614201334999e-07+0j) [Y2 Y3 X4 Z5 Z6 X7] +
(6.628614201334997e-07+0j) [Y2 Y3 Y5 Y6] +
(-0.006087822480561846+0j) [Y2 Y3 X6 X7] +
(0.1741995615505566+0j) [Z2] +
(0.2200397733437609+0j) [Z2 Z3] +
(-1.597317197742011e-06+0j) [Z2 X4 Z5 X6] +
(-1.597317197742011e-06+0j) [Z2 Y4 Z5 Y6] +
(0.13766872645852585+0j) [Z2 Z4] +
(-9.344557776085109e-07+0j) [Z2 X5 Z6 X7] +
(-9.344557776085109e-07+0j) [Z2 Y5 Z6 Y7] +
(0.14722943218766182+0j) [Z2 Z5] +
(0.14973486803496913+0j) [Z2 Z6] +
(0.15582269051553096+0j) [Z2 Z7] +
(0.17419956155055677+0j) [Z3] +
(-9.344557776085109e-07+0j) [Z3 X4 Z5 X6] +
(-9.344557776085109e-07+0j) [Z3 Y4 Z5 Y6] +
(0.14722943218766182+0j) [Z3 Z4] +
(-1.597317197742011e-06+0j) [Z3 X5 Z6 X7] +
(-1.597317197742011e-06+0j) [Z3 Y5 Z6 Y7] +
(0.13766872645852585+0j) [Z3 Z5] +
(0.15582269051553096+0j) [Z3 Z6] +
(0.14973486803496913+0j) [Z3 Z7] +
(-0.028685183716105907+0j) [X4 X5 Y6 Y7] +
(0.028685183716105907+0j) [X4 Y5 Y6 X7] +
(-7.037887511102813e-06+0j) [X4 Z5 X6] +
(7.95441317578875e-06+0j) [X4 Z5 X6 Z7] +
(-8.194261371831374e-06+0j) [X4 X6] +
(0.028685183716105907+0j) [Y4 X5 X6 Y7] +
(-0.028685183716105907+0j) [Y4 Y5 X6 X7] +
(-7.037887511102813e-06+0j) [Y4 Z5 Y6] +
(7.95441317578875e-06+0j) [Y4 Z5 Y6 Z7] +
(-8.194261371831374e-06+0j) [Y4 Y6] +
(-0.15961432501810047+0j) [Z4] +
(-8.194261371831374e-06+0j) [Z4 X5 Z6 X7] +
(-8.194261371831374e-06+0j) [Z4 Y5 Z6 Y7] +
(0.14926355147388917+0j) [Z4 Z5] +
(0.1127038692033222+0j) [Z4 Z6] +
(0.1413890529194281+0j) [Z4 Z7] +
(-7.037887511102811e-06+0j) [X5 Z6 X7] +
(7.95441317578875e-06+0j) [X5 X7] +
(-7.037887511102811e-06+0j) [Y5 Z6 Y7] +
(7.95441317578875e-06+0j) [Y5 Y7] +
(-0.15961432501810044+0j) [Z5] +
(0.1413890529194281+0j) [Z5 Z6] +
(0.1127038692033222+0j) [Z5 Z7] +
(-0.18066792656583353+0j) [Z6] +
(0.15435748657223614+0j) [Z6 Z7] +
(-0.1806679265658335+0j) [Z7]

Finally, the generate_hamiltonian() function is used to automate the construction of the electronic Hamiltonian using the functions described above.

An example usage is shown below:

qubit_hamiltonian, n_qubits = qml.qchem.generate_hamiltonian(
    name,
    'h2o.xyz',
    charge,
    multiplicity,
    basis_set,
    qc_package='pyscf',
    n_active_electrons=4,
    n_active_orbitals=4,
    mapping='jordan_wigner'
)

print("Number of qubits required to perform quantum simulations: {:}".format(n_qubits))
print("Electronic Hamiltonian of the water molecule represented in the Pauli basis")
print(qubit_hamiltonian)

Out:

Number of qubits required to perform quantum simulations: 8
Electronic Hamiltonian of the water molecule represented in the Pauli basis
(-73.13873231352538) [I0]
+ (0.22757269005453337) [Z0]
+ (0.04375263801065992) [Y0 Z1 Z2 Z3 Y4]
+ (0.04375263801065992) [X0 Z1 Z2 Z3 X4]
+ (-5.1053965500124275e-06) [Y0 Z1 Z2 Z3 Z4 Z5 Y6]
+ (-5.1053965500124275e-06) [X0 Z1 Z2 Z3 Z4 Z5 X6]
+ (0.22757269005453368) [Z1]
+ (0.04375263801065992) [Y1 Z2 Z3 Z4 Y5]
+ (0.04375263801065992) [X1 Z2 Z3 Z4 X5]
+ (-5.105396550012429e-06) [Y1 Z2 Z3 Z4 Z5 Z6 Y7]
+ (-5.105396550012429e-06) [X1 Z2 Z3 Z4 Z5 Z6 X7]
+ (0.17419956155055602) [Z2]
+ (0.1741995615505561) [Z3]
+ (-0.1596143250181002) [Z4]
+ (-7.037887511023129e-06) [Y4 Z5 Y6]
+ (-7.037887511023129e-06) [X4 Z5 X6]
+ (-0.15961432501810022) [Z5]
+ (-7.037887511023129e-06) [Y5 Z6 Y7]
+ (-7.037887511023129e-06) [X5 Z6 X7]
+ (-0.1806679265658343) [Z6]
+ (-0.18066792656583447) [Z7]
+ (0.19392534613270165) [Z0 Z1]
+ (0.030787505389143904) [Y0 Z2 Z3 Y4]
+ (0.030787505389143904) [X0 Z2 Z3 X4]
+ (-3.7696594520574203e-06) [Y0 Z2 Z3 Z4 Z5 Y6]
+ (-3.7696594520574203e-06) [X0 Z2 Z3 Z4 Z5 X6]
+ (0.013873381748426086) [Y0 X1 X2 Y3]
+ (-0.013873381748426086) [Y0 Y1 X2 X3]
+ (-0.013873381748426086) [X0 X1 Y2 Y3]
+ (0.013873381748426086) [X0 Y1 Y2 X3]
+ (0.030787505389143904) [Z0 Y1 Z2 Z3 Z4 Y5]
+ (0.030787505389143904) [Z0 X1 Z2 Z3 Z4 X5]
+ (0.01782514099578654) [Y0 X1 X4 Y5]
+ (-0.01782514099578654) [Y0 Y1 X4 X5]
+ (-0.01782514099578654) [X0 X1 Y4 Y5]
+ (0.01782514099578654) [X0 Y1 Y4 X5]
+ (-1.0358477602204953e-06) [Y0 X1 X5 Y6]
+ (-1.0358477602204953e-06) [Y0 Y1 Y5 Y6]
+ (-1.0358477602204953e-06) [X0 X1 X5 X6]
+ (-1.0358477602204953e-06) [X0 Y1 Y5 X6]
+ (-3.7696594520574203e-06) [Z0 Y1 Z2 Z3 Z4 Z5 Z6 Y7]
+ (-3.7696594520574203e-06) [Z0 X1 Z2 Z3 Z4 Z5 Z6 X7]
+ (1.0358477602204953e-06) [Y0 X1 X4 Z5 Z6 Y7]
+ (-1.0358477602204953e-06) [Y0 Y1 X4 Z5 Z6 X7]
+ (-1.0358477602204953e-06) [X0 X1 Y4 Z5 Z6 Y7]
+ (1.0358477602204953e-06) [X0 Y1 Y4 Z5 Z6 X7]
+ (0.017366118994651427) [Y0 X1 X6 Y7]
+ (-0.017366118994651427) [Y0 Y1 X6 X7]
+ (-0.017366118994651427) [X0 X1 Y6 Y7]
+ (0.017366118994651427) [X0 Y1 Y6 X7]
+ (0.16756653265461252) [Z0 Z2]
+ (0.030104623143456875) [Y0 Z1 Z3 Y4]
+ (0.030104623143456875) [X0 Z1 Z3 X4]
+ (-3.610297130648659e-06) [Y0 Z1 Z3 Z4 Z5 Y6]
+ (-3.610297130648659e-06) [X0 Z1 Z3 Z4 Z5 X6]
+ (0.18143991440303858) [Z0 Z3]
+ (0.02981242451734581) [Y0 Z1 Z2 Y4]
+ (0.02981242451734581) [X0 Z1 Z2 X4]
+ (-3.2774831955656085e-06) [Y0 Z1 Z2 Z4 Z5 Y6]
+ (-3.2774831955656085e-06) [X0 Z1 Z2 Z4 Z5 X6]
+ (0.0002921986261110637) [Y0 Z1 Y2 Y3 Z4 Y5]
+ (0.0002921986261110637) [Y0 Z1 Y2 X3 Z4 X5]
+ (0.0002921986261110637) [X0 Z1 X2 Y3 Z4 Y5]
+ (0.0002921986261110637) [X0 Z1 X2 X3 Z4 X5]
+ (-3.328139350830503e-07) [Y0 Z1 Y2 Y3 Z4 Z5 Z6 Y7]
+ (-3.328139350830503e-07) [Y0 Z1 Y2 X3 Z4 Z5 Z6 X7]
+ (-3.328139350830503e-07) [X0 Z1 X2 Y3 Z4 Z5 Z6 Y7]
+ (-3.328139350830503e-07) [X0 Z1 X2 X3 Z4 Z5 Z6 X7]
+ (0.11952438964682682) [Z0 Z4]
+ (-1.855120121600645e-06) [Z0 Y4 Z5 Y6]
+ (-1.855120121600645e-06) [Z0 X4 Z5 X6]
+ (-3.2112283484973394e-06) [Y0 Z1 Z2 Z3 Z5 Y6]
+ (-3.2112283484973394e-06) [X0 Z1 Z2 Z3 Z5 X6]
+ (0.13734953064261335) [Z0 Z5]
+ (0.011307274008848282) [Y0 Z1 Z2 Z3 Y4 Z5]
+ (0.011307274008848282) [X0 Z1 Z2 Z3 X4 Z5]
+ (-6.524373848818892e-06) [Y0 Z1 Z2 Z3 Z4 Y6]
+ (-6.524373848818892e-06) [X0 Z1 Z2 Z3 Z4 X6]
+ (-2.89096788182114e-06) [Z0 Y5 Z6 Y7]
+ (-2.89096788182114e-06) [Z0 X5 Z6 X7]
+ (3.313145500321553e-06) [Y0 Z1 Z2 Z3 Y4 Y5 Z6 Y7]
+ (3.313145500321553e-06) [Y0 Z1 Z2 Z3 Y4 X5 Z6 X7]
+ (3.313145500321553e-06) [X0 Z1 Z2 Z3 X4 Y5 Z6 Y7]
+ (3.313145500321553e-06) [X0 Z1 Z2 Z3 X4 X5 Z6 X7]
+ (0.014564531231173024) [Y0 Z1 Z2 Z3 Z4 X5 X6 Y7]
+ (-0.014564531231173024) [Y0 Z1 Z2 Z3 Z4 Y5 X6 X7]
+ (-0.014564531231173024) [X0 Z1 Z2 Z3 Z4 X5 Y6 Y7]
+ (0.014564531231173024) [X0 Z1 Z2 Z3 Z4 Y5 Y6 X7]
+ (0.1340171526196369) [Z0 Z6]
+ (0.025104957138844506) [Y0 Z1 Z2 Z3 Y4 Z6]
+ (0.025104957138844506) [X0 Z1 Z2 Z3 X4 Z6]
+ (0.1513832716142883) [Z0 Z7]
+ (0.010540425907671482) [Y0 Z1 Z2 Z3 Y4 Z7]
+ (0.010540425907671482) [X0 Z1 Z2 Z3 X4 Z7]
+ (4.18393255961293e-06) [Y0 Z1 Z2 Z3 Z4 Z5 Y6 Z7]
+ (4.18393255961293e-06) [X0 Z1 Z2 Z3 Z4 Z5 X6 Z7]
+ (0.18143991440303858) [Z1 Z2]
+ (0.02981242451734581) [Y1 Z3 Z4 Y5]
+ (0.02981242451734581) [X1 Z3 Z4 X5]
+ (-3.2774831955656085e-06) [Y1 Z3 Z4 Z5 Z6 Y7]
+ (-3.2774831955656085e-06) [X1 Z3 Z4 Z5 Z6 X7]
+ (0.0002921986261110637) [Y1 X2 X3 Y4]
+ (-0.0002921986261110637) [Y1 Y2 X3 X4]
+ (-0.0002921986261110637) [X1 X2 Y3 Y4]
+ (0.0002921986261110637) [X1 Y2 Y3 X4]
+ (-3.328139350830503e-07) [Y1 X2 X3 Z4 Z5 Y6]
+ (3.328139350830503e-07) [Y1 Y2 X3 Z4 Z5 X6]
+ (3.328139350830503e-07) [X1 X2 Y3 Z4 Z5 Y6]
+ (-3.328139350830503e-07) [X1 Y2 Y3 Z4 Z5 X6]
+ (0.16756653265461252) [Z1 Z3]
+ (0.030104623143456875) [Y1 Z2 Z4 Y5]
+ (0.030104623143456875) [X1 Z2 Z4 X5]
+ (-3.610297130648659e-06) [Y1 Z2 Z4 Z5 Z6 Y7]
+ (-3.610297130648659e-06) [X1 Z2 Z4 Z5 Z6 X7]
+ (0.13734953064261335) [Z1 Z4]
+ (0.01130727400884828) [Y1 Z2 Z3 Y5]
+ (0.01130727400884828) [X1 Z2 Z3 X5]
+ (-6.524373848818892e-06) [Y1 Z2 Z3 Z5 Z6 Y7]
+ (-6.524373848818892e-06) [X1 Z2 Z3 Z5 Z6 X7]
+ (-2.89096788182114e-06) [Z1 Y4 Z5 Y6]
+ (-2.89096788182114e-06) [Z1 X4 Z5 X6]
+ (3.3131455003215527e-06) [Y1 Z2 Z3 X4 X5 Y6]
+ (-3.3131455003215527e-06) [Y1 Z2 Z3 Y4 X5 X6]
+ (-3.3131455003215527e-06) [X1 Z2 Z3 X4 Y5 Y6]
+ (3.3131455003215527e-06) [X1 Z2 Z3 Y4 Y5 X6]
+ (-0.014564531231173022) [Y1 Z2 Z3 X4 X6 Y7]
+ (-0.014564531231173022) [Y1 Z2 Z3 Y4 Y6 Y7]
+ (-0.014564531231173022) [X1 Z2 Z3 X4 X6 X7]
+ (-0.014564531231173022) [X1 Z2 Z3 Y4 Y6 X7]
+ (0.11952438964682682) [Z1 Z5]
+ (-1.855120121600645e-06) [Z1 Y5 Z6 Y7]
+ (-1.855120121600645e-06) [Z1 X5 Z6 X7]
+ (-3.2112283484973394e-06) [Y1 Z2 Z3 Z4 Z6 Y7]
+ (-3.2112283484973394e-06) [X1 Z2 Z3 Z4 Z6 X7]
+ (0.1513832716142883) [Z1 Z6]
+ (0.010540425907671482) [Y1 Z2 Z3 Z4 Y5 Z6]
+ (0.010540425907671482) [X1 Z2 Z3 Z4 X5 Z6]
+ (4.18393255961293e-06) [Y1 Z2 Z3 Z4 Z5 Y7]
+ (4.18393255961293e-06) [X1 Z2 Z3 Z4 Z5 X7]
+ (0.1340171526196369) [Z1 Z7]
+ (0.025104957138844506) [Y1 Z2 Z3 Z4 Y5 Z7]
+ (0.025104957138844506) [X1 Z2 Z3 Z4 X5 Z7]
+ (0.2200397733437609) [Z2 Z3]
+ (0.009560705729135997) [Y2 X3 X4 Y5]
+ (-0.009560705729135997) [Y2 Y3 X4 X5]
+ (-0.009560705729135997) [X2 X3 Y4 Y5]
+ (0.009560705729135997) [X2 Y3 Y4 X5]
+ (6.628614202011549e-07) [Y2 X3 X5 Y6]
+ (6.628614202011549e-07) [Y2 Y3 Y5 Y6]
+ (6.628614202011549e-07) [X2 X3 X5 X6]
+ (6.628614202011549e-07) [X2 Y3 Y5 X6]
+ (-6.628614202011549e-07) [Y2 X3 X4 Z5 Z6 Y7]
+ (6.628614202011549e-07) [Y2 Y3 X4 Z5 Z6 X7]
+ (6.628614202011549e-07) [X2 X3 Y4 Z5 Z6 Y7]
+ (-6.628614202011549e-07) [X2 Y3 Y4 Z5 Z6 X7]
+ (0.006087822480561854) [Y2 X3 X6 Y7]
+ (-0.006087822480561854) [Y2 Y3 X6 X7]
+ (-0.006087822480561854) [X2 X3 Y6 Y7]
+ (0.006087822480561854) [X2 Y3 Y6 X7]
+ (0.137668726458526) [Z2 Z4]
+ (-1.5973171978618649e-06) [Z2 Y4 Z5 Y6]
+ (-1.5973171978618649e-06) [Z2 X4 Z5 X6]
+ (0.14722943218766196) [Z2 Z5]
+ (-9.3445577766071e-07) [Z2 Y5 Z6 Y7]
+ (-9.3445577766071e-07) [Z2 X5 Z6 X7]
+ (0.14973486803496922) [Z2 Z6]
+ (0.15582269051553105) [Z2 Z7]
+ (0.14722943218766196) [Z3 Z4]
+ (-9.3445577766071e-07) [Z3 Y4 Z5 Y6]
+ (-9.3445577766071e-07) [Z3 X4 Z5 X6]
+ (0.137668726458526) [Z3 Z5]
+ (-1.5973171978618649e-06) [Z3 Y5 Z6 Y7]
+ (-1.5973171978618649e-06) [Z3 X5 Z6 X7]
+ (0.15582269051553105) [Z3 Z6]
+ (0.14973486803496922) [Z3 Z7]
+ (0.14926355147388937) [Z4 Z5]
+ (-8.194261372585457e-06) [Y4 Y6]
+ (-8.194261372585457e-06) [X4 X6]
+ (-8.194261372585457e-06) [Z4 Y5 Z6 Y7]
+ (-8.194261372585457e-06) [Z4 X5 Z6 X7]
+ (0.028685183716105848) [Y4 X5 X6 Y7]
+ (-0.028685183716105848) [Y4 Y5 X6 X7]
+ (-0.028685183716105848) [X4 X5 Y6 Y7]
+ (0.028685183716105848) [X4 Y5 Y6 X7]
+ (0.11270386920332234) [Z4 Z6]
+ (0.1413890529194282) [Z4 Z7]
+ (7.95441317659586e-06) [Y4 Z5 Y6 Z7]
+ (7.95441317659586e-06) [X4 Z5 X6 Z7]
+ (0.1413890529194282) [Z5 Z6]
+ (7.95441317659586e-06) [Y5 Y7]
+ (7.95441317659586e-06) [X5 X7]
+ (0.11270386920332234) [Z5 Z7]
+ (0.15435748657223625) [Z6 Z7]

You have completed the tutorial! Now, select your favorite molecule and build its electronic Hamiltonian.

To see how simple it is to implement the VQE algorithm to compute the ground-state energy of your molecule using PennyLane, take a look at the tutorial A brief overview of VQE.

Note

If you have built your electronic Hamiltonian independently by using OpenFermion tools, no problem! The convert_hamiltonian() function converts the OpenFermion QubitOperator to PennyLane observables.

References

  1. Jorge Kohanoff. “Electronic structure calculations for solids and molecules: theory and computational methods”. (Cambridge University Press, 2006).
  2. Frank Jensen. “Introduction to Computational Chemistry”. (John Wiley & Sons, 2016).
  3. Alberto Peruzzo, Jarrod McClean et al., “A variational eigenvalue solver on a photonic quantum processor”. Nature Communications 5, 4213 (2014).
  4. Rolf Seeger, John Pople. “Self‐consistent molecular orbital methods. XVIII. Constraints and stability in Hartree–Fock theory”. Journal of Chemical Physics 66, 3045 (1977).
  5. Jacob T. Seeley, Martin J. Richard, Peter J. Love. “The Bravyi-Kitaev transformation for quantum computation of electronic structure”. Journal of Chemical Physics 137, 224109 (2012).

Total running time of the script: ( 0 minutes 1.236 seconds)

Gallery generated by Sphinx-Gallery