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
QubitUnitary(U, wires) Apply an arbitrary 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 Fock basis.

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}R_x(\phi) = \frac{1}{2}\left[R_x(\phi+\pi/2)+R_x(\phi-\pi/2)\right]\)
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}R_y(\phi) = \frac{1}{2}\left[R_y(\phi+\pi/2)+R_y(\phi-\pi/2)\right]\)
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}R_z(\phi) = \frac{1}{2}\left[R_z(\phi+\pi/2)+R_z(\phi-\pi/2)\right]\)
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}R_\phi(\phi) = \frac{1}{2}\left[R_\phi(\phi+\pi/2)+R_\phi(\phi-\pi/2)\right]\)
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: 1
  • Gradient recipe: \(\frac{d}{d\phi}R(\phi) = \frac{1}{2}\left[R(\phi+\pi/2)+R(\phi-\pi/2)\right]\). 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 unitary matrix

Details:

  • Number of wires: None (applied to the entire system)
  • Number of parameters: 1
  • Gradient recipe: None (uses finite difference)
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: None (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 Fock basis.

Details:

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