Truth Tables

The aigverse library provides support for working with truth tables, which are a fundamental representation of Boolean functions. The TruthTable class offers efficient manipulation and analysis of Boolean functions.

Note

Truth tables provide a complete specification of a Boolean function by listing all possible input combinations and their corresponding outputs. They are particularly useful for small functions where exhaustive enumeration is feasible.

Creating Truth Tables

Truth tables can be created in several ways:

 1from aigverse.utils import TruthTable
 2
 3# Initialize a truth table with 3 variables (2^3 = 8 entries)
 4tt = TruthTable(3)
 5
 6# Create a truth table from a hex string (representing the Majority function)
 7tt.create_from_hex_string("e8")
 8print(f"Truth table from hex string: {tt.to_binary()}")
 9
10# Create a truth table for an AND function
11tt_and = TruthTable(2)
12tt_and.create_from_hex_string("8")
13print(f"AND function: {tt_and.to_binary()}")
14
15# Create a truth table for an OR function
16tt_or = TruthTable(2)
17tt_or.create_from_hex_string("e")
18print(f"OR function: {tt_or.to_binary()}")
Truth table from hex string: 11101000
AND function: 1000
OR function: 1110

Basic Manipulation

Truth tables provide various methods for bit manipulation:

 1# Create a truth table
 2tt = TruthTable(3)
 3tt.create_from_hex_string("e8")  # Majority function
 4
 5# Get individual bits
 6print(f"Original truth table: {tt.to_binary()}")
 7print(f"Bit at position 0: {int(tt.get_bit(0))}")
 8print(f"Bit at position 7: {int(tt.get_bit(7))}")
 9
10# Flip bits
11tt.flip_bit(0)
12tt.flip_bit(7)
13print(f"After flipping bits 0 and 7: {tt.to_binary()}")
14
15# Clear the truth table
16tt.clear()
17print(f"After clearing: {tt.to_binary()}")
18
19# Check if constant
20print(f"Is constant 0? {tt.is_const0()}")
Original truth table: 11101000
Bit at position 0: 0
Bit at position 7: 1
After flipping bits 0 and 7: 01101001
After clearing: 00000000
Is constant 0? True

Truth Table Properties

You can analyze various properties of truth tables:

 1# Create a truth table for XOR
 2tt_xor = TruthTable(2)
 3tt_xor.create_from_hex_string("6")  # XOR function
 4print(f"XOR function: {tt_xor.to_binary()}")
 5
 6# Get number of variables and bits
 7print(f"Number of variables: {tt_xor.num_vars()}")
 8print(f"Number of bits: {tt_xor.num_bits()}")
 9
10# Check if the function is balanced (equal number of 0s and 1s)
11num_ones = sum(int(tt_xor.get_bit(i)) for i in range(tt_xor.num_bits()))
12is_balanced = num_ones == tt_xor.num_bits() // 2
13print(f"Is balanced? {is_balanced}")
XOR function: 0110
Number of variables: 2
Number of bits: 4
Is balanced? True

Truth Table Simulation

The simulation of AIGs and other logic networks using truth tables is covered in the Simulation section of the Algorithms documentation. This approach allows you to obtain the truth tables for outputs and internal nodes of a logic network.

pickle Support

Truth tables support Python’s pickle protocol, allowing you to serialize and deserialize them for persistent storage or use in data science workflows.

 1import pickle
 2
 3# Create a truth table
 4tt = TruthTable(3)
 5tt.create_from_hex_string("d8")  # ITE function
 6
 7# Pickle the truth table
 8with open("tt.pkl", "wb") as f:
 9    pickle.dump(tt, f)
10
11# Unpickle the truth table
12with open("tt.pkl", "rb") as f:
13    unpickled_tt = pickle.load(f)
14
15# Verify that the unpickled object is identical
16print(f"Original:    {tt.to_binary()}")
17print(f"Unpickled:   {unpickled_tt.to_binary()}")
18print(f"Equivalent:  {tt == unpickled_tt}")
Original:    11011000
Unpickled:   11011000
Equivalent:  True

You can also pickle multiple truth tables at once by storing them in a list or tuple.