Comprehensive Python library for simulating quantum systems dynamics and quantum information processing.
—
The fundamental quantum object manipulation, state construction, and basic operations in QuTiP. The Qobj class serves as the foundation for all quantum computations.
The main quantum object class that represents quantum states, operators, and superoperators with automatic type detection and efficient sparse matrix storage.
class Qobj:
def __init__(self, inpt=None, dims=None, shape=None, type=None,
isherm=None, isunitary=None, copy=True):
"""
Create a quantum object.
Parameters:
- inpt: Array-like data or another Qobj
- dims: List of dimensions for tensor structure
- shape: Tuple of matrix dimensions
- type: 'ket', 'bra', 'oper', or 'super'
- isherm: Boolean indicating Hermiticity
- isunitary: Boolean indicating unitarity
- copy: Whether to copy input data
"""
def copy(self) -> 'Qobj':
"""Create a copy of the quantum object."""
def dag(self) -> 'Qobj':
"""Return the adjoint (conjugate transpose) of the quantum object."""
def conj(self) -> 'Qobj':
"""Return the complex conjugate of the quantum object."""
def trans(self) -> 'Qobj':
"""Return the transpose of the quantum object."""
def norm(self, norm=None, sparse=False, tol=0, maxiter=100000):
"""
Calculate the norm of the quantum object.
Parameters:
- norm: Type of norm ('tr', 'fro', 'one', 'max', 'l2')
- sparse: Use sparse eigensolver
- tol: Tolerance for sparse solver
- maxiter: Maximum iterations for sparse solver
Returns:
- float: The calculated norm
"""
def tr(self):
"""Calculate the trace of the quantum object."""
def ptrace(self, sel):
"""
Partial trace over specified subsystems.
Parameters:
- sel: Integer or list of integers specifying subsystems to trace out
Returns:
- Qobj: Reduced quantum object
"""
def eigenstates(self, sparse=False, sort='low', eigvals=0, tol=0, maxiter=100000):
"""
Calculate eigenvalues and eigenstates.
Parameters:
- sparse: Use sparse eigensolver
- sort: 'low' or 'high' for eigenvalue ordering
- eigvals: Number of eigenvalues to find (0 for all)
- tol: Tolerance for sparse solver
- maxiter: Maximum iterations
Returns:
- tuple: (eigenvalues, eigenstates)
"""
def eigenenergies(self, sparse=False, sort='low', eigvals=0, tol=0, maxiter=100000):
"""
Calculate eigenvalues only.
Parameters: Same as eigenstates()
Returns:
- array: Eigenvalues
"""
def expm(self, dtype=None) -> 'Qobj':
"""Calculate matrix exponential."""
def logm(self, dtype=None) -> 'Qobj':
"""Calculate matrix logarithm."""
def sqrtm(self, dtype=None) -> 'Qobj':
"""Calculate matrix square root."""
def unit(self, inplace=False) -> 'Qobj':
"""
Return normalized quantum object.
Parameters:
- inplace: Modify object in-place
Returns:
- Qobj: Normalized quantum object
"""
def tidyup(self, atol=None, rtol=None) -> 'Qobj':
"""
Remove small matrix elements.
Parameters:
- atol: Absolute tolerance
- rtol: Relative tolerance
Returns:
- Qobj: Cleaned quantum object
"""Functions for creating basic quantum objects from arrays and other data structures.
def qeye(dimensions) -> Qobj:
"""
Create identity operator.
Parameters:
- dimensions: Integer or list of dimensions
Returns:
- Qobj: Identity operator
"""
def qzero(dimensions) -> Qobj:
"""
Create zero operator.
Parameters:
- dimensions: Integer or list of dimensions
Returns:
- Qobj: Zero operator
"""
def qdiags(diagonals, offsets=None, dims=None, shape=None) -> Qobj:
"""
Create diagonal quantum object.
Parameters:
- diagonals: Array-like diagonal elements
- offsets: Integer or list of diagonal offsets
- dims: Dimensions for tensor structure
- shape: Matrix shape
Returns:
- Qobj: Diagonal quantum object
"""
def ket(label, dim=None) -> Qobj:
"""
Create ket state from label.
Parameters:
- label: String or integer label
- dim: Total dimension of Hilbert space
Returns:
- Qobj: Ket state
"""
def bra(label, dim=None) -> Qobj:
"""
Create bra state from label.
Parameters:
- label: String or integer label
- dim: Total dimension of Hilbert space
Returns:
- Qobj: Bra state
"""
def ket2dm(psi) -> Qobj:
"""
Convert ket to density matrix.
Parameters:
- psi: Input ket state
Returns:
- Qobj: Density matrix |ψ⟩⟨ψ|
"""Functions for working with composite quantum systems and reduced states.
def ptrace(obj: Qobj, sel) -> Qobj:
"""
Partial trace over specified subsystems.
Parameters:
- obj: Input quantum object
- sel: Integer or list of integers specifying subsystems to keep
Returns:
- Qobj: Reduced quantum object after tracing out unselected subsystems
"""import qutip as qt
import numpy as np
# Create quantum objects
psi = qt.basis(2, 0) # |0⟩ state in 2-level system
rho = psi * psi.dag() # Density matrix |0⟩⟨0|
H = qt.sigmaz() # Pauli-Z Hamiltonian
# Basic operations
psi_dag = psi.dag() # Conjugate transpose
norm_psi = psi.norm() # Norm calculation
trace_rho = rho.tr() # Trace
# Matrix functions
U = (-1j * H * 0.5).expm() # Time evolution operator e^(-iHt/2)
sqrt_rho = rho.sqrtm() # Matrix square root
# Eigenanalysis
evals, estates = H.eigenstates() # Find eigenvalues and eigenstates
ground_energy = H.groundstate()[0] # Ground state energy
# Two-qubit system partial trace
psi_00 = qt.tensor(qt.basis(2,0), qt.basis(2,0)) # |00⟩
rho_total = psi_00 * psi_00.dag() # Total density matrix
rho_A = qt.ptrace(rho_total, 0) # Reduced state of first qubit
# Create custom operators
data = np.array([[1, 0.5], [0.5, 0]])
custom_op = qt.Qobj(data) # Custom 2x2 operator
custom_op.isherm # Check if Hermitianclass Qobj:
"""
Quantum object representing states, operators, and superoperators.
Attributes:
data: Underlying matrix data (sparse or dense)
dims: List representing tensor structure dimensions
shape: Tuple of matrix dimensions (rows, cols)
type: String type ('ket', 'bra', 'oper', 'super')
isherm: Boolean indicating Hermiticity
isunitary: Boolean indicating unitarity
superrep: Superoperator representation ('super', 'choi', etc.)
"""