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

Sampler-based VQE for diagonal operators, plus QAOA #8669

Merged
merged 55 commits into from
Sep 29, 2022

Conversation

Cryoris
Copy link
Contributor

@Cryoris Cryoris commented Sep 2, 2022

Summary

Add a VQE variant based on the sampler primitive, which only allows for optimization of diagonal ("classical") Hamiltonians.

Adds QAOA based on the above.
closes #8472

Details and comments

This differs from the estimator-based, standard VQE in that this version

  • takes a BaseSampler and not a BaseEstimator
  • allows for a custom aggregation function (such as CVaR)
  • keeps track of the best overall measurement (not possible/useful in estimator-based case)
  • returns samples of the final state (not possible in estimator-based case we generally have to measure in more than one basis)

Gradient support

In this PR, the gradient support has not yet been added. That's because it's not quite clear what's the best way to integrate the gradients. Here are the options we came up with, and we decided that we will first not support them and then directly implement option 4 if possible:

Option 1 The SamplingVQE takes a sampler and sampler gradient as input
pro: it is consistent as it takes a sampler and a sampler gradient
problem: I cannot support aggregation functions like CVaR, since I cannot easily compute the gradient if I have only access to the samples

Option 2 The SamplingVQE takes a sampler and an empty estimator gradient (i.e. without an estimator set)
pro: I build the diagonal estimator inside the SamplingVQE, and plug it into the estimator gradient
problem: consistency suffers, since I have to make the estimator in the gradients optional

Option 3 Make the SamplingVQE take a DiagonalEstimator and a EstimatorGradient equipped with said diagonal estimator
pro: consistent
problem: users have to build out the diagonal estimator, and the Sampling VQE will take an estimator

Option 4 Change how gradients work and introduce a general ParameterShift() (& co) which is not sampler or estimator specific
pro: makes my life easy
pro: makes users life easy as they don’t have to write out these nasty gradient names
problem: doesn’t exist yet

Todo

  • Support batching
  • See TODOs in tests

@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the the following people are requested to review this:

@Cryoris Cryoris added this to the 0.22 milestone Sep 2, 2022
@Cryoris Cryoris added the mod: algorithms Related to the Algorithms module label Sep 2, 2022
@Cryoris Cryoris marked this pull request as ready for review September 6, 2022 14:26
@Cryoris Cryoris requested review from a team, manoelmarques and woodsp-ibm as code owners September 6, 2022 14:26
@woodsp-ibm
Copy link
Member

@Cryoris See also my DM to you for some comments

@coveralls
Copy link

coveralls commented Sep 7, 2022

Pull Request Test Coverage Report for Build 3154881397

  • 268 of 296 (90.54%) changed or added relevant lines in 5 files are covered.
  • 13 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.2%) to 84.593%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/algorithms/minimum_eigensolvers/qaoa.py 24 28 85.71%
qiskit/algorithms/minimum_eigensolvers/sampling_mes.py 47 53 88.68%
qiskit/algorithms/minimum_eigensolvers/sampling_vqe.py 116 122 95.08%
qiskit/algorithms/minimum_eigensolvers/diagonal_estimator.py 78 90 86.67%
Files with Coverage Reduction New Missed Lines %
qiskit/extensions/quantum_initializer/squ.py 2 79.78%
qiskit/compiler/transpiler.py 11 89.71%
Totals Coverage Status
Change from base Build 3154878985: 0.2%
Covered Lines: 61115
Relevant Lines: 72246

💛 - Coveralls

@declanmillar
Copy link
Member

This PR will also include a QAOA implementation that subclasses SamplingVQE.

@woodsp-ibm woodsp-ibm changed the title Sampler-based VQE for diagonal operators Sampler-based VQE for diagonal operators, plus QAOA Sep 14, 2022
Copy link
Member

@woodsp-ibm woodsp-ibm left a comment

Choose a reason for hiding this comment

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

@declanmillar @Cryoris Thanks for the new function and primitive conversions. LGTM!

@mergify mergify bot merged commit 16b5df1 into Qiskit:main Sep 29, 2022
ElePT added a commit to ElePT/qiskit that referenced this pull request Jun 27, 2023
* plain optimization works

* support for auxops

* add first round of tests

* cast to real

* lint & more tests

* add VariationalAlgo inheritance

* batching and diagonal estimator as BaseEstimator

* add gradient support

* remove gradient support for now

* fix test filename

* fix old and new batching

* fix broadcasting from a single long array

* add slsqp batch test

* Put Sampler first (no quote intended)

* Add QAOA based on primitives.

* Add QAOA based on primitives.

* force kwargs; match function names with vqe

* remove usused imports

* formatting

* remove usused imports

* Apply suggestions from code review

Remove redundant tests and gates

Co-authored-by: ElePT <57907331+ElePT@users.noreply.github.com>

* correct docs and typehints

* Correct docstring and error text

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>

* update docstrings

* add callback to samplingvqe

* fix merge conflicts

* remove edit to base estimator

* formatting

* update _run for DiagonalEstimator based on BaseEstimator updates

* remove unused import

* bring sampling vqe in line with vqe

* fix docstring indentation

* Correct copyright/

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>

* Fix copyright.

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>

* Use QuasiDistribution typehint, rather than Mapping.

* add callback test fro sampling vqe

* Test initial point is used for QAOA

* formatting

* Test scipy optimizer callable

* Test QAOA with random initial point

* update samplingvqe and qaoa docstrings and add releasenote

* add release note for SamplingVQE and QAOA

* fix hyperlink in docstring

* fix typehints, docstrings, errors

* Convert baseoperator operators inside qaoa

* repeat all SamplingVQE tests with BaseOperator and PauliSumOp instances

Co-authored-by: Declan Millar <declan.millar@ibm.com>
Co-authored-by: ElePT <57907331+ElePT@users.noreply.github.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
ElePT added a commit to ElePT/qiskit-algorithms-test that referenced this pull request Jul 17, 2023
* plain optimization works

* support for auxops

* add first round of tests

* cast to real

* lint & more tests

* add VariationalAlgo inheritance

* batching and diagonal estimator as BaseEstimator

* add gradient support

* remove gradient support for now

* fix test filename

* fix old and new batching

* fix broadcasting from a single long array

* add slsqp batch test

* Put Sampler first (no quote intended)

* Add QAOA based on primitives.

* Add QAOA based on primitives.

* force kwargs; match function names with vqe

* remove usused imports

* formatting

* remove usused imports

* Apply suggestions from code review

Remove redundant tests and gates

Co-authored-by: ElePT <57907331+ElePT@users.noreply.github.com>

* correct docs and typehints

* Correct docstring and error text

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>

* update docstrings

* add callback to samplingvqe

* fix merge conflicts

* remove edit to base estimator

* formatting

* update _run for DiagonalEstimator based on BaseEstimator updates

* remove unused import

* bring sampling vqe in line with vqe

* fix docstring indentation

* Correct copyright/

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>

* Fix copyright.

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>

* Use QuasiDistribution typehint, rather than Mapping.

* add callback test fro sampling vqe

* Test initial point is used for QAOA

* formatting

* Test scipy optimizer callable

* Test QAOA with random initial point

* update samplingvqe and qaoa docstrings and add releasenote

* add release note for SamplingVQE and QAOA

* fix hyperlink in docstring

* fix typehints, docstrings, errors

* Convert baseoperator operators inside qaoa

* repeat all SamplingVQE tests with BaseOperator and PauliSumOp instances

Co-authored-by: Declan Millar <declan.millar@ibm.com>
Co-authored-by: ElePT <57907331+ElePT@users.noreply.github.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog mod: algorithms Related to the Algorithms module priority: high
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rewrite QAOA with primitives.
7 participants