# 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.# pylint: disable=protected-access"""This module contains the qml.vn_entropy measurement."""fromcollections.abcimportSequencefromtypingimportOptionalimportpennylaneasqmlfrompennylane.wiresimportWiresfrom.measurementsimportStateMeasurement,VnEntropy
[docs]defvn_entropy(wires,log_base=None)->"VnEntropyMP":r"""Von Neumann entropy of the system prior to measurement. .. math:: S( \rho ) = -\text{Tr}( \rho \log ( \rho )) Args: wires (Sequence[int] or int): The wires of the subsystem log_base (float): Base for the logarithm. Returns: VnEntropyMP: Measurement process instance **Example:** .. code-block:: python3 dev = qml.device("default.qubit", wires=2) @qml.qnode(dev) def circuit_entropy(x): qml.IsingXX(x, wires=[0, 1]) return qml.vn_entropy(wires=[0]) Executing this QNode: >>> circuit_entropy(np.pi/2) 0.6931472 It is also possible to get the gradient of the previous QNode: >>> param = np.array(np.pi/4, requires_grad=True) >>> qml.grad(circuit_entropy)(param) tensor(0.62322524, requires_grad=True) .. note:: Calculating the derivative of :func:`~pennylane.vn_entropy` is currently supported when using the classical backpropagation differentiation method (``diff_method="backprop"``) with a compatible device and finite differences (``diff_method="finite-diff"``). .. note:: ``qml.vn_entropy`` can also be used to compute the entropy of entanglement between two subsystems by computing the Von Neumann entropy of either of the subsystems. .. seealso:: :func:`pennylane.math.vn_entropy`, :func:`pennylane.math.vn_entanglement_entropy` """wires=Wires(wires)returnVnEntropyMP(wires=wires,log_base=log_base)
[docs]classVnEntropyMP(StateMeasurement):"""Measurement process that computes the Von Neumann entropy of the system prior to measurement. Please refer to :func:`~pennylane.vn_entropy` for detailed documentation. Args: wires (.Wires): The wires the measurement process applies to. This can only be specified if an observable was not provided. id (str): custom label given to a measurement instance, can be useful for some applications where the instance has to be identified log_base (float): Base for the logarithm. """def_flatten(self):metadata=(("wires",self.raw_wires),("log_base",self.log_base))return(None,None),metadata# pylint: disable=too-many-arguments, unused-argumentdef__init__(self,wires:Optional[Wires]=None,id:Optional[str]=None,log_base:Optional[float]=None,):self.log_base=log_basesuper().__init__(wires=wires,id=id)@propertydefhash(self):"""int: returns an integer hash uniquely representing the measurement process"""fingerprint=(self.__class__.__name__,tuple(self.wires.tolist()),self.log_base)returnhash(fingerprint)@propertydefreturn_type(self):returnVnEntropy@propertydefnumeric_type(self):returnfloat