ket.gates¶
Quantum gate definitions.
All quantum gates accept one or more Quant objects as input
and return them unchanged, enabling method chaining:
from ket import *
p = Process()
a, b = p.alloc(2)
S(X(a)) # Apply X then S on qubit `a`.
CNOT(H(a), b) # Apply H on `a`, then CNOT with `a` as control.
For parameterized gates (e.g., rotation gates), partial application is supported: if the qubit argument is omitted, a gate callable is returned instead of being applied immediately:
from math import pi
from ket import *
# Create reusable gate instances with pre-set angles
s_gate = P(pi / 2) # returns a callable, not applied yet
t_gate = P(pi / 4)
p = Process()
q = p.alloc()
s_gate(q) # now applied
t_gate(q)
Gates that accept two-qubit arguments (e.g., RXX,
CNOT) also support partial application: passing only the
angle returns a two-qubit gate callable.
Observable mode (with obs():):
Several single-qubit gate functions (X, Y,
Z, I) have a dual role: inside an
obs context manager block they construct
Pauli objects for Hamiltonian building rather than
applying the physical gate to the circuit.
Summary¶
Functions
Apply the Identity gate. |
|
Apply the Pauli X gate. |
|
Apply the Pauli Y gate. |
|
Apply the Pauli Z gate. |
|
Construct a binary-encoded QUBO observable for a qubit register. |
|
Apply the Hadamard gate. |
|
Apply the X-axes rotation gate. |
|
Apply the Y-axes rotation gate. |
|
Apply the Z-axes rotation gate. |
|
Apply the Phase shift gate. |
|
Apply the S gate. |
|
Apply the T gate. |
|
Apply the S-dagger gate. |
|
Apply the T-dagger gate. |
|
Apply the U3 gate. |
|
Apply the Controlled NOT gate. |
|
Apply the Multi-Controlled Z gate. |
|
Apply the SWAP gate. |
|
Apply the XX rotation gate. |
|
Apply the ZZ rotation gate. |
|
Apply the RYY rotation gate. |
|
Apply the Sqrt X gate. |
|
Apply a global phase to a quantum operation. |
|
Apply the Reconfigurable Beam Splitter (RBS) gate. |
|
Context manager for constructing quantum observables in symbolic form. |
|
Apply the Quantum Fourier Transform (QFT) to the given qubits. |
|
Time evolution \(e^{-iHt}\) for a Hamiltonian. |
Functions¶
- I(qubits: Quant) Quant[source]¶
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}\)
- X(qubits: Quant) Quant[source]¶
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[source]¶
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[source]¶
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}\)
- B(qubit: Quant)[source]¶
Construct a binary-encoded QUBO observable for a qubit register.
Returns a
Hamiltonianwith eigenvalue0for \(\left|0\right\rangle\) and eigenvalue1for \(\left|1\right\rangle\). When applied to a register, the result is the tensor product, giving eigenvalue equal to the binary integer encoded in the register:\[B = \frac{\mathbf{1} - Z}{2}\]This observable is essential for building QUBO Hamiltonians where the cost function is a polynomial over binary variables.
Example
from ket import * p = Process() q = p.alloc(3) # QUBO cost: x0 + x1*x2 - 2*x0*x2 cost = B(q[0]) + B(q[1]) * B(q[2]) - 2 * B(q[0]) * B(q[2])
- Parameters:
qubit – The qubit(s) to apply the observable to. If a multi-qubit register is passed, the result is the tensor product of \(B\) over all qubits.
- Returns:
The binary-encoded observable.
- H(qubits: Quant) Quant[source]¶
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}\)
- RX( ) Quant | Callable[[Quant], Quant][source]¶
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}\)
- RY( ) Quant | Callable[[Quant], Quant][source]¶
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}\)
- RZ( ) Quant | Callable[[Quant], Quant][source]¶
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}\)
- P( ) Quant | Callable[[Quant], Quant][source]¶
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}\)
- 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}\)
- 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}\)
- 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}\)
- 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( ) Quant | Callable[[Quant], Quant][source]¶
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}\)
- CNOT( ) tuple[Quant, Quant][source]¶
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}\)
- CZ(*qubits: Quant) tuple[Quant, ...][source]¶
Apply the Multi-Controlled Z gate.
Matrix
Effect
\(\begin{bmatrix}1 & 0 & \cdots & 0 & 0 \\ 0 & 1 & \cdots & 0 & 0 \\\vdots & \vdots & \ddots & \vdots & \vdots \\ 0 & 0 & \cdots & 1 & 0 \\0 & 0 & \cdots & 0 & -1\end{bmatrix}\)
\(\begin{matrix}\text{CZ}\left|0\cdots0\right> = & \left|0\cdots0\right> \\\text{CZ}\left|0\cdots1\right> = & \left|0\cdots1\right> \\\text{CZ}\left|1\cdots0\right> = & \left|1\cdots0\right> \\\text{CZ}\left|1\cdots1\right> = & -\left|1\cdots1\right> \\\end{matrix}\)
- SWAP( ) tuple[Quant, Quant][source]¶
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}\)
- RXX( ) tuple[Quant, Quant] | Callable[[Quant, Quant], tuple[Quant, Quant]][source]¶
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}\)
- RZZ( ) tuple[Quant, Quant] | Callable[[Quant, Quant], tuple[Quant, Quant]][source]¶
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}\)
- RYY( ) tuple[Quant, Quant] | Callable[[Quant, Quant], tuple[Quant, Quant]][source]¶
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}\)
- SX(qubits: Quant) Quant¶
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}\)
- global_phase(
- theta: float,
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
from math import pi from ket import * @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}\).
- RBS( ) tuple[Quant, Quant] | Callable[[Quant, Quant], tuple[Quant, Quant]][source]¶
Apply the Reconfigurable Beam Splitter (RBS) 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}\)
- obs(num_qubits: int | None = None)[source]¶
Context manager for constructing quantum observables in symbolic form.
Inside a
with obs():block, the Pauli gate functions (X,Y,Z,I) returnPauliobjects instead of applying the physical gate to the circuit. These objects can be combined with arithmetic operators to buildHamiltonianobjects suitable forexp_value.This approach mirrors the mathematical notation for observables and avoids manually constructing
Pauliinstances.Example
from ket import * p = Process() q = p.alloc(4) edges = [(q[0], q[1]), (q[1], q[2]), (q[2], q[3])] with obs(): # MaxCut QUBO cost Hamiltonian h_c = -0.5 * sum(1 - Z(i) * Z(j) for i, j in edges) ev = exp_value(h_c)
Pass
num_qubitsto build an observable before allocating any qubits. The block then yields that many placeholder qubits (ObsQubits) and the result is an unbound Hamiltonian. Bind it to real qubits withbind, the call shorthandh_c(qubits), or by passing the qubits toexp_valueorevolve:from ket import * with obs(4) as q: edges = [(q[0], q[1]), (q[1], q[2]), (q[2], q[3])] h_c = -0.5 * sum(1 - Z(i) * Z(j) for i, j in edges) p = Process() r = p.alloc(4) ev = exp_value(h_c, r) # same as exp_value(h_c(r))
- Parameters:
num_qubits – Number of placeholder qubits to yield. If
None, the block yieldsNoneand observables are built on process qubits.
- QFT(qubits, do_swap: bool = True)[source]¶
Apply the Quantum Fourier Transform (QFT) to the given qubits.
Implements the standard recursive QFT circuit:
\[\text{QFT}\left|x\right\rangle = \frac{1}{\sqrt{N}} \sum_{k=0}^{N-1} e^{2\pi i x k / N} \left|k\right\rangle\]where \(N = 2^n\) and \(n\) is the number of qubits.
The circuit consists of Hadamard gates and controlled phase rotations, followed by an optional SWAP network that puts the output in the standard bit-order (most-significant qubit first). Set
do_swap=Falsewhen the SWAP is handled externally.Example
from ket import * p = Process(simulator="dense", num_qubits=4) q = p.alloc(4) # Initialize to |1⟩ and apply QFT X(q[3]) QFT(q) state = dump(q) print(state.show())
- Parameters:
qubits – The register to apply the QFT to. Must have at least 1 qubit.
do_swap – If
True(default), applies the qubit-reversal SWAP network at the end so the output is in standard bit order.
- evolve(
- hamiltonian: Hamiltonian | Pauli | Callable[[Quant], Hamiltonian | Pauli],
- qubits: Quant | None = None,
Time evolution \(e^{-iHt}\) for a Hamiltonian.
For each Pauli term \(c_k P_k\) in the Hamiltonian, applies the corresponding single-step Trotter evolution gate \(e^{-i c_k P_k}\) to the qubits in that term.
Note
This implements a single Trotter step, not a full time evolution. For accurate dynamics, you may need to call
evolvemultiple times with a small coefficient or compose it with other techniques.Example
from ket import * p = Process() q = p.alloc(2) with obs(): # Transverse-field Ising: -J Z0Z1 - h X0 H_ising = -1.0 * Z(q[0]) * Z(q[1]) - 0.5 * X(q[0]) evolve(H_ising) # one Trotter step with t encoded in the coefficients with obs(2) as v: H_free = -1.0 * Z(v[0]) * Z(v[1]) - 0.5 * X(v[0]) evolve(H_free, q) # unbound Hamiltonian, same as evolve(H_free(q))
A function that builds the Hamiltonian from the qubits also works, so
partial(evolve, h)is a gate:def cost(edges, q): return -0.5 * sum(1 - Z(q[i]) * Z(q[j]) for i, j in edges) evolve(partial(cost, [(0, 1), (1, 2)]), q)
- Parameters:
hamiltonian – The Hamiltonian to simulate. The coefficient of each term serves as the rotation angle. It may also be a function that takes
qubitsand returns the Hamiltonian; it is called inside awith obs():block.qubits – Qubits to bind an unbound Hamiltonian to, or to call a Hamiltonian function on: placeholder
ibecomesqubits[i]. Required for an unbound Hamiltonian or a function, and not allowed for a bound Hamiltonian.