q1ss.binalg.binvec

Binary vectors.

Bit

Bit

Type alias for a bit value.

alias of Literal[0, 1]

binvec

final class binvec(data, *, readonly=False, copy=False)[source]

Bases: bintensor

A mutable binary vector.

__bytes__()[source]

Converts this binary vector to bytes.

Return type:

bytes

__getitem__(idx)[source]

If the index is an integer, returns the corresponding entry of the vec. If the index is a slice or a list/array of integers, returns the vec containing the selected entries.

Parameters:

idx (int | slice | list[int])

Return type:

Bit | binvec

__imatmul__(other)[source]

Inplace binary vector-matrix multiplication.

Raises:

ShapeError – if the intermediate dimensions don’t match.

Parameters:

other (binmat)

Return type:

binvec

__int__()[source]

Converts the binary vector to an integer.

Return type:

int

__matmul__(other)[source]

Binary vector-vector inner product or vector-matrix multiplication.

Raises:

ShapeError – if the intermediate dimensions don’t match.

Parameters:

other (binvec | binmat)

Return type:

Bit | binvec

static __new__(cls, data, *, readonly=False, copy=False)[source]

Creates a new vector from binary data.

If readonly=True, the resulting tensor and its data are readonly. If copy=True, a fresh copy of the given data is used.

Warning

The internal logic of binvec presumes that the given data will not be mutated externally to the binvec object after construction. If a fresh copy is needed, pass copy=True at construction.

Parameters:
  • data (npt.ArrayLike)

  • readonly (bool; default = False)

  • copy (bool; default = False)

Return type:

Self

__or__(other)[source]

Horizontal stacking of two vectors.

Parameters:

other (binvec)

Return type:

binvec

__setitem__(idx, value)[source]

Sets a single value, or a slice/selection of values.

Parameters:
Return type:

None

property bin

Binary string representation of this binary vector.

Return type:

str

static el(idx, dim, *, readonly=False)[source]

Returns the canonical basis vector with given index in given dimension.

Parameters:
  • idx (int)

  • dim (int)

  • readonly (bool; default = False)

Return type:

binvec

static from_bool(bits, *, readonly=False)[source]

Constructs a binary vector from an interable of boolean values.

Parameters:
Return type:

binvec

static from_bytes(b, num_bits=None, *, readonly=False)[source]

Converts bytes to a binary vector containing the corresponding bits. The binary vector has length 8*len(b) by default, containing all bits, but length can be truncated by specifying a desired num_bits between len(b)-7 and len(b) (both inclusive). If a length is specified, the bits ignored at the end must all be zero.

Parameters:
Return type:

binvec

static from_int(bits, n)[source]

Constructs a binary vector from an integer.

Parameters:
Return type:

binvec

static from_str(bits, *, readonly=False)[source]

Constructs a binary vector from a string with chars '0' and '1'.

Parameters:
  • bits (str)

  • readonly (bool; default = False)

Return type:

binvec

static hstack(vecs, *, readonly=False)[source]

Stacks the given vectors horizontally.

Parameters:
Return type:

binvec

static iter_all(dim, *, readonly=False)[source]

Iterates through all binary vectors with given dimension.

Parameters:
  • dim (int)

  • readonly (bool; default = False)

Return type:

Iterator[binvec]

static iter_std_basis(dim, *, readonly=False)[source]

Iterates through all standard basis binary vectors with given dimension.

Parameters:
  • dim (int)

  • readonly (bool; default = False)

Return type:

Iterator[binvec]

static random(dim, *, rng=None, readonly=False)[source]

Random binary vector with given dimension.

Parameters:
  • dim (int)

  • rng (np.random.Generator | int | None)

  • readonly (bool; default = False)

Return type:

binvec

static validate_bitstr(bits)[source]

Validate a binary string. Raises TypeError or ValueError for invalid strings, returns True otherwise.

Parameters:

bits (str)

Return type:

Literal[True]

static validate_dim(dim, *, positive=True)[source]

Validate a binvec dimension. Raises TypeError or ValueError for invalid dimensions, returns True otherwise.

Parameters:
  • dim (int)

  • positive (bool; default = True)

Return type:

Literal[True]

static zeros(dim, *, readonly=False)[source]

Constructs a zero binary vector with given dimension.

Parameters:
  • dim (int)

  • readonly (bool; default = False)

Return type:

binvec