-
Notifications
You must be signed in to change notification settings - Fork 603
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
Momentum-QNG optimization algo #6240
Conversation
A generalization of the Quantum Natural Gradient (QNG) optimizer by considering a discrete-time Langevin equation with QNG force.
A generalization of the Quantum Natural Gradient (QNG) optimizer by considering a discrete-time Langevin equation with QNG force. For details of the theory and derivation of Momentum-QNG, please, see: Oleksandr Borysenko, Mykhailo Bratchenko, Ilya Lukin, Mykola Luhanko, Ihor Omelchenko, Andrii Sotnikov and Alessandro Lomi. "Application of Langevin Dynamics to Advance the Quantum Natural Gradient Optimization Algorithm" <https://arxiv.org/abs/2409.01978> We are grateful to David Wierichs for his generous help with the multi-argument variant of the MomentumQNGOptimizer class. In PennyLane, the MomentumQNGOptimizer class is a subclass of the QNGOptimizer class and requires one additional hyperparameter (the momentum coefficient) :math:`0 \leq \rho < 1`, the default value being :math:`\rho=0.9`. For :math:`\rho=0` Momentum-QNG reduces to the basic QNG.
A generalization of the Quantum Natural Gradient (QNG) optimizer by considering a discrete-time Langevin equation with QNG force. For details of the theory and derivation of Momentum-QNG, please, see: Oleksandr Borysenko, Mykhailo Bratchenko, Ilya Lukin, Mykola Luhanko, Ihor Omelchenko, Andrii Sotnikov and Alessandro Lomi. "Application of Langevin Dynamics to Advance the Quantum Natural Gradient Optimization Algorithm" <https://arxiv.org/abs/2409.01978> We are grateful to David Wierichs for his generous help with the multi-argument variant of the MomentumQNGOptimizer class. In PennyLane, the MomentumQNGOptimizer class is a subclass of the QNGOptimizer class and requires one additional hyperparameter (the momentum coefficient) :math:`0 \leq \rho < 1`, the default value being :math:`\rho=0.9`. For :math:`\rho=0` Momentum-QNG reduces to the basic QNG.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #6240 +/- ##
=======================================
Coverage 99.70% 99.70%
=======================================
Files 444 445 +1
Lines 42236 42260 +24
=======================================
+ Hits 42113 42137 +24
Misses 123 123 ☔ View full report in Codecov by Sentry. |
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.
Hi @borbysh,
thanks a lot for opening this pull request!
I leave a first batch of small comments on the docstrings/changelog entry :)
Feel free to modify my suggestions further or reject them. Most of them are concerned with bringing the texts a bit closer to the standard PennyLane style.
Let me know how you'd like to proceed regarding tests. :) 🎉
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Dear David!
Thank you very much for your help!
I committed all the changes you proposed.
Now I'm turning test_qng.py into test_momentum_qng.py)
One thing remains unclear to me: where is the script that runs these tests?
Sincerely yours
Alex
…On Mon, Sep 9, 2024 at 1:07 PM David Wierichs ***@***.***> wrote:
***@***.**** requested changes on this pull request.
Hi @borbysh <https://github.com/borbysh>,
thanks a lot for opening this pull request!
I leave a first batch of small comments on the docstrings/changelog entry
:)
Feel free to modify my suggestions further or reject them. Most of them
are concerned with bringing the texts a bit closer to the standard
PennyLane style.
Let me know how you'd like to proceed regarding tests. :) 🎉
------------------------------
In doc/releases/changelog-dev.md
<#6240 (comment)>
:
> @@ -3,7 +3,10 @@
# Release 0.39.0-dev (development release)
<h3>New features since last release</h3>
-
+* A new class MomentumQNGOptimizer is added. It inherits the basic QNGOptimizer class and requires one additional hyperparameter (the
This will lead to nicer rendering of code objects :)
⬇️ Suggested change
-* A new class MomentumQNGOptimizer is added. It inherits the basic QNGOptimizer class and requires one additional hyperparameter (the
+* A new class `MomentumQNGOptimizer` is added. It inherits the basic `QNGOptimizer` class and requires one additional hyperparameter (the
------------------------------
In doc/releases/changelog-dev.md
<#6240 (comment)>
:
> Lillian M. A. Frederiksen,
Christina Lee,
William Maxwell,
-Lee J. O'Riordan,
\ No newline at end of file
+Lee J. O'Riordan,
+Oleksandr Borysenko
We tend to do ordering by last name here :)
------------------------------
In pennylane/optimize/momentum_qng.py
<#6240 (comment)>
:
> @@ -0,0 +1,171 @@
+# Copyright 2024 Xanadu Quantum Technologies Inc.
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""Quantum natural gradient optimizer with momentum"""
+import pennylane as qml
Not needed.
⬇️ Suggested change
-import pennylane as qml
------------------------------
In pennylane/optimize/momentum_qng.py
<#6240 (comment)>
:
> +
+# pylint: disable=too-many-branches
+# pylint: disable=too-many-arguments
+from pennylane import numpy as pnp
+from pennylane.utils import _flatten, unflatten
+
+from .qng import QNGOptimizer
+
+class MomentumQNGOptimizer(QNGOptimizer):
+ r"""A generalization of the Quantum Natural Gradient (QNG) optimizer by considering a discrete-time Langevin equation
+ with QNG force. For details of the theory and derivation of Momentum-QNG, please, see:
+
+ Oleksandr Borysenko, Mykhailo Bratchenko, Ilya Lukin, Mykola Luhanko, Ihor Omelchenko,
+ Andrii Sotnikov and Alessandro Lomi.
+ "Application of Langevin Dynamics to Advance the Quantum Natural Gradient Optimization Algorithm"
+ <https://arxiv.org/abs/2409.01978>
This is the somewhat funky hyperref syntax for RST documents (the format
for the documentation)
⬇️ Suggested change
- <https://arxiv.org/abs/2409.01978>
+ `arXiv:2409.01978 <https://arxiv.org/abs/2409.01978>`__
------------------------------
In pennylane/optimize/momentum_qng.py
<#6240 (comment)>
:
> +from pennylane.utils import _flatten, unflatten
+
+from .qng import QNGOptimizer
+
+class MomentumQNGOptimizer(QNGOptimizer):
+ r"""A generalization of the Quantum Natural Gradient (QNG) optimizer by considering a discrete-time Langevin equation
+ with QNG force. For details of the theory and derivation of Momentum-QNG, please, see:
+
+ Oleksandr Borysenko, Mykhailo Bratchenko, Ilya Lukin, Mykola Luhanko, Ihor Omelchenko,
+ Andrii Sotnikov and Alessandro Lomi.
+ "Application of Langevin Dynamics to Advance the Quantum Natural Gradient Optimization Algorithm"
+ <https://arxiv.org/abs/2409.01978>
+
+ We are grateful to David Wierichs for his generous help with the multi-argument variant of the MomentumQNGOptimizer class.
+
+ In PennyLane, the MomentumQNGOptimizer class is a subclass of the QNGOptimizer class and requires one additional
The context that this is in PennyLane is always given in the
documentation, so I would skip it. In RST, code-like formatting works with
two backticks ``:
⬇️ Suggested change
- In PennyLane, the MomentumQNGOptimizer class is a subclass of the QNGOptimizer class and requires one additional
+ ``MomentumQNGOptimizer`` is a subclass of the ``QNGOptimizer`` class and requires one additional
------------------------------
In pennylane/optimize/momentum_qng.py
<#6240 (comment)>
:
> + using the ``metric_tensor_fn`` keyword argument.
+
+ For the following cases, providing ``metric_tensor_fn`` may be useful:
+
+ * For hybrid classical-quantum models, the "mixed geometry" of the model
+ makes it unclear which metric should be used for which parameter.
+ For example, parameters of quantum nodes are better suited to
+ one metric (such as the QNG), whereas others (e.g., parameters of classical nodes)
+ are likely better suited to another metric.
+
+ * For multi-QNode models, we don't know what geometry is appropriate
+ if a parameter is shared amongst several QNodes.
+
+ **Examples:**
+
+ TODO
Just marking this as to do via this Github comment so we don't forget :)
------------------------------
In pennylane/optimize/momentum_qng.py
<#6240 (comment)>
:
> + See the next examples of the Pennylane code, benchmarking the Momentum-QNG optimizer
+ together with the basic QNG, Momentum and Adam:
+ QAOA: https://github.com/borbysh/Momentum-QNG/blob/main/QAOA_depth4.ipynb
+ VQE: https://github.com/borbysh/Momentum-QNG/blob/main/portfolio_optimization.ipynb
⬇️ Suggested change
- See the next examples of the Pennylane code, benchmarking the Momentum-QNG optimizer
- together with the basic QNG, Momentum and Adam:
- QAOA: https://github.com/borbysh/Momentum-QNG/blob/main/QAOA_depth4.ipynb
- VQE: https://github.com/borbysh/Momentum-QNG/blob/main/portfolio_optimization.ipynb
+ Also see the examples from the reference above, benchmarking the Momentum-QNG optimizer
+ against the basic QNG, Momentum and Adam:
+ - `QAOA <https://github.com/borbysh/Momentum-QNG/blob/main/QAOA_depth4.ipynb>`__
+ - `VQE <https://github.com/borbysh/Momentum-QNG/blob/main/portfolio_optimization.ipynb>`__
------------------------------
In pennylane/optimize/momentum_qng.py
<#6240 (comment)>
:
> + momentum=0.9 (float): the user-defined hyperparameter :math:`\rho`
+ stepsize=0.01 (float): the user-defined hyperparameter :math:`\eta`
Matching the order of the kwargs in __init__:
⬇️ Suggested change
- momentum=0.9 (float): the user-defined hyperparameter :math:`\rho`
- stepsize=0.01 (float): the user-defined hyperparameter :math:`\eta`
+ stepsize=0.01 (float): the user-defined hyperparameter :math:`\eta`
+ momentum=0.9 (float): the user-defined hyperparameter :math:`\rho`
—
Reply to this email directly, view it on GitHub
<#6240 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ANRT6235N543JBPISTE66ZTZVVXPJAVCNFSM6AAAAABN2ZUGHOVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDEOBZGQZTQNJWGI>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
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.
Thanks for adding the tests, @borbysh .
I left suggestions where to change the optimizer to the new class.
Then we'll have to adapt the tests to actually expect the right behaviour (namely the one of MomentumQNGOptimizer
instead of QNGOptimizer
)
The tests are run automatically in the continuous integration, which you can see at the bottom of the pull request overview.
In order to run them locally, install the developer's requirements via
pip install -r requirements-dev.txt
Afterwards, you can run the tests with pytest:
python -m pytest tests/optimizer/test_momentum_qng.py
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
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.
Dear David!
I made corrections according to your advises.
Regards
Alex
Co-authored-by: Pietropaolo Frisoni <pietropaolo.frisoni@xanadu.ai>
Co-authored-by: Pietropaolo Frisoni <pietropaolo.frisoni@xanadu.ai>
Co-authored-by: Pietropaolo Frisoni <pietropaolo.frisoni@xanadu.ai>
Co-authored-by: Pietropaolo Frisoni <pietropaolo.frisoni@xanadu.ai>
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.
Dear Pietropaolo!
Thank you very much for your review and suggestions!
I accepted all the changes you proposed.
Regards,
Alex
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.
Dear Pietropaolo,
thank you very much for your review and comments!
I made some changes according to your suggestions.
Regards
Alex
P.S. Looks like a duplicate comment, but the more - the better)
@borbysh Awesome! I am happy to approve once we have a green CI :) I think you need to pull from the master branch and resolve the conflicts in the changelog file before the tests start. If you have issues I can take care of it myself (just let me know) |
I took the liberty to perform the merge :) |
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'm a bit confused. I only changed "m_t" to "metric_tensor" in the left-hand side in line 126. I didn't alter the right-hand side and it passed tests 3 weeks ago)
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 also took the liberty to fix one line of code with black to pass the test : )
Congrats again @borbysh !🎉
Thank you very much, David Wierichs and Pietropaolo Frisoni! |
@borbysh You wanna push the "squash and merge" button? :) |
With a great pleasure, If you tell me where it is! |
There should be a large green button above the comment box here. |
I'm in panic. Can't find it |
Haha, no worries :D |
No worries, I'm merging this PR now. Thank you so much for your contribution @borbysh 🚀 |
@borbysh It's the 'Squash and merge' green tab on the very bottom. I can merge the PR for you if you wish Edit: Looks like it's no longer necessary : ) |
Thank you very much, guys! I owe you some good drinks! |
We fix a couple of typos in the doc discovered while reviewing #6240
…QNGOptimizer` update with singular metric tensor (#6471) **Context:** The newly added `MomentumQNGOptimizer` in #6240 does not use the keyword arguments `approx` and `lam`. **Description of the Change:** Pass the unused arguments to `super().__init__` **Possible Drawbacks:** No tests are added for this, but these two parameters are inherited from the base class, and they are never tested in the existing test module for the base class to begin with. --------- Co-authored-by: dwierichs <david.wierichs@xanadu.ai> Co-authored-by: JerryChen97 <chenys13@outlook.com>
Before submitting
Please complete the following checklist when submitting a PR:
All new features must include a unit test.
If you've fixed a bug or added code that should be tested, add a test to the
test directory!
All new functions and code must be clearly commented and documented.
If you do make documentation changes, make sure that the docs build and
render correctly by running
make docs
.Ensure that the test suite passes, by running
make test
.Add a new entry to the
doc/releases/changelog-dev.md
file, summarizing thechange, and including a link back to the PR.
The PennyLane source code conforms to
PEP8 standards.
We check all of our code against Pylint.
To lint modified files, simply
pip install pylint
, and thenrun
pylint pennylane/path/to/file.py
.When all the above are checked, delete everything above the dashed
line and fill in the pull request template.
Context:
Description of the Change:
Benefits:
Possible Drawbacks:
Related GitHub Issues: