Source code for pennylane.templates.embeddings.angle
# 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 ``AngleEmbedding`` template."""# pylint: disable-msg=too-many-branches,too-many-arguments,protected-accessimportpennylaneasqmlfrompennylane.operationimportAnyWires,Operationfrompennylane.opsimportRX,RY,RZROT={"X":RX,"Y":RY,"Z":RZ}
[docs]classAngleEmbedding(Operation):r""" Encodes :math:`N` features into the rotation angles of :math:`n` qubits, where :math:`N \leq n`. The rotations can be chosen as either :class:`~pennylane.ops.RX`, :class:`~pennylane.ops.RY` or :class:`~pennylane.ops.RZ` gates, as defined by the ``rotation`` parameter: * ``rotation='X'`` uses the features as angles of RX rotations * ``rotation='Y'`` uses the features as angles of RY rotations * ``rotation='Z'`` uses the features as angles of RZ rotations The length of ``features`` has to be smaller or equal to the number of qubits. If there are fewer entries in ``features`` than rotations, the circuit does not apply the remaining rotation gates. Args: features (tensor_like): input tensor of shape ``(N,)``, where N is the number of input features to embed, with :math:`N\leq n` wires (Any or Iterable[Any]): wires that the template acts on rotation (str): type of rotations used id (str): custom label given to an operator instance, can be useful for some applications where the instance has to be identified. Example: Angle embedding encodes the features by using the specified rotation operation. .. code-block:: python dev = qml.device('default.qubit', wires=3) @qml.qnode(dev) def circuit(feature_vector): qml.AngleEmbedding(features=feature_vector, wires=range(3), rotation='Z') qml.Hadamard(0) return qml.probs(wires=range(3)) X = [1,2,3] Here, we have also used rotation angles :class:`RZ`. If not specified, :class:`RX` is used as default. The resulting circuit is: >>> print(qml.draw(circuit, level="device")(X)) 0: ──RZ(1.00)──H─┤ ╭Probs 1: ──RZ(2.00)────┤ ├Probs 2: ──RZ(3.00)────┤ ╰Probs """num_wires=AnyWiresgrad_method=Nonedef_flatten(self):hyperparameters=(("rotation",self._rotation),)returnself.data,(self.wires,hyperparameters)def__repr__(self):returnf"AngleEmbedding({self.data[0]}, wires={self.wires.tolist()}, rotation={self._rotation})"def__init__(self,features,wires,rotation="X",id=None):ifrotationnotinROT:raiseValueError(f"Rotation option {rotation} not recognized.")shape=qml.math.shape(features)[-1:]n_features=shape[0]ifn_features>len(wires):raiseValueError(f"Features must be of length {len(wires)} or less; got length {n_features}.")self._rotation=rotationself._hyperparameters={"rotation":ROT[rotation]}wires=wires[:n_features]super().__init__(features,wires=wires,id=id)@propertydefnum_params(self):return1@propertydefndim_params(self):return(1,)
[docs]@staticmethoddefcompute_decomposition(features,wires,rotation):# pylint: disable=arguments-differr"""Representation of the operator as a product of other operators. .. math:: O = O_1 O_2 \dots O_n. .. seealso:: :meth:`~.AngleEmbedding.decomposition`. Args: features (tensor_like): input tensor of dimension ``(len(wires),)`` wires (Any or Iterable[Any]): wires that the operator acts on rotation (.Operator): rotation gate class Returns: list[.Operator]: decomposition of the operator **Example** >>> features = torch.tensor([1., 2.]) >>> qml.AngleEmbedding.compute_decomposition(features, wires=["a", "b"], rotation=qml.RX) [RX(tensor(1.), wires=['a']), RX(tensor(2.), wires=['b'])] """batched=qml.math.ndim(features)>1# We will iterate over the first axis of `features` together with iterating over the wires.# If the leading dimension is a batch dimension, exchange the wire and batching axes.features=qml.math.T(features)ifbatchedelsefeaturesreturn[rotation(features[i],wires=wires[i])foriinrange(len(wires))]