BitBasis
Types and operations for basis represented by bits in linear algebra.
For more details please ref to BitBasis.jl.
BitBasis.@bit_str — Macro@bit_str -> BitStr64Construct a bit string. such as bit"0000". The bit strings also supports string join. Just use it like normal strings.
Example
julia> bit"10001"
10001 ₍₂₎
julia> bit"100_111_101"
100111101 ₍₂₎
julia> join(bit"1001", bit"11", bit"1110")
1001111110 ₍₂₎
julia> onehot(bit"1001")
16-element Vector{ComplexF64}:
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
1.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
BitBasis.@dit_str — Macro@dit_str -> DitStr64Construct a dit string. such as dit"0201;3". The dit strings also supports string join. Just use it like normal strings.
Example
julia> dit"10201;3"
10201 ₍₃₎
julia> dit"100_121_121;3"
100121121 ₍₃₎
julia> join(dit"1021;3", dit"11;3", dit"1210;3")
1021111210 ₍₃₎
julia> onehot(dit"1021;3")
81-element Vector{ComplexF64}:
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
⋮
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.0 + 0.0im
BitBasis.@lbit_str — Macro@lbit_str -> LongBitStrLong bit string version of @bit_str macro.
BitBasis.@ldit_str — Macro@ldit_str -> LongDitStrLong dit string version of @dit_str macro.
Base.:== — Method==(lhs::SubDitStr{D,N,T}, rhs::DitStr{D,N,T}) -> Bool
==(lhs::DitStr{D,N,T}, rhs::SubDitStr{D,N,T}) -> Bool
==(lhs::SubDitStr{D,N,T}, rhs::SubDitStr{D,N,T}) -> BoolCompare the equality between SubDitStr and DitStr.
BitBasis.allone — Methodallone(index::Integer, mask::Integer) -> BoolReturn true if all masked position of index is 1.
Example
true if all masked positions are 1.
julia> allone(0b1011, 0b1011)
true
julia> allone(0b1011, 0b1001)
true
julia> allone(0b1011, 0b0100)
falseBitBasis.anyone — Methodanyone(index::Integer, mask::Integer) -> BoolReturn true if any masked position of index is 1.
Example
true if any masked positions is 1.
julia> anyone(0b1011, 0b1001)
true
julia> anyone(0b1011, 0b1100)
true
julia> anyone(0b1011, 0b0100)
falseBitBasis.baddrs — Methodbaddrs(b::Integer) -> Vectorget the locations of nonzeros bits, i.e. the inverse operation of bmask.
BitBasis.basis — Methodbasis(ditstr) -> UnitRange{DitStr{D,N,T}}
basis(DitStr{D,N,T}) -> UnitRange{DitStr{D,N,T}}Returns the UnitRange for basis in Hilbert Space of qudits.
BitBasis.bdistance — Methodbdistance(i::Integer, j::Integer) -> IntReturn number of different bits.
BitBasis.bfloat — Methodbfloat(b::Integer; nbits::Int=bit_length(b)) -> Float64float view, with current bit numbering. See also bfloat_r.
Ref: wiki: bit numbering
BitBasis.bfloat — Methodbfloat(b::BitStr) -> Float64BitBasis.bfloat_r — Methodbfloat_r(b::Integer; nbits::Int=bit_length(b)) -> Float64float view, with reversed bit numbering. See also bfloat.
BitBasis.bfloat_r — Methodbfloat_r(b::BitStr) -> Float64BitBasis.bint — Methodbint(b::BitStr) -> IntegerBitBasis.bint — Methodbint(b; nbits=nothing) -> Intinteger view, with LSB 0 bit numbering. See also wiki: bit numbering
BitBasis.bint_r — Methodbint_r(b; nbits::Int) -> Integerinteger read in inverse order.
BitBasis.bint_r — Methodbint_r(b::BitStr) -> IntegerBitBasis.bit_length — Methodbit_length(x::Integer) -> IntReturn the number of bits required to represent input integer x.
BitBasis.bitarray — Methodbitarray(v::Vector, [nbits::Int]) -> BitArray
bitarray(v::Int, nbits::Int) -> BitArray
bitarray(nbits::Int) -> FunctionConstruct BitArray from an integer vector, if nbits not supplied, it is 64. If an integer is supplied, it returns a function mapping a Vector/Int to bitarray.
BitBasis.bmask — Functionbmask(::Type{T}) where T <: Integer -> zero(T)
bmask([T::Type], positions::Int...) -> T
bmask([T::Type], range::UnitRange{Int}) -> TReturn an integer mask of type T where 1 is the position masked according to positions or range. Directly use T will return an empty mask 0.
BitBasis.breflect — Functionbreflect(b::Integer[, masks::Vector{Integer}]; nbits) -> IntegerReturn left-right reflected integer.
Example
Reflect the order of bits.
julia> breflect(0b1011; nbits=4) == 0b1101
trueBitBasis.breflect — Methodbreflect(bit_str[, masks])Return left-right reflected bit string.
BitBasis.bsizeof — Methodbsizeof(::Type)Returns the size of given type in number of binary digits.
BitBasis.btruncate — Methodbtruncate(b, n)Truncate bits b to given length n.
BitBasis.controldo — Methodcontroldo(f, itr::IterControl)Execute f while iterating itr.
this is faster but equivalent than using itr as an iterator. See also itercontrol.
BitBasis.controller — Methodcontroller([T=Int, ]cbits, cvals) -> FunctionReturn a function that checks whether a basis at cbits takes specific value cvals.
BitBasis.flip — Methodflip(index::Integer, mask::Integer) -> IntegerReturn an Integer with bits at masked position flipped.
Example
julia> flip(0b1011, 0b1011) |> BitStr{4}
0000 ₍₂₎BitBasis.group_shift! — Methodgroup_shift!(nbits, positions)Shift bits on positions together.
BitBasis.hypercubic — Methodhypercubic(A::Array) -> Arrayget the hypercubic representation for an array.
BitBasis.indicator — Methodindicator(::Type{T}, k) -> TReturn an integer with k-th bit set to 1.
BitBasis.indices_with — Methodindices_with(n::Int, locs::Vector{Int}, vals::Vector{Int}) -> Vector{Int}Return indices with specific positions locs with value vals in a hilbert space of n qubits.
BitBasis.invorder — Methodinvorder(X::AbstractVecOrMat)Inverse the order of given vector/matrix X.
BitBasis.ismatch — Methodismatch(index::Integer, mask::Integer, target::Integer) -> BoolReturn true if bits at positions masked by mask equal to 1 are equal to target.
Example
julia> n = 0b11001; mask = 0b10100; target = 0b10000;
julia> ismatch(n, mask, target)
trueBitBasis.itercontrol — Methoditercontrol([T=Int], nbits, positions, bit_configs)Returns an iterator which iterate through controlled subspace of bits.
Example
To iterate through all the bits satisfy 0xx10x1 where x means an arbitrary bit.
julia> for each in itercontrol(7, [1, 3, 4, 7], (1, 0, 1, 0))
println(string(each, base=2, pad=7))
end
0001001
0001011
0011001
0011011
0101001
0101011
0111001
0111011
BitBasis.log2dim1 — Methodlog2dim1(X)Returns the log2 of the first dimension's size.
BitBasis.log2i — Functionlog2i(x::Integer) -> IntegerReturn log2(x), this integer version of log2 is fast but only valid for number equal to 2^n.
BitBasis.neg — Methodneg(b::BitStr) -> BitStrBitBasis.neg — Methodneg(index::Integer, nbits::Int) -> IntegerReturn an integer with all bits flipped (with total number of bit nbits).
Example
julia> neg(0b1111, 4) |> BitStr{4}
0000 ₍₂₎
julia> neg(0b0111, 4) |> BitStr{4}
1000 ₍₂₎BitBasis.next_reordered_basis — Methodnext_reordered_basis(basis, takers, differ)Returns the next reordered basis accroding to current basis.
BitBasis.onehot — Methodonehot([T=Float64], dit_str[; nbatch])Create an onehot vector in type Vector{T} or a batch of onehot vector in type Matrix{T}, where index x + 1 is one. One can specify the value of the nonzero entry by inputing a pair.
BitBasis.packbits — Methodpackbits(arr::AbstractArray) -> AbstractArraypack bits to integers, usually take a BitArray as input.
BitBasis.readat — Methodreadat(x, loc...) -> IntegerRead the dit config at given location.
BitBasis.readbit — Methodreadbit(x, loc...)Read the bit config at given location.
BitBasis.reorder — Functionreorder(X::AbstractArray, orders)Reorder X according to orders.
BitBasis.setbit — Methodsetbit(index::Integer, mask::Integer) -> Integerset the bit at masked position to 1.
Example
julia> setbit(0b1011, 0b1100) |> BitStr{4}
1111 ₍₂₎
julia> setbit(0b1011, 0b0100) |> BitStr{4}
1111 ₍₂₎
julia> setbit(0b1011, 0b0000) |> BitStr{4}
1011 ₍₂₎BitBasis.swapbits — Methodswapbits(n::Integer, mask_ij::Integer) -> Integer
swapbits(n::Integer, i::Int, j::Int) -> IntegerReturn an integer with bits at i and j flipped.
Example
julia> swapbits(0b1011, 0b1100) == 0b0111
truelocations i and j specified by mask could be faster when bmask is not straight forward but known by constant.
BitBasis.unsafe_reorder — Functionunsafe_reorder(X::AbstractArray, orders)Reorder X according to orders.
BitBasis.unsafe_sub — Methodunsafe_sub(a::UnitRange, b::NTuple{N}) -> NTuple{N}Returns result in type Tuple of a .- b. This will not check the length of a and b, use at your own risk.
BitBasis.unsafe_sub — Methodunsafe_sub(a::UnitRange{T}, b::Vector{T}) where TReturns a .- b, fallback version when b is a Vector.
BitBasis.BitStr — TypeBitStr{N,T} <: IntegerThe struct for bit string with fixed length N and storage type T. It is an alias of DitStr{2,N,T}.
BitStr{N,T}(integer)
BitStr64{N}(integer)
BitStr64(vector)
LongBitStr{N}(integer)
LongBitStr(vector)Returns a BitStr. When the input is an integer, the bits are read from right to left. When the input is a vector, the bits are read from left to right.
Examples
BitStr supports some basic arithmetic operations. It acts like an integer, but supports some frequently used methods for binary basis.
julia> bit"0101" * 2
1010 ₍₂₎
julia> join([bit"101" for i in 1:10])
"101 ₍₂₎101 ₍₂₎101 ₍₂₎101 ₍₂₎101 ₍₂₎101 ₍₂₎101 ₍₂₎101 ₍₂₎101 ₍₂₎101 ₍₂₎"
julia> repeat(bit"101", 2)
101101 ₍₂₎
julia> bit"1101"[2]
0BitBasis.DitStr — TypeDitStr{D,N,T<:Integer} <: IntegerThe struct for dit string with fixed length N and storage type T, where dit is a extension of dit from binary system to a d-ary system.
DitStr{D,N,T}(integer)
DitStr{D,N}(integer)
DitStr{D}(vector)Returns a DitStr. When the input is an integer, the dits are read from right to left. When the input is a vector, the dits are read from left to right.
Examples
julia> DitStr{3}([1,2,1,1,0])
01121 ₍₃₎
julia> DitStr{3, 5}(71)
02122 ₍₃₎BitBasis.DitStr — MethodDitStr(dit::SubDitStr{D,N,T}) -> DitStr{D,N,T}Raise type SubDitStr to DitStr.
julia> x = DitStr{3, 5}(71)
02122 ₍₃₎
julia> sx = SubDitStr(x, 2, 4)
SubDitStr{3, 5, Int64}(02122 ₍₃₎, 1, 3)
julia> DitStr(sx)
212 ₍₃₎BitBasis.IterControl — TypeIterControl{S}
IterControl(n::Int, base::Int, masks, factors) -> IterControlIterator to iterate through controlled subspace. See also itercontrol. S is the number of chunks, n is the size of Hilbert space, base is the base of counter, masks and factors are helpers for enumerating over the target Hilbert Space.
BitBasis.LongLongUInt — TypeLongLongUInt{C} <: IntegerA LongLongUInt{C} is an integer with C UInt numbers to store the value.
BitBasis.ReorderedBasis — TypeReorderedBasis{N, T}Lazy reorderd basis.
BitBasis.ReorderedBasis — MethodReorderedBasis(orders::NTuple{N, <:Integer})Returns a lazy set of reordered basis.
BitBasis.SubDitStr — TypeSubDitStr{D,N,T<:Integer} <: IntegerThe struct as a SubString-like object for DitStr(SubString is an official implementation of sliced strings, see String for reference). This slicing returns a view into the parent DitStr instead of making a copy (similar to the @views macro for strings).
SubDitStr can be used to describe the qubit configuration within the subspace of the entire Hilbert space.It provides similar getindex, length functions as DitStr.
SubDitStr(dit::DitStr{D,N,T}, i::Int, j::Int)
SubDitStr(dit::DitStr{D,N,T}, r::AbstractUnitRange{<:Integer})Or by @views macro for DitStr (this macro makes your life easier by supporting begin and end syntax):
@views dit[i:j]Returns a SubDitStr.
Examples
julia> x = DitStr{3, 5}(71)
02122 ₍₃₎
julia> sx = SubDitStr(x, 2, 4)
SubDitStr{3, 5, Int64}(02122 ₍₃₎, 1, 3)
julia> @views x[2:end]
SubDitStr{3, 5, Int64}(02122 ₍₃₎, 1, 4)
julia> sx == dit"212;3"
true