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 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.


AmplitudeEmbedding(features, wires[, …])

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[, do_queue, id])

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, where \(N\leq M\).

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[, …])

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

Class Inheritance Diagram

Inheritance diagram of pennylane.templates.embeddings.amplitude.AmplitudeEmbedding, pennylane.templates.embeddings.angle.AngleEmbedding, pennylane.templates.embeddings.basis.BasisEmbedding, pennylane.templates.embeddings.displacement.DisplacementEmbedding, pennylane.templates.embeddings.iqp.IQPEmbedding, pennylane.templates.embeddings.qaoa.QAOAEmbedding, pennylane.templates.embeddings.squeezing.SqueezingEmbedding


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.


BasicEntanglerLayers(weights[, wires, …])

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.

GateFabric(weights, wires, init_state[, …])

Implements a local, expressive, and quantum-number-preserving ansatz proposed by Anselmetti et al. in arXiv:2104.05692.

ParticleConservingU1(weights, wires[, …])

Implements the heuristic VQE ansatz for quantum chemistry simulations using the particle-conserving gate \(U_{1,\mathrm{ex}}\) proposed by Barkoutsos et al. in arXiv:1805.04340.

ParticleConservingU2(weights, wires[, …])

Implements the heuristic VQE ansatz for Quantum Chemistry simulations using the particle-conserving entangler \(U_\mathrm{ent}(\vec{\theta}, \vec{\phi})\) proposed in arXiv:1805.04340.

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.

Class Inheritance Diagram

Inheritance diagram of pennylane.templates.layers.basic_entangler.BasicEntanglerLayers, pennylane.templates.layers.cv_neural_net.CVNeuralNetLayers, pennylane.templates.layers.gate_fabric.GateFabric, pennylane.templates.layers.particle_conserving_u1.ParticleConservingU1, pennylane.templates.layers.particle_conserving_u2.ParticleConservingU2, pennylane.templates.layers.random.RandomLayers, pennylane.templates.layers.simplified_two_design.SimplifiedTwoDesign, pennylane.templates.layers.strongly_entangling.StronglyEntanglingLayers


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).


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

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


AllSinglesDoubles(weights, wires, hf_state)

Builds a quantum circuit to prepare correlated states of molecules by applying all SingleExcitation and DoubleExcitation operations to the initial Hartree-Fock state.

ApproxTimeEvolution(hamiltonian, time, n[, …])

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

ArbitraryUnitary(weights, wires[, do_queue, id])

Implements an arbitrary unitary on the specified wires.

DoubleExcitationUnitary(weight[, wires1, …])

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

GroverOperator([wires, work_wires, do_queue, id])

Performs the Grover Diffusion Operator.

Permute(permutation, wires[, do_queue, id])

Applies a permutation to a set of wires.


Apply a quantum Fourier transform (QFT).

QuantumMonteCarlo(probs, func, target_wires, …)

Performs the quantum Monte Carlo estimation algorithm.

QuantumPhaseEstimation(unitary, …[, …])

Performs the quantum phase estimation circuit.

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.

Class Inheritance Diagram

Inheritance diagram of pennylane.templates.subroutines.all_singles_doubles.AllSinglesDoubles, pennylane.templates.subroutines.approx_time_evolution.ApproxTimeEvolution, pennylane.templates.subroutines.arbitrary_unitary.ArbitraryUnitary, pennylane.templates.subroutines.double_excitation_unitary.DoubleExcitationUnitary, pennylane.templates.subroutines.grover.GroverOperator, pennylane.templates.subroutines.permute.Permute, pennylane.templates.subroutines.qft.QFT, pennylane.templates.subroutines.qmc.QuantumMonteCarlo, pennylane.templates.subroutines.qpe.QuantumPhaseEstimation, pennylane.templates.subroutines.single_excitation_unitary.SingleExcitationUnitary, pennylane.templates.subroutines.uccsd.UCCSD

State preparations

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


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. (2004).

Class Inheritance Diagram

Inheritance diagram of pennylane.templates.state_preparations.arbitrary_state_preparation.ArbitraryStatePreparation, pennylane.templates.state_preparations.basis.BasisStatePreparation, pennylane.templates.state_preparations.mottonen.MottonenStatePreparation

Custom templates

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


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.


check_is_in_options(element, options, msg)

Check that the value of element is in options.


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.


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


Turn inpt into an array and return its shape.

Utility functions for quantum Monte Carlo


func_to_unitary(func, M)

Calculates the unitary that encodes a function onto an ancilla qubit register.

make_Q(A, R)

Calculates the \(\mathcal{Q}\) matrix that encodes the expectation value according to the probability unitary \(\mathcal{A}\) and the function-encoding unitary \(\mathcal{R}\).


Calculates the unitary matrix corresponding to an input probability distribution.