aigverse.utils

Provides utility data structures and functions.

Module Contents

class TruthTable(num_vars: int)

Represents a dynamic Boolean truth table.

__eq__(other: object) bool

Checks equality with another truth table.

__ne__(other: object) bool

Checks inequality with another truth table.

__lt__(other: TruthTable) bool

Lexicographically compares two truth tables.

__and__(other: TruthTable) TruthTable

Computes bitwise AND with another truth table.

__or__(other: TruthTable) TruthTable

Computes bitwise OR with another truth table.

__xor__(other: TruthTable) TruthTable

Computes bitwise XOR with another truth table.

__invert__() TruthTable

Computes bitwise NOT.

__len__() int

Returns the number of bits.

__getitem__(index: int) bool

Returns one bit by index.

Parameters:

index – Bit index.

Returns:

The bit value.

Raises:

IndexError – If index is out of range.

__setitem__(index: int, value: bool) None

Sets one bit by index.

Parameters:
  • index – Bit index.

  • value – New bit value.

Raises:

IndexError – If index is out of range.

__iter__() Iterator[bool]

Returns an iterator over all bits.

num_vars() int

Returns the number of variables.

num_blocks() int

Returns the number of storage blocks.

num_bits() int

Returns the number of truth table bits.

__copy__() TruthTable

Returns a shallow copy of the truth table.

__deepcopy__(arg: dict, /) TruthTable

Returns a deep copy of the truth table.

__assign__(other: TruthTable) TruthTable

Assigns from another truth table with a compatible shape.

Parameters:

other – Source truth table.

Returns:

The updated truth table.

__hash__() int

Returns a hash value for dictionary/set usage.

__getstate__() tuple

Returns pickle state as (num_vars, words).

__setstate__(state: tuple) None

Restores a truth table from pickle state.

Parameters:

state – Tuple (num_vars, words).

Raises:
  • RuntimeError – If the serialized state is malformed.

  • TypeError – If nanobind cannot convert the pickle payload to the expected C++ types.

set_bit(index: int) None

Sets one bit to 1.

Parameters:

index – Bit index.

Raises:

IndexError – If index is out of range.

get_bit(index: int) bool

Returns one bit value.

Parameters:

index – Bit index.

Returns:

The bit value.

Raises:

IndexError – If index is out of range.

clear_bit(index: int) None

Clears one bit to 0.

Parameters:

index – Bit index.

Raises:

IndexError – If index is out of range.

flip_bit(index: int) None

Toggles one bit value.

Parameters:

index – Bit index.

Raises:

IndexError – If index is out of range.

get_block(block_index: int) int

Returns one 64-bit storage block.

Parameters:

block_index – Block index.

Returns:

The block value.

Raises:

IndexError – If block_index is out of range.

create_nth_var(var_index: int, complement: bool = False) None

Creates the projection function for one variable.

Parameters:
  • var_index – Variable index to project.

  • complement – Whether to complement the projection.

Raises:

IndexError – If var_index is out of range.

create_from_binary_string(binary: str) None

Loads bit values from a binary string.

Parameters:

binary – Binary string of length num_bits.

Raises:

ValueError – If the string length does not match num_bits.

create_from_hex_string(hexadecimal: str) None

Loads bit values from a hexadecimal string.

Parameters:

hexadecimal – Hex string matching the truth table size.

Raises:

ValueError – If the string length does not match the expected size.

create_random() None

Fills the truth table with random bits.

create_majority() None

Fills the truth table with the majority function.

clear() None

Clears all bits to 0.

count_ones() int

Returns the number of set bits.

count_zeroes() int

Returns the number of cleared bits.

is_const0() bool

Returns whether all bits are 0.

is_const1() bool

Returns whether all bits are 1.

to_binary() str

Returns the truth table as a binary string.

to_hex() str

Returns the truth table as a hexadecimal string.

ternary_majority(a: TruthTable, b: TruthTable, c: TruthTable) TruthTable

Computes the ternary majority of three truth tables.

Parameters:
  • a – First truth table.

  • b – Second truth table.

  • c – Third truth table.

Returns:

The bitwise majority truth table.

cofactor0(tt: TruthTable, var_index: int) TruthTable

Computes the cofactor with respect to assigning one variable to 0.

Parameters:
  • tt – Input truth table.

  • var_index – Index of the variable to cofactor.

Returns:

The cofactored truth table with var_index fixed to 0.

Raises:

ValueError – If var_index is out of range.

cofactor1(tt: TruthTable, var_index: int) TruthTable

Computes the cofactor with respect to assigning one variable to 1.

Parameters:
  • tt – Input truth table.

  • var_index – Index of the variable to cofactor.

Returns:

The cofactored truth table with var_index fixed to 1.

Raises:

ValueError – If var_index is out of range.