Skip to content
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

Guard against N_steps != 1 #51

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ max-args = 6
notice-rgx = """
# This code is a Qiskit project.
#
# \\(C\\) Copyright IBM 2024\\.
# \\(C\\) Copyright IBM \\d{4}((,\\s)\\d{4})*\\.
Copy link
Member Author

Choose a reason for hiding this comment

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

This is an ad-hoc fix. We should have a more general discussion on how to handle copyright headers in the Qiskit addons.

Here, I did what I also used to do back in the application modules (which is to ensure the year of the latest change of a file is present with at most 2 years being listed). However, this regex cannot enforce that pattern consistently. In the application modules we had a tool to check these headers. We could consider using something similar but rather than copying this tool everywhere I would rather do something similar to the extremal-python-dependencies tool and ship this separately (if we decide that this is needed).

#
# 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
14 changes: 11 additions & 3 deletions qiskit_addon_mpf/backends/tenpy_layers/evolver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is a Qiskit project.
#
# (C) Copyright IBM 2024.
# (C) Copyright IBM 2024, 2025.
#
# 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 @@ -66,13 +66,21 @@ def evolve(self, N_steps: int, dt: float) -> TruncationError:
"""Perform a single time step of TEBD.

Args:
N_steps: should always be ``1`` in this case. See
:external:class:`~tenpy.algorithms.tebd.TEBDEngine` for more details.
N_steps: should always be ``1`` for this time-evolver, otherwise an error will be raised
(see below).
dt: the time-step to use.

Returns:
The truncation error.

Raises:
RuntimeError: if ``N_steps`` is not equal to ``1``.
"""
if N_steps != 1:
raise RuntimeError(
"The LayerwiseEvolver only supports a single evolution step at a time!"
)

if dt is not None: # pragma: no branch
assert dt == self._U_param["delta_t"]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
features:
- |
Guards against unexpected behavior when ``N_steps != 1`` in
:meth:`qiskit_addon_mpf.backends.tenpy_layers.LayerwiseEvolver.evolve`.
39 changes: 39 additions & 0 deletions test/backends/tenpy_layers/test_evolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This code is a Qiskit project.
#
# (C) Copyright IBM 2024, 2025.
#
# 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.


import pytest
from qiskit.circuit import QuantumCircuit
from qiskit_addon_mpf.backends import HAS_TENPY

if HAS_TENPY:
from qiskit_addon_mpf.backends.tenpy_layers import LayerModel, LayerwiseEvolver
from qiskit_addon_mpf.backends.tenpy_tebd import MPOState


@pytest.mark.skipif(not HAS_TENPY, reason="Tenpy is required for these unittests")
class TestLayerwiseEvolver:
def test_N_steps_guard(self):
L = 6

qc = QuantumCircuit(L)
for i in range(0, L - 1, 2):
qc.rzz(1.0, i, i + 1)

model = LayerModel.from_quantum_circuit(qc)

common_state = MPOState.initialize_from_lattice(model.lat)

algo = LayerwiseEvolver(common_state, [model], {})

with pytest.raises(RuntimeError):
algo.evolve(2, 0.1)
Loading