Código fuente para qilisdk.functionals.analog_evolution

# Copyright 2025 Qilimanjaro Quantum Tech
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Callable, ClassVar, Iterator

from loguru import logger

from qilisdk.analog.schedule import Schedule
from qilisdk.core import Parameter
from qilisdk.core.parameterizable import Parameterizable
from qilisdk.core.qtensor import InitialState, QTensor
from qilisdk.functionals.functional import PrimitiveFunctional
from qilisdk.functionals.functional_result import FunctionalResult
from qilisdk.yaml import yaml


@yaml.register_class
[documentos] class AnalogEvolution(PrimitiveFunctional): """ Simulate the dynamics induced by a time-dependent Hamiltonian schedule. Example: .. code-block:: python from qilisdk.analog import Schedule, Z from qilisdk.core import ket from qilisdk.functionals import AnalogEvolution from qilisdk.readout import Readout h0 = Z(0) schedule = Schedule(hamiltonians={"h0": h0}, total_time=10.0) functional = AnalogEvolution(schedule, initial_state=ket(0)) result = backend.execute(functional, readout=Readout().with_state_tomography()) state = result.state # QTensor """
[documentos] result_type: ClassVar[type[FunctionalResult]] = FunctionalResult
def __init__( self, schedule: Schedule, initial_state: QTensor | InitialState, store_intermediate_results: bool = False, ) -> None: """ Args: schedule (Schedule): Annealing or control schedule describing the Hamiltonian evolution. initial_state (QTensor | InitialState): Quantum state used as the simulation starting point. If a symbolic state is provided, it will be resolved during execution. store_intermediate_results (bool, optional): Keep intermediate states if produced by the backend. Defaults to False. Raises: ValueError: if the number of qubits of the initial state doesn't match the number of qubits in the schedule. """ super().__init__() self._initial_state = initial_state
[documentos] self.schedule = schedule
[documentos] self.store_intermediate_results = store_intermediate_results
if isinstance(initial_state, QTensor) and initial_state.nqubits != schedule.nqubits: raise ValueError( f"The initial state provided acts on {initial_state.nqubits} qubits while the schedule acts on {schedule.nqubits} qubits" ) logger.debug( "[AnalogEvolution] Created AnalogEvolution over schedule with {} qubits (T={}, store_intermediate_results={})", schedule.nqubits, schedule.T, store_intermediate_results, ) @property
[documentos] def initial_state(self) -> QTensor | InitialState: """ The initial state of the simulation. Returns: QTensor | InitialState: The initial state. """ return self._initial_state
def _iter_parameter_children(self) -> Iterator[Parameterizable]: """Yield the schedule as the sole parameterizable child. Yields: Iterator[Parameterizable]: The underlying ``Schedule``. """ yield self.schedule
[documentos] def set_parameter_values( self, values: list[float], where: Callable[[Parameter], bool] | None = None, ) -> None: """ Assign parameter values by position and clear caches. Args: values (list[float]): New values ordered consistently with ``get_parameter_names()``. where (Callable[[Parameter], bool] | None): Optional predicate selecting parameters to update. """ logger.trace("[AnalogEvolution] Setting {} parameter values on AnalogEvolution", len(values)) self.schedule.set_parameter_values(values=values, where=where)
[documentos] def set_parameters(self, parameters: dict[str, int | float]) -> None: """ Assign parameter values by name and clear caches. Args: parameters (dict[str, int | float]): Mapping from parameter labels to numeric values. """ self.schedule.set_parameters(parameters)
[documentos] def set_parameter_bounds(self, ranges: dict[str, tuple[float, float]]) -> None: """ Update parameter bounds and clear caches. Args: ranges (dict[str, tuple[float, float]]): Bounds keyed by parameter label. """ self.schedule.set_parameter_bounds(ranges)
def __repr__(self) -> str: lines = [ f"{type(self).__qualname__}(", f" schedule={self.schedule!r},", f" initial_state={self.initial_state!r},", f" store_intermediate_results={self.store_intermediate_results!r},", ")", ] return "\n".join(lines)