qml.templates.embeddings.IQPEmbedding

IQPEmbedding(features, wires, n_repeats=1, pattern=None)[source]

Encodes \(n\) features into \(n\) qubits using diagonal gates of an IQP circuit.

The embedding has been proposed by Havlicek et al. (2018).

The basic IQP circuit can be repeated by specifying n_repeats. Repetitions can make the embedding “richer” through interference.

Warning

IQPEmbedding calls a circuit that involves non-trivial classical processing of the features. The features argument is therefore not differentiable when using the template, and gradients with respect to the features cannot be computed by PennyLane.

An IQP circuit is a quantum circuit of a block of Hadamards, followed by a block of gates that are diagonal in the computational basis. Here, the diagonal gates are single-qubit RZ rotations, applied to each qubit and encoding the \(n\) features, followed by two-qubit ZZ entanglers, \(e^{-i x_i x_j \sigma_z \otimes \sigma_z}\). The entangler applied to wires (wires[i], wires[j]) encodes the product of features features[i]*features[j]. The pattern in which the entanglers are applied is either the default, or a custom pattern:

  • If pattern is not specified, the default pattern will be used, in which the entangling gates connect all pairs of neighbours:


    ../../_images/iqp.png

  • Else, pattern is a list of wire pairs [[a, b], [c, d],...], applying the entangler on wires [a, b], [c, d], etc. For example, pattern = [[0, 1], [1, 2]] produces the following entangler pattern:


    ../../_images/iqp_custom.png

    Since diagonal gates commute, the order of the entanglers does not change the result.

Parameters
  • features (array) – array of features to encode

  • wires (Iterable or Wires) – Wires that the template acts on. Accepts an iterable of numbers or strings, or a Wires object.

  • n_repeats (int) – number of times the basic embedding is repeated

  • pattern (list[int]) – specifies the wires and features of the entanglers

Raises

ValueError – if inputs do not have the correct format

A typical usage example of the template is the following:

import pennylane as qml
from pennylane.templates import IQPEmbedding

dev = qml.device('default.qubit', wires=3)

@qml.qnode(dev)
def circuit(features=None):
    IQPEmbedding(features=features, wires=range(3))
    return [qml.expval(qml.PauliZ(w)) for w in range(3)]

circuit(features=[1., 2., 3.])

Do not pass features as a positional argument to the qnode

The features argument cannot be passed to the quantum node as a positional argument. This is due to the fact that the embedding performs non-trivial calculations on the features. As a consequence, the following code will produce an error:

@qml.qnode(dev)
def circuit(features):
   IQPEmbedding(features=features, wires=range(3), n_repeats=2)
   return [qml.expval(qml.PauliZ(w)) for w in range(3)]

circuit([1., 2., 3.])
>>> ValueError: 'features' cannot be differentiable

Repeating the embedding

The embedding can be repeated by specifying the n_repeats argument:

@qml.qnode(dev)
def circuit(features=None):
    IQPEmbedding(features=features, wires=range(3), n_repeats=4)
    return [qml.expval(qml.PauliZ(w)) for w in range(3)]

circuit(features=[1., 2., 3.])

Every repetition uses exactly the same quantum circuit.

Using a custom entangler pattern

A custom entangler pattern can be used by specifying the pattern argument. A pattern has to be a nested list of dimension (K, 2), where K is the number of entanglers to apply.

pattern = [[1, 2], [0, 2], [1, 0]]

@qml.qnode(dev)
def circuit(features=None):
    IQPEmbedding(features=features, wires=range(3), pattern=pattern)
    return [qml.expval(qml.PauliZ(w)) for w in range(3)]

circuit(features=[1., 2., 3.])

Since diagonal gates commute, the order of the wire pairs has no effect on the result.

from pennylane import numpy as np

pattern1 = [[1, 2], [0, 2], [1, 0]]
pattern2 = [[1, 0], [0, 2], [1, 2]]  # a reshuffling of pattern1

@qml.qnode(dev)
def circuit(features=None, pattern=None):
    IQPEmbedding(features=features, wires=range(3), pattern=pattern, n_repeats=3)
    return [qml.expval(qml.PauliZ(w)) for w in range(3)]

res1 = circuit(features=[1., 2., 3.], pattern=pattern1)
res2 = circuit(features=[1., 2., 3.], pattern=pattern2)

assert np.allclose(res1, res2)

Non-consecutive wires

In principle, the user can also pass a non-consecutive wire list to the template. For single qubit gates, the i’th feature is applied to the i’th wire index (which may not be the i’th wire). For the entanglers, the product of i’th and j’th features is applied to the wire indices at the i’th and j’th position in wires.

For example, for wires=[2, 0, 1] the RZ block applies the first feature to wire 2, the second feature to wire 0, and the third feature to wire 1.

Likewise, using the default pattern, the entangler block applies the product of the first and second feature to the wire pair [2, 0], the product of the second and third feature to [2, 1], and so forth.