Source code for pennylane.templates.subroutines.time_evolution.commuting_evolution

# Copyright 2018-2021 Xanadu Quantum Technologies Inc.

# 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.
r"""
Contains the CommutingEvolution template.
"""

import copy

from pennylane import capture, math, ops
from pennylane.core.apply import apply
from pennylane.core.operator import Operation, Operator1
from pennylane.core.queuing import QueuingManager
from pennylane.decomposition import add_decomps, register_resources, resource_rep
from pennylane.ops.op_math.linear_combination import Hamiltonian
from pennylane.pauli import PauliWord
from pennylane.wires import Wires, WiresLike

from .approx_time_evolution import ApproxTimeEvolution


[docs] class CommutingEvolution(Operation): r"""Applies the time-evolution operator for a Hamiltonian expressed as a linear combination of mutually commuting Pauli words. A commuting Hamiltonian is of the form .. math:: H \ = \ \displaystyle\sum_{j} c_j P_j, where :math:`P_j` are mutually commutative Pauli words and :math:`c_j` are real coefficients. The time-evolution under a commuting Hamiltonian is given by a unitary of the form .. math:: U(t) \ = \ e^{-i H t} \ = \exp(-i t \displaystyle\sum_j c_j P_j) = \displaystyle\prod_j \exp(-i t c_j P_j). If the Hamiltonian has a small number of unique eigenvalues, partial derivatives of observable expectation values, i.e. .. math:: \langle 0 | W(t)^\dagger O W(t) | 0 \rangle, where :math:`W(t) = V U(t) Y` for some :math:`V` and :math:`Y`, taken with respect to :math:`t` may be efficiently computed through generalized parameter shift rules. When initialized, this template will automatically compute the parameter-shift rule if given the Hamiltonian's eigenvalue frequencies, i.e., the unique positive differences between eigenvalues. .. warning:: This template uses the :class:`~.ApproxTimeEvolution` operation with ``n=1`` in order to implement the time evolution, as a single-step Trotterization is exact for a commuting Hamiltonian. - If the input Hamiltonian contains Pauli words which do not commute, the compilation of the time evolution operator to a sequence of gates will not equate to the exact propagation under the given Hamiltonian. - Furthermore, if the specified frequencies do not correspond to the true eigenvalue frequency spectrum of the commuting Hamiltonian, computed gradients will be incorrect in general. Args: hamiltonian (.Hamiltonian): The commuting Hamiltonian defining the time-evolution operator. The Hamiltonian must be explicitly written in terms of products of Pauli gates (:class:`~.PauliX`, :class:`~.PauliY`, :class:`~.PauliZ`, and :class:`~.Identity`). time (int or float): The time of evolution, namely the parameter :math:`t` in :math:`e^{- i H t}`. Keyword args: frequencies (tuple[int or float]): The unique positive differences between eigenvalues in the spectrum of the Hamiltonian. If the frequencies are not given, the cost function partial derivative will be computed using the standard two-term shift rule applied to the constituent Pauli words in the Hamiltonian individually. shifts (tuple[int or float]): The parameter shifts to use in obtaining the generalized parameter shift rules. If unspecified, equidistant shifts are used. .. details:: :title: Usage Details The template is used inside a qnode: .. code-block:: python import pennylane as qp n_wires = 2 dev = qp.device('default.qubit', wires=n_wires) coeffs = [1, -1] obs = [qp.X(0) @ qp.Y(1), qp.Y(0) @ qp.X(1)] hamiltonian = qp.Hamiltonian(coeffs, obs) frequencies = (2, 4) @qp.qnode(dev) def circuit(time): qp.X(0) qp.CommutingEvolution(hamiltonian, time, frequencies) return qp.expval(qp.Z(0)) >>> circuit(1) np.float64(0.653...) """ grad_method = None resource_keys = {"words"} def _flatten(self): h = self.hyperparameters["hamiltonian"] data = (self.data[0], h) return data, (self.hyperparameters["frequencies"], self.hyperparameters["shifts"]) @classmethod def _primitive_bind_call(cls, *args, **kwargs): return cls._primitive.bind(*args, **kwargs) @classmethod def _unflatten(cls, data, metadata) -> "CommutingEvolution": return cls(data[1], data[0], frequencies=metadata[0], shifts=metadata[1]) @property def resource_params(self) -> dict: return { "words": tuple(self.hyperparameters["hamiltonian"].pauli_rep.keys()), } def __init__(self, hamiltonian, time, frequencies=None, shifts=None): # pylint: disable-next=import-outside-toplevel from pennylane.gradients.general_shift_rules import generate_shift_rule if getattr(hamiltonian, "pauli_rep", None) is None: raise TypeError( f"hamiltonian must be a linear combination of pauli words. Got {hamiltonian}" ) trainable_hamiltonian = any(math.requires_grad(d) for d in hamiltonian.data) if frequencies is not None and not trainable_hamiltonian: c, s = generate_shift_rule(frequencies, shifts).T recipe = math.stack([c, math.ones_like(c), s]).T self.grad_recipe = (recipe,) + (None,) * len(hamiltonian.data) self.grad_method = "A" self._hyperparameters = { "hamiltonian": hamiltonian, "frequencies": frequencies, "shifts": shifts, } super().__init__(time, *hamiltonian.parameters, wires=hamiltonian.wires)
[docs] def map_wires(self, wire_map: dict): # pylint: disable=protected-access new_op = copy.deepcopy(self) new_op._wires = Wires([wire_map.get(wire, wire) for wire in self.wires]) new_op._hyperparameters["hamiltonian"] = new_op._hyperparameters["hamiltonian"].map_wires( wire_map ) return new_op
[docs] def queue(self, context=QueuingManager): context.remove(self.hyperparameters["hamiltonian"]) context.append(self) return self
[docs] @staticmethod # pylint: disable-next=arguments-differ def compute_decomposition(time, *_, wires, hamiltonian, **__): r"""Representation of the operator as a product of other operators. .. math:: O = O_1 O_2 \dots O_n. Args: *time_and_coeffs (list[tensor_like or float]): list of coefficients of the Hamiltonian, prepended by the time variable wires (Any or Iterable[Any]): wires that the operator acts on hamiltonian (.Hamiltonian): The commuting Hamiltonian defining the time-evolution operator. frequencies (tuple[int or float]): The unique positive differences between eigenvalues in the spectrum of the Hamiltonian. shifts (tuple[int or float]): The parameter shifts to use in obtaining the generalized parameter shift rules. If unspecified, equidistant shifts are used. .. seealso:: :meth:`~.CommutingEvolution.decomposition`. Returns: list[.Operator]: decomposition of the operator """ # uses standard PauliRot decomposition through ApproxTimeEvolution. return [ApproxTimeEvolution(hamiltonian, time, 1)]
[docs] def adjoint(self): hamiltonian = self.hyperparameters["hamiltonian"] time = self.parameters[0] frequencies = self.hyperparameters["frequencies"] shifts = self.hyperparameters["shifts"] return CommutingEvolution(hamiltonian, -time, frequencies, shifts)
def _commuting_evolution_standard_resources(words: tuple[PauliWord]): return {resource_rep(ApproxTimeEvolution, words=words, n=1): 1} @register_resources(_commuting_evolution_standard_resources, name="to_approx_time_evolution") def _commuting_evolution_standard( # pylint: disable=unused-argument time: list, *_, wires: WiresLike, hamiltonian: Hamiltonian, **__, ): if capture.enabled() and isinstance(hamiltonian, Operator1): # Reconstruct the closed-over Hamiltonian as a captured value. It is consumed by # ``ApproxTimeEvolution`` below, so it is not collected as a separate circuit operation. hamiltonian = apply(hamiltonian) ApproxTimeEvolution(hamiltonian, time, 1) add_decomps(CommutingEvolution, _commuting_evolution_standard) @register_resources( lambda base_params, **_: {resource_rep(CommutingEvolution, **base_params): 1}, name="negate_time", ) def _adjoint_commuting_evolution_standard(time, *params, base, **__): hamiltonian = ops.functions.bind_new_parameters(base.hyperparameters["hamiltonian"], params) CommutingEvolution( hamiltonian, -time, frequencies=base.hyperparameters["frequencies"], shifts=base.hyperparameters["shifts"], ) add_decomps("Adjoint(CommutingEvolution)", _adjoint_commuting_evolution_standard)