Skip to content

Implements VarQTE algorithm with primitives.#8746

Merged
mergify[bot] merged 227 commits into
Qiskit:mainfrom
dlasecki:varqte-primitives
Jan 19, 2023
Merged

Implements VarQTE algorithm with primitives.#8746
mergify[bot] merged 227 commits into
Qiskit:mainfrom
dlasecki:varqte-primitives

Conversation

@dlasecki
Copy link
Copy Markdown
Contributor

@dlasecki dlasecki commented Sep 14, 2022

Summary

This PR implements the VarQTE algorithm with primitives - it uses primitive-enabled QFI and gradients.

Closes: #8485.

Details and comments

This PR depends on #8683 and #8688 and some deprecations-related PRs which need to be merged before this PR. Please ignore files related to these PRs that appear in this PR.

In the current stage the feature does not work yet and further adjustments are needed. Here are the major ones:

  • Unify all the signatures across the algorithm.
  • Add higher-level primitives as arguments in entry points.
  • Update unit tests to the new version and assure correctness.
  • Reduce the use of opflow.
  • Clean CI.
  • Remove old VarQTE (variational package) - no deprecation needed because it was never released. Done by Removes old VarQTE algorithm code (without primitives). #8750

def __init__(
self,
varqte_linear_solver: VarQTELinearSolver,
param_dict: dict[Parameter, float],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For inputs we have been trying to be more flexible in terms of types so I am wondering about Mapping rather than dict - do we really need the specific type? I see this also in the solve method of var_qte_linear_solver where I guess this gets used - maybe there are other places too that would need changing were this changed.

Comment thread qiskit/algorithms/time_evolvers/variational/solvers/ode/forward_euler_solver.py Outdated
Comment on lines +52 to +53
self.ode_function_type = ode_function_type
self.lse_solver = lse_solver
Copy link
Copy Markdown
Member

@woodsp-ibm woodsp-ibm Jan 18, 2023

Choose a reason for hiding this comment

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

These are public vars - we should have docs for these - assuming they wanted/needed to be kept public

var_principle: VariationalPrinciple,
hamiltonian: BaseOperator,
ansatz: QuantumCircuit,
gradient_params: list[Parameter] | None = None,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we support Sequence rather than it needing to be the specific list type?

Comment thread qiskit/algorithms/time_evolvers/variational/solvers/var_qte_linear_solver.py Outdated
def __init__(
self,
ansatz: QuantumCircuit,
initial_parameters: dict[Parameter, float] | list[float] | np.ndarray,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Similar comment on types dict & list or Mapping & Sequence?

Comment thread qiskit/algorithms/time_evolvers/variational/var_qite.py Outdated
Comment on lines +58 to +59
variational_principle: VariationalPrinciple | None = None,
estimator: BaseEstimator | None = None,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is None really valid here for these. In _evolve variational_principle is passed directly to a function that seems not to allow None, and estimator is used directly in evaluating operators. To me it looks like None is invalid

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

True, removed None

Comment on lines +87 to +98
self.ansatz = ansatz
self.initial_parameters = initial_parameters
self.variational_principle = variational_principle
self.estimator = estimator
self.num_timesteps = num_timesteps
self.lse_solver = lse_solver
# OdeFunction abstraction kept for potential extensions - unclear at the moment;
# currently hidden from the user
self._ode_function_factory = OdeFunctionFactory(lse_solver=lse_solver)
self.ode_solver = ode_solver
self.imag_part_tol = imag_part_tol
self.num_instability_tol = num_instability_tol
Copy link
Copy Markdown
Member

@woodsp-ibm woodsp-ibm Jan 18, 2023

Choose a reason for hiding this comment

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

The public instance vars here should be documented if they are wanted to public (via Attriibutes)

I see this base class is not listed in the docs, so the of the parents of var_q(i/r)te, the parent link for this is not accessible. And if it contains all these public instance vars for common setting/getting of these values then I think it should be there in the docs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Added attributes and class to the docs.

observables for which expected on an evolved state are calculated at each timestep.
These values are in fact lists of tuples formatted as (mean, standard deviation).
times (np.array | None): Optional list of times at which each observable has been evaluated.
parameter_values (np.array | None): Optional list of parameter values obtained after
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The first four attributes above are from the parent class and documented there. Duplicating these here does seem like its awkward for maintenance etc so ideally it should just be the additional value supplied here I would have thought. I cannot see how this looks as it does not appear to be in the set of classes that are listed for the documentation and I guess it should be.

self.num_instability_tol = num_instability_tol

def evolve(self, evolution_problem: TimeEvolutionProblem) -> VarQTEResult:
"""Apply Variational Quantum Time Evolution w.r.t. the given operator.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The w.r.t. causes the summary to look odd as it takes the first sentence and sees that as the end of the w.r.t. Maybe we can change w.r.t. - would on or to work on their own - with respect to seems like a mouthful here

image

Comment on lines +34 to +35
qgt: BaseQGT | None = None,
gradient: BaseEstimatorGradient | None = None,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should None be supported here. While it is in derived classes they take care of None before calling super here. Since qgt and gradient are public instance vars any attempt to set them I think should not include None - assuming they need to be kept as public instance vars. In which case they also need docs.

ElePT and others added 6 commits January 18, 2023 19:29
…ples/imaginary_mc_lachlan_principle.py

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
…ples/real_mc_lachlan_principle.py

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

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
@ElePT
Copy link
Copy Markdown
Contributor

ElePT commented Jan 18, 2023

Thank you @woodsp-ibm for your review. I have tried to implement all the changes you suggested, including using Sequence and Mapping when possible, listing public attributes as such in the docstrings, adding VarQTE and VarQTEResult to the docs, removing imprecise defaults to None.

parameters present in the initial state or if the list of values provided is not the
same length as the list of parameters.
TypeError: If an unsupported type of ``param_values`` provided.
ValueError: If an unsupported type of ``param_values`` provided.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As the error is around the type was the TypeError not more applicable to raise?

@ElePT ElePT force-pushed the varqte-primitives branch from e4226ca to d55e9d4 Compare January 18, 2023 21:33
Copy link
Copy Markdown
Collaborator

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for all the effort to push this over the finish line @ElePT!

@mergify mergify Bot merged commit 671bd1f into Qiskit:main Jan 19, 2023
ElePT added a commit to ElePT/qiskit that referenced this pull request Jun 27, 2023
* Implemented observables_evaluator.py with primitives.

* Added evolvers problems and interfaces to time_evolvers package.

* Mostly updated trotter_qrte.py to use primitives.

* Added observables_evaluator.py that uses primitives.

* Added observables_evaluator.py that uses primitives.

* Updated trotter_qrte.py to use primitives.

* Updated imports

* Updated typehints and limited use of opflow.

* Updated typehints and limited use of opflow.

* Removed files out of scope for this PR.

* Added annotations import.

* Added observables_evaluator.py with primitives.

* Added ListOrDict support to observables_evaluator.py.

* wip qfi

* Included CR suggestions.

* Applied some CR comments.

* Applied some CR comments.

* Added reno.

* Added reno.

* Accepting Statevector.

* Added attributes docs.

* Support for 0 operator.

* wip qfi

* Add pending deprecation

* Add pending deprecation for evolvers

* Code refactoring.

* Code refactoring.

* Renamed classes and linked to algorithms init.

* fix docstring

* Improved reno.

* Improved reno.

* Returning variances and shots.

* Code refactoring.

* Black fix.

* Unit test fix.

* Reduced use of opflow.

* Drafted conversion to primitives; updated type hints.

* Handle empty inputs gracefully.

* Applied CR comments.

* Applied CR comments.

* Add deprecation msg to evolvers package

* Drafted integreation with primitive-based QFI and gradients; reduced opflow; updated imports.

* Import added.

* updated

* added tests

* updated

* Applied CR comments.

* updated

* updated

* lin comb qfi

* Eliminated cyclic import.

* fix

* Draft adjusting code to primitives.

* Code refactoring

* Draft adjusted unit tests to primitives.

* Updated init.

* finilizing

* fix lint

* started working on block diag qfi

* Updated code to BaseQFI.

* Removed opflow from tests, made most VarQITE tests passing correctly.

* Used correct bases in var principles.

* Drafted the use of energy term in real case.

* Drafted tests with shot-based primitive.

* updated utils

* fix

* fix options

* fix

* lint

* fix

* fix list

* remove estimator from base qfi

* fix

* make a directory for gradient tests

* added option to baseqfi + fied shots behavior

* fixed qfi's derivative type complex

* fix

* fix

* make parameter_values back to float

* Code and docs refactoring.

* Made VarQRTE work correctly.

* Support BaseOperator in VarQRTE.

* Finished VarQITE tests.

* Finished VarQRTE tests.

* Added reno.

* Updated algorithms init.

* Code and docs refactoring.

* Code refactoring.

* Revert "Code refactoring."

This reverts commit a8334de.

* Code refactoring.

* updated

* fix lint

* updated

* updated

* Update qiskit/algorithms/time_evolvers/variational/__init__.py

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Update qiskit/algorithms/time_evolvers/variational/variational_principles/imaginary_mc_lachlan_principle.py

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Removed unused error calculator.

* Dropped BaseOperator type for ansatz.

* Code refactoring.

* Validation of gradient types added.

* Added support for time-dep Hamiltonians.

* Made initial parameters required.

* Re-added optional estimator for aux ops.

* Added comments; improved exception handling.

* Added supports_observables method.

* fix import cycles

* fix docstring titles

* change imports in unit tests

* fix docs

* Removed dead code.

* Fixed bug with binding time parameter.

* Added time-dependent VarQRTE unit test.

* Updated tests after main marge.

* Added time-dependent VarQITE unit test.

* add test for time dep hamiltonian

* Add varqte_result, update varqite tests

* Update var_qrte test

* Fix lint

* Fix test

* Remove opflow dependencies

* Fix lint

* Remove redundant typehints

* Add parameters, times, observables to result

* Fix lint

* Unwrap PauliSumOp in VarQTE

* Fix tests

* Replace QFI with QGT

* Update copyright

* Update tests with QGT

* Update imports

* Update error message

* Address cyclic import

* Fix black

* Fix cyclic imports

* Fix linebreak

* Update qiskit/algorithms/time_evolvers/variational/var_qte.py

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Update qiskit/algorithms/time_evolvers/variational/__init__.py

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Apply suggestions documentation

* Update qiskit/algorithms/time_evolvers/variational/solvers/ode/ode_function_factory.py

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

* Update qiskit/algorithms/time_evolvers/time_evolution_result.py

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

* Update qiskit/algorithms/time_evolvers/variational/var_qte.py

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

* Update qiskit/algorithms/time_evolvers/variational/variational_principles/imaginary_mc_lachlan_principle.py

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

* Update qiskit/algorithms/time_evolvers/variational/variational_principles/real_mc_lachlan_principle.py

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

* Apply review comments

* Update qiskit/algorithms/time_evolvers/variational/solvers/var_qte_linear_solver.py

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Update qiskit/algorithms/time_evolvers/variational/solvers/var_qte_linear_solver.py

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Apply review Julien

* Update qiskit/algorithms/time_evolvers/variational/solvers/var_qte_linear_solver.py

* Update qiskit/algorithms/time_evolvers/variational/solvers/var_qte_linear_solver.py

* Update qiskit/algorithms/time_evolvers/variational/solvers/var_qte_linear_solver.py

* Update qiskit/algorithms/time_evolvers/variational/variational_principles/imaginary_mc_lachlan_principle.py

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

* Update qiskit/algorithms/time_evolvers/variational/variational_principles/real_mc_lachlan_principle.py

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

* Update qiskit/algorithms/time_evolvers/variational/solvers/var_qte_linear_solver.py

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

* Update qiskit/algorithms/time_evolvers/variational/var_qite.py

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

* Apply latest suggestions

* Change typehint

* Fix lint

* Fix unit test

Co-authored-by: a-matsuo <MATSUOA@jp.ibm.com>
Co-authored-by: Manoel Marques <Manoel.Marques@ibm.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Co-authored-by: a-matsuo <47442626+a-matsuo@users.noreply.github.com>
Co-authored-by: Julien Gacon <gaconju@gmail.com>
Co-authored-by: Manoel Marques <manoelmrqs@gmail.com>
Co-authored-by: ElePT <57907331+ElePT@users.noreply.github.com>
Co-authored-by: ElePT <epenatap@gmail.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
* Implemented observables_evaluator.py with primitives.

* Added evolvers problems and interfaces to time_evolvers package.

* Mostly updated trotter_qrte.py to use primitives.

* Added observables_evaluator.py that uses primitives.

* Added observables_evaluator.py that uses primitives.

* Updated trotter_qrte.py to use primitives.

* Updated imports

* Updated typehints and limited use of opflow.

* Updated typehints and limited use of opflow.

* Removed files out of scope for this PR.

* Added annotations import.

* Added observables_evaluator.py with primitives.

* Added ListOrDict support to observables_evaluator.py.

* wip qfi

* Included CR suggestions.

* Applied some CR comments.

* Applied some CR comments.

* Added reno.

* Added reno.

* Accepting Statevector.

* Added attributes docs.

* Support for 0 operator.

* wip qfi

* Add pending deprecation

* Add pending deprecation for evolvers

* Code refactoring.

* Code refactoring.

* Renamed classes and linked to algorithms init.

* fix docstring

* Improved reno.

* Improved reno.

* Returning variances and shots.

* Code refactoring.

* Black fix.

* Unit test fix.

* Reduced use of opflow.

* Drafted conversion to primitives; updated type hints.

* Handle empty inputs gracefully.

* Applied CR comments.

* Applied CR comments.

* Add deprecation msg to evolvers package

* Drafted integreation with primitive-based QFI and gradients; reduced opflow; updated imports.

* Import added.

* updated

* added tests

* updated

* Applied CR comments.

* updated

* updated

* lin comb qfi

* Eliminated cyclic import.

* fix

* Draft adjusting code to primitives.

* Code refactoring

* Draft adjusted unit tests to primitives.

* Updated init.

* finilizing

* fix lint

* started working on block diag qfi

* Updated code to BaseQFI.

* Removed opflow from tests, made most VarQITE tests passing correctly.

* Used correct bases in var principles.

* Drafted the use of energy term in real case.

* Drafted tests with shot-based primitive.

* updated utils

* fix

* fix options

* fix

* lint

* fix

* fix list

* remove estimator from base qfi

* fix

* make a directory for gradient tests

* added option to baseqfi + fied shots behavior

* fixed qfi's derivative type complex

* fix

* fix

* make parameter_values back to float

* Code and docs refactoring.

* Made VarQRTE work correctly.

* Support BaseOperator in VarQRTE.

* Finished VarQITE tests.

* Finished VarQRTE tests.

* Added reno.

* Updated algorithms init.

* Code and docs refactoring.

* Code refactoring.

* Revert "Code refactoring."

This reverts commit a8334deb8e474e847e68158dc09c1f0b02527f66.

* Code refactoring.

* updated

* fix lint

* updated

* updated

* Update qiskit/algorithms/time_evolvers/variational/__init__.py

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Update qiskit/algorithms/time_evolvers/variational/variational_principles/imaginary_mc_lachlan_principle.py

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Removed unused error calculator.

* Dropped BaseOperator type for ansatz.

* Code refactoring.

* Validation of gradient types added.

* Added support for time-dep Hamiltonians.

* Made initial parameters required.

* Re-added optional estimator for aux ops.

* Added comments; improved exception handling.

* Added supports_observables method.

* fix import cycles

* fix docstring titles

* change imports in unit tests

* fix docs

* Removed dead code.

* Fixed bug with binding time parameter.

* Added time-dependent VarQRTE unit test.

* Updated tests after main marge.

* Added time-dependent VarQITE unit test.

* add test for time dep hamiltonian

* Add varqte_result, update varqite tests

* Update var_qrte test

* Fix lint

* Fix test

* Remove opflow dependencies

* Fix lint

* Remove redundant typehints

* Add parameters, times, observables to result

* Fix lint

* Unwrap PauliSumOp in VarQTE

* Fix tests

* Replace QFI with QGT

* Update copyright

* Update tests with QGT

* Update imports

* Update error message

* Address cyclic import

* Fix black

* Fix cyclic imports

* Fix linebreak

* Update qiskit/algorithms/time_evolvers/variational/var_qte.py

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Update qiskit/algorithms/time_evolvers/variational/__init__.py

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Apply suggestions documentation

* Update qiskit/algorithms/time_evolvers/variational/solvers/ode/ode_function_factory.py

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

* Update qiskit/algorithms/time_evolvers/time_evolution_result.py

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

* Update qiskit/algorithms/time_evolvers/variational/var_qte.py

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

* Update qiskit/algorithms/time_evolvers/variational/variational_principles/imaginary_mc_lachlan_principle.py

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

* Update qiskit/algorithms/time_evolvers/variational/variational_principles/real_mc_lachlan_principle.py

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

* Apply review comments

* Update qiskit/algorithms/time_evolvers/variational/solvers/var_qte_linear_solver.py

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Update qiskit/algorithms/time_evolvers/variational/solvers/var_qte_linear_solver.py

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Apply review Julien

* Update qiskit/algorithms/time_evolvers/variational/solvers/var_qte_linear_solver.py

* Update qiskit/algorithms/time_evolvers/variational/solvers/var_qte_linear_solver.py

* Update qiskit/algorithms/time_evolvers/variational/solvers/var_qte_linear_solver.py

* Update qiskit/algorithms/time_evolvers/variational/variational_principles/imaginary_mc_lachlan_principle.py

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

* Update qiskit/algorithms/time_evolvers/variational/variational_principles/real_mc_lachlan_principle.py

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

* Update qiskit/algorithms/time_evolvers/variational/solvers/var_qte_linear_solver.py

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

* Update qiskit/algorithms/time_evolvers/variational/var_qite.py

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

* Apply latest suggestions

* Change typehint

* Fix lint

* Fix unit test

Co-authored-by: a-matsuo <MATSUOA@jp.ibm.com>
Co-authored-by: Manoel Marques <Manoel.Marques@ibm.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Co-authored-by: a-matsuo <47442626+a-matsuo@users.noreply.github.com>
Co-authored-by: Julien Gacon <gaconju@gmail.com>
Co-authored-by: Manoel Marques <manoelmrqs@gmail.com>
Co-authored-by: ElePT <57907331+ElePT@users.noreply.github.com>
Co-authored-by: ElePT <epenatap@gmail.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: Added Add an "Added" entry in the GitHub Release changelog.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rewrite VarQTE with primitives.

9 participants