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.jl follow NVIDIA's C API, and change with the cuQuantum release. Use them together with handle.

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 i of the index of a state vector's amplitudes is qubit i. 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 CuStateVec that holds n_svs state 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 as adjoint, 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 computeExpectation and abs2SumArray, 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.

source
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.

source
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.

source
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.

source
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.

source
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.

source
cuStateVec.abs2SumOnZBasis — Method
abs2SumOnZBasis(sv::CuStateVec, basisInds) -> abs2sum0, abs2sum1

The 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.

source
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.

source
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.

source
cuStateVec.applyGeneralizedPermutationMatrix! — Function
applyGeneralizedPermutationMatrix!(sv::CuStateVec, permutation, diagonals, adjoint::Bool,
                                   targets, controls, controlValues=ones(length(controls)))
    -> sv

Apply 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.

source
cuStateVec.applyMatrix! — Function
applyMatrix!(sv::CuStateVec, matrix, adjoint::Bool, targets, controls,
             controlValues=ones(length(controls))) -> sv

Apply 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.

source
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))) -> sv

Apply 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.

source
cuStateVec.applyPauliRotation! — Function
applyPauliRotation!(sv::CuStateVec, theta, paulis::Vector{<:Pauli}, targets, controls,
                    controlValues=ones(length(controls))) -> sv

Apply $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.

source
cuStateVec.applySubSVOffset! — Method
applySubSVOffset!(sampler::CuStateVecSampler, sub_sv_ord, n_sub_svs, offset, norm)
    -> sampler

Treat 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.

source
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.

source
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.

source
cuStateVec.collapseOnZBasis! — Method
collapseOnZBasis!(sv::CuStateVec, parity, basisInds, norm) -> sv

Collapse 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.

source
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.

source
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.

source
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.

source
cuStateVec.initialize! — Method
initialize!(sv::CuStateVec, sv_type::custatevecStateVectorType_t) -> sv

Set the state vector to a predefined state: CUSTATEVEC_STATE_VECTOR_TYPE_ZERO ($|0…0⟩$), _UNIFORM, _GHZ or _W.

Wraps custatevecInitializeStateVector.

source
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.

source
cuStateVec.measureOnZBasis! — Function
measureOnZBasis!(sv::CuStateVec, basisInds, randnum,
                 collapse=CUSTATEVEC_COLLAPSE_NONE) -> sv, parity

Measure 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.

source
cuStateVec.migrate! — Method
migrate!(migrator::CuStateVecSubSVMigrator{T}, slice_index, src, dst, i_begin, i_end)
    -> migrator

Copy 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.

source
cuStateVec.multiDeviceSwapIndexBits! — Method
multiDeviceSwapIndexBits!(sub_svs::Vector{<:CuStateVec}, devices::Vector{CuDevice},
                          indexBitSwaps::Vector{<:Pair}, maskBitString, maskOrdering,
                          device_network_type::custatevecDeviceNetworkType_t) -> sub_svs

Swap 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.

source
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.

source
cuStateVec.setExtraWorkspace! — Method
setExtraWorkspace!(a::CuStateVecAccessor, buffer::CuVector{UInt8}) -> a

Give 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.

source
cuStateVec.swapIndexBits! — Method
swapIndexBits!(sv::CuStateVec, bitSwaps::Vector{<:Pair}, maskBitString, maskOrdering)
    -> sv

Swap 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.

source
cuStateVec.testMatrixType — Function
testMatrixType(matrix, adjoint::Bool, matrix_type=CUSTATEVEC_MATRIX_TYPE_GENERAL,
               compute_type=...) -> Float64

How far matrix (or its adjoint) is from being of matrix_type (CUSTATEVEC_MATRIX_TYPE_UNITARY or _HERMITIAN), as a residual norm.

Wraps custatevecTestMatrixType.

source
cuStateVec.version — Method
cuStateVec.version() -> VersionNumber

The version of the cuStateVec library in use, not that of the CUDA toolkit.

source

Private