Comprehensive Python library for simulating quantum systems dynamics and quantum information processing.
—
Construction of quantum operators including Pauli matrices, creation/annihilation operators, spin operators, and fundamental quantum mechanical operators.
Creation, annihilation, and number operators for harmonic oscillator systems.
def create(N: int, offset: int = 0) -> Qobj:
"""
Create creation operator â†.
Parameters:
- N: Dimension of Hilbert space
- offset: Lowest number state index
Returns:
- Qobj: Creation operator
"""
def destroy(N: int, offset: int = 0) -> Qobj:
"""
Create annihilation operator â.
Parameters:
- N: Dimension of Hilbert space
- offset: Lowest number state index
Returns:
- Qobj: Annihilation operator
"""
def num(N: int, offset: int = 0) -> Qobj:
"""
Create number operator â†â.
Parameters:
- N: Dimension of Hilbert space
- offset: Lowest number state index
Returns:
- Qobj: Number operator
"""
def position(N: int, offset: int = 0) -> Qobj:
"""
Create position operator x̂ = (↠+ â)/√2.
Parameters:
- N: Dimension of Hilbert space
- offset: Lowest number state index
Returns:
- Qobj: Position operator
"""
def momentum(N: int, offset: int = 0) -> Qobj:
"""
Create momentum operator p̂ = -i(↠- â)/√2.
Parameters:
- N: Dimension of Hilbert space
- offset: Lowest number state index
Returns:
- Qobj: Momentum operator
"""Pauli matrices and related two-level system operators.
def sigmax() -> Qobj:
"""
Create Pauli-X (σₓ) operator.
Returns:
- Qobj: Pauli-X matrix [[0,1],[1,0]]
"""
def sigmay() -> Qobj:
"""
Create Pauli-Y (σᵧ) operator.
Returns:
- Qobj: Pauli-Y matrix [[0,-i],[i,0]]
"""
def sigmaz() -> Qobj:
"""
Create Pauli-Z (σᵤ) operator.
Returns:
- Qobj: Pauli-Z matrix [[1,0],[0,-1]]
"""
def sigmap() -> Qobj:
"""
Create raising operator σ₊ = (σₓ + iσᵧ)/2.
Returns:
- Qobj: Raising operator [[0,1],[0,0]]
"""
def sigmam() -> Qobj:
"""
Create lowering operator σ₋ = (σₓ - iσᵧ)/2.
Returns:
- Qobj: Lowering operator [[0,0],[1,0]]
"""Angular momentum and spin operators for arbitrary spin values.
def jmat(j: float, op: str = None) -> Qobj:
"""
Create spin operator matrices.
Parameters:
- j: Spin quantum number (half-integer)
- op: 'x', 'y', 'z', '+', '-' for specific component, None for all
Returns:
- Qobj or tuple: Single operator or tuple (Jₓ, Jᵧ, Jᵤ) for op=None
"""
def spin_Jx(j: float) -> Qobj:
"""
Create x-component of spin operator.
Parameters:
- j: Spin quantum number
Returns:
- Qobj: Jₓ operator
"""
def spin_Jy(j: float) -> Qobj:
"""
Create y-component of spin operator.
Parameters:
- j: Spin quantum number
Returns:
- Qobj: Jᵧ operator
"""
def spin_Jz(j: float) -> Qobj:
"""
Create z-component of spin operator.
Parameters:
- j: Spin quantum number
Returns:
- Qobj: Jᵤ operator
"""
def spin_Jp(j: float) -> Qobj:
"""
Create raising operator J₊.
Parameters:
- j: Spin quantum number
Returns:
- Qobj: J₊ operator
"""
def spin_Jm(j: float) -> Qobj:
"""
Create lowering operator J₋.
Parameters:
- j: Spin quantum number
Returns:
- Qobj: J₋ operator
"""
def spin_J_set(j: float) -> tuple:
"""
Create complete set of spin operators.
Parameters:
- j: Spin quantum number
Returns:
- tuple: (Jₓ, Jᵧ, Jᵤ) operators
"""Creation and annihilation operators for fermionic systems.
def fcreate(N: int, i: int = 0) -> Qobj:
"""
Create fermionic creation operator with anti-commutation relations.
Parameters:
- N: Number of fermionic modes
- i: Mode index (0 to N-1)
Returns:
- Qobj: Fermionic creation operator
"""
def fdestroy(N: int, i: int = 0) -> Qobj:
"""
Create fermionic annihilation operator with anti-commutation relations.
Parameters:
- N: Number of fermionic modes
- i: Mode index (0 to N-1)
Returns:
- Qobj: Fermionic annihilation operator
"""Operators for continuous variable quantum optics.
def squeeze(N: int, z: complex, offset: int = 0) -> Qobj:
"""
Create single-mode squeezing operator S(z) = exp((z*↲ - z*â²)/2).
Parameters:
- N: Dimension of Hilbert space
- z: Complex squeezing parameter
- offset: Lowest number state index
Returns:
- Qobj: Squeezing operator
"""
def squeezing(a1: Qobj, a2: Qobj, z: complex) -> Qobj:
"""
Create two-mode squeezing operator.
Parameters:
- a1: Annihilation operator for mode 1
- a2: Annihilation operator for mode 2
- z: Complex squeezing parameter
Returns:
- Qobj: Two-mode squeezing operator
"""
def displace(N: int, alpha: complex, offset: int = 0) -> Qobj:
"""
Create displacement operator D(α) = exp(α↠- α*â).
Parameters:
- N: Dimension of Hilbert space
- alpha: Complex displacement amplitude
- offset: Lowest number state index
Returns:
- Qobj: Displacement operator
"""Identity, zero, and other fundamental operators.
def qeye(dimensions) -> Qobj:
"""
Create identity operator.
Parameters:
- dimensions: Integer dimension or list of dimensions for tensor products
Returns:
- Qobj: Identity operator
"""
def identity(dimensions) -> Qobj:
"""
Create identity operator (alias for qeye).
Parameters:
- dimensions: Integer dimension or list of dimensions
Returns:
- Qobj: Identity operator
"""
def qzero(dimensions) -> Qobj:
"""
Create zero operator.
Parameters:
- dimensions: Integer dimension or list of dimensions
Returns:
- Qobj: Zero operator
"""
def qdiags(diagonals, offsets=None, dims=None, shape=None) -> Qobj:
"""
Create diagonal operator from diagonal elements.
Parameters:
- diagonals: Array-like diagonal elements
- offsets: Integer or list of diagonal offsets
- dims: Dimensions for tensor structure
- shape: Matrix shape
Returns:
- Qobj: Diagonal operator
"""
def commutator(A: Qobj, B: Qobj, kind: str = 'normal') -> Qobj:
"""
Calculate commutator [A,B] = AB - BA or anti-commutator {A,B} = AB + BA.
Parameters:
- A: First operator
- B: Second operator
- kind: 'normal' for commutator, 'anti' for anti-commutator
Returns:
- Qobj: Commutator or anti-commutator
"""Phase, charge, tunneling, and other specialized quantum operators.
def phase(N: int, phi0: float = 0) -> Qobj:
"""
Create Pegg-Barnett phase operator.
Parameters:
- N: Dimension of Hilbert space
- phi0: Reference phase
Returns:
- Qobj: Phase operator
"""
def charge(Nmax: int, Nmin: int = None, fock: bool = True) -> Qobj:
"""
Create charge operator for superconducting circuits.
Parameters:
- Nmax: Maximum charge number
- Nmin: Minimum charge number
- fock: Use Fock basis if True
Returns:
- Qobj: Charge operator
"""
def tunneling(N: int, m: int = 1) -> Qobj:
"""
Create tunneling operator between adjacent sites.
Parameters:
- N: Number of sites
- m: Number of particles that tunnel
Returns:
- Qobj: Tunneling operator
"""
def qft(N: int, swap: bool = True) -> Qobj:
"""
Create quantum Fourier transform operator.
Parameters:
- N: Dimension (must be power of 2 for qubit systems)
- swap: Include bit-reversal swaps
Returns:
- Qobj: Quantum Fourier transform operator
"""
def swap(N: int = None, targets: list = [0, 1]) -> Qobj:
"""
Create swap operator for exchanging two systems.
Parameters:
- N: Total number of qubits (if None, inferred from targets)
- targets: List of two indices to swap
Returns:
- Qobj: Swap operator
"""Operators for three-level quantum systems.
def qutrit_ops() -> list:
"""
Create set of qutrit operators (Gell-Mann matrices).
Returns:
- list: List of 8 qutrit operators (3×3 Gell-Mann matrices)
"""import qutip as qt
import numpy as np
# Harmonic oscillator operators
N = 10 # Truncated Hilbert space
a = qt.destroy(N) # Annihilation operator
a_dag = qt.create(N) # Creation operator
n_op = qt.num(N) # Number operator
x_op = qt.position(N) # Position operator
p_op = qt.momentum(N) # Momentum operator
# Verify canonical commutation relation [â,â†] = 1
comm = qt.commutator(a, a_dag)
print(comm) # Should be identity
# Pauli operators for qubits
sx = qt.sigmax() # σₓ
sy = qt.sigmay() # σᵧ
sz = qt.sigmaz() # σᵤ
sp = qt.sigmap() # σ₊
sm = qt.sigmam() # σ₋
# Verify Pauli algebra: σₓσᵧ = iσᵤ
print(sx * sy - 1j * sz) # Should be zero
# Spin-1/2 operators (same as Pauli/2)
Jx, Jy, Jz = qt.jmat(0.5)
print(2 * Jx == sx) # True
# Spin-1 operators
J1x, J1y, J1z = qt.jmat(1.0)
print(J1x.shape) # (3, 3) for spin-1
# Squeezing and displacement
z = 0.5 # Squeezing parameter
alpha = 1.0 + 0.5j # Displacement amplitude
S = qt.squeeze(N, z) # Squeezing operator
D = qt.displace(N, alpha) # Displacement operator
# Create squeezed coherent state
psi = D * S * qt.basis(N, 0) # D(α)S(z)|0⟩
# Multi-mode operators
a1 = qt.tensor(qt.destroy(5), qt.qeye(5)) # Mode 1 in two-mode system
a2 = qt.tensor(qt.qeye(5), qt.destroy(5)) # Mode 2 in two-mode system
# Two-mode squeezing
S_two = qt.squeezing(a1, a2, 0.3)
# Fermionic operators for 3-mode system
c0_dag = qt.fcreate(3, 0) # Create fermion in mode 0
c1 = qt.fdestroy(3, 1) # Destroy fermion in mode 1
# Anti-commutation relation {cᵢ†,cⱼ} = δᵢⱼ
anticomm = qt.commutator(c0_dag, qt.fdestroy(3, 0), kind='anti')
print(anticomm) # Should be identity
# Custom Hamiltonians
H_ho = 0.5 * p_op**2 + 0.5 * x_op**2 # Harmonic oscillator
H_qubit = 0.5 * sz # Two-level system
H_coupled = qt.tensor(sz, sz) + 0.1 * qt.tensor(sx, sx) # Coupled qubits# All operator construction functions return Qobj instances with type='oper'
# representing quantum mechanical operators as matrices