BitBasis

BitBasis

Types and operations for basis represented by bits in linear algebra.

For more details please ref to BitBasis.jl.

@bit_str -> BitStr

Construct 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 (17)

julia> bit"100_111_101"
100111101 (317)

julia> bcat(bit"1001", bit"11", bit"1110")
1001111110 (638)

julia> v = collect(1:16);

julia> v[bit"1001"]
10

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.alloneMethod.
allone(b::BitStr, mask::Integer) -> Bool

Return true if all masked position of index is 1.

Example

true if all masked positions are 1.

julia> allone(bit"1011", 0b1011)
true

julia> allone(bit"1011", 0b1001)
true

julia> allone(bit"1011", 0b0100)
false
BitBasis.alloneMethod.
allone(index::Integer, mask::Integer) -> Bool

Return 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)
false
BitBasis.anyoneMethod.
anyone(b::BitStr, mask::Integer) -> Bool

Return true if any masked position of index is 1.

Example

true if any masked positions is 1.

julia> anyone(bit"1011", 0b1001)
true

julia> anyone(bit"1011", 0b1100)
true

julia> anyone(bit"1011", 0b0100)
false
BitBasis.anyoneMethod.
anyone(index::Integer, mask::Integer) -> Bool

Return 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)
false
BitBasis.baddrsMethod.
baddrs(b::Integer) -> Vector

get the locations of nonzeros bits, i.e. the inverse operation of bmask.

BitBasis.baddrsMethod.
baddrs(b::Integer) -> Vector

get the locations of nonzeros bits, i.e. the inverse operation of bmask.

BitBasis.basisMethod.
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.bdistanceMethod.
bdistance(i::Integer, j::Integer) -> Int

Return number of different bits.

BitBasis.bfloatMethod.
bfloat(b::BitStr) -> Float64

float view, with MSB 0 bit numbering. See also wiki: bit numbering

BitBasis.bfloatMethod.
bfloat(b::Integer; nbits::Int=bit_length(b)) -> Float64

float view, with current bit numbering. See also bfloat_r.

Ref: wiki: bit numbering

BitBasis.bfloat_rMethod.
bfloat_r(b::Integer; nbits::Int) -> Float64

float view, with bits read in inverse order.

BitBasis.bfloat_rMethod.
bfloat_r(b::Integer; nbits::Int=bit_length(b)) -> Float64

float view, with reversed bit numbering. See also bfloat.

BitBasis.bintMethod.
bint(b; nbits=nothing) -> Int

integer view, with LSB 0 bit numbering. See also wiki: bit numbering

BitBasis.bintMethod.
bint(b; nbits=nothing) -> Int

integer view, with LSB 0 bit numbering. See also wiki: bit numbering

BitBasis.bint_rMethod.
bint_r(b; nbits::Int) -> Integer

integer read in inverse order.

BitBasis.bint_rMethod.
bint_r(b; nbits::Int) -> Integer

integer read in inverse order.

BitBasis.bitMethod.
bit(x[; len=ndigits(x, base=2)])

Create a BitStr accroding to integer x to given length len.

BitBasis.bitMethod.
bit(string)

Create a BitStr with given string of bits. See also @bit_str.

BitBasis.bitMethod.
bit(;len)

Lazy curried version of bit.

bit_length(x::Integer) -> Int

Return the number of bits required to represent input integer x.

bit_literal(xs...)

Create a BitStr by input bits xs.

Example

julia> bit_literal(1, 0, 1, 0, 1, 1)
110101 (53)
BitBasis.bitarrayMethod.
bitarray(v::Vector, [nbits::Int]) -> BitArray
bitarray(v::Int, nbits::Int) -> BitArray
bitarray(nbits::Int) -> Function

Construct 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.bmaskFunction.
bmask(::Type{T}) where T <: Integer -> zero(T)
bmask([T::Type], positions::Int...) -> T
bmask([T::Type], range::UnitRange{Int}) -> T

Return 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.breflectFunction.
breflect(b::Integer[, masks::Vector{Integer}]; nbits) -> Integer

Return left-right reflected integer.

Example

Reflect the order of bits.

julia> breflect(0b1011; nbits=4) == 0b1101
true
BitBasis.breflectMethod.
breflect(bit_str[, masks])

Return left-right reflected bit string.

BitBasis.bsizeofMethod.
bsizeof(::Type)

Returns the size of given type in number of binary digits.

BitBasis.btruncateMethod.
truncate(b, n)

Truncate bits b to given length n.

BitBasis.controldoMethod.
controldo(f, itr::IterControl)

Execute f while iterating itr.

Note

this is faster but equivalent than using itr as an iterator. See also itercontrol.

controller(cbits, cvals) -> Function

Return a function that checks whether a basis at cbits takes specific value cvals.

BitBasis.flipMethod.
flip(bit_str, mask::Integer) -> Integer

Return an BitStr with bits at masked position flipped.

Example

julia> flip(bit"1011", 0b1011)
0000 (0)
BitBasis.flipMethod.
flip(index::Integer, mask::Integer) -> Integer

Return an Integer with bits at masked position flipped.

Example

julia> flip(0b1011, 0b1011) |> bit(len=4)
0000 (0)
group_shift!(nbits, positions)

Shift bits on positions together.

hypercubic(A::Array) -> Array

get the hypercubic representation for an array.

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.invorderMethod.
invorder(X::AbstractVecOrMat)

Inverse the order of given vector/matrix X.

BitBasis.ismatchMethod.
ismatch(index::Integer, mask::Integer, target::Integer) -> Bool

Return 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)
true
BitBasis.ismatchMethod.
ismatch(index::Integer, mask::Integer, target::Integer) -> Bool

Return 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)
true
itercontrol([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.log2dim1Method.
log2dim1(X)

Returns the log2 of the first dimension's size.

BitBasis.log2iFunction.
log2i(x::Integer) -> Integer

Return log2(x), this integer version of log2 is fast but only valid for number equal to 2^n.

BitBasis.negMethod.
neg(bit_str) -> Integer

Return an BitStr with all bits flipped.

Example

julia> neg(bit"1111", 4)
0000 (0)

julia> neg(bit"0111", 4)
1000 (8)
BitBasis.negMethod.
neg(index::Integer, nbits::Int) -> Integer

Return 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.onehotMethod.
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.onehotMethod.
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.packbitsMethod.
packbits(arr::AbstractArray) -> AbstractArray

pack bits to integers, usually take a BitArray as input.

BitBasis.readbitMethod.
readbit(x, loc...)

Read the bit config at given location.

BitBasis.reorderFunction.
reorder(X::AbstractArray, orders)

Reorder X according to orders.

Tip

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.setbitMethod.
setbit(b::BitStr, mask::Integer) -> Integer

set the bit at masked position to 1.

Example

julia> setbit(bit"1011", 0b1100)
1111 (15)

julia> setbit(bit"1011", 0b0100)
1111 (15)

julia> setbit(bit"1011", 0b0000)
1011 (11)
BitBasis.setbitMethod.
setbit(index::Integer, mask::Integer) -> Integer

set 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.swapbitsMethod.
swapbits(n::BitStr, mask_ij::Integer) -> BitStr
swapbits(n::BitStr, i::Int, j::Int) -> BitStr

Return a BitStr with bits at i and j flipped.

Example

julia> swapbits(0b1011, 0b1100) == 0b0111
true
Warning

mask_ij should only contain two 1, swapbits will not check it, use at your own risk.

BitBasis.swapbitsMethod.
swapbits(n::Integer, mask_ij::Integer) -> Integer
swapbits(n::Integer, i::Int, j::Int) -> Integer

Return an integer with bits at i and j flipped.

Example

julia> swapbits(0b1011, 0b1100) == 0b0111
true
Tip

locations i and j specified by mask could be faster when bmask is not straight forward but known by constant.

Warning

mask_ij should only contain two 1, swapbits will not check it, use at your own risk.

to_location(x)

Convert bit configuration x to an index.

Example

julia> to_location(1)
2

julia> to_location(bit"111")
111 (7)
BitBasis.BitStrType.
BitStr{T}

String literal for bits.

BitStr(value[, len=ndigits(value)])

Returns a BitStr, by default the length is set to the minimum length required to represent value as bits.

BitStr(str::String)

Parse the input string to a BitStr. See @bit_str for more details.

Example

BitStr supports some basic arithmetic operations. It acts like an integer, but supports some frequently used methods for binary basis.

julia> bit"101" * 2
1010 (10)

julia> bcat(bit"101" for i in 1:10)
101101101101101101101101101101 (766958445)

julia> repeat(bit"101", 2)
101101 (45)

julia> bit"1101"[2]
0
IterControl{N, S, T}

Iterator to iterate through controlled subspace. See also itercontrol. N is the size of whole hilbert space, S is the number of shifts.

ReorderedBasis{N, T}

Lazy reorderd basis.

ReorderedBasis(orders::NTuple{N, <:Integer})

Returns a lazy set of reordered basis.

next_reordered_basis(basis, takers, differ)

Returns the next reordered basis accroding to current basis.

unsafe_reorder(X::AbstractArray, orders)

Reorder X according to orders.

Warning

unsafe_reorder won't check whether the length of orders and the size of first dimension of X match, use at your own risk.

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.

unsafe_sub(a::UnitRange{T}, b::Vector{T}) where T

Returns a .- b, fallback version when b is a Vector.