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

Add tutorial #1075

Open
wants to merge 100 commits into
base: master
Choose a base branch
from
Open

Add tutorial #1075

wants to merge 100 commits into from

Conversation

minhtriet
Copy link

@minhtriet minhtriet commented Apr 17, 2024


Title: Find the first excitation energy of hydrogen molecule with VQD

Summary:

Relevant references:

Possible Drawbacks:

Related GitHub Issues:


If you are writing a demonstration, please answer these questions to facilitate the marketing process.

  • GOALS — Finding first excitation energy is necessary to model reaction

  • AUDIENCE — Chemistry researchers, beginners in quantum computing.

  • KEYWORDS — Quantum Chemistry

  • Which of the following types of documentation is most similar to your file?
    (more details here)

  • Tutorial
  • Demo
  • How-to

[sc-64060]

@minhtriet
Copy link
Author

Hi can I get a thumbnail please :) Right now "/_static/large_demo_thumbnails/thumbnail_large_vqe_vqd.png is copied from another one

Copy link

github-actions bot commented Apr 18, 2024

Thank you for opening this pull request.

You can find the built site at this link.

Deployment Info:

  • Pull Request ID: 1075
  • Deployment SHA: b0d5be46065f41a8dfc8f1f2ff627beed059901c
    (The Deployment SHA refers to the latest commit hash the docs were built from)

Note: It may take several minutes for updates to this pull request to be reflected on the deployed site.

Copy link
Contributor

@KetpuntoG KetpuntoG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @minhtriet Thanks for working on the demo and good job 😄
Here I leave you some first comments to make the demo shine more brightly 💪

demonstrations/tutorial_vqe_vqd.metadata.json Show resolved Hide resolved
demonstrations/tutorial_vqe_vqd.py Outdated Show resolved Hide resolved
demonstrations/tutorial_vqe_vqd.py Outdated Show resolved Hide resolved
demonstrations/tutorial_vqe_vqd.py Show resolved Hide resolved
demonstrations/tutorial_vqe_vqd.py Show resolved Hide resolved
demonstrations/tutorial_vqe_vqd.py Outdated Show resolved Hide resolved
demonstrations/tutorial_vqe_vqd.py Outdated Show resolved Hide resolved
demonstrations/tutorial_vqe_vqd.py Outdated Show resolved Hide resolved
demonstrations/tutorial_vqe_vqd.py Outdated Show resolved Hide resolved
demonstrations/tutorial_vqe_vqd.py Outdated Show resolved Hide resolved
Copy link
Contributor

@KetpuntoG KetpuntoG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment it is going well but what do you think if we do a little restructuring? 😄
Here are a few comments. Don't worry about the final code part. Let's work in this first part

demonstrations/tutorial_vqe_vqd.py Show resolved Hide resolved
Comment on lines 16 to 22
import jax
import optax
import pennylane as qml
from pennylane import numpy as np

jax.config.update("jax_platform_name", "cpu")
jax.config.update("jax_enable_x64", True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To follow our usual formatting, could you the libraries in the first cell where they are used.

demonstrations/tutorial_vqe_vqd.py Show resolved Hide resolved
Comment on lines 23 to 33

h2_dataset = qml.data.load("qchem", molname="H2", bondlength=0.742, basis="STO-3G")
h2 = h2_dataset[0]
H, qubits = h2.hamiltonian, len(h2.hamiltonian.wires)
print("Number of qubits = ", qubits)
print("The Hamiltonian is ", H)

######################################################################
# The `hf_state` is our starting point to find the ground state energy. Let’s see what it is.
#
h2.hf_state
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend replacing the current code with this one. On the other hand, there is no need to explain neither what Hartree Fock is nor what Double Excitation is. Here you can point to the demo instead of the introduction.

import pennylane as qml
from pennylane import numpy as np

h2 = qml.data.load("qchem", molname="H2", bondlength=0.742, basis="STO-3G")[0]
H, qubits = h2.hamiltonian, len(h2.hamiltonian.wires)

def generate_ground_state(wires):
    
    qml.BasisState(h2.hf_state, wires=wires)

    for op in h2.vqe_gates:  # use the gates data the datasets package provided
        op = qml.map_wires(op, {op.wires[i]: wires[i] for i in range(len(wires))})
        qml.apply(op)


dev = qml.device("default.qubit")

@qml.qnode(dev)
def circuit():
  generate_ground_state(range(qubits))
  return qml.expval(H)

print("The ground energy of H2 is: ", circuit())

Comment on lines 35 to 47
######################################################################
# In the Hartree Fock representation, a qubit with state :math:`1`/:math:`0` means that there is/isn’t an
# electron occupying the respective spinning molecular orbital. Here we are starting from the config where the two electrons occupy the lowest two energy levels.
#
# Let’s also see the gates used to evolve the hf state.
#
print(h2.vqe_gates)

######################################################################
# This is a single Double excitement gate with the rotation angle of ~ 0.2732 radians.
#
excitation_angle = 0.27324054462951564

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not focus on this

Comment on lines 49 to 67
# Setting expectation
# -------------------------------------------
#
# Before any training takes place, let’s first look at some of the experimental value.
# The energy of an atom at :math:`n` th excitement level is denoted as :math:`E_n`. Physicists starts the value
# of :math:`n` from :math:`1`. It is to avoid dividing by zero in the equation :math:`E_n=\frac{E_I}{n^2}`, where :math:`E_I` is the ionization energy.
#
# - Ground state energy:
# - :math:`H_2` molecule: -1.1357 Hartree ! Help I cannot find the source for this
#
# - 1st level excitation energy! Help I cannot find the source for this
#
# All the measures are in :math:`eV` (electron volt). However, later when running the optimization circuit, we would meet another unit called :math:`Ha` (Hartree energy). They both measure energy, just like Joule or calorie
# but in the scale for basic particles. Let's define a unit conversion function.
#


def hartree_energy_to_ev(hartree: float):
return hartree * 27.2107
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my point of view, there is no need to look for these values, we will use the demo to deduce them (In the previous block with the code, you already have the energy value -1.13). I would suggest eliminating this part

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



######################################################################
# Ansatz and the ground state
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading this section, I get the feeling that you start with the code very quickly, how about making a theoretical section explaining VQD: "The VQD Algorithm".

After this, once we have worked through this part, we could see how to implement the code section: "VQD in PennyLane" for example.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I told you to delete the VQE formula, it may make sense to include it in this context.
Something like, although the usual way to find the ground state is through this formula [...] this is not enough in this case since we are not looking for the minimum. Therefore, we must create a penalty that results in this formula [...]. By taking a sufficiently large beta we can guarantee that [...]. In addition, iteratively, we could calculate other excited states [...].

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

@minhtriet minhtriet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @KetpuntoG for the restructure. There are several things I love about this

  • New structure makes it easy to read
  • Test if the excited energy found is correct

Just have a couple of points I don't understand yet. Perhaps people who read the tutorial may have the same question :)

demonstrations/tutorial_vqe_vqd.py Outdated Show resolved Hide resolved
@@ -0,0 +1,251 @@
r"""Ground State and Excited State of H2 Molecule using VQE and VQD
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I made it more VQD focused :) Thank you

Comment on lines 49 to 67
# Setting expectation
# -------------------------------------------
#
# Before any training takes place, let’s first look at some of the experimental value.
# The energy of an atom at :math:`n` th excitement level is denoted as :math:`E_n`. Physicists starts the value
# of :math:`n` from :math:`1`. It is to avoid dividing by zero in the equation :math:`E_n=\frac{E_I}{n^2}`, where :math:`E_I` is the ionization energy.
#
# - Ground state energy:
# - :math:`H_2` molecule: -1.1357 Hartree ! Help I cannot find the source for this
#
# - 1st level excitation energy! Help I cannot find the source for this
#
# All the measures are in :math:`eV` (electron volt). However, later when running the optimization circuit, we would meet another unit called :math:`Ha` (Hartree energy). They both measure energy, just like Joule or calorie
# but in the scale for basic particles. Let's define a unit conversion function.
#


def hartree_energy_to_ev(hartree: float):
return hartree * 27.2107
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



######################################################################
# Ansatz and the ground state
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for i in range(n_qubits):
qml.CSWAP(wires=[0, 1 + i + n_qubits, 1 + i])
qml.Hadamard(wires=0)
return qml.expval(qml.Z(0))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Wikipedia shouldn't we take the qml(prob) and see the 0th value? I am trying to understand expval(Z) here

Suppose we have x=α|0>+β|1> then expval(Z)=<x|Z|x>=α^2-β^2? But we expected α^2 only here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expected value is over Z is equivalent to P(0) - P(1). I invite you to do the maths and check the solution 😄 I'll look for a reference to add, so the reader can go there

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes of course α^2-β^2 is P(0) - P(1)

demonstrations/tutorial_vqe_vqd.py Show resolved Hide resolved
demonstrations/tutorial_vqe_vqd.py Show resolved Hide resolved
demonstrations/tutorial_vqe_vqd.py Outdated Show resolved Hide resolved
demonstrations/tutorial_vqe_vqd.py Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants