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

[unitaryhack] Updated molecular hydrogen tutorial #1296

Merged
merged 11 commits into from
Jun 9, 2022
Merged

[unitaryhack] Updated molecular hydrogen tutorial #1296

merged 11 commits into from
Jun 9, 2022

Conversation

lockwo
Copy link
Contributor

@lockwo lockwo commented May 17, 2022

Description

This is made to address #1094. The goal is to address both components of that issue. I made it so the hamiltonian is calculated using openfermion. The hamiltonian is not the same as in the paper (still 2 qubits, just not the same ops). It would be very easy to programmatically change that, I just don't know how to do that exact symmetry reduction in openfermion. Additionally, the hamiltonian is a mitiq.Observable and the executor is no longer a wrapper for the energy function but just the density matrix simulator. I am new to mitiq so this second part is what I think that issue meant, but if it should change just let me know.

Checklist

Check off the following once complete (or if not applicable) after opening the PR. The PR will be reviewed once this checklist is complete and all tests are passing.

If some items remain, you can mark this a draft pull request.

License

  • I license this contribution under the terms of the GNU GPL, version 3 and grant Unitary Fund the right to provide additional permissions as described in section 7 of the GNU GPL, version 3.

Tips

  • If the validation check fails:

    1. Run make check-types (from the root directory of the repository) and fix any mypy errors.

    2. Run make check-style and fix any flake8 errors.

    3. Run make format to format your code with the black autoformatter.

    For more information, check the Mitiq style guidelines.

  • Write "Fixes #XYZ" in the description if this PR fixes Issue #XYZ.

@github-actions
Copy link

Binder 👈 Launch a binder notebook on branch lockwo/mitiq/master

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Hello @lockwo, thank you for submitting a PR to Mitiq! We will respond as soon as possible, and if you have any questions in the meantime, you can ask us on the Unitary Fund Discord.

@codecov
Copy link

codecov bot commented May 17, 2022

Codecov Report

Merging #1296 (15340e1) into master (0440b16) will increase coverage by 0.01%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master    #1296      +/-   ##
==========================================
+ Coverage   98.13%   98.15%   +0.01%     
==========================================
  Files          60       61       +1     
  Lines        2739     2762      +23     
==========================================
+ Hits         2688     2711      +23     
  Misses         51       51              
Impacted Files Coverage Δ
mitiq/benchmarks/__init__.py 100.00% <0.00%> (ø)
mitiq/benchmarks/quantum_volume_circuits.py 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0440b16...15340e1. Read the comment docs.

@lockwo
Copy link
Contributor Author

lockwo commented May 17, 2022

I think the check is unsuccessful (Couldn't find cache key for notebook file source/examples/molecular_hydrogen.myst) because OpenFermion isn't installed (based on executablebooks/jupyter-cache#51) since it builds locally.

@andreamari
Copy link
Member

Thanks @lockwo, very interesting!
You can try to add openfermion in https://github.com/unitaryfund/mitiq/blob/master/dev_requirements.txt under the documentation and examples section.
Not sure if it works (especially with windows tests) but worth trying.

As a second option, we can try to add !pip install openfermion directly on the notebook.

@nathanshammah
Copy link
Member

Thanks @lockwo. I like the fact that the hard coded table is replaced with values taken from generate_molecular_hamiltonian.

  • It would be nice to add a bit of details about this function and its parameters.
  • It would be nice to keep a reference of {cite}OMalley_2016_PRX, even if now the data is not directly taken from there.

@lockwo
Copy link
Contributor Author

lockwo commented May 19, 2022

Thanks @lockwo. I like the fact that the hard coded table is replaced with values taken from generate_molecular_hamiltonian.

  • It would be nice to add a bit of details about this function and its parameters.
  • It would be nice to keep a reference of {cite}OMalley_2016_PRX, even if now the data is not directly taken from there.

I expanded a bit on the function description. Is there anything specific you would like to see? As for the citation, I still see it there (in the image). Did you want that referenced again when talking about the coefficients?
image

@lockwo
Copy link
Contributor Author

lockwo commented May 19, 2022

Thanks @lockwo, very interesting! You can try to add openfermion in https://github.com/unitaryfund/mitiq/blob/master/dev_requirements.txt under the documentation and examples section. Not sure if it works (especially with windows tests) but worth trying.

As a second option, we can try to add !pip install openfermion directly on the notebook.

Great, this first suggestion worked!

@andreamari
Copy link
Member

Hi @lockwo,
as you mentioned, the Hamiltonian produced by openfermion is probably different from the formula visible in the comment above.
Any idea to fix this inconsistency?
Some ideas from my side:

  • Change the formula to something more generic e.g. a sum over generic Pauli operators.
  • Change the formula to something exactly matching the operators produced by openfermion
  • Keep the previous table of parameters and add openfermion as a "second option" to generate the Hamiltonian.
  • Other options are welcome.

@lockwo
Copy link
Contributor Author

lockwo commented May 19, 2022

Hi @lockwo, as you mentioned, the Hamiltonian produced by openfermion is probably different from the formula visible in the comment above. Any idea to fix this inconsistency? Some ideas from my side:

  • Change the formula to something more generic e.g. a sum over generic Pauli operators.
  • Change the formula to something exactly matching the operators produced by openfermion
  • Keep the previous table of parameters and add openfermion as a "second option" to generate the Hamiltonian.
  • Other options are welcome.

So the formula is the similar it just doesn't have the YY term (hence why the unmitigated case seems to do a tiny bit better it looks), the symmetry reduction comes from: https://arxiv.org/abs/1701.08213. However, this also makes it so the circuit doesn't need the initial X gate on the second qubit. I think a combination of your suggestions might work. Perhaps adding the exact description of the OF Hamiltonian in addition to providing the parameters from the original work might be a good way to do it. I will make those changes in a little bit to see how they look.

@andreamari
Copy link
Member

Sounds good! Considering that unitaryhack will start in two weeks there is no rush.

@lockwo
Copy link
Contributor Author

lockwo commented May 29, 2022

Alright, finally got around to making those changes.

@lockwo lockwo marked this pull request as ready for review May 30, 2022 19:37
@lockwo lockwo changed the title Updated molecular hydrogen tutorial [draft] [unitaryhack] [unitaryhack] Updated molecular hydrogen tutorial Jun 2, 2022
@andreamari
Copy link
Member

Thanks, @lockwo. Next week I'll make a proper review, but looks already very good!

@lockwo
Copy link
Contributor Author

lockwo commented Jun 4, 2022

Sounds good!

Copy link
Member

@andreamari andreamari left a comment

Choose a reason for hiding this comment

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

Thanks @lockwo, I added some comments.
Please have a look when you have some time.
This is close to be accepted.
Very nice work.

qubit_operator = qubit_operator_to_pauli_sum(bk_op)
return Observable(*qubit_operator)

hamiltonians = [get_hamiltonian(i) for i in radii]
Copy link
Member

@andreamari andreamari Jun 7, 2022

Choose a reason for hiding this comment

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

Is there any simple way to show that these Hemiltonians have indeed the structure mentioned in the latex formula?
E.g. something like

print(get_hamiltonian( some_both_lenght)).

Or any other way to check which Hamiltonian OpenFermion is actually producing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a print function for the hamiltonian, so you can see it is of the same form. Let me know if that is what you were looking for.

@andreamari andreamari linked an issue Jun 7, 2022 that may be closed by this pull request
2 tasks
@nathanshammah
Copy link
Member

+1 this looks very neat, especially for the use of OpenFermion for the tabulated data instead of a hardcoded table.

@nathanshammah
Copy link
Member

nathanshammah commented Jun 8, 2022

Can you add the use of OpenFermion in the notebook title? To differentiate from #1337 that is done with PennyLane.

@lockwo
Copy link
Contributor Author

lockwo commented Jun 9, 2022

Hmmm it has somehow broken the build, it works when I build locally. I see an error with the QAOA tutorial, but I'm not sure if that is the source (since this link is now dead: https://quantumai.google/cirq/tutorials/qaoa) I will fix it anyway and see

@lockwo
Copy link
Contributor Author

lockwo commented Jun 9, 2022

That was the problem actually (not sure if you want to make that fix a separate PR, but it is in this one).

@lockwo
Copy link
Contributor Author

lockwo commented Jun 9, 2022

Can you add the use of OpenFermion in the notebook title? To differentiate from #1337 that is done with PennyLane.

Done

Copy link
Member

@andreamari andreamari 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 @lockwo, LGTM!
And thanks for fixing the broken link too!

Moreover congratulations for merging a unitaryhack PR and so for getting the associated bounty!

@andreamari andreamari merged commit fe58bb2 into unitaryfund:master Jun 9, 2022
andreamari added a commit that referenced this pull request Jun 9, 2022
andreamari added a commit that referenced this pull request Jun 9, 2022
* Revert "[unitaryhack] Updated molecular hydrogen tutorial (#1296)"

This reverts commit fe58bb2.

* fix broken link
@andreamari
Copy link
Member

Hi @lockwo, there was a problem with OpenFermion not working with Windows tests on the master branch. So we had to temporarily revert the merging commit.

Of course we still want to have your example but we need to merge it more carefully. Could you open a new PR with the same code as this one? Re-opening this one is fine too (if possible).

@lockwo
Copy link
Contributor Author

lockwo commented Jun 9, 2022

Just made it

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.

[unitaryhack-bounty] Improve H2 example
3 participants