# Qubit quantum operations¶

Module name: pennylane.ops.qubit

This section contains the available built-in discrete-variable quantum operations supported by PennyLane, as well as their conventions.

## Gates¶

 Hadamard(wires) The Hadamard operator PauliX(wires) The Pauli X operator PauliY(wires) The Pauli Y operator PauliZ(wires) The Pauli Z operator CNOT(wires) The controlled-NOT operator CZ(wires) The controlled-Z operator SWAP(wires) The swap operator RX(phi, wires) The single qubit X rotation RY(phi, wires) The single qubit Y rotation RZ(phi, wires) The single qubit Z rotation PhaseShift(phi, wires) Arbitrary single qubit local phase shift Rot(phi, theta, omega, wires) Arbitrary single qubit rotation CRX(phi, wires) The controlled-RX operator CRY(phi, wires) The controlled-RY operator CRZ(phi, wires) The controlled-RZ operator CRot(phi, theta, omega, wires) The controlled-Rot operator QubitUnitary(U, wires) Apply an arbitrary fixed unitary matrix.

## State preparation¶

 BasisState(n, wires) Prepares a single computational basis state. QubitStateVector(state, wires) Prepare subsystems using the given ket vector in the computational basis.

## Observables¶

 Hadamard(wires) The Hadamard operator PauliX(wires) The Pauli X operator PauliY(wires) The Pauli Y operator PauliZ(wires) The Pauli Z operator Hermitian(A, wires) An arbitrary Hermitian observable.

### Code details¶

class Hadamard(wires)[source]

The Hadamard operator

$\begin{split}H = \frac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1\\ 1 & -1\end{bmatrix}.\end{split}$

Details:

• Number of wires: 1
• Number of parameters: 0
Parameters: wires (Sequence[int] or int) – the wire the operation acts on
class PauliX(wires)[source]

The Pauli X operator

$\begin{split}\sigma_x = \begin{bmatrix} 0 & 1 \\ 1 & 0\end{bmatrix}.\end{split}$

Details:

• Number of wires: 1
• Number of parameters: 0
Parameters: wires (Sequence[int] or int) – the wire the operation acts on
class PauliY(wires)[source]

The Pauli Y operator

$\begin{split}\sigma_y = \begin{bmatrix} 0 & -i \\ i & 0\end{bmatrix}.\end{split}$

Details:

• Number of wires: 1
• Number of parameters: 0
Parameters: wires (Sequence[int] or int) – the wire the operation acts on
class PauliZ(wires)[source]

The Pauli Z operator

$\begin{split}\sigma_z = \begin{bmatrix} 1 & 0 \\ 0 & -1\end{bmatrix}.\end{split}$

Details:

• Number of wires: 1
• Number of parameters: 0
Parameters: wires (Sequence[int] or int) – the wire the operation acts on
class CNOT(wires)[source]

The controlled-NOT operator

$\begin{split}CNOT = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 1\\ 0 & 0 & 1 & 0 \end{bmatrix}.\end{split}$

Note

The first wire provided corresponds to the control qubit.

Details:

• Number of wires: 2
• Number of parameters: 0
Parameters: wires (Sequence[int] or int) – the wires the operation acts on
class CZ(wires)[source]

The controlled-Z operator

$\begin{split}CZ = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & -1 \end{bmatrix}.\end{split}$

Note

The first wire provided corresponds to the control qubit.

Details:

• Number of wires: 2
• Number of parameters: 0
Parameters: wires (Sequence[int] or int) – the wires the operation acts on
class SWAP(wires)[source]

The swap operator

$\begin{split}SWAP = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0\\ 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 1 \end{bmatrix}.\end{split}$

Note

The first wire provided corresponds to the control qubit.

Details:

• Number of wires: 2
• Number of parameters: 0
Parameters: wires (Sequence[int] or int) – the wires the operation acts on
class RX(phi, wires)[source]

The single qubit X rotation

$\begin{split}R_x(\phi) = e^{-i\phi\sigma_x/2} = \begin{bmatrix} \cos(\phi/2) & -i\sin(\phi/2) \\ -i\sin(\phi/2) & \cos(\phi/2) \end{bmatrix}.\end{split}$

Details:

• Number of wires: 1
• Number of parameters: 1
• Gradient recipe: $$\frac{d}{d\phi}f(R_x(\phi)) = \frac{1}{2}\left[f(R_x(\phi+\pi/2)) - f(R_x(\phi-\pi/2))\right]$$ where $$f$$ is an expectation value depending on $$R_x(\phi)$$.
Parameters: phi (float) – rotation angle $$\phi$$ wires (Sequence[int] or int) – the wire the operation acts on
class RY(phi, wires)[source]

The single qubit Y rotation

$\begin{split}R_y(\phi) = e^{-i\phi\sigma_y/2} = \begin{bmatrix} \cos(\phi/2) & -\sin(\phi/2) \\ \sin(\phi/2) & \cos(\phi/2) \end{bmatrix}.\end{split}$

Details:

• Number of wires: 1
• Number of parameters: 1
• Gradient recipe: $$\frac{d}{d\phi}f(R_y(\phi)) = \frac{1}{2}\left[f(R_y(\phi+\pi/2)) - f(R_y(\phi-\pi/2))\right]$$ where $$f$$ is an expectation value depending on $$R_y(\phi)$$.
Parameters: phi (float) – rotation angle $$\phi$$ wires (Sequence[int] or int) – the wire the operation acts on
class RZ(phi, wires)[source]

The single qubit Z rotation

$\begin{split}R_z(\phi) = e^{-i\phi\sigma_z/2} = \begin{bmatrix} e^{-i\phi/2} & 0 \\ 0 & e^{i\phi/2} \end{bmatrix}.\end{split}$

Details:

• Number of wires: 1
• Number of parameters: 1
• Gradient recipe: $$\frac{d}{d\phi}f(R_z(\phi)) = \frac{1}{2}\left[f(R_z(\phi+\pi/2)) - f(R_z(\phi-\pi/2))\right]$$ where $$f$$ is an expectation value depending on $$R_z(\phi)$$.
Parameters: phi (float) – rotation angle $$\phi$$ wires (Sequence[int] or int) – the wire the operation acts on
class PhaseShift(phi, wires)[source]

Arbitrary single qubit local phase shift

$\begin{split}R_\phi(\phi) = e^{i\phi/2}R_z(\phi) = \begin{bmatrix} 1 & 0 \\ 0 & e^{i\phi} \end{bmatrix}.\end{split}$

Details:

• Number of wires: 1
• Number of parameters: 1
• Gradient recipe: $$\frac{d}{d\phi}f(R_\phi(\phi)) = \frac{1}{2}\left[f(R_\phi(\phi+\pi/2)) - f(R_\phi(\phi-\pi/2))\right]$$ where $$f$$ is an expectation value depending on $$R_{\phi}(\phi)$$.
Parameters: phi (float) – rotation angle $$\phi$$ wires (Sequence[int] or int) – the wire the operation acts on
class Rot(phi, theta, omega, wires)[source]

Arbitrary single qubit rotation

$\begin{split}R(\phi,\theta,\omega) = RZ(\omega)RY(\theta)RZ(\phi)= \begin{bmatrix} e^{-i(\phi+\omega)/2}\cos(\theta/2) & -e^{i(\phi-\omega)/2}\sin(\theta/2) \\ e^{-i(\phi-\omega)/2}\sin(\theta/2) & e^{i(\phi+\omega)/2}\cos(\theta/2) \end{bmatrix}.\end{split}$

Details:

• Number of wires: 1
• Number of parameters: 3
• Gradient recipe: $$\frac{d}{d\phi}f(R(\phi, \theta, \omega)) = \frac{1}{2}\left[f(R(\phi+\pi/2, \theta, \omega)) - f(R(\phi-\pi/2, \theta, \omega))\right]$$ where $$f$$ is an expectation value depending on $$R(\phi, \theta, \omega)$$. This gradient recipe applies for each angle argument $$\{\phi, \theta, \omega\}$$.
Parameters: phi (float) – rotation angle $$\phi$$ theta (float) – rotation angle $$\theta$$ omega (float) – rotation angle $$\omega$$ wires (Sequence[int] or int) – the wire the operation acts on
class CRX(phi, wires)[source]

The controlled-RX operator

$\begin{split}CR_x(\phi) = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0\\ 0 & 0 & \cos(\phi/2) & -i\sin(\phi/2)\\ 0 & 0 & -i\sin(\phi/2) & \cos(\phi/2) \end{bmatrix}.\end{split}$

Note

The first wire provided corresponds to the control qubit.

Details:

• Number of wires: 2
• Number of parameters: 1
• Gradient recipe: $$\frac{d}{d\phi}f(CR_x(\phi)) = \frac{1}{2}\left[f(CR_x(\phi+\pi/2)) - f(CR_x(\phi-\pi/2))\right]$$ where $$f$$ is an expectation value depending on $$CR_x(\phi)$$.
Parameters: phi (float) – rotation angle $$\phi$$ wires (Sequence[int] or int) – the wire the operation acts on
class CRY(phi, wires)[source]

The controlled-RY operator

$\begin{split}CR_y(\phi) = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0\\ 0 & 0 & \cos(\phi/2) & -\sin(\phi/2)\\ 0 & 0 & \sin(\phi/2) & \cos(\phi/2) \end{bmatrix}.\end{split}$

Note

The first wire provided corresponds to the control qubit.

Details:

• Number of wires: 2
• Number of parameters: 1
• Gradient recipe: $$\frac{d}{d\phi}f(CR_y(\phi)) = \frac{1}{2}\left[f(CR_y(\phi+\pi/2)) - f(CR_y(\phi-\pi/2))\right]$$ where $$f$$ is an expectation value depending on $$CR_y(\phi)$$.
Parameters: phi (float) – rotation angle $$\phi$$ wires (Sequence[int] or int) – the wire the operation acts on
class CRZ(phi, wires)[source]

The controlled-RZ operator

$\begin{split}CR_z(\phi) = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0\\ 0 & 0 & e^{-i\phi/2} & 0\\ 0 & 0 & 0 & e^{i\phi/2} \end{bmatrix}.\end{split}$

Note

The first wire provided corresponds to the control qubit.

Details:

• Number of wires: 2
• Number of parameters: 1
• Gradient recipe: $$\frac{d}{d\phi}f(CR_z(\phi)) = \frac{1}{2}\left[f(CR_z(\phi+\pi/2)) - f(CR_z(\phi-\pi/2))\right]$$ where $$f$$ is an expectation value depending on $$CR_z(\phi)$$.
Parameters: phi (float) – rotation angle $$\phi$$ wires (Sequence[int] or int) – the wire the operation acts on
class CRot(phi, theta, omega, wires)[source]

The controlled-Rot operator

$\begin{split}CR(\phi, \theta, \omega) = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0\\ 0 & 0 & e^{-i(\phi+\omega)/2}\cos(\theta/2) & -e^{i(\phi-\omega)/2}\sin(\theta/2)\\ 0 & 0 & e^{-i(\phi-\omega)/2}\sin(\theta/2) & e^{i(\phi+\omega)/2}\cos(\theta/2) \end{bmatrix}.\end{split}$

Note

The first wire provided corresponds to the control qubit.

Details:

• Number of wires: 2
• Number of parameters: 3
• Gradient recipe: $$\frac{d}{d\phi}f(CR(\phi, \theta, \omega)) = \frac{1}{2}\left[f(CR(\phi+\pi/2, \theta, \omega)) - f(CR(\phi-\pi/2, \theta, \omega))\right]$$ where $$f$$ is an expectation value depending on $$CR(\phi, \theta, \omega)$$. This gradient recipe applies for each angle argument $$\{\phi, \theta, \omega\}$$.
Parameters: phi (float) – rotation angle $$\phi$$ theta (float) – rotation angle $$\theta$$ omega (float) – rotation angle $$\omega$$ wires (Sequence[int] or int) – the wire the operation acts on
class QubitUnitary(U, wires)[source]

Apply an arbitrary fixed unitary matrix.

Details:

• Number of wires: The operation can act on any number of wires.
• Number of parameters: 1
• Gradient recipe: None
Parameters: U (array[complex]) – square unitary matrix wires (Sequence[int] or int) – the wire(s) the operation acts on
class BasisState(n, wires)[source]

Prepares a single computational basis state.

Details:

• Number of wires: All (applied to the entire system)
• Number of parameters: 1
• Gradient recipe: None (integer parameters not supported)
Parameters: n (array) – prepares the basis state $$\ket{n}$$, where n is an array of integers from the set $$\{0, 1\}$$, i.e., if n = np.array([0, 1, 0]), prepares the state $$|010\rangle$$. wires (Sequence[int] or int) – the wire(s) the operation acts on
class QubitStateVector(state, wires)[source]

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

Details:

• Number of wires: All (applied to the entire system)
• Number of parameters: 1
• Gradient recipe: None
Parameters: state (array[complex]) – a state vector of size 2**len(wires) wires (Sequence[int] or int) – the wire(s) the operation acts on
class Hermitian(A, wires)[source]

An arbitrary Hermitian observable.

For a Hermitian matrix $$A$$, the expectation command returns the value

$\braket{A} = \braketT{\psi}{\cdots \otimes I\otimes A\otimes I\cdots}{\psi}$

where $$A$$ acts on the requested wires.

If acting on $$N$$ wires, then the matrix $$A$$ must be of size $$2^N\times 2^N$$.

Details:

• Number of wires: Any
• Number of parameters: 1
• Gradient recipe: None
Parameters: A (array) – square hermitian matrix wires (Sequence[int] or int) – the wire(s) the operation acts on