-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Allow callables as optimizers in VQE and QAOA #7191
Merged
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4be7b58
allow callables as optimizers in VQE
Cryoris 60e2bb1
Missing quote
Cryoris 20c2e4c
fix lint
Cryoris eb267db
Merge branch 'vqe-callables' of github.com:Cryoris/qiskit-terra into …
Cryoris e0f8fff
Merge branch 'main' into vqe-callables
Cryoris 5f6f0eb
don't use typing.Protocol
Cryoris 6a8e4de
Merge branch 'vqe-callables' of github.com:Cryoris/qiskit-terra into …
Cryoris be35c47
Merge branch 'main' into vqe-callables
Cryoris 5f9fca1
Merge branch 'main' into vqe-callables
Cryoris 514982e
comments from review
Cryoris ed4c8e6
Merge branch 'main' into vqe-callables
woodsp-ibm ff6552e
Merge branch 'main' into vqe-callables
Cryoris c056cc4
add qaoa test
Cryoris 5e3a1bf
Merge branch 'vqe-callables' of github.com:Cryoris/qiskit-terra into …
Cryoris 55ad156
lint
Cryoris e429f5c
Merge branch 'main' into vqe-callables
ikkoham 177997e
Merge branch 'main' into vqe-callables
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
releasenotes/notes/vqe-optimizer-callables-1aa14d78c855d383.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
features: | ||
- | | ||
Allow callables as optimizers in :class:`~qiskit.algorithms.VQE` and | ||
:class:`~qiskit.algorithms.QAOA`. Now, the optimizer can either be one of Qiskit's optimizers, | ||
such as :class:`~qiskit.algorithms.optimizers.SPSA` or a callable with the following signature: | ||
|
||
.. code-block::python | ||
Cryoris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
from qiskit.algorithms.optimizers import OptimizerResult | ||
|
||
def my_optimizer(fun, x0, jac=None, bounds=None) -> OptimizerResult: | ||
# Args: | ||
# fun (callable): the function to minimize | ||
# x0 (np.ndarray): the initial point for the optimization | ||
# jac (callable, optional): the gradient of the objective function | ||
# bounds (list, optional): a list of tuples specifying the parameter bounds | ||
|
||
result = OptimizerResult() | ||
result.x = # optimal parameters | ||
result.fun = # optimal function value | ||
Comment on lines
+19
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kind of weird that you don't construct the whole class in one go at initialisation time, but I gave up that fight in Python long ago. |
||
return result | ||
|
||
The above signature also allows to directly pass any SciPy minimizer, for instance as | ||
Cryoris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. code-block::python | ||
Cryoris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
from functools import partial | ||
from scipy.optimize import minimize | ||
|
||
optimizer = partial(minimize, method="L-BFGS-B) | ||
Cryoris marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this type hint actually correct for any SciPy minimiser? It seems to imply that the first four arguments are positional, but the first four positional arguments of their minimisers are (objective, x0, args, jac).
In an ideal world, I think we'd put this in
__init__.py
, and write some documentation in the docstring about the types. It can have a Sphinx cross-ref (.. _algorithms_minimum_eigensolvers_minimizer:
, then you reference with:ref:
algorithms_minimum_eigensolvers_minimizer`), then the various class docstrings could all link to it. That said, I don't think you need to worry much about it right now.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.
In fact the callable must have the named arguments
fun, x0, jac, bounds
but I didn't quite know how to put that in a type hint using only what's available now 🤔 I updated the class docs to highlight thisThere 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.
how about using Callback Protocol? PEP544
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.
We tried that but it's only supported from Python 3.8 onwards, see the discussion above: #7191 (comment)
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.
Protocol is back ported by typing-extensions. https://pypi.org/project/typing-extensions/ so you can use it.
(I'm sorry I overlooked...)
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.
Yeah but that would create an additional dependency to a new package, so we didn't add it (yet) 😄