aigverse.utils¶
Provides utility data structures and functions.
Module Contents¶
- class TruthTable(num_vars: int)¶
Represents a dynamic Boolean 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.
- __getitem__(index: int) bool¶
Returns one bit by index.
- Parameters:
index – Bit index.
- Returns:
The bit value.
- Raises:
IndexError – If
indexis 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
indexis out of range.
- __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.
- __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
indexis out of range.
- get_bit(index: int) bool¶
Returns one bit value.
- Parameters:
index – Bit index.
- Returns:
The bit value.
- Raises:
IndexError – If
indexis out of range.
- clear_bit(index: int) None¶
Clears one bit to
0.- Parameters:
index – Bit index.
- Raises:
IndexError – If
indexis out of range.
- flip_bit(index: int) None¶
Toggles one bit value.
- Parameters:
index – Bit index.
- Raises:
IndexError – If
indexis 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_indexis 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_indexis 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.
- 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_indexfixed to0.- Raises:
ValueError – If
var_indexis 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_indexfixed to1.- Raises:
ValueError – If
var_indexis out of range.