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 bcat. Just use it like normal strings.
Example
julia> bit"10001"
10001 ₍₂₎
julia> bit"100_111_101"
100111101 ₍₂₎
julia> bcat(bit"1001", bit"11", bit"1110")
1001111110 ₍₂₎
julia> onehot(bit"1001")
16-element Array{Float64,1}:
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
1.0
0.0
0.0
0.0
0.0
0.0
0.0
BitBasis.@lbit_str — Macro.@bit_str -> LongBitStrLong bit string version of @bit_str macro.
BitBasis.allone — Method.allone(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 — Method.anyone(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 — Method.baddrs(b::Integer) -> Vectorget the locations of nonzeros bits, i.e. the inverse operation of bmask.
BitBasis.basis — Method.basis([IntType], nbits::Int) -> UnitRange{IntType}
basis([IntType], state::AbstractArray) -> UnitRange{IntType}Returns the UnitRange for basis in Hilbert Space of nbits qubits. If an array is supplied, it will return a basis having the same size with the first diemension of array.
BitBasis.bdistance — Method.bdistance(i::Integer, j::Integer) -> IntReturn number of different bits.
BitBasis.bfloat — Method.bfloat(b::Integer; nbits::Int=bit_length(b)) -> Float64float view, with current bit numbering. See also bfloat_r.
Ref: wiki: bit numbering
BitBasis.bfloat — Method.bfloat(b::BitStr) -> Float64BitBasis.bfloat_r — Method.bfloat_r(b::Integer; nbits::Int=bit_length(b)) -> Float64float view, with reversed bit numbering. See also bfloat.
BitBasis.bfloat_r — Method.bfloat_r(b::BitStr) -> Float64BitBasis.bint — Method.bint(b::BitStr) -> IntegerBitBasis.bint — Method.bint(b; nbits=nothing) -> Intinteger view, with LSB 0 bit numbering. See also wiki: bit numbering
BitBasis.bint_r — Method.bint_r(b; nbits::Int) -> Integerinteger read in inverse order.
BitBasis.bint_r — Method.bint_r(b::BitStr) -> IntegerBitBasis.bit_length — Method.bit_length(x::Integer) -> IntReturn the number of bits required to represent input integer x.
BitBasis.bit_literal — Method.bit_literal(xs...)Create a BitStr by input bits xs.
Example
julia> bit_literal(1, 0, 1, 0, 1, 1)
110101 ₍₂₎BitBasis.bitarray — Method.bitarray(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 — Function.bmask(::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 — Function.breflect(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 — Method.breflect(bit_str[, masks])Return left-right reflected bit string.
BitBasis.bsizeof — Method.bsizeof(::Type)Returns the size of given type in number of binary digits.
BitBasis.btruncate — Method.truncate(b, n)Truncate bits b to given length n.
BitBasis.controldo — Method.controldo(f, itr::IterControl)Execute f while iterating itr.
this is faster but equivalent than using itr as an iterator. See also itercontrol.
BitBasis.controller — Method.controller(cbits, cvals) -> FunctionReturn a function that checks whether a basis at cbits takes specific value cvals.
BitBasis.flip — Method.flip(index::Integer, mask::Integer) -> IntegerReturn an Integer with bits at masked position flipped.
Example
julia> flip(0b1011, 0b1011) |> bit(len=4)
0000 (0)BitBasis.group_shift! — Method.group_shift!(nbits, positions)Shift bits on positions together.
BitBasis.hypercubic — Method.hypercubic(A::Array) -> Arrayget the hypercubic representation for an array.
BitBasis.indices_with — Method.indices_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 — Method.invorder(X::AbstractVecOrMat)Inverse the order of given vector/matrix X.
BitBasis.ismatch — Method.ismatch(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.log2dim1 — Method.log2dim1(X)Returns the log2 of the first dimension's size.
BitBasis.log2i — Function.log2i(x::Integer) -> IntegerReturn log2(x), this integer version of log2 is fast but only valid for number equal to 2^n.
BitBasis.neg — Method.neg(b::BitStr) -> BitStrBitBasis.neg — Method.neg(index::Integer, nbits::Int) -> IntegerReturn an integer with all bits flipped (with total number of bit nbits).
Example
julia> neg(0b1111, 4) |> bit(len=4)
0000 (0)
julia> neg(0b0111, 4) |> bit(len=4)
1000 (8)BitBasis.onehot — Method.onehot([T=Float64], bit_str[, nbatch])Returns an onehot vector in type Vector{T}, or a batch of onehot vector in type Matrix{T}, where the bit_str-th element is one.
BitBasis.onehot — Method.onehot([T=Float64], nbits, x::Integer; nbatch::Int])Create an onehot vector in type Vector{T} or a batch of onehot vector in type Matrix{T}, where index x + 1 is one.
BitBasis.packbits — Method.packbits(arr::AbstractArray) -> AbstractArraypack bits to integers, usually take a BitArray as input.
BitBasis.readbit — Method.readbit(x, loc...)Read the bit config at given location.
BitBasis.reorder — Function.reorder(X::AbstractArray, orders)Reorder X according to orders.
Although orders can be any iterable, Tuple is preferred inorder to gain as much performance as possible. But the conversion won't take much anyway.
BitBasis.setbit — Method.setbit(index::Integer, mask::Integer) -> Integerset the bit at masked position to 1.
Example
julia> setbit(0b1011, 0b1100) |> bit(len=4)
1111 (15)
julia> setbit(0b1011, 0b0100) |> bit(len=4)
1111 (15)
julia> setbit(0b1011, 0b0000) |> bit(len=4)
1011 (11)BitBasis.swapbits — Method.swapbits(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.
mask_ij should only contain two 1, swapbits will not check it, use at your own risk.
BitBasis.BitStr — Type.BitStr{N,T} <: Integerstruct for bit string with fixed length N, the storage type is T.
BitStr{N,T}(value)
BitStr64{N}(value)
LongBitStr{N}(value)Returns a BitStr.
Example
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> bcat(bit"101" for i in 1:10)
101101101101101101101101101101 (766958445)
julia> repeat(bit"101", 2)
101101 ₍₂₎
julia> bit"1101"[2]
0BitBasis.IterControl — Type.IterControl{S}
IterControl(n::Int, base::Int, masks, ks) -> IterControlIterator to iterate through controlled subspace. See also itercontrol. S is the number of shifts, n is the size of Hilbert space, base is the base of counter, masks and ks are helpers for enumerating over the target Hilbert Space.
BitBasis.ReorderedBasis — Type.ReorderedBasis{N, T}Lazy reorderd basis.
BitBasis.ReorderedBasis — Method.ReorderedBasis(orders::NTuple{N, <:Integer})Returns a lazy set of reordered basis.
BitBasis.next_reordered_basis — Method.next_reordered_basis(basis, takers, differ)Returns the next reordered basis accroding to current basis.
BitBasis.unsafe_reorder — Function.unsafe_reorder(X::AbstractArray, orders)Reorder X according to orders.
unsafe_reorder won't check whether the length of orders and the size of first dimension of X match, use at your own risk.
BitBasis.unsafe_sub — Method.unsafe_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 — Method.unsafe_sub(a::UnitRange{T}, b::Vector{T}) where TReturns a .- b, fallback version when b is a Vector.