qml.templates.layers.BasicEntanglerLayers

BasicEntanglerLayers(weights, wires, rotation=None)[source]

Layers consisting of one-parameter single-qubit rotations on each qubit, followed by a closed chain or ring of CNOT gates.

The ring of CNOT gates connects every qubit with its neighbour, with the last qubit being considered as a neighbour to the first qubit.

../../_images/basic_entangler.png

The number of layers \(L\) is determined by the first dimension of the argument weights. When using a single wire, the template only applies the single qubit gates in each layer.

Note

This template follows the convention of dropping the entanglement between the last and the first qubit when using only two wires, so the entangler is not repeated on the same wires. In this case, only one CNOT gate is applied in each layer:

../../_images/basic_entangler_2wires.png
Parameters
  • weights (array[float]) – array of weights with shape (L, len(wires)), each weight is used as a parameter for the rotation

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

  • rotation (pennylane.ops.Operation) – one-parameter single-qubit gate to use, if None, RX is used as default

Raises

ValueError – if inputs do not have the correct format

The template is used inside a qnode:

import pennylane as qml
from pennylane.templates import BasicEntanglerLayers
from math import pi

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

@qml.qnode(dev)
def circuit(weights):
    BasicEntanglerLayers(weights=weights, wires=range(n_wires))
    return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_wires)]
>>> circuit([[pi, pi, pi]])
[1., 1., -1.]

Parameter initialization function

The init module has two parameter initialization functions, basic_entangler_layers_normal and basic_entangler_layers_uniform.

from pennylane.init import basic_entangler_layers_normal

n_layers = 4
weights = basic_entangler_layers_normal(n_layers=n_layers, n_wires=n_wires)

circuit(weights)

No periodic boundary for two wires

When using two wires, the convention is to drop the periodic boundary condition. This means that the connection from the second to the first wire is omitted.

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

@qml.qnode(dev)
def circuit(weights):
    BasicEntanglerLayers(weights=weights, wires=range(n_wires))
    return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_wires)]
>>> circuit([[pi, pi]])
[-1, 1]

Changing the rotation gate

Any single-qubit gate can be used as a rotation gate, as long as it only takes a single parameter. The default is the RX gate.

@qml.qnode(dev)
def circuit(weights):
    BasicEntanglerLayers(weights=weights, wires=range(n_wires), rotation=qml.RZ)
    return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_wires)]

Accidentally using a gate that expects more parameters throws a ValueError: Wrong number of parameters.