Skip to content

Commit

Permalink
split out trotter utils
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsung committed Jul 4, 2024
1 parent 47368d3 commit 56b454a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 38 deletions.
2 changes: 1 addition & 1 deletion python/ffsim/trotter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

"""Hamiltonian simulation via Trotter-Suzuki formulas."""

from ffsim.trotter.double_factorized import simulate_trotter_double_factorized
from ffsim.trotter.qdrift import simulate_qdrift_double_factorized
from ffsim.trotter.trotter import simulate_trotter_double_factorized

__all__ = [
"simulate_qdrift_double_factorized",
Expand Down
49 changes: 49 additions & 0 deletions python/ffsim/trotter/_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# (C) Copyright IBM 2023.
#
# 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.

"""Trotter simulation utilities."""

from __future__ import annotations

from collections.abc import Iterator


def simulate_trotter_step_iterator(
n_terms: int, time: float, order: int = 0
) -> Iterator[tuple[int, float]]:
if order == 0:
for i in range(n_terms):
yield i, time
else:
yield from simulate_trotter_step_iterator_symmetric(n_terms, time, order)


def simulate_trotter_step_iterator_symmetric(
n_terms: int, time: float, order: int
) -> Iterator[tuple[int, float]]:
if order == 1:
for i in range(n_terms - 1):
yield i, time / 2
yield n_terms - 1, time
for i in reversed(range(n_terms - 1)):
yield i, time / 2
else:
split_time = time / (4 - 4 ** (1 / (2 * order - 1)))
for _ in range(2):
yield from simulate_trotter_step_iterator_symmetric(
n_terms, split_time, order - 1
)
yield from simulate_trotter_step_iterator_symmetric(
n_terms, time - 4 * split_time, order - 1
)
for _ in range(2):
yield from simulate_trotter_step_iterator_symmetric(
n_terms, split_time, order - 1
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

from __future__ import annotations
"""Trotter simulation for double-factorized Hamiltonian."""

from collections.abc import Iterator
from __future__ import annotations

import numpy as np
import scipy.linalg
Expand All @@ -21,40 +21,7 @@
apply_orbital_rotation,
)
from ffsim.hamiltonians import DoubleFactorizedHamiltonian


def _simulate_trotter_step_iterator(
n_terms: int, time: float, order: int = 0
) -> Iterator[tuple[int, float]]:
if order == 0:
for i in range(n_terms):
yield i, time
else:
yield from _simulate_trotter_step_iterator_symmetric(n_terms, time, order)


def _simulate_trotter_step_iterator_symmetric(
n_terms: int, time: float, order: int
) -> Iterator[tuple[int, float]]:
if order == 1:
for i in range(n_terms - 1):
yield i, time / 2
yield n_terms - 1, time
for i in reversed(range(n_terms - 1)):
yield i, time / 2
else:
split_time = time / (4 - 4 ** (1 / (2 * order - 1)))
for _ in range(2):
yield from _simulate_trotter_step_iterator_symmetric(
n_terms, split_time, order - 1
)
yield from _simulate_trotter_step_iterator_symmetric(
n_terms, time - 4 * split_time, order - 1
)
for _ in range(2):
yield from _simulate_trotter_step_iterator_symmetric(
n_terms, split_time, order - 1
)
from ffsim.trotter._util import simulate_trotter_step_iterator


def simulate_trotter_double_factorized(
Expand Down Expand Up @@ -137,7 +104,7 @@ def _simulate_trotter_step_double_factorized(
order: int,
z_representation: bool,
) -> tuple[np.ndarray, np.ndarray]:
for term_index, time in _simulate_trotter_step_iterator(
for term_index, time in simulate_trotter_step_iterator(
1 + len(diag_coulomb_mats), time, order
):
if term_index == 0:
Expand Down

0 comments on commit 56b454a

Please sign in to comment.