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

Implement eigensolvers (VQD) with primitives #8640

Merged
merged 192 commits into from
Sep 29, 2022
Merged

Conversation

ElePT
Copy link
Contributor

@ElePT ElePT commented Aug 30, 2022

Summary

This PR addresses #8491 and #8492 and provides an implementation of VQD using the new primitive interfaces. The new implementation comes with a change of namespace for the whole eigensolvers module (from eigen_solvers to eigensolvers), which is more consistent with the naming conventions used throughout the stack.

Closes #8491
Closes #8492

Details and comments

Blocked by:

Closely related to (but not blocked by):

To ensure consistency with the changes made in VQE (#8702), the following elements have been removed:

  • The final eigenstate that used to be part of the EigensolverResult. To reproduce this one may use a Sampler and the optimal point from VQD outside the algorithm.
  • The setting property and print_settings method.
  • All trivial getters and setters, just use public attributes instead (expect for initial_point, which requires it for the VariationalAlgorithm interface).
  • expectation and include_custom. These are not required anymore when using an Estimator.

Additionally, the following changes have been applied:

  • Removed the option to send a custom Gradient through the VQD intialization. This option will be on hold until FidelityGradients using primitives are enabled.
  • Created a new NumPyEigensolverResult class that allows the NumPyEigensolver to still return the eigenstate.
  • Enforced "non-essential" arguments to be keyword-only.

This PR also fixes a bug found in qiskit/algorithms/observables_evaluator.py where the variance/shots are not correctly extracted from the estimator metadata, and adds a corresponding unit test to verify the fix.

TO-DO:

TO-AGREE-ON:

@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Aug 30, 2022
@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:

@ElePT ElePT closed this Aug 30, 2022
@coveralls
Copy link

coveralls commented Aug 30, 2022

Pull Request Test Coverage Report for Build 3150026480

  • 372 of 398 (93.47%) changed or added relevant lines in 8 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.05%) to 84.551%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/algorithms/eigensolvers/eigensolver.py 32 33 96.97%
qiskit/algorithms/eigensolvers/vqd.py 184 194 94.85%
qiskit/algorithms/eigensolvers/numpy_eigensolver.py 145 160 90.63%
Totals Coverage Status
Change from base Build 3150022884: 0.05%
Covered Lines: 60756
Relevant Lines: 71857

💛 - Coveralls

@ElePT ElePT reopened this Aug 30, 2022
@Cryoris Cryoris added Changelog: New Feature Include in the "Added" section of the changelog mod: algorithms Related to the Algorithms module and removed Community PR PRs from contributors that are not 'members' of the Qiskit repo Changelog: New Feature Include in the "Added" section of the changelog labels Aug 30, 2022
@ElePT ElePT marked this pull request as ready for review September 7, 2022 13:06
@ElePT ElePT requested review from a team and manoelmarques as code owners September 7, 2022 13:06

prev_circs = []
for state in range(step - 1):
prev_circs.append(self.ansatz.bind_parameters(prev_states[state]))
Copy link
Member

Choose a reason for hiding this comment

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

I imagine we could build up these circuits just once as we go step by step. Here it will be rebuilding (binding) them each time which will be seen as different circuits on down through the primitives. Maybe this can be some future improvement in the interests of time if its not easily changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will leave as a future improvement. Not resolving the conversation for that purpose.

def _build_vqd_result() -> VQDResult:

result = VQDResult()
result.optimal_point = []
Copy link
Member

Choose a reason for hiding this comment

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

It seems with lists we are going beyond the typehints for VariationalAlgo. And even on the initial point getter/setter though its Sequence float I guess ideally it would be a list of initial points not a single one, though I think here we only support an single value that is used on every step.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed extension of VariationalResult

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we need to think about improving initial_point as input gong forwards since I think it ought to be a list of points one for each step, maybe we support a single value, like now for all. But ideally I should be able to feed in the optimal points outcome which is a list of points so each step has its optimizer set accordingly.

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 also agree, I will note it as a future improvement (again, not resolving the comment to be able to find it later).


Algorithms to find eigenvalues of an operator. For chemistry these can be used to find excited
states of a molecule, and qiskit-nature has some algorithms that leverage chemistry specific
knowledge to do this in that application domain.
knowledge to do this in that application domain.These algorithms are pending depreciation.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
knowledge to do this in that application domain.These algorithms are pending depreciation.
knowledge to do this in that application domain. These algorithms are pending depreciation.

Copy link
Contributor Author

@ElePT ElePT Sep 28, 2022

Choose a reason for hiding this comment

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

I also realized it says depreciation instead of deprecation 🤣. Changing both egeinsolvers and minimum eigensolvers

Copy link
Member

@woodsp-ibm woodsp-ibm Sep 28, 2022

Choose a reason for hiding this comment

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

depreciation

Well they are losing their value as such! 🤣

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.

@ElePT Thanks for the great work!

@mergify mergify bot merged commit bebc1f8 into Qiskit:main Sep 29, 2022
ElePT added a commit to ElePT/qiskit that referenced this pull request Jun 27, 2023
* New eigensolver namespace

* Add unittests

* Remove unit test

* Progress

* Revert vqd changes

* Restore temporary changes

* Change test names

* Code cleanup

* More cleanup

* Remove run options

* Fix seed test callback

* Add warning gradients

* Fix style

* Move aux ops evaluation to vqd

* Make black

* Fix cyclic imports

* Fix style unit tests

* Remove eigenstate from result class

* Add eigenstates to init

* Fix black

* Add NumPyEigensolverResult

* Fix lint

* Add eigensolvers with primitives (VQD)

* Update base class

* Update numpy eigensolver

* Apply changes VQE

* Update

* Add TODO

* Remove opflow code

* Update copyright

* Change run_options

* Add obs.eval, fix bug, add unit test, apply comments

* Fix black

* Fix attributes, remove gradient

* Remove defaults

* Fix lint, add base operator support

* Add BaseOperator support and unit tests

* Fix style

* Fix style

* Fix lint

* Fix sphinx

* Update qiskit/algorithms/eigensolvers/vqd.py

* Update qiskit/algorithms/eigensolvers/vqd.py

* Apply VQE changes

* Add metadata to result

* Fix old reno cross reference

* fix lint

* modify aux ops metadata

* Add metadata to callback

* Return metadata in observables evaluator

* fix copyright

* Update test/python/algorithms/test_numpy_eigen_solver.py

* Remove opflow from vqd tests

* Add optimizer result

* fix tests

* Fix lint

* Fix black

* Fix cyclic imports

* Fix black againnn

* Add minimally working VQE with estimator primitive implementation.

No gradients, no tests etc.

* Revert from dataclass results to original result classes.

* Enforce positional and keyword VQE arguments.

* Move aux op eval logic to function

* Update docstring.

* Remove max_evals_grouped. Force to set directly on optimizer.

* Remove validate min import.

* Make note that eval_observables will be used to eval aux ops.

* Add initial vqe tests.

* Have VQE inherit from VariationalAlgorithm.

* Move energy evaluation to unnested function.

* Construct h2_op using SparsePauliOp

* Add gradient with primitives support.

* Update docstrings

* update broadcast handling

* update eval_energy output for batching

* add incomplete QNSPA test

* fix batch evaluation of QNSPSA test

* remove vqe callback

* move estimator to first arg

* remove usused imports

* add minimum eigensolvers test init file

* add aux ops tests and prepare for new eval_operators

* no longer support account for Nones in aux_ops

* correct typing for MinimumEigensolver

* Compute default initial point using ansatz bounds.

* Add NumPyMinimumEigensolverResult

* Fix type hints

* Fix type hints

* Formatting

* Do not store NumPyMES result inside the algo.

* Provide default values for ansatz and estimator

* Formatting

* fix old and new batching

* Add tests for NumpyMES and import in module.

* Use lazy formatting in log messages

* Use lazy formatting in log messages

* Add back callback to VQE.

* minor renaming

* raise algorithm error if primitive jobs fail

* Add return documentation

* Improve var names and docstrings.

* Apply suggestions from code review

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

* minor formatting

* minor formatting

* Ensure evaluate_energy/gradient match

Co-authored-by: Max Rossmannek <max.rossmannek@uzh.ch>

* return bounds logic; fix some docstrings and minor refactor

* Force keyword arguments in vqe.

* Use estimate_observables function

* break up numpy mes tests with subTest

* formatting

* remove redundant eval_aux_ops

* add typehints to docstring attributes

* remove usused imports

* remove default ansatz

* remove usused imports

* update typehints

* avoid changing the original ansatz

* avoid changing the original ansatz

* create separate function to build vqe result

* Correct aux operator eignvalue type hint

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

* add variance to callback

* use std_dev in callback rather than variance

* formatting

* return variance and shots in callback

* return full metadata in callback

* Move validation functions to algorithms/utils

* correct the callback attribute documentation

* correct the callback attribute typehint docstring

* update VQE docstring

* release note and pending-depreciate old algs

* update vqe class docstring

* Apply suggestions from code review

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

* Do not copy ansatz

* Note pending depreciation of old algs

* fix docstrings and imports

* Fix issues with building docs

* Include OptimizerResult in VQEResult

* Remove trailing whitespace

* Fix math notation in docstring

* estimate_obervables to return metadata @ElePT +VQE

* add example in release note

* Update evaluate_observables docstring

* Fix observables_evaluator tests.

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

* fix trotter_qrte tests and remove depreciation

* formatting

* remove unused import

* Apply suggestions to docstring from code review

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

* Update VQE docstring

* Remove printing

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

* Add parameters to estimate observables

* Fix estimate obs. unit test

* Update arg description

* Update arg description

* keep equation part of sentence

* dict -> dict[str, Any]

* Update qiskit/algorithms/optimizers/spsa.py

Apply suggestion Imamichi-san

* introduce FilterType and aux_operator_eigenvalues -> aux_operators_evaluated

* Correct typehint

Co-authored-by: Ikko Hamamura <ikkoham@users.noreply.github.com>

* Adapt to VQE

* Update qiskit/algorithms/eigen_solvers/numpy_eigen_solver.py

* Pull from main

* Remove change vqe

* Refine docs, typehints, remove opflow

* add check for PauliSumOp

* Fix import

* reformat test

* Remove metadata unit tests

* Add operator tests

* Reformat tests

* Fix observables eval test

* Apply initial feedback

* Change numpy eig. aux ops metadata

* Apply review comments

* Fix unittests

* Update reno

* Apply review comments

* Fix result names, typehints

* Fix docs

Co-authored-by: Manoel Marques <manoel.marques@ibm.com>
Co-authored-by: Declan Millar <declan.millar@ibm.com>
Co-authored-by: dlasecki <dal@zurich.ibm.com>
Co-authored-by: Max Rossmannek <max.rossmannek@uzh.ch>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Co-authored-by: Ikko Hamamura <ikkoham@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
* New eigensolver namespace

* Add unittests

* Remove unit test

* Progress

* Revert vqd changes

* Restore temporary changes

* Change test names

* Code cleanup

* More cleanup

* Remove run options

* Fix seed test callback

* Add warning gradients

* Fix style

* Move aux ops evaluation to vqd

* Make black

* Fix cyclic imports

* Fix style unit tests

* Remove eigenstate from result class

* Add eigenstates to init

* Fix black

* Add NumPyEigensolverResult

* Fix lint

* Add eigensolvers with primitives (VQD)

* Update base class

* Update numpy eigensolver

* Apply changes VQE

* Update

* Add TODO

* Remove opflow code

* Update copyright

* Change run_options

* Add obs.eval, fix bug, add unit test, apply comments

* Fix black

* Fix attributes, remove gradient

* Remove defaults

* Fix lint, add base operator support

* Add BaseOperator support and unit tests

* Fix style

* Fix style

* Fix lint

* Fix sphinx

* Update qiskit/algorithms/eigensolvers/vqd.py

* Update qiskit/algorithms/eigensolvers/vqd.py

* Apply VQE changes

* Add metadata to result

* Fix old reno cross reference

* fix lint

* modify aux ops metadata

* Add metadata to callback

* Return metadata in observables evaluator

* fix copyright

* Update test/python/algorithms/test_numpy_eigen_solver.py

* Remove opflow from vqd tests

* Add optimizer result

* fix tests

* Fix lint

* Fix black

* Fix cyclic imports

* Fix black againnn

* Add minimally working VQE with estimator primitive implementation.

No gradients, no tests etc.

* Revert from dataclass results to original result classes.

* Enforce positional and keyword VQE arguments.

* Move aux op eval logic to function

* Update docstring.

* Remove max_evals_grouped. Force to set directly on optimizer.

* Remove validate min import.

* Make note that eval_observables will be used to eval aux ops.

* Add initial vqe tests.

* Have VQE inherit from VariationalAlgorithm.

* Move energy evaluation to unnested function.

* Construct h2_op using SparsePauliOp

* Add gradient with primitives support.

* Update docstrings

* update broadcast handling

* update eval_energy output for batching

* add incomplete QNSPA test

* fix batch evaluation of QNSPSA test

* remove vqe callback

* move estimator to first arg

* remove usused imports

* add minimum eigensolvers test init file

* add aux ops tests and prepare for new eval_operators

* no longer support account for Nones in aux_ops

* correct typing for MinimumEigensolver

* Compute default initial point using ansatz bounds.

* Add NumPyMinimumEigensolverResult

* Fix type hints

* Fix type hints

* Formatting

* Do not store NumPyMES result inside the algo.

* Provide default values for ansatz and estimator

* Formatting

* fix old and new batching

* Add tests for NumpyMES and import in module.

* Use lazy formatting in log messages

* Use lazy formatting in log messages

* Add back callback to VQE.

* minor renaming

* raise algorithm error if primitive jobs fail

* Add return documentation

* Improve var names and docstrings.

* Apply suggestions from code review

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

* minor formatting

* minor formatting

* Ensure evaluate_energy/gradient match

Co-authored-by: Max Rossmannek <max.rossmannek@uzh.ch>

* return bounds logic; fix some docstrings and minor refactor

* Force keyword arguments in vqe.

* Use estimate_observables function

* break up numpy mes tests with subTest

* formatting

* remove redundant eval_aux_ops

* add typehints to docstring attributes

* remove usused imports

* remove default ansatz

* remove usused imports

* update typehints

* avoid changing the original ansatz

* avoid changing the original ansatz

* create separate function to build vqe result

* Correct aux operator eignvalue type hint

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

* add variance to callback

* use std_dev in callback rather than variance

* formatting

* return variance and shots in callback

* return full metadata in callback

* Move validation functions to algorithms/utils

* correct the callback attribute documentation

* correct the callback attribute typehint docstring

* update VQE docstring

* release note and pending-depreciate old algs

* update vqe class docstring

* Apply suggestions from code review

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

* Do not copy ansatz

* Note pending depreciation of old algs

* fix docstrings and imports

* Fix issues with building docs

* Include OptimizerResult in VQEResult

* Remove trailing whitespace

* Fix math notation in docstring

* estimate_obervables to return metadata @ElePT +VQE

* add example in release note

* Update evaluate_observables docstring

* Fix observables_evaluator tests.

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

* fix trotter_qrte tests and remove depreciation

* formatting

* remove unused import

* Apply suggestions to docstring from code review

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

* Update VQE docstring

* Remove printing

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

* Add parameters to estimate observables

* Fix estimate obs. unit test

* Update arg description

* Update arg description

* keep equation part of sentence

* dict -> dict[str, Any]

* Update qiskit/algorithms/optimizers/spsa.py

Apply suggestion Imamichi-san

* introduce FilterType and aux_operator_eigenvalues -> aux_operators_evaluated

* Correct typehint

Co-authored-by: Ikko Hamamura <ikkoham@users.noreply.github.com>

* Adapt to VQE

* Update qiskit/algorithms/eigen_solvers/numpy_eigen_solver.py

* Pull from main

* Remove change vqe

* Refine docs, typehints, remove opflow

* add check for PauliSumOp

* Fix import

* reformat test

* Remove metadata unit tests

* Add operator tests

* Reformat tests

* Fix observables eval test

* Apply initial feedback

* Change numpy eig. aux ops metadata

* Apply review comments

* Fix unittests

* Update reno

* Apply review comments

* Fix result names, typehints

* Fix docs

Co-authored-by: Manoel Marques <manoel.marques@ibm.com>
Co-authored-by: Declan Millar <declan.millar@ibm.com>
Co-authored-by: dlasecki <dal@zurich.ibm.com>
Co-authored-by: Max Rossmannek <max.rossmannek@uzh.ch>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Co-authored-by: Ikko Hamamura <ikkoham@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 Variational Quantum Deflation with primitives. Rewrite Eigensolvers with primitives.
8 participants