Source code for qtealeaves.observables.timecorrelator
# This code is part of qtealeaves.
#
# 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.
"""
The time correlator measures the correlation ``<A_i(t=0) B_j(t)>`` during
a time evolution. The index `i` is fixed, while `j` is running over the
system size. It cannot be used for fermionic operators.
"""
from collections.abc import Iterator
from typing import Any, Self
import h5py
import numpy as np
from qtealeaves.tooling import QTeaLeavesError
from .tnobase import _TNObsBase
__all__ = ["TimeCorr"]
_AbstractTN = Any
[docs]
class TimeCorr(_TNObsBase):
"""Correlation ``<A_i(t=0) B_j(t)>``. Currently no measurable ansatz.
**Arguments**
name : str
Define a label under which we can finde the observable in
the result dictionary.
operator : list of two strings
Identifiers/strings for the operators to be measured. The
first operator is applied at `t=0`, the second at all `t`
during measurements of the time evolution.
site_idx : int
Specify on which site the first operator should be measured
on.
"""
_measurable_ansaetze = ()
def __init__(self, name: str, operators: list[str], site_idx: int) -> None:
super().__init__(name)
self.operators = [operators]
self.site_inds = [site_idx]
[docs]
@classmethod
def empty(cls) -> Self:
"""
Documentation see :func:`_TNObsBase.empty`.
"""
obj = cls("", [], -1)
obj.name = []
obj.operators = []
obj.site_inds = []
return obj
def __iadd__(self, other: Any) -> Self:
"""
Documentation see :func:`_TNObsBase.__iadd__`.
"""
if isinstance(other, TimeCorr):
self.name += other.name
self.operators += other.operators
self.site_inds += other.site_inds
else:
raise QTeaLeavesError(
f"__iadd__ not defined for types {type(self)} and {type(other)}."
)
return self
[docs]
def collect_operators(self) -> Iterator[tuple[str, str]]:
"""
Documentation see :func:`_TNObsBase.collect_operators`.
"""
for elem in self.operators:
yield (elem[0], "l")
yield (elem[1], "r")
[docs]
def write_results(
self, fh: h5py.File, state_ansatz: type[_AbstractTN], **kwargs: Any
) -> None:
"""
See :func:`_TNObsBase.write_results`.
"""
is_measured = self.check_measurable(state_ansatz)
fg = fh.create_group(str(self), track_order=True)
fg.attrs["is_measured"] = is_measured
if is_measured:
for name in self.name:
corr_mat = self.results_buffer[name]
if np.any(np.abs(np.imag(corr_mat)) > 1e-12):
# write as complex
fg.create_dataset(name, data=corr_mat)
else:
# write as real
fg.create_dataset(name, data=np.real(corr_mat))
self.results_buffer = {}