예제 #1
0
def BasisEmbedding(features, wires):
    r"""Encodes :math:`n` binary features into a basis state of :math:`n` qubits.

    For example, for ``features=np.array([0, 1, 0])``, the quantum system will be
    prepared in state :math:`|010 \rangle`.

    .. warning::

        ``BasisEmbedding`` calls a circuit whose architecture depends on the binary features.
        The ``features`` argument is therefore not differentiable when using the template, and
        gradients with respect to the argument cannot be computed by PennyLane.

    Args:
        features (array): binary input array of shape ``(n, )``
        wires (Sequence[int] or int): qubit indices that the template acts on

    Raises:
        ValueError: if inputs do not have the correct format
    """

    #############
    # Input checks
    wires, n_wires = _check_wires(wires)
    _check_shape(features, (n_wires,))

    # basis_state is guaranteed to be a list
    if any([b not in [0, 1] for b in features]):
        raise ValueError("Basis state must only consist of 0s and 1s, got {}".format(features))
    ###############

    features = np.array(features)
    BasisState(features, wires=wires)
예제 #2
0
def BasisEmbedding(features, wires):
    r"""Encodes :math:`n` binary features into a basis state of :math:`n` qubits.

    For example, for ``features=[0, 1, 0]``, the quantum system will be prepared in state :math:`|010 \rangle`.

    .. note::

        BasisEmbedding uses PennyLane's :class:`~pennylane.ops.BasisState` and only works in conjunction with
        devices that implement this function.

    Args:
        features (array): Binary input array of shape ``(n, )``
        wires (Sequence[int]): sequence of qubit indices that the template acts on

    Raises:
        ValueError: if ``features`` or ``wires`` is invalid
    """
    if not isinstance(wires, Iterable):
        raise ValueError(
            "Wires must be passed as a list of integers; got {}.".format(
                wires))

    if len(features) > len(wires):
        raise ValueError(
            "Number of bits to embed cannot be larger than number of wires, which is {}; "
            "got {}.".format(len(wires), len(features)))

    BasisState(features, wires=wires)
예제 #3
0
    def expand(self):

        weights = self.parameters[0]

        with qml.tape.QuantumTape() as tape:

            BasisState(self.hf_state, wires=self.wires)

            for i, d_wires in enumerate(self.doubles):
                qml.DoubleExcitation(weights[len(self.singles) + i], wires=d_wires)

            for j, s_wires in enumerate(self.singles):
                qml.SingleExcitation(weights[j], wires=s_wires)

        return tape
예제 #4
0
    def expand(self):

        weights = self.parameters[0]

        with qml.tape.QuantumTape() as tape:

            BasisState(self.init_state_flipped, wires=self.wires)

            for i, (w1, w2) in enumerate(self.d_wires):
                qml.templates.DoubleExcitationUnitary(
                    weights[len(self.s_wires) + i], wires1=w1, wires2=w2)

            for j, s_wires_ in enumerate(self.s_wires):
                qml.templates.SingleExcitationUnitary(weights[j],
                                                      wires=s_wires_)

        return tape