|  | 
|  | 1 | +# Copyright 2024 Xanadu Quantum Technologies Inc. | 
|  | 2 | + | 
|  | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | +# you may not use this file except in compliance with the License. | 
|  | 5 | +# You may obtain a copy of the License at | 
|  | 6 | + | 
|  | 7 | +#     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | + | 
|  | 9 | +# Unless required by applicable law or agreed to in writing, software | 
|  | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | +# See the License for the specific language governing permissions and | 
|  | 13 | +# limitations under the License. | 
|  | 14 | + | 
|  | 15 | +""" | 
|  | 16 | +The class representing a Phase noise channel. | 
|  | 17 | +""" | 
|  | 18 | + | 
|  | 19 | +from __future__ import annotations | 
|  | 20 | +from typing import Sequence | 
|  | 21 | +from mrmustard.lab_dev.circuit_components import CircuitComponent | 
|  | 22 | +from mrmustard.physics.ansatz.array_ansatz import ArrayAnsatz | 
|  | 23 | +from mrmustard.physics.representations import Representation | 
|  | 24 | +from mrmustard import math | 
|  | 25 | +import numpy as np | 
|  | 26 | +from .base import Channel | 
|  | 27 | +from ..utils import make_parameter | 
|  | 28 | + | 
|  | 29 | +__all__ = ["PhaseNoise"] | 
|  | 30 | + | 
|  | 31 | + | 
|  | 32 | +class PhaseNoise(Channel): | 
|  | 33 | +    r""" | 
|  | 34 | +    The Phase noise channel. | 
|  | 35 | +
 | 
|  | 36 | +    This class represents the application of a random phase. The distributiuon of the phase | 
|  | 37 | +    is assumed to be a Gaussian with mean zero, and standard deviation `phase_stdev`. | 
|  | 38 | +
 | 
|  | 39 | +    Args: | 
|  | 40 | +        modes: The modes the channel is applied to | 
|  | 41 | +        phase_stdev: The standard deviation of the random phase noise. | 
|  | 42 | +
 | 
|  | 43 | +    ..details:: | 
|  | 44 | +        The Fock representation is connected to the Fourier coefficients of the distribution. | 
|  | 45 | +    """ | 
|  | 46 | + | 
|  | 47 | +    short_name = "P~" | 
|  | 48 | + | 
|  | 49 | +    def __init__( | 
|  | 50 | +        self, | 
|  | 51 | +        modes: Sequence[int], | 
|  | 52 | +        phase_stdev: float | Sequence[float], | 
|  | 53 | +        phase_stdev_trainable: bool = False, | 
|  | 54 | +        phase_stdev_bounds: tuple[float | None, float | None] = (0.0, None), | 
|  | 55 | +    ): | 
|  | 56 | +        super().__init__(name="PhaseNoise") | 
|  | 57 | +        self._add_parameter( | 
|  | 58 | +            make_parameter(phase_stdev_trainable, phase_stdev, "phase_stdev", phase_stdev_bounds) | 
|  | 59 | +        ) | 
|  | 60 | +        self._representation = self.from_ansatz( | 
|  | 61 | +            modes_in=modes, modes_out=modes, ansatz=None | 
|  | 62 | +        ).representation | 
|  | 63 | + | 
|  | 64 | +    def __custom_rrshift__(self, other: CircuitComponent) -> CircuitComponent: | 
|  | 65 | +        r""" | 
|  | 66 | +        Since PhaseNoise admits a particularly nice form in the Fock basis, we have implemented its right-shift operation separately. | 
|  | 67 | +
 | 
|  | 68 | +        Args: | 
|  | 69 | +            other: the component other than the PhaseNoise object that is present in the contraction | 
|  | 70 | +
 | 
|  | 71 | +        Output: | 
|  | 72 | +            the result of the contraction. | 
|  | 73 | +        """ | 
|  | 74 | + | 
|  | 75 | +        if not other.wires.bra or not other.wires.ket: | 
|  | 76 | +            other = other @ other.adjoint | 
|  | 77 | +        array = math.asnumpy(other.fock_array()) | 
|  | 78 | +        mode_indices = np.indices(array.shape) | 
|  | 79 | +        for mode in self.modes: | 
|  | 80 | +            phase_factors = np.exp( | 
|  | 81 | +                -0.5 | 
|  | 82 | +                * (mode_indices[mode] - mode_indices[other.n_modes + mode]) ** 2 | 
|  | 83 | +                * self.phase_stdev.value**2 | 
|  | 84 | +            ) | 
|  | 85 | +            array *= phase_factors | 
|  | 86 | +        return CircuitComponent(Representation(ArrayAnsatz(array, False), other.wires), self.name) | 
0 commit comments