CuDensityMat API Reference
CuQuantum.CuDensityMat — Module
CuDensityMatJulia bindings for NVIDIA's cuDensityMat library — high-performance density matrix simulation for analog quantum dynamics (Lindblad master equation, time-dependent Hamiltonians, backward differentiation/gradients).
Part of the CuQuantum.jl package.
CuQuantum.CuDensityMat.DenseMixedState — Type
DenseMixedState{T}(ws, hilbert_space_dims; batch_size=1)Mixed state in dense (density-matrix) representation.
Arguments
ws::WorkStream: Execution contexthilbert_space_dims::Vector{Int}orTuple: Local Hilbert space dimensionsbatch_size::Int=1: Batch dimension
Example
ws = WorkStream()
rho = DenseMixedState{ComplexF64}(ws, (2, 2))
allocate_storage!(rho)CuQuantum.CuDensityMat.DensePureState — Type
DensePureState{T}(ws, hilbert_space_dims; batch_size=1)Pure state in dense (state-vector) representation.
Arguments
ws::WorkStream: Execution contexthilbert_space_dims::Vector{Int}orTuple: Local Hilbert space dimensionsbatch_size::Int=1: Batch dimension
Example
ws = WorkStream()
psi = DensePureState{ComplexF64}(ws, (2, 2, 2))
allocate_storage!(psi)CuQuantum.CuDensityMat.ElementaryOperator — Type
ElementaryOperatorWraps a cudensitymatElementaryOperator_t handle. Represents a single-mode operator that can be dense or multidiagonal.
Create via create_elementary_operator or create_elementary_operator_batch.
CuQuantum.CuDensityMat.Expectation — Type
ExpectationWraps a cudensitymatExpectation_t handle for computing expectation values of an operator with respect to a quantum state.
CuQuantum.CuDensityMat.MatrixOperator — Type
MatrixOperatorWraps a cudensitymatMatrixOperator_t handle. Represents a full Hilbert-space dense matrix operator stored locally on a single GPU.
CuQuantum.CuDensityMat.Operator — Type
OperatorWraps a cudensitymatOperator_t handle. A composite operator consisting of multiple OperatorTerms, each with a duality flag and scalar coefficient.
CuQuantum.CuDensityMat.OperatorAction — Type
OperatorActionWraps a cudensitymatOperatorAction_t handle. Computes the action of one or more operators on input states, accumulating into an output state.
CuQuantum.CuDensityMat.OperatorSpectrum — Type
OperatorSpectrumWraps a cudensitymatOperatorSpectrum_t handle for computing eigenvalues and eigenstates of an operator.
CuQuantum.CuDensityMat.OperatorTerm — Type
OperatorTermWraps a cudensitymatOperatorTerm_t handle. Represents one or more tensor products of elementary operators acting on specified modes.
CuQuantum.CuDensityMat.WorkStream — Type
WorkStream(; stream=nothing, memory_limit=nothing, device_id=nothing)A workspace context bundling a CuDensityMat library handle, workspace descriptor, and CUDA stream. All CuDensityMat operations require a WorkStream.
Keyword Arguments
stream::Union{Nothing, CUDA.CuStream}: CUDA stream. Uses the default stream ifnothing.memory_limit::Union{Nothing, Int}: Maximum workspace memory in bytes. Default is unlimited.device_id::Union{Nothing, Int}: CUDA device ordinal. Uses current device ifnothing.
Examples
ws = WorkStream() # default stream, current device
ws = WorkStream(stream=CUDA.CuStream()) # explicit stream
close(ws) # release resourcesBase.close — Method
close(ws::WorkStream)Explicitly release all resources held by the WorkStream.
Base.isopen — Method
isopen(ws::WorkStream)Check if the WorkStream is still valid (not yet closed/finalized).
CuQuantum.CuDensityMat._get_workspace_limit — Method
_get_workspace_limit(ws, user_limit) -> IntReturn the workspace size limit in bytes. If user_limit is nothing, returns ws.memory_limit if set, or 80% of free GPU memory.
CuQuantum.CuDensityMat.allocate_storage! — Method
allocate_storage!(state)Allocate an appropriately sized buffer and attach it to the state.
CuQuantum.CuDensityMat.append_elementary_product! — Method
append_elementary_product!(term, elem_operators, modes_acted_on, mode_action_duality;
coefficient=1.0+0im, kwargs...)Append a tensor product of elementary operators to the term.
Arguments
elem_operators: Vector ofElementaryOperatorsmodes_acted_on: Flattened vector of mode indices (0-based) each operator acts onmode_action_duality: Per-mode duality flags (0=ket, nonzero=bra) — same length asmodes_acted_oncoefficient: Static complex scalar coefficient (default: 1.0+0im)
CuQuantum.CuDensityMat.append_elementary_product_batch! — Method
append_elementary_product_batch!(term, elem_operators, modes_acted_on,
mode_action_duality, batch_size, static_coefficients; kwargs...)Append a batched tensor product of elementary operators to the term.
Arguments
static_coefficients: GPU array of ComplexF64 coefficients (length batch_size)total_coefficients: GPU storage for total coefficients, ornothing
CuQuantum.CuDensityMat.append_matrix_product! — Method
append_matrix_product!(term, matrix_operators, conjugations, action_duality;
coefficient=1.0+0im, kwargs...)Append a product of matrix operators to the term.
Arguments
matrix_operators: Vector ofMatrixOperatorsconjugations: Per-operator conjugation flags (0=normal, nonzero=conjugate-transpose)action_duality: Per-operator duality flags (0=ket, nonzero=bra)coefficient: Static complex scalar coefficient (default: 1.0+0im)
CuQuantum.CuDensityMat.append_term! — Method
append_term!(operator, term; duality=0, coefficient=1.0+0im, kwargs...)Append an operator term to the composite operator.
Arguments
duality: 0 for ket-side (default), nonzero for bra-sidecoefficient: Static complex scalar coefficient (default: 1.0+0im)
CuQuantum.CuDensityMat.append_term_batch! — Method
append_term_batch!(operator, term, batch_size, static_coefficients;
duality=0, kwargs...)Append a batched operator term.
Arguments
static_coefficients: GPU array of ComplexF64 coefficients (length batch_size)total_coefficients: GPU storage for total coefficients, ornothing
CuQuantum.CuDensityMat.attach_storage! — Method
attach_storage!(state, data::CuVector{T})Attach a GPU buffer to the state. The buffer must be F-contiguous and match the required storage size.
CuQuantum.CuDensityMat.clone — Method
clone(state, buf::CuVector{T}) -> StateClone a state with a new storage buffer.
CuQuantum.CuDensityMat.component_storage_size — Method
component_storage_size(state) -> Vector{Int}Storage size in bytes for each component.
CuQuantum.CuDensityMat.compute_action! — Method
compute_action!(ws, action, states_in, state_out;
time=0.0, batch_size=0, params=nothing)Execute the operator action: state_out = sum_i operator_i(states_in[i]).
CuQuantum.CuDensityMat.compute_expectation! — Method
compute_expectation!(ws, expectation, state, result;
time=0.0, batch_size=1, num_params=0, params=nothing)Compute expectation value: result = Tr(operator * state).
Arguments
result: GPU array to store the expectation value(s). For complex operators, useCuVector{ComplexF64}of lengthbatch_size.
CuQuantum.CuDensityMat.compute_operator_action! — Method
compute_operator_action!(ws, operator, state_in, state_out;
time=0.0, batch_size=0, params=nothing)Execute single-operator action: state_out = operator * state_in.
CuQuantum.CuDensityMat.compute_operator_action_backward! — Method
compute_operator_action_backward!(ws, operator, state_in, state_out_adj,
state_in_adj, params_grad; time=0.0, batch_size=0,
num_params=0, params=nothing)Compute backward differentiation of a single-operator action.
Computes ∂L/∂statein (into `stateinadj) and ∂L/∂params (intoparamsgrad) given ∂L/∂state_out (stateoutadj`).
Arguments
state_in: Forward-pass input statestate_out_adj: Adjoint of forward-pass output (∂L/∂state_out)state_in_adj: Output — receives ∂L/∂state_in (accumulated, += semantics)params_grad: GPU vector — receives ∂L/∂params (accumulated, += semantics)
CuQuantum.CuDensityMat.compute_spectrum! — Method
compute_spectrum!(ws, spectrum, num_eigenstates, eigenstates, eigenvalues;
time=0.0, batch_size=1, num_params=0, params=nothing) -> tolerancesCompute eigenvalues and eigenstates.
Arguments
num_eigenstates: Number of eigenvalues/eigenstates to compute (≤ max from prepare)eigenstates: Vector of pre-allocatedAbstractStates to receive eigenstateseigenvalues: GPU array to receive eigenvalues. For Hermitian operators, useCuVector{Float64}of lengthnum_eigenstates * batch_size. For non-Hermitian, useCuVector{ComplexF64}.
Returns
tolerances: Vector{Float64} of convergence tolerances for each eigenvalue
CuQuantum.CuDensityMat.configure_spectrum! — Method
configure_spectrum!(ws, spectrum, attribute, value)Configure a spectrum solver parameter.
Arguments
attribute: One of:CUDENSITYMAT_OPERATOR_SPECTRUM_CONFIG_MAX_EXPANSION— max Krylov expansion ratio (Int32, default 5)CUDENSITYMAT_OPERATOR_SPECTRUM_CONFIG_MAX_RESTARTS— max restarts (Int32, default 20)CUDENSITYMAT_OPERATOR_SPECTRUM_CONFIG_MIN_BLOCK_SIZE— min block size (Int32, default 1)
value: Int32 configuration value
CuQuantum.CuDensityMat.create — Method
create() -> cudensitymatHandle_tCreate a CuDensityMat library context handle.
CuQuantum.CuDensityMat.create_elementary_operator — Method
create_elementary_operator(ws, space_mode_extents, data::CuArray{T};
sparsity=:none, diagonal_offsets=Int32[],
tensor_callback=nothing, tensor_gradient_callback=nothing) -> ElementaryOperatorCreate a dense or multidiagonal elementary operator for a single mode.
Arguments
ws::WorkStream: Execution contextspace_mode_extents::Vector{Int}: Dimensions of the space modes (e.g.,[d]for single qudit)data::CuArray{T}: Operator data on GPU (column-major)sparsity::nonefor dense,:multidiagonalfor sparsediagonal_offsets: Required for multidiagonal sparsity
CuQuantum.CuDensityMat.create_elementary_operator_batch — Method
create_elementary_operator_batch(ws, space_mode_extents, data::CuArray{T}, batch_size;
kwargs...) -> ElementaryOperatorCreate a batched elementary operator.
CuQuantum.CuDensityMat.create_expectation — Method
create_expectation(ws, operator) -> ExpectationCreate an expectation value descriptor for the given operator.
CuQuantum.CuDensityMat.create_matrix_operator — Method
create_matrix_operator(ws, space_mode_extents, data::CuArray{T};
tensor_callback=nothing, tensor_gradient_callback=nothing) -> MatrixOperatorCreate a dense local matrix operator covering the full Hilbert space.
CuQuantum.CuDensityMat.create_matrix_operator_batch — Method
create_matrix_operator_batch(ws, space_mode_extents, data::CuArray{T}, batch_size;
kwargs...) -> MatrixOperatorCreate a batched dense local matrix operator.
CuQuantum.CuDensityMat.create_operator — Method
create_operator(ws, hilbert_space_dims) -> OperatorCreate an empty composite operator for the given Hilbert space.
CuQuantum.CuDensityMat.create_operator_action — Method
create_operator_action(ws, operators::Vector{Operator}) -> OperatorActionCreate an operator action descriptor for multiple operators.
CuQuantum.CuDensityMat.create_operator_spectrum — Method
create_operator_spectrum(ws, operator;
is_hermitian=true,
spectrum_kind=CUDENSITYMAT_OPERATOR_SPECTRUM_LARGEST) -> OperatorSpectrumCreate an eigenspectrum solver for the given operator.
Arguments
is_hermitian: Whether the operator is Hermitian (enables optimized solver)spectrum_kind: Which eigenvalues to compute:CUDENSITYMAT_OPERATOR_SPECTRUM_LARGEST— largest by magnitudeCUDENSITYMAT_OPERATOR_SPECTRUM_SMALLEST— smallest by magnitudeCUDENSITYMAT_OPERATOR_SPECTRUM_LARGEST_REAL— largest real partCUDENSITYMAT_OPERATOR_SPECTRUM_SMALLEST_REAL— smallest real part
CuQuantum.CuDensityMat.create_operator_term — Method
create_operator_term(ws, hilbert_space_dims) -> OperatorTermCreate an empty operator term for the given Hilbert space dimensions.
CuQuantum.CuDensityMat.create_workspace — Method
create_workspace(handle) -> cudensitymatWorkspaceDescriptor_tCreate a workspace descriptor.
CuQuantum.CuDensityMat.destroy — Method
destroy(handle::cudensitymatHandle_t)Destroy a CuDensityMat library context handle.
CuQuantum.CuDensityMat.destroy_workspace — Method
destroy_workspace(workspace)Destroy a workspace descriptor.
CuQuantum.CuDensityMat.get_num_ranks — Method
get_num_ranks(ws::WorkStream) -> IntReturn the number of distributed processes.
CuQuantum.CuDensityMat.get_proc_rank — Method
get_proc_rank(ws::WorkStream) -> IntReturn the rank of the current process.
CuQuantum.CuDensityMat.handle — Method
handle() -> cudensitymatHandle_tGet a cuDensityMat library handle for the current CUDA context. Handles are cached per-context and reused.
CuQuantum.CuDensityMat.initialize_zero! — Method
initialize_zero!(state)Set all elements of the state to zero using the C API.
CuQuantum.CuDensityMat.inner_product — Method
inner_product(left, right) -> Vector{T}Compute inner product(s) ⟨left|right⟩. Returns a CPU vector of length batch_size.
CuQuantum.CuDensityMat.inplace_accumulate! — Method
inplace_accumulate!(dest, src, factors=1)Accumulate: dest += factors * src. Both states must be compatible.
CuQuantum.CuDensityMat.inplace_scale! — Method
inplace_scale!(state, factors)Scale the state by scalar factor(s).
factors can be a scalar, a Vector, or a CuVector of length batch_size.
CuQuantum.CuDensityMat.local_info — Method
local_info(state) -> (shape::Tuple, offsets::Tuple)Local storage buffer dimensions and mode offsets. The last dimension is always the batch dimension.
CuQuantum.CuDensityMat.norm — Method
norm(state) -> Vector{real(T)}Compute the squared Frobenius norm(s). Returns a CPU vector of length batch_size.
CuQuantum.CuDensityMat.num_components — Method
num_components(state) -> IntNumber of storage components (1 for single-GPU, >1 for distributed).
CuQuantum.CuDensityMat.prepare_action! — Method
prepare_action!(ws, action, states_in, state_out;
compute_type=CUDENSITYMAT_COMPUTE_64F, workspace_limit=nothing)Prepare the operator action computation (one-time setup).
If workspace_limit is nothing (default), uses 80% of free GPU memory.
CuQuantum.CuDensityMat.prepare_expectation! — Method
prepare_expectation!(ws, expectation, state;
compute_type=CUDENSITYMAT_COMPUTE_64F, workspace_limit=nothing)Prepare expectation value computation (one-time setup).
CuQuantum.CuDensityMat.prepare_operator_action! — Method
prepare_operator_action!(ws, operator, state_in, state_out;
compute_type=CUDENSITYMAT_COMPUTE_64F, workspace_limit=nothing)Prepare single-operator action computation.
If workspace_limit is nothing (default), uses 80% of free GPU memory.
CuQuantum.CuDensityMat.prepare_operator_action_backward! — Method
prepare_operator_action_backward!(ws, operator, state_in, state_out_adj;
compute_type=CUDENSITYMAT_COMPUTE_64F, workspace_limit=nothing)Prepare backward differentiation of a single-operator action.
Arguments
state_in: The forward-pass input statestate_out_adj: The adjoint of the forward-pass output state (∂L/∂state_out)
CuQuantum.CuDensityMat.prepare_spectrum! — Method
prepare_spectrum!(ws, spectrum, max_eigenstates, state;
compute_type=CUDENSITYMAT_COMPUTE_64F, workspace_limit=nothing)Prepare eigenspectrum computation.
Arguments
max_eigenstates: Maximum number of eigenvalues/eigenstates to computestate: A template state (defines the Hilbert space structure)
CuQuantum.CuDensityMat.release_handle — Method
release_handle(h::cudensitymatHandle_t)Return a handle to the cache for reuse.
CuQuantum.CuDensityMat.release_workspace! — Method
release_workspace!(ws::WorkStream)Release the workspace buffer and create a fresh workspace descriptor.
CuQuantum.CuDensityMat.set_communicator! — Method
set_communicator!(ws::WorkStream, provider::Symbol; comm_ptr=nothing, comm_size=nothing)Configure distributed execution on the WorkStream.
Arguments
provider::none,:mpi, or:ncclcomm_ptr: Pointer to the communicator (MPIComm* or ncclCommt*)comm_size: Size of the communicator object in bytes
MPI Example
using MPI
MPI.Init()
comm = MPI.COMM_WORLD
set_communicator!(ws, :mpi;
comm_ptr=MPI.API.MPI_Comm_c2f(comm),
comm_size=sizeof(MPI.MPI_Comm))CuQuantum.CuDensityMat.set_random_seed! — Method
set_random_seed!(ws::WorkStream, seed::Integer)Reset the random seed used by the library's internal random number generator.
CuQuantum.CuDensityMat.state_view — Method
state_view(state) -> CuArrayReturn a multidimensional view of the state's storage buffer.
CuQuantum.CuDensityMat.storage_size — Method
storage_size(state) -> IntStorage buffer size in number of elements of the state's data type.
CuQuantum.CuDensityMat.trace — Method
trace(state) -> Vector{T}Compute the trace(s). Returns a CPU vector of length batch_size.
CuQuantum.CuDensityMat.unregister_callback! — Method
unregister_callback!(refs)Release a callback from the global registry, allowing GC. Call this after destroying the operator that uses the callback.
CuQuantum.CuDensityMat.version — Method
version()Return the CuDensityMat library version as a VersionNumber.
CuQuantum.CuDensityMat.workspace_allocate! — Method
workspace_allocate!(ws::WorkStream, size::Int; memspace=:device, kind=:scratch)Allocate and attach a workspace buffer of the given size.
CuQuantum.CuDensityMat.workspace_query_size — Method
workspace_query_size(ws::WorkStream; memspace=:device, kind=:scratch) -> IntQuery the required workspace buffer size in bytes after a prepare call.
CuQuantum.CuDensityMat.wrap_scalar_callback — Method
wrap_scalar_callback(f; gradient=nothing) -> (cb, gcb, refs)Wrap a Julia function as a scalar callback for time-dependent coefficients.
Arguments
f: Function with signaturef(time::Float64, params::Matrix{Float64}, storage::Vector{T})whereTmatches the operator's data type.paramsis(numParams, batchSize),storageis(batchSize,). Write the coefficient values intostorage.gradient: Optional gradient function with signatureg(time::Float64, params::Matrix{Float64}, scalar_grad::Vector{T}, params_grad::Matrix{Float64})
Returns
cb::cudensitymatWrappedScalarCallback_t— the callback structgcb::cudensitymatWrappedScalarGradientCallback_t— the gradient callback structrefs— opaque reference holder (keep alive while callback is in use)
Example
# Time-dependent coefficient: f(t) = exp(iΩt) where Ω = params[1,:]
function my_coeff(time, params, storage)
for i in axes(storage, 1)
ω = params[1, i]
storage[i] = complex(cos(ω * time), sin(ω * time))
end
end
cb, gcb, refs = wrap_scalar_callback(my_coeff)CuQuantum.CuDensityMat.wrap_tensor_callback — Method
wrap_tensor_callback(f; gradient=nothing) -> (cb, gcb, refs)Wrap a Julia function as a tensor callback for time-dependent operator elements.
Arguments
f: Function with signaturef(time::Float64, params::Matrix{Float64}, storage::Array{T})wherestorageshape is(d1, d2, ..., d1, d2, ..., batchSize)for dense operators. Write the tensor elements intostorage.gradient: Optional gradient function.
Returns
cb::cudensitymatWrappedTensorCallback_tgcb::cudensitymatWrappedTensorGradientCallback_trefs— opaque reference holder