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

Initial state preparation demo #950

Merged
merged 76 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
ae28709
first draft of initial state demo
Chiffafox Oct 5, 2023
0224d75
very rough draft of the initial state demo
Chiffafox Oct 5, 2023
c6adb66
wip on the VQE part of demo
Chiffafox Oct 13, 2023
ab25ec1
new draft of initial state demo
Chiffafox Oct 13, 2023
4bd11b6
add header for vqe section
Chiffafox Oct 13, 2023
d37e40d
Merge branch 'dev' into qml_isp_demo
Chiffafox Oct 13, 2023
a04ff4b
[skip ci] text edits
Chiffafox Oct 13, 2023
eb8ccb0
Merge branch 'qml_isp_demo' of github.com:PennyLaneAI/qml into qml_is…
Chiffafox Oct 13, 2023
b24b4e7
trigger build
Chiffafox Oct 13, 2023
f532439
fixes to metadata
Chiffafox Oct 13, 2023
f817918
fixes to metadata
Chiffafox Oct 13, 2023
6345c47
fix metadata and add author data
Chiffafox Oct 13, 2023
7795c36
turn dmrg part into a note to avoid dependency
Chiffafox Oct 13, 2023
2a2c17c
final dmrg reference removed
Chiffafox Oct 13, 2023
cb01d13
fix previewimage metadata
Chiffafox Oct 13, 2023
9fc415d
remove printing from vqe execution and fix images not showing up
Chiffafox Oct 13, 2023
eacbc99
update demos qchem rst file
Chiffafox Oct 13, 2023
df9dcb2
update demos qchem rst file
Chiffafox Oct 13, 2023
b93c675
rearrange text and code
soranjh Oct 13, 2023
cbe6db2
Merge branch 'qml_isp_demo' of https://github.com/PennyLaneAI/qml int…
soranjh Oct 13, 2023
19b7b7e
Update _static/authors/stepan_fomichev.txt
Chiffafox Oct 16, 2023
270d876
typos
Chiffafox Oct 16, 2023
e5dafb2
Merge branch 'qml_isp_demo' of github.com:PennyLaneAI/qml into qml_is…
Chiffafox Oct 16, 2023
13d4faa
typos and use StatePrep
Chiffafox Oct 16, 2023
7582f2d
fix italicize
Chiffafox Oct 16, 2023
220891d
update block2 install, new release has all funcs we need
Chiffafox Oct 16, 2023
fa4a97b
visual edits
Chiffafox Oct 16, 2023
25c4657
links for all external packages
Chiffafox Oct 16, 2023
88dbacf
improve the overlap section
Chiffafox Oct 16, 2023
cb3576e
light style edits and changed to h3+ example with printing
Chiffafox Oct 17, 2023
8da4859
style edits
Chiffafox Oct 17, 2023
4c3ab86
move tol description higher
Chiffafox Oct 17, 2023
0c231de
style edits
Chiffafox Oct 17, 2023
696808a
style edits from code review
Chiffafox Oct 17, 2023
4d711a4
clarify dmrg code block
Chiffafox Oct 17, 2023
7204149
add dmrg and shci output as text
Chiffafox Oct 17, 2023
561824c
added text output for shci and dmrg
Chiffafox Oct 17, 2023
f458913
final stylistic edits and finish h3+
Chiffafox Oct 17, 2023
f2be96f
print tot iterations and generate figure
Chiffafox Oct 17, 2023
7250f50
modify overlap section to reflect new example
Chiffafox Oct 17, 2023
3aa940c
remove pre-computed figures
Chiffafox Oct 17, 2023
4c0debd
replace header image
Chiffafox Oct 17, 2023
4bb2738
point to new image
Chiffafox Oct 17, 2023
f8f5e18
stylistic edits
Chiffafox Oct 17, 2023
addba50
style edits
Chiffafox Oct 17, 2023
c2408fa
style edits
Chiffafox Oct 17, 2023
55a63a6
style edits to overlap output
Chiffafox Oct 17, 2023
acb039f
style edits
Chiffafox Oct 17, 2023
905eeee
final stylistic changes
Chiffafox Oct 17, 2023
b583155
Apply suggestions from code review
Chiffafox Oct 17, 2023
f50d1db
Update demonstrations/tutorial_initial_state_preparation.py
KetpuntoG Oct 18, 2023
f8c5e20
thumbnails + black
KetpuntoG Oct 18, 2023
b58d9bc
Merge branch 'qml_isp_demo' of https://github.com/PennyLaneAI/qml int…
KetpuntoG Oct 18, 2023
fb01731
random space in the name
KetpuntoG Oct 18, 2023
afcd9b5
fixing uri
KetpuntoG Oct 18, 2023
75e909d
stylistic edits
Chiffafox Oct 18, 2023
823c3b9
style edits
Chiffafox Oct 18, 2023
fa4f1b9
comments on best initial state options
Chiffafox Oct 18, 2023
23d5f67
Merge branch 'dev' into qml_isp_demo
Chiffafox Oct 18, 2023
cd9c239
shci and application:vqe headers style edit
Chiffafox Oct 18, 2023
5bae116
bold text edit
Chiffafox Oct 18, 2023
1d1e118
final style edits
Chiffafox Oct 18, 2023
6cf7cc4
Apply suggestions from code review
Chiffafox Oct 18, 2023
c360578
Merge branch 'qml_isp_demo' of github.com:PennyLaneAI/qml into qml_is…
Chiffafox Oct 18, 2023
04fa37d
Apply suggestions from code review
Chiffafox Oct 18, 2023
6583bac
final visual edits
Chiffafox Oct 18, 2023
7dd1aa7
combine with github edits
Chiffafox Oct 18, 2023
4bb9e26
remove blank lines
soranjh Oct 19, 2023
b0793f1
style edits
Chiffafox Oct 19, 2023
57e0732
Merge branch 'qml_isp_demo' of github.com:PennyLaneAI/qml into qml_is…
Chiffafox Oct 19, 2023
3fb43db
remove bold font
Chiffafox Oct 19, 2023
d91252a
Merge branch 'dev' into qml_isp_demo
Chiffafox Oct 19, 2023
bb2679a
CCSD header fix
Chiffafox Oct 19, 2023
a884e30
cisd paragraph render error
Chiffafox Oct 19, 2023
145c7b3
split big VQE codeblock
Chiffafox Oct 20, 2023
0620b90
final text edits and typo fixes
Chiffafox Oct 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions demonstrations/tutorial_initial_state_preparation.metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"title": "Initial State Preparation for Quantum Chemistry",
"authors": [
{
"id": "stepan_fomichev"
}
],
"dateOfPublication": "2023-10-20T00:00:00+00:00",
"dateOfLastModification": "2023-10-20T00:00:00+00:00",
"categories": [
"Quantum Chemistry"
],
"tags": [],
"previewImages": [
// {
// "type": "thumbnail",
// "uri": "/_images/thumbnail_tutorial_fermionic_operators.png"
// }
],
"seoDescription": "Prepare initial states for quantum algorithms from output of traditional quantum chemistry methods.",
"doi": "",
"canonicalURL": "/qml/demos/tutorial_initial_state_preparation",
"references": [
{
// "id": "surjan",
// "type": "book",
// "title": "Second Quantized Approach to Quantum Chemistry",
// "authors": "Peter R. Surjan",
// "year": "1989",
// "publisher": "Springer-Verlag",
// "url": ""
}
],
"basedOnPapers": [
{
// initial statep prep paper
}
],
"referencedByPapers": [],
"relatedContent": [
{
"type": "demonstration",
"id": "tutorial_quantum_chemistry",
"weight": 1.0
},
{
"type": "demonstration",
"id": "tutorial_vqe",
"weight": 1.0
}
]
}
272 changes: 272 additions & 0 deletions demonstrations/tutorial_initial_state_preparation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
r"""

Initial state preparation for quantum chemistry
===============================================

.. meta::
:property="og:description": Understand the concept of the initial state, and learn how to prepare it with PennyLane
:property="og:image": https://pennylane.ai/qml/_images/thumbnail_tutorial_initial_state_preparation.png

.. related::
tutorial_quantum_chemistry Building molecular Hamiltonians
tutorial_vqe A brief overview of VQE

*Author: Stepan Fomichev — Posted: 20 October 2023. Last updated: 20 October 2023.*
Chiffafox marked this conversation as resolved.
Show resolved Hide resolved

It was 1968, the height of the Cold War, and on a routine surveillance mission the
US submarine Scorpion goes missing. Stakes are high -- a nuclear sub is lost at sea! --
but despite the round-the-clock efforts of dozens of ships and aircraft, after a week
the search is called off. The search area -- "somewhere off the Eastern Seaboard" --
is simply too big to comb through by brute force.

But then a group of statisticians gets involved. Combining information from underwater
sonar listening stations and averaging insights from a variety of experts, they are
able to zero in on the a few most promising search quadrants -- and soon after, the sub
is found in one of them.

Much like searching the oceanic floor for a sunken ship, searching for the ground state
in the gigantic Hilbert space of a typical molecule requires expert guidance -- an
Chiffafox marked this conversation as resolved.
Show resolved Hide resolved
initial guess for what the state could be. In this demo, you will learn about different
strategies for preparing such initial states, and specifically how to do that in
PennyLane.

How do initial states affect quantum algorithms?
------------------------------------------------
From the variational quantum eigensolver (VQE) to quantum phase estimation (QPE), to even
Chiffafox marked this conversation as resolved.
Show resolved Hide resolved
the recent ISQ-era algorithms like the Lin-Tong approach, many quantum algorithms for
obtaining the ground state of a chemical system require a good initial state to be
useful. (add three images here)

1. In the case of VQE, as we will see later in this demo, a good initial state
directly translates into fewer optimization steps.
2. In QPE, the probability of measuring the ground-state energy is directly
proportional to the overlap squared $|c_0|^2$ of the initial and ground states

.. math::

\ket{\psi_{\text{in}}} = c_0 \ket{\psi_0} + c_1 \ket{\psi_1} + ...

3. Finally, in Lin-Tong the overlap with the ground-state affects the size of the
step in the cumulative distribution function, the bigger step making it easier to
detect the jump and thus resolve the position of the ground-state energy.
Chiffafox marked this conversation as resolved.
Show resolved Hide resolved

We see that in all these cases, having a high-quality initial state can seriously
reduce the runtime of the algorithm. By high-quality, we just mean that the prepared
state in some sense minimizes the effort of the quantum algorithm.

Where do I get good initial states?
-----------------------------------
Much like when searching for a sunken submarine, there are a lot of things you might try
to prepare a good guess for the ground-state.

Seeing as we are already using a quantum computer, we could turn to a quantum algorithm
to do the job. One idea is based on the **adiabatic principle** that tells us that the
eigenstates of Hamiltonians smoothly evolving with some parameter :math:`\lambda`
are smoothly evolving also. If we start from a simple Hamiltonian :math:`H(\lambda=0)`
whose ground state is easy to prepare, then evolve to the Hamiltonian
:math:`H(\lambda=1)` of interest, we should end up with the ground state of the final
Hamiltonian, or at least something close to it. In practice, the speed of state prep
(how quickly we can evolve :math:`lambda`) depends on the spectral gap
:math:`\Delta` between the ground and first excited states: and this gap turns out to
Chiffafox marked this conversation as resolved.
Show resolved Hide resolved
be quite small precisely in interesting molecules. There are other ideas like
quantum imaginary-time evolution and variational methods (like VQE itself), but they are
similarly limited by long runtimes or the need for expensive classical optimization.

On the other hand, we could rely on traditional computational chemistry techniques to
get us most of the way to an initial state. We could run a method like configuration
interaction with singles and doubles (CISD), or coupled cluster (CCSD), take the result
and implement it on the quantum computer. It won't not be the ground-state itself, but
it will certainly be better than the computational basis state :math:`\ket{0}^{\otimes N}`.
It is this second approach that we focus on in this demo.

Importing initial states into PennyLane
---------------------------------------
The current version of PennyLane can import initial states from the following methods

1. Configuration interaction with singles and doubles (CISD) from PySCF
The first line of attack for state prep, CISD is unsophisticated but fast.
It won't be much help for strongly correlated molecules, but it is better
than Hartree-Fock.
2. Coupled cluster with singles and doubles (CCSD) from PySCF
In our implementation, we reconstruct the CCSD wavefunction to second order,
making it a marginal improvement on the CISD approach.
3. Density-matrix renormalization grourp (DMRG), from the Block2 library
A powerful method based on matrix-product states, DMRG is considered state of the
art for quantum chemistry simulations, capable of handling reasonably large
systems and correlated molecules.
4. Semistochastic heat-bath configuration interaction (SHCI), from the Dice library
A member of the selective configuration interaction family of methods, SHCI is
right there with DMRG in terms of accuracy and speed, and often used alongside it
as a cross-check.

These methods are incredibly diverse in terms of their outputs, not always returning an
object that can be turned into a PennyLane statevector. We have done this hard work: all
that you need to do is run these methods and pass their outputs to PennyLane's
`import_state`.

Here is how to do this for CISD / CCSD methods via PySCF: we show the version based on
the restricted Hartree-Fock orbitals, but the unrestricted versions are available too.
"""

from pyscf import gto, scf, ci
from pennylane import import_state
mol = gto.M(atom=[['H', (0, 0, 0)], ['H', (0,0,0.71)]], basis='sto6g', symmetry='d2h')
myhf = scf.RHF(mol).run()
myci = ci.CISD(myhf).run()
wf_cisd = import_state(myci, tol=1e-1)
print(wf_cisd)

# [pennylane output]

##############################################################################
# The general function `import_state` can automatically detect the input type and apply
# the appropriate conversion protocol. It works similarly for CCSD

from pyscf import cc
mycc = cc.CCSD(myhf).run()
wf_ccsd = import_state(mycc, tol=1e-1)

##############################################################################
# The second attribute tol specifies the cutoff beyond which contributions to the
# wavefunctions are neglected. Internally, wavefunctions are stored in their Slater
# determinant representation, and if their prefactor coefficient is below `tol`, those
# determinants are dropped from the expression.

KetpuntoG marked this conversation as resolved.
Show resolved Hide resolved
##############################################################################
# The next two examples involve running external libraries Block2 and Dice, whose
# installation can require some care. We recommend the install guide in our internal
# package `Overlapper` for best performance.
#
# To obtain an initial state from a DMRG calculation using Block2, the following is sufficient

from pyscf import mcscf
from pyblock2.driver.core import DMRGDriver, SymmetryTypes
from pyblock2._pyscf.ao2mo import integrals as itg
mc = mcscf.CASCI(mf, 2, 2)
ncas, n_elec, spin, ecore, h1e, g2e, orb_sym = itg.get_rhf_integrals(mf, mc.ncore, mc.ncas, g2e_symm=8)
driver = DMRGDriver(scratch="./tmp", symm_type=SymmetryTypes.SU2)
driver.initialize_system(n_sites=ncas, n_elec=n_elec, spin=spin, orb_sym=orb_sym)
mpo = driver.get_qc_mpo(h1e=h1e, g2e=g2e, ecore=ecore)
ket = driver.get_random_mps(tag="GS")
driver.dmrg('add some attributes here')
wavefunction = driver.get_csf_coefficients(ket)
wf_dmrg = import_state(wavefunction, tol=1e-1)

##############################################################################
# For Dice, the following does the trick

from pyscf.shciscf import shci
import numpy as np
mol = gto.M(atom=[['Li', (0, 0, 0)], ['Li', (0,0,0.71)]], basis='sto6g', symmetry="d2h")
myhf = scf.RHF(mol).run()
ncas, nelecas_a, nelecas_b = mol.nao, mol.nelectron // 2, mol.nelectron // 2
myshci = mcscf.CASCI(myhf, ncas, (nelecas_a, nelecas_b))
output_file = f"shci_output.out"
myshci.fcisolver = shci.SHCI(myhf.mol)
myshci.fcisolver.outputFile = output_file
e_tot, e_ci, ss, mo_coeff, mo_energies = myshci.kernel(verbose=5)
(dets, coeffs) = [post-process shci_output.out to get tuple of
dets (list of strs) and coeffs (list of floats)]
wf_shci = import_state((dets, coeffs), tol=1e-1)

# If you are interested in a library that wraps all these methods and makes it easy to
# generate initial states from them, you might be interested in trying out Overlapper
# package.

##############################################################################
Chiffafox marked this conversation as resolved.
Show resolved Hide resolved
# Let us now demonstrate how the choice of a better initial state shortens the runtime
# of VQE for obtaining the ground-state energy of a molecule. Start by setting up the VQE
# calculation for our molecule.

dev = qml.device("default.qubit", wires=qubits)

def circuit_VQE(theta, wires, initstate):
qml.QubitStateVector(initstate, wires=wires)
Chiffafox marked this conversation as resolved.
Show resolved Hide resolved
qml.DoubleExcitation(theta, wires=wires)

@qml.qnode(dev, interface="autograd")
soranjh marked this conversation as resolved.
Show resolved Hide resolved
def cost_fn(theta):
circuit_VQE(theta, wires=wires)
return qml.expval(H)

stepsize = 0.4
max_iterations = 30
opt = qml.GradientDescentOptimizer(stepsize=stepsize)
theta = np.array(0.0, requires_grad=True)

##############################################################################
# Next, run VQE with the bad basis state

delta_E = 10
conv_tol = 1e-8
iteration = 0
while abs(delta_E) > conv_tol and iteration < max_iterations:
theta, prev_energy = opt.step_and_cost(cost_fn, theta, initstate=hf_state)
samples = cost_fn(theta)
delta_E = samples - prev_energy
print(f"theta = {theta:.5f}, prev energy = {prev_energy:.5f}, de = {delta_E:.5f}")
iteration += 1

energy_VQE = cost_fn(theta)
theta_opt = theta

print("VQE energy: %.4f" % (energy_VQE))
print(f"Optimal parameters: {theta_opt:.5f}")
print(f"Convergence achieved in {iteration} iterations")

##############################################################################
# Now notice how the runtime is shortened to merely a handlful of iterations with the DMRG state

delta_E = 10
conv_tol = 1e-8
iteration = 0
while abs(delta_E) > conv_tol and iteration < max_iterations:
theta, prev_energy = opt.step_and_cost(cost_fn, theta, initstate=wf_dmrg)
samples = cost_fn(theta)
delta_E = samples - prev_energy
print(f"theta = {theta:.5f}, prev energy = {prev_energy:.5f}, de = {delta_E:.5f}")
iteration += 1

energy_VQE = cost_fn(theta)
theta_opt = theta

print("VQE energy: %.4f" % (energy_VQE))
print(f"Optimal parameters: {theta_opt:.5f}")
print(f"Convergence achieved in {iteration} iterations")

##############################################################################
# We can also consider what happens when you make the molecule more correlated. Simpler
# methods like CISD / CCSD will begin to faulter, whiler SHCI and DMRG will continue to
# perform at a high level

### show example ###

Chiffafox marked this conversation as resolved.
Show resolved Hide resolved
##############################################################################
# Finally, it is straightforward to compare the initial states through overlap -- the main
# metric of success for initial states in quantum algorithms. Because in PennyLane these
# are statevectors, computing an overlap is as easy as computing a dot product

ovlp = np.dot(wf_dmrg, wf_shci)

##############################################################################
# Summary
# -------
Chiffafox marked this conversation as resolved.
Show resolved Hide resolved
# This demo explains the concept of the initial state for quantum algorithms. Using the
# example of VQE, it demonstrates how a better choice of state -- obtained, for example
Chiffafox marked this conversation as resolved.
Show resolved Hide resolved
# from a sophisticated computational chemistry method like CCSD, SHCI or DMRG -- can lead
Chiffafox marked this conversation as resolved.
Show resolved Hide resolved
# to much better algorithmic performance. It also shows simple workflows for how to run
# these computational chemistry methods, from libraries such as PySCF, Block2 and Dice,
# to generate outputs that can then be converted to PennyLane's statevector format
# with a single line of code.
#
# References
# ----------
#
# .. [#surjan]
#
# Peter R. Surjan, "Second Quantized Approach to Quantum Chemistry". Springer-Verlag, 1989.
#
#
# About the author
# ----------------
# .. include:: ../_static/authors/stepan_fomichev.txt
Loading