Quantum operators

PennyLane supports a wide variety of quantum operators—such as gates, noisy channels, state preparations and measurements. These operators can be used in quantum functions, like shown in the following example:

import pennylane as qml

def my_quantum_function(x, y):
    qml.RZ(x, wires=0)
    qml.CNOT(wires=[0,1])
    qml.RY(y, wires=1)
    qml.AmplitudeDamping(0.1, wires=0)
    return qml.expval(qml.PauliZ(1))

This quantum function uses the RZ, CNOT, RY gates, the AmplitudeDamping noisy channel as well as the PauliZ observable.

Functions applied to operators extract information (such as the matrix representation) or transform operators (like turning a gate into a controlled gate).

PennyLane supports the following operators and operator functions:

Operator functions

Various functions and transforms are available for manipulating operators, and extracting information. These can be broken down into two main categories:

Operator to Operator functions

adjoint(fn[, lazy])

Create the adjoint of an Operator or a function that applies the adjoint of the provided function.

ctrl(op, control[, control_values, work_wires])

Create a method that applies a controlled version of the provided op.

cond(condition[, true_fn, false_fn, elifs])

Quantum-compatible if-else conditionals --- condition quantum operations on parameters such as the results of mid-circuit qubit measurements.

exp(op[, coeff, num_steps, id])

Take the exponential of an Operator times a coefficient.

sum(*summands[, grouping_type, method, id, lazy])

Construct an operator which is the sum of the given operators.

pow(base[, z, lazy, id])

Raise an Operator to a power.

prod(*ops[, id, lazy])

Construct an operator which represents the generalized product of the operators provided.

s_prod(scalar, operator[, lazy, id])

Construct an operator which is the scalar product of the given scalar and operator provided.

generator(op[, format])

Returns the generator of an operation.

map_wires(input, wire_map[, queue, replace])

Changes the wires of an operator, tape, qnode or quantum function according to the given wire map.

dot(coeffs, ops[, pauli, grouping_type, method])

Returns the dot product between the coeffs vector and the ops list of operators.

evolve(*args, **kwargs)

This method is dispatched and its functionality depends on the type of the input op.

simplify(input)

Simplifies an operator, tape, qnode or quantum function by reducing its arithmetic depth or number of rotation parameters.

These operator functions act on operators to produce new operators.

>>> op = qml.prod(qml.PauliX(0), qml.PauliZ(1))
>>> op = qml.sum(qml.Hadamard(0), op)
>>> op = qml.s_prod(1.2, op)
>>> op
1.2 * (Hadamard(wires=[0]) + X(0) @ Z(1))

Operator to Other functions

matrix(op[, wire_order])

The matrix representation of an operation or quantum circuit.

eigvals(op[, k, which])

The eigenvalues of one or more operations.

is_commuting(operation1, operation2)

Check if two operations are commuting using a lookup table.

is_hermitian(op)

Check if the operation is hermitian.

is_unitary(op)

Check if the operation is unitary.

iterative_qpe(base[, aux_wire, iters, ancilla])

Performs the iterative quantum phase estimation circuit.

These operator functions act on operators and return other data types. All operator functions can be used on instantiated operators.

>>> op = qml.RX(0.54, wires=0)
>>> qml.matrix(op)
[[0.9637709+0.j         0.       -0.26673144j]
[0.       -0.26673144j 0.9637709+0.j        ]]

Some operator functions can also be used in a functional form:

>>> x = torch.tensor(0.6, requires_grad=True)
>>> matrix_fn = qml.matrix(qml.RX)
>>> matrix_fn(x, wires=0)
tensor([[0.9553+0.0000j, 0.0000-0.2955j],
        [0.0000-0.2955j, 0.9553+0.0000j]], grad_fn=<StackBackward0>)

In the functional form, they are usually differentiable with respect to gate arguments:

>>> loss = torch.real(torch.trace(matrix_fn(x, wires=0)))
>>> loss.backward()
>>> x.grad
tensor(-0.2955)

Some operator transforms can also act on multiple operators, by passing quantum functions, QNodes or tapes:

>>> def circuit(theta):
...     qml.RX(theta, wires=1)
...     qml.Z(wires=0)
>>> qml.matrix(circuit)(np.pi / 4)
array([[ 0.92387953+0.j,  0.+0.j ,  0.-0.38268343j,  0.+0.j],
[ 0.+0.j,  -0.92387953+0.j,  0.+0.j,  0. +0.38268343j],
[ 0. -0.38268343j,  0.+0.j,  0.92387953+0.j,  0.+0.j],
[ 0.+0.j,  0.+0.38268343j,  0.+0.j,  -0.92387953+0.j]])

Matrix to Operator functions

pauli_decompose(H[, hide_identity, ...])

Decomposes a Hermitian matrix into a linear combination of Pauli operators.

qsvt(A, angles, wires[, convention])

Implements the quantum singular value transformation (QSVT) circuit.

These functions take a matrix and return an associated native PennyLane operator. For example:

>>> mat = np.array([[1, 1], [1, -1]])
>>> h = qml.pauli_decompose(mat)
>>> type(h)
pennylane.ops.op_math.linear_combination.LinearCombination
>>> print(h)
1.0 * X(0) + 1.0 * Z(0)

Qubit operators

Non-parametrized gates

Identity

The Identity operator

Hadamard

The Hadamard operator

PauliX

The Pauli X operator

PauliY

The Pauli Y operator

PauliZ

The Pauli Z operator

S

The single-qubit phase gate

T

The single-qubit T gate

SX

The single-qubit Square-Root X operator.

CNOT

The controlled-NOT operator

CZ

The controlled-Z operator

CY

The controlled-Y operator

CH

The controlled-Hadamard operator

SWAP

The swap operator

ISWAP

The i-swap operator

ECR

An echoed RZX(\(\pi/2\)) gate.

SISWAP

The square root of i-swap operator.

SQISW

alias of pennylane.ops.qubit.non_parametric_ops.SISWAP

CSWAP

The controlled-swap operator

Toffoli

Toffoli (controlled-controlled-X) gate.

MultiControlledX

Apply a Pauli X gate controlled on an arbitrary computational basis state.

Barrier

The Barrier operator, used to separate the compilation process into blocks or as a visual tool.

WireCut

The wire cut operation, used to manually mark locations for wire cuts.

Parametrized gates

Rot

Arbitrary single qubit rotation

RX

The single qubit X rotation

RY

The single qubit Y rotation

RZ

The single qubit Z rotation

MultiRZ

Arbitrary multi Z rotation.

PauliRot

Arbitrary Pauli word rotation.

PhaseShift

Arbitrary single qubit local phase shift

ControlledPhaseShift

A qubit controlled phase shift.

CPhase

alias of pennylane.ops.op_math.controlled_ops.ControlledPhaseShift

PCPhase

A projector-controlled phase gate.

CPhaseShift00

A qubit controlled phase shift.

CPhaseShift01

A qubit controlled phase shift.

CPhaseShift10

A qubit controlled phase shift.

CRX

The controlled-RX operator

CRY

The controlled-RY operator

CRZ

The controlled-RZ operator

CRot

The controlled-Rot operator

U1

U1 gate.

U2

U2 gate.

U3

Arbitrary single qubit unitary.

IsingXX

Ising XX coupling gate

IsingXY

Ising (XX + YY) coupling gate

IsingYY

Ising YY coupling gate

IsingZZ

Ising ZZ coupling gate

PSWAP

Phase SWAP gate

GlobalPhase

A global phase operation that multiplies all components of the state by \(e^{-i \phi}\).

Quantum chemistry gates

SingleExcitation

Single excitation rotation.

SingleExcitationPlus

Single excitation rotation with positive phase-shift outside the rotation subspace.

SingleExcitationMinus

Single excitation rotation with negative phase-shift outside the rotation subspace.

DoubleExcitation

Double excitation rotation.

DoubleExcitationPlus

Double excitation rotation with positive phase-shift outside the rotation subspace.

DoubleExcitationMinus

Double excitation rotation with negative phase-shift outside the rotation subspace.

OrbitalRotation

Spin-adapted spatial orbital rotation.

FermionicSWAP

Fermionic SWAP rotation.

Electronic Hamiltonians built independently using OpenFermion tools can be readily converted to a PennyLane observable using the import_operator() function.

Gates constructed from a matrix

QubitUnitary

Apply an arbitrary unitary matrix with a dimension that is a power of two.

ControlledQubitUnitary

Apply an arbitrary fixed unitary to wires with control from the control_wires.

DiagonalQubitUnitary

Apply an arbitrary diagonal unitary matrix with a dimension that is a power of two.

SpecialUnitary

Gate from the group \(SU(N)\) with \(N=2^n\) for \(n\) qubits.

BlockEncode

Construct a unitary \(U(A)\) such that an arbitrary matrix \(A\) is encoded in the top-left block.

Gates performing arithmetics

QubitCarry

Apply the QubitCarry operation to four input wires.

QubitSum

Apply a QubitSum operation on three input wires.

IntegerComparator

Apply a controlled Pauli X gate using integer comparison as the condition.

State preparation

BasisState

Prepares a single computational basis state.

StatePrep

Prepare subsystems using the given ket vector in the computational basis.

QubitDensityMatrix

Prepare subsystems using the given density matrix.

Noisy channels

AmplitudeDamping

Single-qubit amplitude damping error channel.

GeneralizedAmplitudeDamping

Single-qubit generalized amplitude damping error channel.

PhaseDamping

Single-qubit phase damping error channel.

DepolarizingChannel

Single-qubit symmetrically depolarizing error channel.

BitFlip

Single-qubit bit flip (Pauli \(X\)) error channel.

PhaseFlip

Single-qubit bit flip (Pauli \(Z\)) error channel.

ResetError

Single-qubit Reset error channel.

PauliError

Pauli operator error channel for an arbitrary number of qubits.

QubitChannel

Apply an arbitrary fixed quantum channel.

ThermalRelaxationError

Thermal relaxation error channel.

Observables

Hadamard

The Hadamard operator

Hermitian

An arbitrary Hermitian observable.

Identity

The Identity operator

PauliX

The Pauli X operator

PauliY

The Pauli Y operator

PauliZ

The Pauli Z operator

Projector

Observable corresponding to the state projector \(P=\ket{\phi}\bra{\phi}\).

Hamiltonian

Operator representing a Hamiltonian.

SparseHamiltonian

A Hamiltonian represented directly as a sparse matrix in Compressed Sparse Row (CSR) format.

Continuous-Variable (CV) operators

If you would like to learn more about the CV model of quantum computing, check out the quantum photonics page of the Strawberry Fields documentation.

CV gates

Identity

The Identity operator

Beamsplitter

Beamsplitter interaction.

ControlledAddition

Controlled addition operation.

ControlledPhase

Controlled phase operation.

CrossKerr

Cross-Kerr interaction.

CubicPhase

Cubic phase shift.

Displacement

Phase space displacement.

InterferometerUnitary

A linear interferometer transforming the bosonic operators according to the unitary matrix \(U\).

Kerr

Kerr interaction.

QuadraticPhase

Quadratic phase shift.

Rotation

Phase space rotation.

Squeezing

Phase space squeezing.

TwoModeSqueezing

Phase space two-mode squeezing.

CV state preparation

CatState

Prepares a cat state.

CoherentState

Prepares a coherent state.

DisplacedSqueezedState

Prepares a displaced squeezed vacuum state.

FockDensityMatrix

Prepare subsystems using the given density matrix in the Fock basis.

FockState

Prepares a single Fock state.

FockStateVector

Prepare subsystems using the given ket vector in the Fock basis.

GaussianState

Prepare subsystems in a given Gaussian state.

SqueezedState

Prepares a squeezed vacuum state.

ThermalState

Prepares a thermal state.

CV observables

FockStateProjector

The number state observable \(\ket{n}\bra{n}\).

Identity

The Identity operator

NumberOperator

The photon number observable \(\langle \hat{n}\rangle\).

TensorN

The tensor product of the NumberOperator acting on different wires.

QuadP

The momentum quadrature observable \(\hat{p}\).

PolyXP

An arbitrary second-order polynomial observable.

QuadOperator

The generalized quadrature observable \(\x_\phi = \x cos\phi+\p\sin\phi\).

QuadX

The position quadrature observable \(\hat{x}\).

Qutrit operators

Qutrit non-parametrized gates

TShift

The qutrit shift operator

TClock

Ternary Clock gate

THadamard

The ternary Hadamard operator

TAdd

The 2-qutrit controlled add gate

TSWAP

The ternary swap operator.

Qutrit gates constructed from a matrix

QutritUnitary

Apply an arbitrary, fixed unitary matrix.

ControlledQutritUnitary

Apply an arbitrary fixed unitary to wires with control from the control_wires.

Qutrit parametrized gates

TRX

The single qutrit X rotation

TRY

The single qutrit Y rotation

TRZ

The single qutrit Z rotation

Qutrit State preparation

QutritBasisState

Prepares a single computational basis state for a qutrit system.

Qutrit noisy channels

QutritDepolarizingChannel

Single-qutrit symmetrically depolarizing error channel.

QutritAmplitudeDamping

Single-qutrit amplitude damping error channel.

TritFlip

Single-qutrit trit flip error channel, used for applying "bit flips" on each qutrit subspace.

QutritChannel

Apply an arbitrary fixed qutrit channel.

Qutrit Observables

THermitian

An arbitrary Hermitian observable for qutrits.

GellMann

The Gell-Mann observables for qutrits

Pulse-level operators

If you would like to learn more about the implementation of pulse-level control in PennyLane, see the pulse module documentation.

Pulse operator

ParametrizedEvolution

Parametrized evolution gate, created by passing a ParametrizedHamiltonian to the evolve() function

ParametrizedHamiltonian

Callable object holding the information representing a parametrized Hamiltonian.