Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dependencies in quantum chemistry tutorial #475

Merged
merged 10 commits into from
Apr 22, 2022
Merged
76 changes: 36 additions & 40 deletions demonstrations/tutorial_quantum_chemistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@
the structure of the molecule in terms of the symbols and the coordinates of
the atoms. Next, we describe how to solve the `Hartree-Fock
equations <https://en.wikipedia.org/wiki/Hartree%E2%80%93Fock_method>`_ for the target
molecule by interfacing with classical quantum chemistry packages. Then, we
outline how to decompose the fermionic Hamiltonian into a set of Pauli operators
whose expectation values can be measured in a quantum computer to estimate the total energy
of the molecule. Finally, we discuss some advanced features that can be used to simulate
molecule. Finally, we discuss some advanced features that can be used to simulate
more complicated systems.

Let's get started!
Expand All @@ -70,7 +67,7 @@
coordinates = np.array([-0.0399, -0.0038, 0.0, 1.5780, 0.8540, 0.0, 2.7909, -0.5159, 0.0])

##############################################################################
# The :func:`~.pennylane_qchem.qchem.read_structure` function can also be used to read the
# The :func:`~.pennylane.qchem.read_structure` function can also be used to read the
# molecular geometry from an external file.


Expand All @@ -79,9 +76,7 @@
symbols, coordinates = qchem.read_structure("h2o.xyz")

##############################################################################
# The xyz format is supported out of the box. If
# `Open Babel <http://openbabel.org/wiki/Main_Page>`_ is installed, any
# format recognized by Open Babel is also supported by PennyLane.
# The xyz format is supported.
#
# Solving the Hartree-Fock equations
# ----------------------------------
Expand All @@ -101,17 +96,8 @@
# field generated by all other electrons [#pople1977]_. The optimized coefficients are precisely
# what we need to build the second-quantized Hamiltonian.
#
# We can call the function :func:`~.pennylane_qchem.qchem.meanfield` to perform
# the Hartree-Fock calculation using either the quantum chemistry package `PySCF
# <https://sunqm.github.io/pyscf/>`_, or `Psi4 <http://www.psicode.org/>`_. Here
# we use PySCF, which is the default option.

hf_file = qchem.meanfield(symbols, coordinates)

##############################################################################
# This function creates a local file that will later be used to compute the Hamiltonian.
#
# .. _hamiltonian:
# PennyLane provides a differentiable Hartree-Fock solver and the functionality to construct a
# fully-differentiable molecular Hamiltonian.
#
# Building the Hamiltonian
# ------------------------
Expand Down Expand Up @@ -149,20 +135,9 @@
#
# where :math:`C_j` is a scalar coefficient and :math:`\sigma_i` represents an
# element of the Pauli group :math:`\{ I, X, Y, Z \}`.
#
# This fermionic-to-qubit transformation is done using
# the :func:`~.pennylane_qchem.qchem.decompose` function, which
# uses `OpenFermion <https://github.com/quantumlib/OpenFermion>`_
# to compute the electron integrals using the previously generated
# results of the mean field calculation. Then, it builds the fermionic Hamiltonian and
# maps it to the qubit representation.

qubit_hamiltonian = qchem.decompose(hf_file, mapping="jordan_wigner")
print("Qubit Hamiltonian of the water molecule")
print(qubit_hamiltonian)

##############################################################################
# It is often convenient to use the :func:`~.pennylane_qchem.qchem.molecular_hamiltonian`
# It PennyLane we have the :func:`~.pennylane.qchem.molecular_hamiltonian`
soranjh marked this conversation as resolved.
Show resolved Hide resolved
soranjh marked this conversation as resolved.
Show resolved Hide resolved
# function which encapsulates all the steps explained above. It simplifies the process of building
# the electronic Hamiltonian to a single line of code. We just need to input the
# symbols and the nuclear coordinates of the molecule, as shown below:
Expand All @@ -173,15 +148,10 @@
print(H)

##############################################################################
# Additionally, if you have built your electronic Hamiltonian independently using
# `OpenFermion <https://github.com/quantumlib/OpenFermion>`_ tools, it can
# be readily converted to a PennyLane observable using the
# :func:`~.pennylane_qchem.qchem.convert_observable` function.
#
# Advanced features
# -----------------
# The :func:`~.pennylane_qchem.qchem.meanfield` function allows us to define additional keyword
# arguments to solve the Hartree-Fock equations of more complicated systems.
# The :func:`~.pennylane.qchem.molecular_hamiltonian` function allows us to define additional
# keyword arguments to solve the Hartree-Fock equations of more complicated systems.
# The net charge of the molecule may be specified to simulate positively or negatively
# charged molecules. For a neutral system we choose

Expand Down Expand Up @@ -242,7 +212,7 @@
# For the water molecule in a minimal basis set we have a total of ten electrons
# and seven molecular orbitals. In this example we define an symmetric active space with
# four electrons and four active orbitals using
# the :func:`~.pennylane_qchem.qchem.active_space` function:
# the :func:`~.pennylane.qchem.active_space` function:

electrons = 10
orbitals = 7
Expand All @@ -256,7 +226,7 @@
print("Number of qubits: {:}".format(2 * len(active)))

##############################################################################
# Finally, we use the :func:`~.pennylane_qchem.qchem.molecular_hamiltonian` function to
# Finally, we use the :func:`~.pennylane.qchem.molecular_hamiltonian` function to
# build the resulting Hamiltonian of the water molecule:

H, qubits = qchem.molecular_hamiltonian(
Expand All @@ -278,6 +248,32 @@
# observable is an approximation of the Hamiltonian generated in the
# section :ref:`hamiltonian`.
#
# OpenFermion-PySCF backend
# -------------------------
# The :func:`~.pennylane.qchem.molecular_hamiltonian` function can be also used to construct the
# molecular Hamiltonian with a non-differentiable backend that uses the
# `OpenFermion-PySCF <https://github.com/quantumlib/OpenFermion-PySCF>`_ plugin interfaced with the
# electronic structure package `PySCF <https://github.com/sunqm/pyscf>`_. The non-differentiable
# backend can be selected by setting `method='pyscf'` in
soranjh marked this conversation as resolved.
Show resolved Hide resolved
# :func:`~.pennylane.qchem.molecular_hamiltonian`:

symbols = ["H", "H"]
geometry = np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 2.0]])
H, qubits = qchem.molecular_hamiltonian(symbols, geometry, method='pyscf')
soranjh marked this conversation as resolved.
Show resolved Hide resolved

##############################################################################
# The non-differentiable backend requires the ``OpenFermion-PySCF`` plugin to be installed by the
soranjh marked this conversation as resolved.
Show resolved Hide resolved
# user with
#
# .. code-block:: bash
#
# pip install openfermionpyscf
soranjh marked this conversation as resolved.
Show resolved Hide resolved
#
# Additionally, if you have built your electronic Hamiltonian independently using
# `OpenFermion <https://github.com/quantumlib/OpenFermion>`_ tools, it can
# be readily converted to a PennyLane observable using the
# :func:`~.pennylane.qchem.import_operator` function.
#
# 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
Expand Down