-
Notifications
You must be signed in to change notification settings - Fork 180
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
base: master
Are you sure you want to change the base?
Add tutorial #1075
Conversation
Hi can I get a thumbnail please :) Right now |
Thank you for opening this pull request. You can find the built site at this link. Deployment Info:
Note: It may take several minutes for updates to this pull request to be reflected on the deployed site. |
There was a problem hiding this 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 💪
Co-authored-by: Guillermo Alonso-Linaje <[email protected]>
Co-authored-by: Guillermo Alonso-Linaje <[email protected]>
Co-authored-by: Guillermo Alonso-Linaje <[email protected]>
Co-authored-by: Guillermo Alonso-Linaje <[email protected]>
Co-authored-by: Guillermo Alonso-Linaje <[email protected]>
There was a problem hiding this 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
Outdated
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) |
There was a problem hiding this comment.
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
Outdated
|
||
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 |
There was a problem hiding this comment.
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())
demonstrations/tutorial_vqe_vqd.py
Outdated
###################################################################### | ||
# 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 | ||
|
There was a problem hiding this comment.
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
demonstrations/tutorial_vqe_vqd.py
Outdated
# 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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
demonstrations/tutorial_vqe_vqd.py
Outdated
|
||
|
||
###################################################################### | ||
# Ansatz and the ground state |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 [...].
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Co-authored-by: Guillermo Alonso-Linaje <[email protected]>
There was a problem hiding this 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
@@ -0,0 +1,251 @@ | |||
r"""Ground State and Excited State of H2 Molecule using VQE and VQD |
There was a problem hiding this comment.
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
demonstrations/tutorial_vqe_vqd.py
Outdated
# 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
demonstrations/tutorial_vqe_vqd.py
Outdated
|
||
|
||
###################################################################### | ||
# Ansatz and the ground state |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
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)
[sc-64060]