Skip to content
Merged
Show file tree
Hide file tree
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.
Aug 30, 2022
d71f50b
Added evolvers problems and interfaces to time_evolvers package.
Aug 30, 2022
7a7c82b
Mostly updated trotter_qrte.py to use primitives.
Aug 30, 2022
54afe25
Added observables_evaluator.py that uses primitives.
Sep 1, 2022
d2b67d9
Added observables_evaluator.py that uses primitives.
Sep 1, 2022
c1276d2
Updated trotter_qrte.py to use primitives.
Sep 1, 2022
e06c6c2
Updated imports
Sep 1, 2022
5a9bebe
Updated typehints and limited use of opflow.
Sep 5, 2022
356deae
Updated typehints and limited use of opflow.
Sep 5, 2022
14ba35d
Removed files out of scope for this PR.
Sep 5, 2022
4cf6f61
Added annotations import.
Sep 5, 2022
ebedff2
Added trotter_qrte.py with unit tests.
Sep 5, 2022
062162e
Merge branch 'evolution-framework-primitives' into trotter-qrte-primi…
Sep 5, 2022
a4a51cc
Refactored trotter_qrte.py.
Sep 5, 2022
39c9735
Added observables_evaluator.py with primitives.
Sep 5, 2022
99adde7
Added ListOrDict support to observables_evaluator.py.
Sep 5, 2022
9e0c0d0
Merge branch 'observable-eval-primitives' into trotter-qrte-primitives
Sep 5, 2022
221ee29
Updated trotter_qrte.py unit tests; code refactoring.
Sep 5, 2022
6462bb0
Included CR suggestions.
Sep 6, 2022
604944c
Applied some CR comments.
Sep 6, 2022
d287c2f
Applied some CR comments.
Sep 6, 2022
23821e0
Added reno.
Sep 7, 2022
80a2e2b
Added reno.
Sep 7, 2022
a2960b2
Accepting Statevector.
Sep 7, 2022
9b5f50a
Added attributes docs.
Sep 7, 2022
4c40e00
Support for 0 operator.
Sep 7, 2022
f9d2fce
Merge branch 'evolution-framework-primitives' into trotter-qrte-primi…
Sep 8, 2022
5f4896a
Merge branch 'observable-eval-primitives' into trotter-qrte-primitives
Sep 8, 2022
183563b
Code refactoring.
Sep 8, 2022
90bed9e
Updated init.
Sep 8, 2022
107dc2b
Merge branch 'main' into evolution-framework-primitives
manoelmarques Sep 8, 2022
2bcf07b
Add pending deprecation
manoelmarques Sep 8, 2022
4adfd1b
Merge branch 'main' into observable-eval-primitives
manoelmarques Sep 8, 2022
387ce78
Add pending deprecation for evolvers
manoelmarques Sep 8, 2022
f5c3eaf
Code refactoring.
Sep 8, 2022
65a2cab
Merge branch 'main' into observable-eval-primitives
Sep 8, 2022
a57b073
Merge remote-tracking branch 'origin/observable-eval-primitives' into…
Sep 8, 2022
553714c
Code refactoring.
Sep 8, 2022
a7e2f60
Renamed classes and linked to algorithms init.
Sep 8, 2022
34c2deb
Merge remote-tracking branch 'origin/evolution-framework-primitives' …
Sep 8, 2022
5c87255
fix docstring
manoelmarques Sep 8, 2022
d03bddf
Improved reno.
Sep 9, 2022
a656db0
Merge branch 'main' into evolution-framework-primitives
Sep 9, 2022
fc842a9
Merge branch 'main' into observable-eval-primitives
Sep 9, 2022
92d6515
Improved reno.
Sep 9, 2022
9ba37ed
Returning variances and shots.
Sep 9, 2022
7680019
Code refactoring.
Sep 9, 2022
58a76d6
Merge branch 'main' into trotter-qrte-primitives
Sep 9, 2022
b2fe7c8
Merge branch 'observable-eval-primitives' into trotter-qrte-primitives
Sep 9, 2022
85263b8
Merge branch 'evolution-framework-primitives' into trotter-qrte-primi…
Sep 9, 2022
5cd8e19
Updated classes and applied CR suggestions.
Sep 9, 2022
e18ba90
Added reno.
Sep 9, 2022
20940d5
Reduced use of opflow in tests.
Sep 9, 2022
1dce473
Black fix.
Sep 9, 2022
24aa2bf
Unit test fix.
Sep 9, 2022
00d9d11
Reduced use of opflow.
Sep 9, 2022
4cceb01
Merge branch 'observable-eval-primitives' into trotter-qrte-primitives
Sep 9, 2022
661b631
Merge branch 'evolution-framework-primitives' into trotter-qrte-primi…
Sep 9, 2022
44b7959
Handle empty inputs gracefully.
Sep 9, 2022
12d9a68
Merge branch 'main' into observable-eval-primitives
Sep 9, 2022
d3c601e
Applied CR comments.
Sep 11, 2022
773b124
Applied CR comments.
Sep 11, 2022
6a968f3
Merge remote-tracking branch 'origin/observable-eval-primitives' into…
Sep 11, 2022
2610736
Merge branch 'evolution-framework-primitives' into trotter-qrte-primi…
Sep 11, 2022
d8154ed
Merge branch 'observable-eval-primitives' into trotter-qrte-primitives
Sep 11, 2022
3d706a7
Updated method names.
Sep 12, 2022
d248e44
Applied CR comments.
Sep 15, 2022
a7e615f
Merge branch 'main' into observable-eval-primitives
Sep 15, 2022
fad2caa
Eliminated cyclic import.
Sep 15, 2022
b27ed9b
Merge branch 'main' into trotter-qrte-primitives
Sep 15, 2022
68069f5
Merge branch 'observable-eval-primitives' into trotter-qrte-primitives
Sep 15, 2022
46512f1
Drafted reduced use of opflow.
Sep 15, 2022
4307172
Drafted reduced use of opflow.
Sep 16, 2022
1127170
Removed use of opflow.
Sep 16, 2022
b0471c3
Code refactoring.
Sep 19, 2022
45b9b65
Merge branch 'main' into trotter-qrte-primitives
Sep 19, 2022
8563b2d
Addressed CR comments.
Sep 20, 2022
0f43b02
Added more tests for error paths.
Sep 20, 2022
28f2645
Merge branch 'main' into trotter-qrte-primitives
Sep 20, 2022
7de440d
Added Trotter section in algorithms init.
Sep 20, 2022
e7032ff
Changed init.
Sep 21, 2022
d09d21e
Merge branch 'main' into trotter-qrte-primitives
Sep 21, 2022
f78ad60
Apply suggestions from code review
Sep 22, 2022
8f7e739
Co-located TrotterQRTE with time evolvers in docs.
Sep 22, 2022
3f881e9
Merge branch 'main' into trotter-qrte-primitives
Sep 22, 2022
0fe14e1
Merge branch 'main' into trotter-qrte-primitives
mergify[bot] Sep 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions qiskit/algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@
TimeEvolutionProblem


Trotterization-based Quantum Real Time Evolution
------------------------------------------------

Package for primitives-enabled Trotterization-based quantum time evolution algorithm - TrotterQRTE.

.. autosummary::
:toctree: ../stubs/

time_evolvers.trotterization


Factorizers
-----------

Expand Down Expand Up @@ -231,6 +242,7 @@

state_fidelities


Exceptions
----------

Expand All @@ -251,6 +263,7 @@
eval_observables
estimate_observables


Utility classes
---------------

Expand Down
29 changes: 29 additions & 0 deletions qiskit/algorithms/time_evolvers/trotterization/__init__.py
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"]
Comment thread
dlasecki marked this conversation as resolved.
174 changes: 174 additions & 0 deletions qiskit/algorithms/time_evolvers/trotterization/trotter_qrte.py
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 releasenotes/notes/trotter-qrte-primitives-8b3e495738b57fc3.yaml
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.
Loading