qml.templates

This module provides a growing library of templates of common variational circuit architectures that can be used to easily build, evaluate, and train quantum nodes.

Embeddings

Embeddings are templates encode features (i.e., classical data) into a quantum state. They can optionally be repeated, and may contain trainable parameters. Embeddings are typically used at the beginning of a circuit.

Functions

AmplitudeEmbedding(features, wires[, pad, …])

Encodes \(2^n\) features into the amplitude vector of \(n\) qubits.

AngleEmbedding(features, wires[, rotation])

Encodes \(N\) features into the rotation angles of \(n\) qubits, where \(N \leq n\).

BasisEmbedding(features, wires)

Encodes \(n\) binary features into a basis state of \(n\) qubits.

DisplacementEmbedding(features, wires[, …])

Encodes \(N\) features into the displacement amplitudes \(r\) or phases \(\phi\) of \(M\) modes,

IQPEmbedding(features, wires[, n_repeats, …])

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

QAOAEmbedding(features, weights, wires[, …])

Encodes \(N\) features into \(n>N\) qubits, using a layered, trainable quantum circuit that is inspired by the QAOA ansatz.

SqueezingEmbedding(features, wires[, method, c])

Encodes \(N\) features into the squeezing amplitudes \(r \geq 0\) or phases \(\phi \in [0, 2\pi)\) of \(M\) modes, where \(N\leq M\).

Layers

Layers are trainable templates that are typically repeated, using different adjustable parameters in each repetition. They implement a transformation from a quantum state to another quantum state.

Functions

BasicEntanglerLayers(weights, wires[, rotation])

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

CVNeuralNetLayers(theta_1, phi_1, varphi_1, …)

A sequence of layers of a continuous-variable quantum neural network, as specified in arXiv:1806.06871.

RandomLayers(weights, wires[, ratio_imprim, …])

Layers of randomly chosen single qubit rotations and 2-qubit entangling gates, acting on randomly chosen qubits.

SimplifiedTwoDesign(initial_layer_weights, …)

Layers consisting of a simplified 2-design architecture of Pauli-Y rotations and controlled-Z entanglers proposed in Cerezo et al.(2020)..

StronglyEntanglingLayers(weights, wires[, …])

Layers consisting of single qubit rotations and entanglers, inspired by the circuit-centric classifier design arXiv:1804.00633.

Subroutines

Subroutines are the most basic template, consisting of a collection of quantum operations, and not fulfilling any of the characteristics of other templates (i.e. to prepare a specific state, to be repeated or to encode features).

Functions

ApproxTimeEvolution(hamiltonian, time, n)

Applies the Trotterized time-evolution operator for an arbitrary Hamiltonian, expressed in terms of Pauli gates.

ArbitraryUnitary(weights, wires)

Implements an arbitrary unitary on the specified wires.

DoubleExcitationUnitary(weight[, wires1, wires2])

Circuit to exponentiate the tensor product of Pauli matrices representing the double-excitation operator entering the Unitary Coupled-Cluster Singles and Doubles (UCCSD) ansatz.

Interferometer(theta, phi, varphi, wires[, …])

General linear interferometer, an array of beamsplitters and phase shifters.

SingleExcitationUnitary(weight[, wires])

Circuit to exponentiate the tensor product of Pauli matrices representing the single-excitation operator entering the Unitary Coupled-Cluster Singles and Doubles (UCCSD) ansatz.

UCCSD(weights, wires[, s_wires, d_wires, …])

Implements the Unitary Coupled-Cluster Singles and Doubles (UCCSD) ansatz.

State preperations

State preperations are templates that prepare a given quantum state, by decomposing it into elementary operations.

Functions

ArbitraryStatePreparation(weights, wires)

Implements an arbitrary state preparation on the specified wires.

BasisStatePreparation(basis_state, wires)

Prepares a basis state on the given wires using a sequence of Pauli X gates.

MottonenStatePreparation(state_vector, wires)

Prepares an arbitrary state on the given wires using a decomposition into gates developed by Möttönen et al.

Custom templates

The template decorator can used to register a quantum function as a template.

pennylane.templates.template(func)

Register a quantum template with PennyLane.

Broadcasting function

The broadcast function creates a new template by broadcasting templates (or normal gates) over wires in a predefined pattern. You can import this function both via qml.broadcast and qml.templates.broadcast.

pennylane.broadcast(unitary, wires, pattern)

Applies a unitary multiple times to a specific pattern of wires.

Layering Function

The layer function creates a new template by repeatedly applying a sequence of quantum gates to a set of wires. You can import this function both via qml.layer and qml.templates.layer.

pennylane.layer(template, depth, *args, **kwargs)

Repeatedly applies a unitary a given number of times.

Utility functions for input checks

Utility functions provided for the templates. These are useful for standard input checks, for example to make sure that arguments have the right shape, range or type.

Functions

check_is_in_options(element, options, msg)

Check that the value of element is in options.

check_no_variable(arg, msg)

Checks that arg does not represent or contain a Variable() object.

check_number_of_layers(list_of_weights)

Check that all sequences in list_of_weights have the same first dimension.

check_shape(inpt, target_shape, msg[, bound])

Check that the shape of inpt is equal to target_shape.

check_shapes(inpt_list, target_shapes, msg)

Check that the shape of elements in the inpt list are equal to the shapes of elements in the target_shapes list.

check_type(element, types, msg)

Check that the type of element is one of types.

check_wires(wires)

Checks that wires is either a non-negative integer or a list of non-negative integers.

get_shape(inpt)

Turn inpt into an array and return its shape.