Source code for hqs_spin_mapper.struqture_interface

"""Interface between the hqs_spin_mapper and struqture_py v1.0 package."""

# Copyright © 2021-2024 HQS Quantum Simulations GmbH. All Rights Reserved.
import sys
import textwrap
from hqs_spin_mapper.spin_mapper_protocols import Supports_SW_Transformation

try:
    from lattice_functions.Fermions import (
        rewrite_as_fundamental_operators,
        separate_into_lists,
    )
except ImportError as e:
    if "mkl" in str(e):
        print(
            textwrap.dedent(
                """\
                ===== ERROR: Missing MKL =====

                The Intel Math Kernel Library (MKL) could not be found. Please ensure
                that the MKL is installed properly.

                When using HQStage, run

                   hqstage envs install-mkl

                to install the MKL. In other cases, please consult the manual."""
            ),
            file=sys.stderr,
        )
        sys.exit(1)
    else:
        raise

from typing import Dict
from struqture_py import fermions, spins, mixed_systems

__all__ = [
    "get_struqture_description",
]


[docs] def get_struqture_description( transformable_system: Supports_SW_Transformation, ) -> mixed_systems.MixedSystem: r"""Return the struqture-1.0 description of the transformed Hamiltonian. Args: transformable_system (Supports_SW_Transformation): Object containing the transformed Hamiltonian Returns: mixed_systems.MixedSystem: Struqture representation of the Hamiltonian """ spin_indices = transformable_system.spin_indices bath_indices = set(range(transformable_system.system_size)) - spin_indices struqture_repr = mixed_systems.MixedPlusMinusOperator(1, 0, 1) adjusted_spin_indices: Dict[int, int] = dict((x, n) for n, x in enumerate(spin_indices)) adjusted_bath_indices: Dict[int, int] = dict((x, n) for n, x in enumerate(bath_indices)) # Performance is the reason for this sequence of function calls # Before export to struqture, we need to guarantee that only spins operators act on dedicated # spin sites. for term in transformable_system.transformed_hamiltonian.expand(False).convert_to_spin_model( transformable_system.spin_indices ): expanded_hamiltonian = rewrite_as_fundamental_operators( term.get_list(), term.get_value(), False, True ) for term in expanded_hamiltonian: operators = term.get_list() sign, spins_ops_x, spins_ops_id, creators, annihilators = separate_into_lists( operators, adjusted_spin_indices, adjusted_bath_indices ) # Quick Fix with prefactor prefactor = 1.0 spin_product = spins.PlusMinusProduct() for x, op_id in zip(spins_ops_x, spins_ops_id): if op_id == "Z": prefactor *= 0.5 spin_product = spin_product.set_pauli(x, op_id) fermion_product = fermions.FermionProduct(creators, annihilators) mp = mixed_systems.MixedPlusMinusProduct([spin_product], [], [fermion_product]) struqture_repr.add_operator_product(mp, (prefactor * sign * term.get_value())) return struqture_repr.to_mixed_system([None], [], [None])