cuStateVec
cuStateVec.jl wraps NVIDIA's cuStateVec library, part of cuQuantum, for simulating quantum circuits on state vectors. It has no integration with Base or LinearAlgebra functions; it offers two levels:
- The functions and types documented below wrap cuStateVec's routines on a
CuStateVec: applying gates, measuring, sampling, computing expectation values, accessing and reordering the elements, and the helper objects for distributed state vectors. - The generated bindings in
libcustatevec.jlfollow NVIDIA's C API, and change with the cuQuantum release. Use them together withhandle.
The wrappers are named after the cuStateVec routine they call, in NVIDIA's camel case, without the custatevec prefix and with a ! when they modify their first argument: custatevecApplyMatrix is applyMatrix!, and custatevecComputeExpectation computeExpectation. Routines of a helper object also drop the object's name, which the type of their first argument gives, and getters drop Get: custatevecSamplerPreprocess is preprocess!, custatevecSamplerGetSquaredNorm squaredNorm, and custatevecInitializeStateVector initialize!. The exceptions are accessorGet and accessorSet!, which keep the object's name rather than shadowing get and set!, and swapParameters, which names the swap parameters that custatevecDistIndexBitSwapSchedulerGetParameters returns.
The wrappers share these conventions:
- Qubits, or index bits, are numbered from 0: bit
iof the index of a state vector's amplitudes is qubiti. Bit strings returned as integers have the first of the requested bits as their least significant bit. - Matrices are column-major, as Julia stores them, and can be in host or device memory.
- Batched wrappers take one
CuStateVecthat holdsn_svsstate vectors of equal length one after the other. - Measurements take random numbers in
[0, 1)from the caller, so that they can be reproduced. Flags such asadjoint, and the enums of the C API (custatevecCollapseOp_t,custatevecMatrixMapType_t, ...), are positional arguments. - The wrappers run on the task's stream. The ones that return host values, such as
computeExpectationandabs2SumArray, wait for them.
Public
cuStateVec.CUSTATEVECError — Type
CUSTATEVECError(code::custatevecStatus_t)The exception thrown when a cuStateVec call fails, with the status code it returned.
cuStateVec.CuStateVec — Type
CuStateVec{T}(n_qubits; sv_type=CUSTATEVEC_STATE_VECTOR_TYPE_ZERO)
CuStateVec(data::CuVector{T})
CuStateVec(data::Vector{T})A state vector with elements of type T, ComplexF32 or ComplexF64. The first form creates one of n_qubits qubits in the state selected by sv_type (see initialize!). The others take the amplitudes, whose number is a power of 2: a CuVector is used directly, a Vector is copied.
The amplitudes are in the field data, a CuVector{T}, and the number of qubits in nbits. Bit i (0-based) of the index of data is qubit i.
cuStateVec.CuStateVecAccessor — Type
CuStateVecAccessor(sv::CuStateVec, bit_ordering::Vector{Int}, mask_bit_string::Vector{Int},
mask_ordering::Vector{Int}; view=false)An accessor that copies elements between the state vector and other buffers, with accessorGet and accessorSet!. The elements are numbered by the bit_ordering bits, the first being the least significant, among the elements whose mask_ordering bits have the values in mask_bit_string. With view = true, the accessor can only read.
Wraps custatevecAccessorDescriptor_t.
cuStateVec.CuStateVecDistIndexBitSwapScheduler — Type
CuStateVecDistIndexBitSwapScheduler(n_global_bits, n_local_bits)A scheduler for swapping index bits of a state vector distributed over 2^n_global_bits sub state vectors of n_local_bits index bits each, for example over several processes. See setIndexBitSwaps! and swapParameters.
Wraps custatevecDistIndexBitSwapSchedulerDescriptor_t.
cuStateVec.CuStateVecSampler — Type
CuStateVecSampler(sv::CuStateVec, shot_count::UInt32)A sampler of the state vector, for up to shot_count samples at a time. Prepare it with preprocess!, then draw samples with sample. The sampler uses the memory of the state vector, which must not change in between.
Wraps custatevecSamplerDescriptor_t.
cuStateVec.CuStateVecSubSVMigrator — Type
CuStateVecSubSVMigrator(device_slices::CuVector{T}, n_device_slices, n_local_bits)A migrator of sub state vectors of n_local_bits index bits between host memory and n_device_slices slices of device_slices, with migrate!. The migrator uses the memory of device_slices directly.
Wraps custatevecSubSVMigratorDescriptor_t.
cuStateVec.Pauli — Type
PauliThe Pauli matrices PauliX(), PauliY() and PauliZ(), and the identity PauliI(), for applyPauliRotation! and computeExpectationsOnPauliBasis.
cuStateVec.PauliI — Type
PauliI()The identity, see Pauli.
cuStateVec.PauliX — Type
PauliX()The Pauli X matrix, see Pauli.
cuStateVec.PauliY — Type
PauliY()The Pauli Y matrix, see Pauli.
cuStateVec.PauliZ — Type
PauliZ()The Pauli Z matrix, see Pauli.
cuStateVec.abs2SumArray — Method
abs2SumArray(sv::CuStateVec, bitordering, maskBitString, maskOrdering) -> Vector{Float64}The sums of the squared magnitudes of the elements of the state vector for each value of the bitordering bits (their probabilities, for a normalized state vector). The index of the result has the first of these bits as its least significant bit. Only the elements whose maskOrdering bits have the values in maskBitString are included.
Wraps custatevecAbs2SumArray.
cuStateVec.abs2SumArrayBatched — Method
abs2SumArrayBatched(sv::CuStateVec, n_svs, bitordering, maskBitStrings, maskOrdering)
-> Vector{Float64}Compute abs2SumArray for each of the n_svs state vectors that sv holds one after the other, with a mask bit string given as an integer in maskBitStrings for each. The results are concatenated.
Wraps custatevecAbs2SumArrayBatched.
cuStateVec.abs2SumOnZBasis — Method
abs2SumOnZBasis(sv::CuStateVec, basisInds) -> abs2sum0, abs2sum1The sums of the squared magnitudes of the elements of the state vector for which the basisInds bits have even and odd parity. For a normalized state vector, these are the probabilities of measuring either parity on that Z product basis.
Wraps custatevecAbs2SumOnZBasis.
cuStateVec.accessorGet — Method
accessorGet(a::CuStateVecAccessor, buffer, i_begin, i_end)Copy the elements [i_begin, i_end) (0-based, in the accessor's bit ordering) of the state vector into buffer. That is a Vector or CuVector with the element type of the state vector and at least i_end - i_begin elements; its size and type are not checked.
Wraps custatevecAccessorGet.
cuStateVec.accessorSet! — Method
accessorSet!(a::CuStateVecAccessor, buffer, i_begin, i_end)Set the elements [i_begin, i_end) (0-based, in the accessor's bit ordering) of the state vector from buffer. That is a Vector or CuVector with the element type of the state vector and at least i_end - i_begin elements; its size and type are not checked.
Wraps custatevecAccessorSet.
cuStateVec.applyGeneralizedPermutationMatrix! — Function
applyGeneralizedPermutationMatrix!(sv::CuStateVec, permutation, diagonals, adjoint::Bool,
targets, controls, controlValues=ones(length(controls)))
-> svApply the generalized permutation matrix $DP$, or its adjoint, to the targets bits of the state vector, controlled by the controls bits having the controlValues. $P$ is given by the 0-based permutation and $D$ by its diagonals, both of length 2^length(targets), in host or device memory.
Wraps custatevecApplyGeneralizedPermutationMatrix.
cuStateVec.applyMatrix! — Function
applyMatrix!(sv::CuStateVec, matrix, adjoint::Bool, targets, controls,
controlValues=ones(length(controls))) -> svApply the gate matrix, or its adjoint, to the targets bits of the state vector, controlled by the controls bits having the controlValues. matrix is a Matrix or CuMatrix of size 2^length(targets), whose row and column indices have the first target as their least significant bit.
Wraps custatevecApplyMatrix.
cuStateVec.applyMatrixBatched! — Function
applyMatrixBatched!(sv::CuStateVec, n_svs, map_type::custatevecMatrixMapType_t,
matrix_inds, matrices, n_matrices, adjoint::Bool, targets, controls,
controlValues=ones(length(controls))) -> svApply gate matrices to each of the n_svs state vectors that sv holds one after the other, as applyMatrix! does. matrices is a Vector or CuVector with the n_matrices matrices one after the other. With CUSTATEVEC_MATRIX_MAP_TYPE_BROADCAST, the first matrix is applied to every state vector, and with _MATRIX_INDEXED, the matrix with the 0-based index matrix_inds[i] to state vector i.
Wraps custatevecApplyMatrixBatched.
cuStateVec.applyPauliRotation! — Function
applyPauliRotation!(sv::CuStateVec, theta, paulis::Vector{<:Pauli}, targets, controls,
controlValues=ones(length(controls))) -> svApply $e^{iθP}$ to the state vector, where $P$ is the product of paulis acting on the targets bits, controlled by the controls bits having the controlValues.
Wraps custatevecApplyPauliRotation.
cuStateVec.applySubSVOffset! — Method
applySubSVOffset!(sampler::CuStateVecSampler, sub_sv_ord, n_sub_svs, offset, norm)
-> samplerTreat the sampler's state vector as part of a larger one. It is the 0-based part sub_sv_ord of n_sub_svs parts, offset is the squared norm of the preceding parts, and norm that of all parts. The sampler must have been prepared with preprocess!; random numbers given to sample that fall outside this part are clamped to it.
Wraps custatevecSamplerApplySubSVOffset.
cuStateVec.batchMeasure! — Function
batchMeasure!(sv::CuStateVec, bitordering, randnum, collapse=CUSTATEVEC_COLLAPSE_NONE)
-> sv, bitstring::Vector{Int32}Measure the bitordering bits of the state vector, using the random number randnum in [0, 1), and return the result in the order of bitordering. With collapse = CUSTATEVEC_COLLAPSE_NORMALIZE_AND_ZERO, the state vector is also collapsed to the result.
Wraps custatevecBatchMeasure.
cuStateVec.batchMeasureWithOffset! — Function
batchMeasureWithOffset!(sv::CuStateVec, bitordering, randnum, offset, abs2sum,
collapse=CUSTATEVEC_COLLAPSE_NONE) -> sv, bitstring::Vector{Int32}Measure as batchMeasure! does, for a state vector that is part of a larger one distributed over several devices or processes. offset is the sum of the squared magnitudes of the parts before this one, and abs2sum that of the whole state vector.
Wraps custatevecBatchMeasureWithOffset.
cuStateVec.collapseByBitString! — Method
collapseByBitString!(sv::CuStateVec, bitstring, bitordering, norm) -> svCollapse the state vector to the elements whose bitordering bits have the values in bitstring, scaling them by 1/sqrt(norm) and setting the others to zero.
Wraps custatevecCollapseByBitString.
cuStateVec.collapseByBitStringBatched! — Method
collapseByBitStringBatched!(sv::CuStateVec, n_svs, bitstrings, bitordering, norms) -> svCollapse each of the n_svs state vectors that sv holds one after the other, as collapseByBitString! does. The bit strings are given as integers in bitstrings, and the normalization constants in norms.
Wraps custatevecCollapseByBitStringBatched.
cuStateVec.collapseOnZBasis! — Method
collapseOnZBasis!(sv::CuStateVec, parity, basisInds, norm) -> svCollapse the state vector on the Z product basis of the basisInds bits: scale the elements for which these bits have the given parity by 1/sqrt(norm), and set the others to zero. norm is typically the probability of that parity, from abs2SumOnZBasis.
Wraps custatevecCollapseOnZBasis.
cuStateVec.computeExpectation — Method
computeExpectation(sv::CuStateVec, matrix, basis_bits) -> Float64The expectation value of the Hermitian matrix (a Matrix or CuMatrix) acting on the basis_bits of the state vector. This waits for the result.
Wraps custatevecComputeExpectation.
cuStateVec.computeExpectationBatched — Method
computeExpectationBatched(sv::CuStateVec, n_svs, matrices, n_matrices, basis_bits)
-> Matrix{ComplexF64}The expectation values of several matrices for several state vectors, as an n_matrices×n_svs matrix. matrices holds the n_matrices matrices one after the other, and sv the n_svs state vectors. This waits for the result.
Wraps custatevecComputeExpectationBatched.
cuStateVec.computeExpectationsOnPauliBasis — Method
computeExpectationsOnPauliBasis(sv::CuStateVec, pauliOps, basisInds) -> Vector{Float64}The expectation values of products of Pauli matrices: pauliOps[i] is a vector of Paulis acting on the bits basisInds[i] of the state vector.
Wraps custatevecComputeExpectationsOnPauliBasis.
cuStateVec.enable_logging — Function
cuStateVec.enable_logging(enable::Bool=true)Forward log messages from cuStateVec to Julia's logging system. API and kernel traces are reported at Debug level, performance hints at Info level, and problems at Error level. Starting Julia with JULIA_DEBUG=cuStateVec enables this automatically, and also shows the Debug-level messages.
cuStateVec.functional — Method
cuStateVec.functional() -> BoolWhether cuStateVec was loaded and initialized successfully.
cuStateVec.handle — Method
cuStateVec.handle()The cuStateVec handle of the current task, for its active context and stream, for calling the C API of cuStateVec (cuStateVec.custatevec*) directly. It converts to a custatevecHandle_t. The handle is borrowed: don't destroy it, or change its stream, math mode or other settings. Its fields are not part of the API.
cuStateVec.initialize! — Method
initialize!(sv::CuStateVec, sv_type::custatevecStateVectorType_t) -> svSet the state vector to a predefined state: CUSTATEVEC_STATE_VECTOR_TYPE_ZERO ($|0…0⟩$), _UNIFORM, _GHZ or _W.
Wraps custatevecInitializeStateVector.
cuStateVec.measureBatched! — Function
measureBatched!(sv::CuStateVec, n_svs, bitordering, randnums,
collapse=CUSTATEVEC_COLLAPSE_NONE) -> sv, bitstrings::Vector{Int64}Measure the bitordering bits of each of the n_svs state vectors that sv holds one after the other, using one random number in [0, 1) for each. The results are returned as integers whose least significant bit is the first of bitordering.
Wraps custatevecMeasureBatched.
cuStateVec.measureOnZBasis! — Function
measureOnZBasis!(sv::CuStateVec, basisInds, randnum,
collapse=CUSTATEVEC_COLLAPSE_NONE) -> sv, parityMeasure the parity of the basisInds bits of the state vector, using the random number randnum in [0, 1). With collapse = CUSTATEVEC_COLLAPSE_NORMALIZE_AND_ZERO, the state vector is also collapsed to the result.
Wraps custatevecMeasureOnZBasis.
cuStateVec.migrate! — Method
migrate!(migrator::CuStateVecSubSVMigrator{T}, slice_index, src, dst, i_begin, i_end)
-> migratorCopy the elements [i_begin, i_end) (0-based) of the sub state vector src into the device slice slice_index, and/or those of the slice out to dst; with both, they are exchanged. src and dst are host vectors, or nothing, in memory the device can access (e.g. pinned with CUDACore.pin). The copies are asynchronous: synchronize before using or freeing that memory.
Wraps custatevecSubSVMigratorMigrate.
cuStateVec.multiDeviceSwapIndexBits! — Method
multiDeviceSwapIndexBits!(sub_svs::Vector{<:CuStateVec}, devices::Vector{CuDevice},
indexBitSwaps::Vector{<:Pair}, maskBitString, maskOrdering,
device_network_type::custatevecDeviceNetworkType_t) -> sub_svsSwap pairs of index bits of a state vector that is distributed over the sub state vectors sub_svs, which reside on the corresponding devices, one of which must be the current one. Their number is a power of two, and the global index bits that select the sub state vector follow the local ones. Only the elements whose maskOrdering bits have the values in maskBitString are swapped.
NVIDIA requires peer access to be enabled in both directions between all these devices (with CUDACore.enable_peer_access), and this crashes otherwise.
Wraps custatevecMultiDeviceSwapIndexBits.
cuStateVec.preprocess! — Method
preprocess!(sampler::CuStateVecSampler) -> samplerPrepare the sampler for sample, allocating its workspace.
Wraps custatevecSamplerPreprocess.
cuStateVec.sample — Method
sample([rng,] sv::CuStateVec, sampled_bits, shot_count) -> Vector{Int64}
sample(sampler::CuStateVecSampler, sampled_bits, randnums;
output=CUSTATEVEC_SAMPLER_OUTPUT_RANDNUM_ORDER) -> Vector{Int64}Sample the values of the sampled_bits of the state vector, as integers whose least significant bit is the first sampled bit. The first form draws shot_count samples, with random numbers from rng (by default the task-local generator). The second one uses a sampler prepared with preprocess!, and a sample for each of the given random numbers in [0, 1); output selects whether the samples are returned in the order of the random numbers or in ascending order.
Wraps custatevecSamplerSample.
cuStateVec.setExtraWorkspace! — Method
setExtraWorkspace!(a::CuStateVecAccessor, buffer::CuVector{UInt8}) -> aGive the accessor an extra workspace, of at least a.ws_size bytes, which speeds up repeated accesses with small buffers. The accessor keeps it alive.
Wraps custatevecAccessorSetExtraWorkspace.
cuStateVec.setIndexBitSwaps! — Method
setIndexBitSwaps!(scheduler::CuStateVecDistIndexBitSwapScheduler, bit_swaps::Vector{<:Pair},
mask_bit_string=Int32[], mask_ordering=Int32[]) -> IntPlan the swaps of index bits of a distributed state vector, and return the number of swap batches needed. Query each batch with swapParameters.
Wraps custatevecDistIndexBitSwapSchedulerSetIndexBitSwaps.
cuStateVec.squaredNorm — Method
squaredNorm(sampler::CuStateVecSampler) -> Float64The squared norm of the sampler's state vector. The sampler must have been prepared with preprocess!.
Wraps custatevecSamplerGetSquaredNorm.
cuStateVec.swapIndexBits! — Method
swapIndexBits!(sv::CuStateVec, bitSwaps::Vector{<:Pair}, maskBitString, maskOrdering)
-> svSwap pairs of index bits of the state vector, i.e. reorder its elements. Only the elements whose maskOrdering bits have the values in maskBitString are swapped.
Wraps custatevecSwapIndexBits.
cuStateVec.swapParameters — Method
swapParameters(scheduler::CuStateVecDistIndexBitSwapScheduler, swap_batch_index,
org_sub_sv_index) -> custatevecSVSwapParameters_tThe parameters of the data transfer of the 0-based sub state vector org_sub_sv_index in the swap batch swap_batch_index, planned with setIndexBitSwaps!.
Wraps custatevecDistIndexBitSwapSchedulerGetParameters.
cuStateVec.testMatrixType — Function
testMatrixType(matrix, adjoint::Bool, matrix_type=CUSTATEVEC_MATRIX_TYPE_GENERAL,
compute_type=...) -> Float64How far matrix (or its adjoint) is from being of matrix_type (CUSTATEVEC_MATRIX_TYPE_UNITARY or _HERMITIAN), as a residual norm.
Wraps custatevecTestMatrixType.
cuStateVec.version — Method
cuStateVec.version() -> VersionNumberThe version of the cuStateVec library in use, not that of the CUDA toolkit.