-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Implemented TrotterQRTE algorithm with primitives. #8706
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
Merged
Merged
Changes from all commits
Commits
Show all changes
86 commits
Select commit
Hold shift + click to select a range
e3233b7
Implemented observables_evaluator.py with primitives.
d71f50b
Added evolvers problems and interfaces to time_evolvers package.
7a7c82b
Mostly updated trotter_qrte.py to use primitives.
54afe25
Added observables_evaluator.py that uses primitives.
d2b67d9
Added observables_evaluator.py that uses primitives.
c1276d2
Updated trotter_qrte.py to use primitives.
e06c6c2
Updated imports
5a9bebe
Updated typehints and limited use of opflow.
356deae
Updated typehints and limited use of opflow.
14ba35d
Removed files out of scope for this PR.
4cf6f61
Added annotations import.
ebedff2
Added trotter_qrte.py with unit tests.
062162e
Merge branch 'evolution-framework-primitives' into trotter-qrte-primi…
a4a51cc
Refactored trotter_qrte.py.
39c9735
Added observables_evaluator.py with primitives.
99adde7
Added ListOrDict support to observables_evaluator.py.
9e0c0d0
Merge branch 'observable-eval-primitives' into trotter-qrte-primitives
221ee29
Updated trotter_qrte.py unit tests; code refactoring.
6462bb0
Included CR suggestions.
604944c
Applied some CR comments.
d287c2f
Applied some CR comments.
23821e0
Added reno.
80a2e2b
Added reno.
a2960b2
Accepting Statevector.
9b5f50a
Added attributes docs.
4c40e00
Support for 0 operator.
f9d2fce
Merge branch 'evolution-framework-primitives' into trotter-qrte-primi…
5f4896a
Merge branch 'observable-eval-primitives' into trotter-qrte-primitives
183563b
Code refactoring.
90bed9e
Updated init.
107dc2b
Merge branch 'main' into evolution-framework-primitives
manoelmarques 2bcf07b
Add pending deprecation
manoelmarques 4adfd1b
Merge branch 'main' into observable-eval-primitives
manoelmarques 387ce78
Add pending deprecation for evolvers
manoelmarques f5c3eaf
Code refactoring.
65a2cab
Merge branch 'main' into observable-eval-primitives
a57b073
Merge remote-tracking branch 'origin/observable-eval-primitives' into…
553714c
Code refactoring.
a7e2f60
Renamed classes and linked to algorithms init.
34c2deb
Merge remote-tracking branch 'origin/evolution-framework-primitives' …
5c87255
fix docstring
manoelmarques d03bddf
Improved reno.
a656db0
Merge branch 'main' into evolution-framework-primitives
fc842a9
Merge branch 'main' into observable-eval-primitives
92d6515
Improved reno.
9ba37ed
Returning variances and shots.
7680019
Code refactoring.
58a76d6
Merge branch 'main' into trotter-qrte-primitives
b2fe7c8
Merge branch 'observable-eval-primitives' into trotter-qrte-primitives
85263b8
Merge branch 'evolution-framework-primitives' into trotter-qrte-primi…
5cd8e19
Updated classes and applied CR suggestions.
e18ba90
Added reno.
20940d5
Reduced use of opflow in tests.
1dce473
Black fix.
24aa2bf
Unit test fix.
00d9d11
Reduced use of opflow.
4cceb01
Merge branch 'observable-eval-primitives' into trotter-qrte-primitives
661b631
Merge branch 'evolution-framework-primitives' into trotter-qrte-primi…
44b7959
Handle empty inputs gracefully.
12d9a68
Merge branch 'main' into observable-eval-primitives
d3c601e
Applied CR comments.
773b124
Applied CR comments.
6a968f3
Merge remote-tracking branch 'origin/observable-eval-primitives' into…
2610736
Merge branch 'evolution-framework-primitives' into trotter-qrte-primi…
d8154ed
Merge branch 'observable-eval-primitives' into trotter-qrte-primitives
3d706a7
Updated method names.
d248e44
Applied CR comments.
a7e615f
Merge branch 'main' into observable-eval-primitives
fad2caa
Eliminated cyclic import.
b27ed9b
Merge branch 'main' into trotter-qrte-primitives
68069f5
Merge branch 'observable-eval-primitives' into trotter-qrte-primitives
46512f1
Drafted reduced use of opflow.
4307172
Drafted reduced use of opflow.
1127170
Removed use of opflow.
b0471c3
Code refactoring.
45b9b65
Merge branch 'main' into trotter-qrte-primitives
8563b2d
Addressed CR comments.
0f43b02
Added more tests for error paths.
28f2645
Merge branch 'main' into trotter-qrte-primitives
7de440d
Added Trotter section in algorithms init.
e7032ff
Changed init.
d09d21e
Merge branch 'main' into trotter-qrte-primitives
f78ad60
Apply suggestions from code review
8f7e739
Co-located TrotterQRTE with time evolvers in docs.
3f881e9
Merge branch 'main' into trotter-qrte-primitives
0fe14e1
Merge branch 'main' into trotter-qrte-primitives
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 hidden or 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
29 changes: 29 additions & 0 deletions
29
qiskit/algorithms/time_evolvers/trotterization/__init__.py
This file contains hidden or 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,29 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2022. | ||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
| """This package contains Trotterization-based Quantum Real Time Evolution algorithm. | ||
| It is compliant with the new Quantum Time Evolution Framework and makes use of | ||
| :class:`qiskit.synthesis.evolution.ProductFormula` and | ||
| :class:`~qiskit.circuit.library.PauliEvolutionGate` implementations. | ||
|
|
||
| Trotterization-based Quantum Real Time Evolution | ||
| ------------------------------------------------ | ||
|
|
||
| .. autosummary:: | ||
| :toctree: ../stubs/ | ||
| :nosignatures: | ||
|
|
||
| TrotterQRTE | ||
| """ | ||
|
|
||
| from qiskit.algorithms.time_evolvers.trotterization.trotter_qrte import TrotterQRTE | ||
|
|
||
| __all__ = ["TrotterQRTE"] | ||
174 changes: 174 additions & 0 deletions
174
qiskit/algorithms/time_evolvers/trotterization/trotter_qrte.py
This file contains hidden or 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,174 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2021, 2022. | ||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
|
|
||
| """An algorithm to implement a Trotterization real time-evolution.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from qiskit import QuantumCircuit | ||
| from qiskit.algorithms.time_evolvers.time_evolution_problem import TimeEvolutionProblem | ||
| from qiskit.algorithms.time_evolvers.time_evolution_result import TimeEvolutionResult | ||
| from qiskit.algorithms.time_evolvers.real_time_evolver import RealTimeEvolver | ||
| from qiskit.algorithms.observables_evaluator import estimate_observables | ||
| from qiskit.opflow import PauliSumOp | ||
| from qiskit.circuit.library import PauliEvolutionGate | ||
| from qiskit.primitives import BaseEstimator | ||
| from qiskit.quantum_info import Pauli | ||
| from qiskit.synthesis import ProductFormula, LieTrotter | ||
|
|
||
|
|
||
| class TrotterQRTE(RealTimeEvolver): | ||
| """Quantum Real Time Evolution using Trotterization. | ||
| Type of Trotterization is defined by a ``ProductFormula`` provided. | ||
|
|
||
| Examples: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from qiskit.opflow import PauliSumOp | ||
| from qiskit.quantum_info import Pauli, SparsePauliOp | ||
| from qiskit import QuantumCircuit | ||
| from qiskit.algorithms import TimeEvolutionProblem | ||
| from qiskit.algorithms.time_evolvers import TrotterQRTE | ||
| from qiskit.primitives import Estimator | ||
|
|
||
| operator = PauliSumOp(SparsePauliOp([Pauli("X"), Pauli("Z")])) | ||
| initial_state = QuantumCircuit(1) | ||
| time = 1 | ||
| evolution_problem = TimeEvolutionProblem(operator, time, initial_state) | ||
| # LieTrotter with 1 rep | ||
| estimator = Estimator() | ||
| trotter_qrte = TrotterQRTE(estimator=estimator) | ||
| evolved_state = trotter_qrte.evolve(evolution_problem).evolved_state | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| product_formula: ProductFormula | None = None, | ||
| estimator: BaseEstimator | None = None, | ||
| ) -> None: | ||
| """ | ||
| Args: | ||
| product_formula: A Lie-Trotter-Suzuki product formula. If ``None`` provided, the | ||
| Lie-Trotter first order product formula with a single repetition is used. | ||
| estimator: An estimator primitive used for calculating expectation values of | ||
| ``TimeEvolutionProblem.aux_operators``. | ||
| """ | ||
|
|
||
| self.product_formula = product_formula | ||
| self.estimator = estimator | ||
|
|
||
| @property | ||
| def product_formula(self) -> ProductFormula: | ||
| """Returns a product formula.""" | ||
| return self._product_formula | ||
|
|
||
| @product_formula.setter | ||
| def product_formula(self, product_formula: ProductFormula | None): | ||
| """Sets a product formula. If ``None`` provided, sets the Lie-Trotter first order product | ||
| formula with a single repetition.""" | ||
| if product_formula is None: | ||
| product_formula = LieTrotter() | ||
| self._product_formula = product_formula | ||
|
|
||
| @property | ||
| def estimator(self) -> BaseEstimator | None: | ||
| """ | ||
| Returns an estimator. | ||
| """ | ||
| return self._estimator | ||
|
|
||
| @estimator.setter | ||
| def estimator(self, estimator: BaseEstimator) -> None: | ||
| """ | ||
| Sets an estimator. | ||
| """ | ||
| self._estimator = estimator | ||
|
|
||
| @classmethod | ||
| def supports_aux_operators(cls) -> bool: | ||
| """ | ||
| Whether computing the expectation value of auxiliary operators is supported. | ||
|
|
||
| Returns: | ||
| ``True`` if ``aux_operators`` expectations in the ``TimeEvolutionProblem`` can be | ||
| evaluated, ``False`` otherwise. | ||
| """ | ||
| return True | ||
|
|
||
| def evolve(self, evolution_problem: TimeEvolutionProblem) -> TimeEvolutionResult: | ||
| """ | ||
| Evolves a quantum state for a given time using the Trotterization method | ||
| based on a product formula provided. The result is provided in the form of a quantum | ||
| circuit. If auxiliary operators are included in the ``evolution_problem``, they are | ||
| evaluated on an evolved state using an estimator primitive provided. | ||
|
|
||
| .. note:: | ||
| Time-dependent Hamiltonians are not supported. | ||
|
|
||
| Args: | ||
| evolution_problem: Instance defining evolution problem. For the included Hamiltonian, | ||
| ``Pauli`` or ``PauliSumOp`` are supported by TrotterQRTE. | ||
|
|
||
| Returns: | ||
| Evolution result that includes an evolved state as a quantum circuit and, optionally, | ||
| auxiliary operators evaluated for a resulting state on an estimator primitive. | ||
|
|
||
| Raises: | ||
| ValueError: If ``t_param`` is not set to ``None`` in the ``TimeEvolutionProblem`` | ||
| (feature not currently supported). | ||
| ValueError: If ``aux_operators`` provided in the time evolution problem but no estimator | ||
| provided to the algorithm. | ||
| ValueError: If the ``initial_state`` is not provided in the ``TimeEvolutionProblem``. | ||
| ValueError: If an unsupported Hamiltonian type is provided. | ||
| """ | ||
| evolution_problem.validate_params() | ||
| if evolution_problem.t_param is not None: | ||
| raise ValueError( | ||
| "TrotterQRTE does not accept a time dependent Hamiltonian," | ||
| "``t_param`` from the ``TimeEvolutionProblem`` should be set to ``None``." | ||
| ) | ||
|
|
||
| if evolution_problem.aux_operators is not None and self.estimator is None: | ||
| raise ValueError( | ||
| "The time evolution problem contained ``aux_operators`` but no estimator was " | ||
| "provided. The algorithm continues without calculating these quantities. " | ||
| ) | ||
| hamiltonian = evolution_problem.hamiltonian | ||
| if not isinstance(hamiltonian, (Pauli, PauliSumOp)): | ||
| raise ValueError( | ||
| f"TrotterQRTE only accepts Pauli | PauliSumOp, {type(hamiltonian)} provided." | ||
| ) | ||
| # the evolution gate | ||
| evolution_gate = PauliEvolutionGate( | ||
| hamiltonian, evolution_problem.time, synthesis=self.product_formula | ||
| ) | ||
|
|
||
| if evolution_problem.initial_state is not None: | ||
| initial_state = evolution_problem.initial_state | ||
| evolved_state = QuantumCircuit(initial_state.num_qubits) | ||
| evolved_state.append(initial_state, evolved_state.qubits) | ||
| evolved_state.append(evolution_gate, evolved_state.qubits) | ||
|
|
||
| else: | ||
| raise ValueError("``initial_state`` must be provided in the ``TimeEvolutionProblem``.") | ||
|
|
||
| evaluated_aux_ops = None | ||
| if evolution_problem.aux_operators is not None: | ||
| evaluated_aux_ops = estimate_observables( | ||
| self.estimator, | ||
| evolved_state, | ||
| evolution_problem.aux_operators, | ||
| evolution_problem.truncation_threshold, | ||
| ) | ||
|
|
||
| return TimeEvolutionResult(evolved_state, evaluated_aux_ops) |
12 changes: 12 additions & 0 deletions
12
releasenotes/notes/trotter-qrte-primitives-8b3e495738b57fc3.yaml
This file contains hidden or 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,12 @@ | ||
| --- | ||
| features: | ||
| - | | ||
| Added :class:`qiskit.algorithms.time_evolvers.trotterization.TrotterQRTE` with | ||
| :class:`qiskit.primitives.BaseEstimator` as ``init`` parameter. It replaces | ||
| :class:`qiskit.algorithms.TrotterQRTE`. | ||
| deprecations: | ||
| - | | ||
| Using :class:`qiskit.algorithms.TrotterQRTE` will now issue a | ||
| ``PendingDeprecationWarning``. This algorithm will be deprecated in a future release and | ||
| subsequently removed after that. This is being replaced by the new | ||
| :class:`qiskit.algorithms.time_evolvers.trotterization.TrotterQRTE` primitive-enabled class. |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.