ket.gates

Quantum gate definitions.

All quantum gates take one or more Quant as input and return them at the end. This allows for concatenating quantum operations.

Example

from ket import *

p = Process()
a, b = p.alloc(2)

S(X(a))  # Apply a Pauli X followed by an S gate on `a`.

CNOT(H(a), b)  # Apply a Hadamard on `a` followed by a CNOT gate on `a` and `b`.

For gates that take classical parameters, such as rotation gates, if non-qubits are passed, it will return a new gate with the classical parameter set.

Example

from math import pi
from ket import *

s_gate = PHASE(pi/2)
t_gate = PHASE(pi/4)

p = Process()
q = p.alloc()

s_gate(q)
t_gate(q)

Functions ket.gates

CNOT(control_qubit, target_qubit)

Apply the Controlled NOT gate.

H(qubits)

Apply the Hadamard gate.

I(qubits)

Apply the Identity gate.

PHASE(theta[, qubits])

Apply the Phase shift gate.

RBS(theta, qubits_a, qubits_b)

Apply the Reconfigurable Beam Splitter (RBS) gate gate.

RX(theta[, qubits])

Apply the X-axes rotation gate.

RXX(theta, qubits_a, qubits_b)

Apply the XX rotation gate.

RY(theta[, qubits])

Apply the Y-axes rotation gate.

RYY(theta, qubits_a, qubits_b)

Apply the RYY rotation gate.

RZ(theta[, qubits])

Apply the Z-axes rotation gate.

RZZ(theta, qubits_a, qubits_b)

Apply the ZZ rotation gate.

S(qubits)

Apply the S gate.

SD(qubits)

Apply the S-dagger gate.

SWAP(qubit_a, qubit_b)

Apply the SWAP gate.

SX(*args[, ket_process])

Apply the Sqrt X gate.

T(qubits)

Apply the T gate.

TD(qubits)

Apply the T-dagger gate.

U3(theta, phi, lambda_[, qubit])

Apply the U3 gate.

X(qubits)

Apply the Pauli X gate.

Y(qubits)

Apply the Pauli Y gate.

Z(qubits)

Apply the Pauli Z gate.

global_phase(theta)

Apply a global phase to a quantum operation.

CNOT(control_qubit: Quant, target_qubit: Quant) tuple[Quant, Quant]

Apply the Controlled NOT gate.

Matrix, Effect

\(\begin{bmatrix}1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0\end{bmatrix}\)

\(\begin{matrix}\text{CNOT}\left|00\right> = & \left|00\right> \\\text{CNOT}\left|01\right> = & \left|01\right> \\\text{CNOT}\left|10\right> = & \left|11\right> \\\text{CNOT}\left|11\right> = & \left|10\right> \\\text{CNOT}\left|\text{c}\right>\left|\text{t}\right> =& \left|\text{c}\right> \left|\text{c}\oplus\text{t}\right>\end{matrix}\)

H(qubits: Quant) Quant

Apply the Hadamard gate.

Matrix, Effect

\(\frac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}\)

\(\begin{matrix}H\left|0\right> = & \frac{1}{\sqrt{2}}\left(\left|0\right> + \left|1\right>\right) \\H\left|1\right> = & \frac{1}{\sqrt{2}}\left(\left|0\right> - \left|1\right>\right)\end{matrix}\)

I(qubits: Quant) Quant

Apply the Identity gate.

Matrix, Effect

\(\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}\)

\(\begin{matrix} I\left|0\right> = & \left|0\right> \\I\left|1\right> = & \left|1\right> \end{matrix}\)

PHASE(theta: float, qubits: Quant | None = None) Quant | Callable[[Quant], Quant]

Apply the Phase shift gate.

Matrix, Effect

\(\begin{bmatrix} 1 & 0 \\ 0 & e^{i\theta} \end{bmatrix}\)

\(\begin{matrix} P\left|0\right> = & \left|0\right> \\P\left|1\right> = & e^{i\theta}\left|1\right> \end{matrix}\)

RBS(theta: float, qubits_a: Quant | None, qubits_b: Quant | None) tuple[Quant, Quant] | Callable[[Quant, Quant], tuple[Quant, Quant]]

Apply the Reconfigurable Beam Splitter (RBS) gate gate.

Matrix

\(\begin{bmatrix}1 & 0 & 0 & 0 \\0 & \cos\theta & \sin\theta & 0 \\0 & -\sin\theta & \cos\theta & 0 \\0 & 0 & 0 & 1\end{bmatrix}\)

RX(theta: float, qubits: Quant | None = None) Quant | Callable[[Quant], Quant]

Apply the X-axes rotation gate.

Matrix, Effect

\(\begin{bmatrix}\cos(\theta/2) & -i\sin(\theta/2) \\ -i\sin(\theta/2) & \cos(\theta/2)\end{bmatrix}\)

\(\begin{matrix}R_x\left|0\right> = & \cos(\theta/2)\left|0\right> - i\sin(\theta/2)\left|1\right> \\R_x\left|1\right> = & -i\sin(\theta/2)\left|0\right> + \cos(\theta/2)\left|1\right>\end{matrix}\)

RXX(theta: float, qubits_a: Quant | None, qubits_b: Quant | None) tuple[Quant, Quant] | Callable[[Quant, Quant], tuple[Quant, Quant]]

Apply the XX rotation gate.

Matrix

\(\begin{bmatrix} \cos\frac{\theta}{2} & 0 & 0 & -i\sin\frac{\theta}{2} \\0 & \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} & 0 \\0 & -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} & 0 \\-i\sin\frac{\theta}{2} & 0 & 0 & \cos\frac{\theta}{2} \end{bmatrix}\)

RY(theta: float, qubits: Quant | None = None) Quant | Callable[[Quant], Quant]

Apply the Y-axes rotation gate.

Matrix, Effect

\(\begin{bmatrix}\cos(\theta/2) & -\sin(\theta/2) \\ \sin(\theta/2) & \cos(\theta/2)\end{bmatrix}\)

\(\begin{matrix}R_y\left|0\right> = & \cos(\theta/2)\left|0\right> + \sin(\theta/2)\left|1\right> \\R_y\left|1\right> = & -\sin(\theta/2)\left|0\right> + \cos(\theta/2)\left|1\right>\end{matrix}\)

RYY(theta: float, qubits_a: Quant | None, qubits_b: Quant | None) tuple[Quant, Quant] | Callable[[Quant, Quant], tuple[Quant, Quant]]

Apply the RYY rotation gate.

Matrix

\(\begin{bmatrix}\cos\frac{\theta}{2} & 0 & 0 & i\sin\frac{\theta}{2} \\0 & \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} & 0 \\0 & -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} & 0 \\i\sin\frac{\theta}{2} & 0 & 0 & \cos\frac{\theta}{2}\end{bmatrix}\)

RZ(theta: float, qubits: Quant | None = None) Quant | Callable[[Quant], Quant]

Apply the Z-axes rotation gate.

Matrix, Effect

\(\begin{bmatrix} e^{-i\theta/2} & 0 \\ 0 & e^{i\theta/2} \end{bmatrix}\)

\(\begin{matrix} R_z\left|0\right> = & e^{-i\theta/2}\left|0\right> \\R_z\left|1\right> = & e^{i\theta/2}\left|1\right> \end{matrix}\)

RZZ(theta: float, qubits_a: Quant | None, qubits_b: Quant | None) tuple[Quant, Quant] | Callable[[Quant, Quant], tuple[Quant, Quant]]

Apply the ZZ rotation gate.

Matrix

\(\begin{bmatrix} e^{-i \frac{\theta}{2}} & 0 & 0 & 0 \\0 & e^{i \frac{\theta}{2}} & 0 & 0\\ 0 & 0 & e^{i \frac{\theta}{2}} & 0 \\0 & 0 & 0 & e^{-i \frac{\theta}{2}} \end{bmatrix}\)

S(qubits: Quant) Quant

Apply the S gate.

Matrix, Effect

\(\begin{bmatrix} 1 & 0 \\ 0 & i \end{bmatrix}\)

\(\begin{matrix} S\left|0\right> = & \left|0\right> \\S\left|1\right> = & i\left|1\right> \end{matrix}\)

SD(qubits: Quant) Quant

Apply the S-dagger gate.

Matrix, Effect

\(\begin{bmatrix} 1 & 0 \\ 0 & -i \end{bmatrix}\)

\(\begin{matrix} S^\dagger\left|0\right> = & \left|0\right> \\S^\dagger\left|1\right> = & -i\left|1\right> \end{matrix}\)

SWAP(qubit_a: Quant, qubit_b: Quant) tuple[Quant, Quant]

Apply the SWAP gate.

Matrix, Effect

\(\begin{bmatrix}1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1\end{bmatrix}\)

\(\begin{matrix}\text{SWAP}\left|00\right> = & \left|00\right> \\\text{SWAP}\left|01\right> = & \left|10\right> \\\text{SWAP}\left|10\right> = & \left|01\right> \\\text{SWAP}\left|11\right> = & \left|11\right> \\\text{SWAP}\left|\text{a}\right>\left|\text{b}\right> =& \left|\text{b}\right> \left|\text{a}\right>\end{matrix}\)

SX(*args, ket_process: Process | None = None, **kwargs)

Apply the Sqrt X gate.

Matrix, Effect

\(\frac{1}{2} \begin{bmatrix} 1+i & 1-i \\ 1-i & 1+i \end{bmatrix}\)

\(\begin{matrix}\sqrt{X}\left|0\right> = & \frac{1}{2} ((1+i)\left|0\right> + (1-i)\left|1\right>) \\\sqrt{X}\left|1\right> = & \frac{1}{2} ((1-i)\left|0\right> + (1+i)\left|1\right>)\end{matrix}\)

T(qubits: Quant) Quant

Apply the T gate.

Matrix, Effect

\(\begin{bmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{bmatrix}\)

\(\begin{matrix} T\left|0\right> = & \left|0\right> \\T\left|1\right> = & e^{i\pi/4}\left|1\right> \end{matrix}\)

TD(qubits: Quant) Quant

Apply the T-dagger gate.

Matrix, Effect

\(\begin{bmatrix} 1 & 0 \\ 0 & e^{-i\pi/4} \end{bmatrix}\)

\(\begin{matrix} T^\dagger\left|0\right> = & \left|0\right> \\T^\dagger\left|1\right> = & e^{-i\pi/4}\left|1\right> \end{matrix}\)

U3(theta: float, phi: float, lambda_: float, qubit: Quant | None = None) Quant | Callable[[Quant], Quant]

Apply the U3 gate.

Matrix, Effect

\(\begin{bmatrix}e^{-i (\phi + \lambda)/2} \cos(\theta/2) & -e^{-i (\phi - \lambda)/2} \sin(\theta/2) \\e^{i (\phi - \lambda)/2} \sin(\theta/2) & e^{i (\phi + \lambda)/2} \cos(\theta/2)\end{bmatrix}\)

\(\begin{matrix}U3\left|0\right> = & e^{-i (\phi + \lambda)/2} \cos(\theta/2)\left|0\right>+ e^{i (\phi - \lambda)/2} \sin(\theta/2) \left|1\right> \\U3\left|1\right> = & -e^{-i (\phi - \lambda)/2} \sin(\theta/2)\left|0\right>+ e^{i (\phi + \lambda)/2} \cos(\theta/2)\left|1\right> \\\end{matrix}\)

X(qubits: Quant) Quant

Apply the Pauli X gate.

Matrix, Effect

\(\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\)

\(\begin{matrix} X\left|0\right> = & \left|1\right> \\X\left|1\right> = & \left|0\right> \end{matrix}\)

Y(qubits: Quant) Quant

Apply the Pauli Y gate.

Matrix, Effect

\(\begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix}\)

\(\begin{matrix} Y\left|0\right> = & i\left|1\right> \\Y\left|1\right> = & -i\left|0\right> \end{matrix}\)

Z(qubits: Quant) Quant

Apply the Pauli Z gate.

Matrix, Effect

\(\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}\)

\(\begin{matrix} Z\left|0\right> = & \left|0\right> \\Z\left|1\right> = & -\left|1\right> \end{matrix}\)

global_phase(theta: float) Callable[[Callable[[Any], Any]], Callable[[Any], Any]]

Apply a global phase to a quantum operation.

Decorator that adds a global phase \(e^{i\theta}\) to a quantum gate \(U\), creating the gate \(e^{i\theta}U\).

In quantum computation, global phases are overall factors that can be applied to quantum states without affecting the measurement outcomes. Mathematically, they represent rotations in the complex plane and are usually ignored because they have no observable consequences. However, in certain contexts, such as controlled quantum operations, the global phase can affect the behavior of the operation.

The addition of a global phase can be important for maintaining consistency in quantum algorithms, particularly when dealing with controlled operations where relative phase differences between different components of the quantum state can impact the computation.

Example:

@global_phase(pi / 2)
def my_z_gate(qubit):
    return RZ(pi, qubit)

This example defines a custom quantum gate equivalent to a Pauli Z operation, where \(Z = e^{i\frac{\pi}{2}}R_z(\pi)\).

Parameters:

theta – The \(\theta\) angle of the global phase \(e^{i\theta}\).