Implements VarQTE algorithm with primitives.#8746
Conversation
… observable-eval-primitives
| def __init__( | ||
| self, | ||
| varqte_linear_solver: VarQTELinearSolver, | ||
| param_dict: dict[Parameter, float], |
There was a problem hiding this comment.
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.
| self.ode_function_type = ode_function_type | ||
| self.lse_solver = lse_solver |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Could we support Sequence rather than it needing to be the specific list type?
| def __init__( | ||
| self, | ||
| ansatz: QuantumCircuit, | ||
| initial_parameters: dict[Parameter, float] | list[float] | np.ndarray, |
There was a problem hiding this comment.
Similar comment on types dict & list or Mapping & Sequence?
| variational_principle: VariationalPrinciple | None = None, | ||
| estimator: BaseEstimator | None = None, |
There was a problem hiding this comment.
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
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. |
| qgt: BaseQGT | None = None, | ||
| gradient: BaseEstimatorGradient | None = None, |
There was a problem hiding this comment.
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.
…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>
|
Thank you @woodsp-ibm for your review. I have tried to implement all the changes you suggested, including using |
| 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. |
There was a problem hiding this comment.
As the error is around the type was the TypeError not more applicable to raise?
e4226ca to
d55e9d4
Compare
* 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>
* 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>

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: