Skip to content

Commit

Permalink
added insert_barrier to TrotterQRTE (qiskit-community#128)
Browse files Browse the repository at this point in the history
Co-authored-by: Elena Peña Tapia <[email protected]>
Co-authored-by: Steve Wood <[email protected]>
  • Loading branch information
3 people authored Jan 26, 2024
1 parent b6894e9 commit eb8f8fc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
15 changes: 12 additions & 3 deletions qiskit_algorithms/time_evolvers/trotterization/trotter_qrte.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of a Qiskit project.
#
# (C) Copyright IBM 2021, 2023.
# (C) Copyright IBM 2021, 2024.
#
# 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
Expand Down Expand Up @@ -58,6 +58,8 @@ def __init__(
product_formula: ProductFormula | None = None,
estimator: BaseEstimator | None = None,
num_timesteps: int = 1,
*,
insert_barriers: bool = False,
) -> None:
"""
Args:
Expand All @@ -67,15 +69,18 @@ def __init__(
``num_timesteps`` and an evaluation of :attr:`.TimeEvolutionProblem.aux_operators`
at every time-step. If ``reps`` is larger than 1, the true number of time-steps will
be ``num_timesteps * reps``.
num_timesteps: The number of time-steps the full evolution time is divided into
(repetitions of ``product_formula``)
estimator: An estimator primitive used for calculating expectation values of
``TimeEvolutionProblem.aux_operators``.
num_timesteps: The number of time-steps the full evolution time is divided into
(repetitions of ``product_formula``).
insert_barriers: If True, insert a barrier after the initial state and after each Trotter
step.
"""

self.product_formula = product_formula
self.num_timesteps = num_timesteps
self.estimator = estimator
self._insert_barriers = insert_barriers

@property
def product_formula(self) -> ProductFormula:
Expand Down Expand Up @@ -192,6 +197,8 @@ def evolve(self, evolution_problem: TimeEvolutionProblem) -> TimeEvolutionResult

evolved_state = QuantumCircuit(initial_state.num_qubits)
evolved_state.append(initial_state, evolved_state.qubits)
if self._insert_barriers:
evolved_state.barrier()

if evolution_problem.aux_operators is not None:
observables = []
Expand Down Expand Up @@ -225,6 +232,8 @@ def evolve(self, evolution_problem: TimeEvolutionProblem) -> TimeEvolutionResult
synthesis=self.product_formula,
)
evolved_state.append(single_step_evolution_gate, evolved_state.qubits)
if self._insert_barriers:
evolved_state.barrier()

if evolution_problem.aux_operators is not None:
observables.append(
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/add-barriers-trotter-248cccabbf9deed1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
features:
- |
Added input argument `insert_barriers` to `TrotterQRTE` to add barriers between Trotter layers.
30 changes: 29 additions & 1 deletion test/time_evolvers/test_trotter_qrte.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of a Qiskit project.
#
# (C) Copyright IBM 2021, 2023.
# (C) Copyright IBM 2021, 2024.
#
# 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
Expand Down Expand Up @@ -211,6 +211,34 @@ def test_trotter_qrte_trotter_hamiltonian_errors(self, operator, initial_state):
"""Test TrotterQRTE with raising errors for evolution problem content."""
self._run_error_test(initial_state, operator, None, None, None, None)

@data(True, False)
def test_barriers(self, insert_barrier):
"""Test TrotterQRTE to insert barriers correctly."""
initial_state = QuantumCircuit(1)
initial_state.x(0)

expected_circuit = QuantumCircuit(1)
expected_circuit.append(initial_state, expected_circuit.qubits)
if insert_barrier:
expected_circuit.barrier()
expected_circuit.rx(1, 0)
expected_circuit.ry(1, 0)
if insert_barrier:
expected_circuit.barrier()
expected_circuit.rx(1, 0)
expected_circuit.ry(1, 0)
if insert_barrier:
expected_circuit.barrier()

operator = SparsePauliOp(["X", "Y"])
evolution_problem = TimeEvolutionProblem(operator, 1, initial_state)
trotter_qrte = TrotterQRTE(num_timesteps=2, insert_barriers=insert_barrier)
evolution_result = trotter_qrte.evolve(evolution_problem)

self.assertEqual(
expected_circuit.decompose(reps=3), evolution_result.evolved_state.decompose(reps=5)
)

@staticmethod
def _run_error_test(initial_state, operator, aux_ops, estimator, t_param, param_value_dict):
time = 1
Expand Down

0 comments on commit eb8f8fc

Please sign in to comment.