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.T(wires=0).inv()
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.
|
Create the adjoint of an Operator or a function that applies the adjoint of the provided function. |
|
Create a method that applies a controlled version of the provided method. |
|
Condition a quantum operation on the results of mid-circuit qubit measurements. |
|
The matrix representation of an operation or quantum circuit. |
|
The eigenvalues of one or more operations. |
|
Returns the generator of an operation. |
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 ]]
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)
tensor([[0.9553+0.0000j, 0.0000-0.2955j],
[0.0000-0.2955j, 0.9553+0.0000j]], grad_fn=<AddBackward0>)
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.5910)
Some operator transform can also act on multiple operators, by passing quantum functions, QNodes or tapes:
>>> def circuit(theta):
... qml.RX(theta, wires=1)
... qml.PauliZ(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]])
Qubit operators¶
Non-parametrized gates¶
The identity observable \(\I\). The Hadamard operator The Pauli X operator The Pauli Y operator The Pauli Z operator The single-qubit phase gate The single-qubit T gate The single-qubit Square-Root X operator. The controlled-NOT operator The controlled-Z operator The controlled-Y operator The swap operator The i-swap operator An echoed RZX(pi/2) gate. The square root of i-swap operator. alias of The controlled-swap operator Toffoli (controlled-controlled-X) gate. Apply a Pauli X gate controlled on an arbitrary computational basis state. The Barrier operator, used to separate the compilation process into blocks or as a visual tool. The wire cut operation, used to manually mark locations for wire cuts.
pennylane.ops.qubit.non_parametric_ops.SISWAP
Parametrized gates¶
Arbitrary single qubit rotation The single qubit X rotation The single qubit Y rotation The single qubit Z rotation Arbitrary multi Z rotation. Arbitrary Pauli word rotation. Arbitrary single qubit local phase shift A qubit controlled phase shift. alias of The controlled-RX operator The controlled-RY operator The controlled-RZ operator The controlled-Rot operator U1 gate. U2 gate. Arbitrary single qubit unitary. Ising XX coupling gate Ising (XX + YY) coupling gate Ising YY coupling gate Ising ZZ coupling gate
pennylane.ops.qubit.parametric_ops.ControlledPhaseShift
Quantum chemistry gates¶
Single excitation rotation. Single excitation rotation with positive phase-shift outside the rotation subspace. Single excitation rotation with negative phase-shift outside the rotation subspace. Double excitation rotation. Double excitation rotation with positive phase-shift outside the rotation subspace. Double excitation rotation with negative phase-shift outside the rotation subspace. Spin-adapted spatial orbital 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¶
Apply an arbitrary fixed unitary matrix. Apply an arbitrary fixed unitary to Apply an arbitrary fixed diagonal unitary matrix.
wires
with control from the control_wires
.
Gates performing arithmetics¶
Apply the Apply a
QubitCarry
operation to four input wires.
QubitSum
operation on three input wires.
State preparation¶
Prepares a single computational basis state. Prepare subsystems using the given ket vector in the computational basis. Prepare subsystems using the given density matrix.
Noisy channels¶
Single-qubit amplitude damping error channel. Single-qubit generalized amplitude damping error channel. Single-qubit phase damping error channel. Single-qubit symmetrically depolarizing error channel. Single-qubit bit flip (Pauli \(X\)) error channel. Single-qubit bit flip (Pauli \(Z\)) error channel. Single-qubit Reset error channel. Pauli operator error channel for an arbitrary number of qubits. Apply an arbitrary fixed quantum channel. Thermal relaxation error channel.
Observables¶
The Hadamard operator An arbitrary Hermitian observable. The identity observable \(\I\). The Pauli X operator The Pauli Y operator The Pauli Z operator Observable corresponding to the computational basis state projector \(P=\ket{i}\bra{i}\). Operator representing a Hamiltonian. 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¶
The identity observable \(\I\). Beamsplitter interaction. Controlled addition operation. Controlled phase operation. Cross-Kerr interaction. Cubic phase shift. Phase space displacement. A linear interferometer transforming the bosonic operators according to the unitary matrix \(U\). Kerr interaction. Quadratic phase shift. Phase space rotation. Phase space squeezing. Phase space two-mode squeezing.
CV state preparation¶
Prepares a cat state. Prepares a coherent state. Prepares a displaced squeezed vacuum state. Prepare subsystems using the given density matrix in the Fock basis. Prepares a single Fock state. Prepare subsystems using the given ket vector in the Fock basis. Prepare subsystems in a given Gaussian state. Prepares a squeezed vacuum state. Prepares a thermal state.
CV observables¶
The number state observable \(\ket{n}\bra{n}\). The identity observable \(\I\). The photon number observable \(\langle \hat{n}\rangle\). The tensor product of the The momentum quadrature observable \(\hat{p}\). An arbitrary second-order polynomial observable. The generalized quadrature observable \(\x_\phi = \x cos\phi+\p\sin\phi\). The position quadrature observable \(\hat{x}\).
NumberOperator
acting on different wires.