Skip to main content

omnibias.boolean.torch.ops.gates

0.1.0a2AlphaPython >=3.10Apache-2.0

Boolean truth tables, multilinear extensions, algebraic spectra, and finite-temperature gate systems.

import omnibias.boolean.torch.ops.gates
Module contract & mathematical conventions

Soft logic gates (torch): product-t-norm and hard-forward threshold gates.

Two families of differentiable gates on {0, 1} inputs:

  • **product-t-norm gates** -- the multilinear extensions of the Boolean gates (and = ab, or = a + b - ab, xor = a + b - 2ab, ...). They are *exact* on the cube vertices {0, 1}^k and smooth in between, so they compose into differentiable circuits whose value at any Boolean input is the true gate output;
  • **threshold gates** -- a hard {0, 1} forward (a linear threshold) with the smooth sigmoid(beta z) surrogate backward from omnibias.binary.torch.ops.binarize01. Use these when you want the forward pass to stay exactly Boolean while still receiving a gradient.

Shared formulas do not imply universal floating-point identity. Verify dtype, shape, derivative-order support, and the stated numerical assumptions for the path you use.

FUNCTION

linear_threshold​

omnibias.boolean.torch.ops.linear_thresholdSource ↗
linear_threshold(inputs: Sequence[Tensor], weights: Sequence[float], bias: float, beta: float=10.0) -> Tensor

Hard linear-threshold gate H(sum_k w_k x_k + bias) with surrogate backward.

Parameters

NameType & defaultContract
inputspositional-or-keywordSequence[Tensor]RequiredSee the operation contract and implementation below.
weightspositional-or-keywordSequence[float]RequiredSee the operation contract and implementation below.
biaspositional-or-keywordfloatRequiredSee the operation contract and implementation below.
betapositional-or-keywordfloatDefault: 10.0See the operation contract and implementation below.
ReturnsTensor
Explicit validation & failure cases
  • When len(inputs) != len(weights), raises ValueError('inputs and weights must have equal length').

These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.

Read the implementation
def linear_threshold(
    inputs: Sequence[Tensor], weights: Sequence[float], bias: float, beta: float = 10.0
) -> Tensor:
    """Hard linear-threshold gate ``H(sum_k w_k x_k + bias)`` with surrogate backward."""
    if len(inputs) != len(weights):
        raise ValueError("inputs and weights must have equal length")
    acc = inputs[0] * weights[0]
    for x, w in zip(inputs[1:], weights[1:], strict=False):
        acc = acc + x * w
    return binarize01(acc + bias, beta)
Other public import paths
  • omnibias.boolean.torch.ops.linear_threshold
  • omnibias.boolean.torch.ops.gates.linear_threshold
FUNCTION

soft_and​

omnibias.boolean.torch.ops.soft_andSource ↗
soft_and(a: Tensor, b: Tensor) -> Tensor

a AND b = a * b (product t-norm).

Parameters

NameType & defaultContract
apositional-or-keywordTensorRequiredSee the operation contract and implementation below.
bpositional-or-keywordTensorRequiredSee the operation contract and implementation below.
ReturnsTensor
Executed regression · two input gates exact on vertices

Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.

def test_two_input_gates_exact_on_vertices(soft, ref) -> None:  # type: ignore[no-untyped-def]
    for a, b in itertools.product((0, 1), repeat=2):
        out = float(soft(torch.tensor(float(a)), torch.tensor(float(b))))
        assert out == pytest.approx(float(ref(a, b)), abs=1e-12)
python -m pytest "packages/omnibias-boolean/tests/test_gates.py::test_two_input_gates_exact_on_vertices[soft_and-<lambda>]" -q
Read the implementation
def soft_and(a: Tensor, b: Tensor) -> Tensor:
    """``a AND b = a * b`` (product t-norm)."""
    return a * b
Other public import paths
  • omnibias.boolean.torch.ops.soft_and
  • omnibias.boolean.torch.ops.gates.soft_and
FUNCTION

soft_implies​

omnibias.boolean.torch.ops.soft_impliesSource ↗
soft_implies(a: Tensor, b: Tensor) -> Tensor

a -> b = 1 - a + a*b.

Parameters

NameType & defaultContract
apositional-or-keywordTensorRequiredSee the operation contract and implementation below.
bpositional-or-keywordTensorRequiredSee the operation contract and implementation below.
ReturnsTensor
Executed regression · two input gates exact on vertices

Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.

def test_two_input_gates_exact_on_vertices(soft, ref) -> None:  # type: ignore[no-untyped-def]
    for a, b in itertools.product((0, 1), repeat=2):
        out = float(soft(torch.tensor(float(a)), torch.tensor(float(b))))
        assert out == pytest.approx(float(ref(a, b)), abs=1e-12)
python -m pytest "packages/omnibias-boolean/tests/test_gates.py::test_two_input_gates_exact_on_vertices[soft_implies-<lambda>]" -q
Read the implementation
def soft_implies(a: Tensor, b: Tensor) -> Tensor:
    """``a -> b = 1 - a + a*b``."""
    return 1.0 - a + a * b
Other public import paths
  • omnibias.boolean.torch.ops.soft_implies
  • omnibias.boolean.torch.ops.gates.soft_implies
FUNCTION

soft_majority3​

omnibias.boolean.torch.ops.soft_majority3Source ↗
soft_majority3(a: Tensor, b: Tensor, c: Tensor) -> Tensor

3-input majority ab + bc + ca - 2abc (exact on vertices).

Parameters

NameType & defaultContract
apositional-or-keywordTensorRequiredSee the operation contract and implementation below.
bpositional-or-keywordTensorRequiredSee the operation contract and implementation below.
cpositional-or-keywordTensorRequiredSee the operation contract and implementation below.
ReturnsTensor
Executed regression · not and majority

Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.

def test_not_and_majority() -> None:
    assert float(gates.soft_not(torch.tensor(0.0))) == 1.0
    assert float(gates.soft_not(torch.tensor(1.0))) == 0.0
    for a, b, c in itertools.product((0, 1), repeat=3):
        out = float(
            gates.soft_majority3(
                torch.tensor(float(a)), torch.tensor(float(b)), torch.tensor(float(c))
            )
        )
        assert out == pytest.approx(float(1 if (a + b + c) >= 2 else 0), abs=1e-12)
python -m pytest "packages/omnibias-boolean/tests/test_gates.py::test_not_and_majority" -q
Read the implementation
def soft_majority3(a: Tensor, b: Tensor, c: Tensor) -> Tensor:
    """3-input majority ``ab + bc + ca - 2abc`` (exact on vertices)."""
    return a * b + b * c + c * a - 2.0 * a * b * c
Other public import paths
  • omnibias.boolean.torch.ops.soft_majority3
  • omnibias.boolean.torch.ops.gates.soft_majority3
FUNCTION

soft_nand​

omnibias.boolean.torch.ops.soft_nandSource ↗
soft_nand(a: Tensor, b: Tensor) -> Tensor

a NAND b = 1 - a*b.

Parameters

NameType & defaultContract
apositional-or-keywordTensorRequiredSee the operation contract and implementation below.
bpositional-or-keywordTensorRequiredSee the operation contract and implementation below.
ReturnsTensor
Executed regression · two input gates exact on vertices

Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.

def test_two_input_gates_exact_on_vertices(soft, ref) -> None:  # type: ignore[no-untyped-def]
    for a, b in itertools.product((0, 1), repeat=2):
        out = float(soft(torch.tensor(float(a)), torch.tensor(float(b))))
        assert out == pytest.approx(float(ref(a, b)), abs=1e-12)
python -m pytest "packages/omnibias-boolean/tests/test_gates.py::test_two_input_gates_exact_on_vertices[soft_nand-<lambda>]" -q
Read the implementation
def soft_nand(a: Tensor, b: Tensor) -> Tensor:
    """``a NAND b = 1 - a*b``."""
    return 1.0 - a * b
Other public import paths
  • omnibias.boolean.torch.ops.soft_nand
  • omnibias.boolean.torch.ops.gates.soft_nand
FUNCTION

soft_nor​

omnibias.boolean.torch.ops.soft_norSource ↗
soft_nor(a: Tensor, b: Tensor) -> Tensor

a NOR b = 1 - (a + b - a*b).

Parameters

NameType & defaultContract
apositional-or-keywordTensorRequiredSee the operation contract and implementation below.
bpositional-or-keywordTensorRequiredSee the operation contract and implementation below.
ReturnsTensor
Executed regression · two input gates exact on vertices

Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.

def test_two_input_gates_exact_on_vertices(soft, ref) -> None:  # type: ignore[no-untyped-def]
    for a, b in itertools.product((0, 1), repeat=2):
        out = float(soft(torch.tensor(float(a)), torch.tensor(float(b))))
        assert out == pytest.approx(float(ref(a, b)), abs=1e-12)
python -m pytest "packages/omnibias-boolean/tests/test_gates.py::test_two_input_gates_exact_on_vertices[soft_nor-<lambda>]" -q
Read the implementation
def soft_nor(a: Tensor, b: Tensor) -> Tensor:
    """``a NOR b = 1 - (a + b - a*b)``."""
    return 1.0 - (a + b - a * b)
Other public import paths
  • omnibias.boolean.torch.ops.soft_nor
  • omnibias.boolean.torch.ops.gates.soft_nor
FUNCTION

soft_not​

omnibias.boolean.torch.ops.soft_notSource ↗
soft_not(a: Tensor) -> Tensor

NOT a = 1 - a.

Parameters

NameType & defaultContract
apositional-or-keywordTensorRequiredSee the operation contract and implementation below.
ReturnsTensor
Executed regression · not and majority

Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.

def test_not_and_majority() -> None:
    assert float(gates.soft_not(torch.tensor(0.0))) == 1.0
    assert float(gates.soft_not(torch.tensor(1.0))) == 0.0
    for a, b, c in itertools.product((0, 1), repeat=3):
        out = float(
            gates.soft_majority3(
                torch.tensor(float(a)), torch.tensor(float(b)), torch.tensor(float(c))
            )
        )
        assert out == pytest.approx(float(1 if (a + b + c) >= 2 else 0), abs=1e-12)
python -m pytest "packages/omnibias-boolean/tests/test_gates.py::test_not_and_majority" -q
Read the implementation
def soft_not(a: Tensor) -> Tensor:
    """``NOT a = 1 - a``."""
    return 1.0 - a
Other public import paths
  • omnibias.boolean.torch.ops.soft_not
  • omnibias.boolean.torch.ops.gates.soft_not
FUNCTION

soft_or​

omnibias.boolean.torch.ops.soft_orSource ↗
soft_or(a: Tensor, b: Tensor) -> Tensor

a OR b = a + b - a*b (probabilistic sum).

Parameters

NameType & defaultContract
apositional-or-keywordTensorRequiredSee the operation contract and implementation below.
bpositional-or-keywordTensorRequiredSee the operation contract and implementation below.
ReturnsTensor
Executed regression · two input gates exact on vertices

Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.

def test_two_input_gates_exact_on_vertices(soft, ref) -> None:  # type: ignore[no-untyped-def]
    for a, b in itertools.product((0, 1), repeat=2):
        out = float(soft(torch.tensor(float(a)), torch.tensor(float(b))))
        assert out == pytest.approx(float(ref(a, b)), abs=1e-12)
python -m pytest "packages/omnibias-boolean/tests/test_gates.py::test_two_input_gates_exact_on_vertices[soft_or-<lambda>]" -q
Read the implementation
def soft_or(a: Tensor, b: Tensor) -> Tensor:
    """``a OR b = a + b - a*b`` (probabilistic sum)."""
    return a + b - a * b
Other public import paths
  • omnibias.boolean.torch.ops.soft_or
  • omnibias.boolean.torch.ops.gates.soft_or
FUNCTION

soft_xnor​

omnibias.boolean.torch.ops.soft_xnorSource ↗
soft_xnor(a: Tensor, b: Tensor) -> Tensor

a XNOR b = 1 - (a + b - 2*a*b).

Parameters

NameType & defaultContract
apositional-or-keywordTensorRequiredSee the operation contract and implementation below.
bpositional-or-keywordTensorRequiredSee the operation contract and implementation below.
ReturnsTensor
Executed regression · two input gates exact on vertices

Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.

def test_two_input_gates_exact_on_vertices(soft, ref) -> None:  # type: ignore[no-untyped-def]
    for a, b in itertools.product((0, 1), repeat=2):
        out = float(soft(torch.tensor(float(a)), torch.tensor(float(b))))
        assert out == pytest.approx(float(ref(a, b)), abs=1e-12)
python -m pytest "packages/omnibias-boolean/tests/test_gates.py::test_two_input_gates_exact_on_vertices[soft_xnor-<lambda>]" -q
Read the implementation
def soft_xnor(a: Tensor, b: Tensor) -> Tensor:
    """``a XNOR b = 1 - (a + b - 2*a*b)``."""
    return 1.0 - (a + b - 2.0 * a * b)
Other public import paths
  • omnibias.boolean.torch.ops.soft_xnor
  • omnibias.boolean.torch.ops.gates.soft_xnor
FUNCTION

soft_xor​

omnibias.boolean.torch.ops.soft_xorSource ↗
soft_xor(a: Tensor, b: Tensor) -> Tensor

a XOR b = a + b - 2*a*b.

Parameters

NameType & defaultContract
apositional-or-keywordTensorRequiredSee the operation contract and implementation below.
bpositional-or-keywordTensorRequiredSee the operation contract and implementation below.
ReturnsTensor
Executed regression · soft gate is differentiable

Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.

def test_soft_gate_is_differentiable() -> None:
    a = torch.tensor(0.3, requires_grad=True)
    b = torch.tensor(0.7, requires_grad=True)
    gates.soft_xor(a, b).backward()
    assert a.grad is not None and b.grad is not None
python -m pytest "packages/omnibias-boolean/tests/test_gates.py::test_soft_gate_is_differentiable" -q
Read the implementation
def soft_xor(a: Tensor, b: Tensor) -> Tensor:
    """``a XOR b = a + b - 2*a*b``."""
    return a + b - 2.0 * a * b
Other public import paths
  • omnibias.boolean.torch.ops.soft_xor
  • omnibias.boolean.torch.ops.gates.soft_xor
FUNCTION

threshold_and​

omnibias.boolean.torch.ops.threshold_andSource ↗
threshold_and(a: Tensor, b: Tensor, beta: float=10.0) -> Tensor

Hard a AND b (fires iff a + b >= 2); sigmoid(beta z) backward.

Parameters

NameType & defaultContract
apositional-or-keywordTensorRequiredSee the operation contract and implementation below.
bpositional-or-keywordTensorRequiredSee the operation contract and implementation below.
betapositional-or-keywordfloatDefault: 10.0See the operation contract and implementation below.
ReturnsTensor
Executed regression · threshold gate surrogate gradient nonzero

Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.

def test_threshold_gate_surrogate_gradient_nonzero() -> None:
    a = torch.tensor(0.4, dtype=torch.float64, requires_grad=True)
    b = torch.tensor(0.4, dtype=torch.float64, requires_grad=True)
    gates.threshold_and(a, b, beta=4.0).backward()
    # Hard forward, but the sigmoid surrogate still passes a gradient.
    assert abs(float(a.grad)) > 0.0
python -m pytest "packages/omnibias-boolean/tests/test_gates.py::test_threshold_gate_surrogate_gradient_nonzero" -q
Regression example · threshold gates hard forward

This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.

def test_threshold_gates_hard_forward() -> None:
    for a, b in itertools.product((0, 1), repeat=2):
        ta, tb = torch.tensor(float(a)), torch.tensor(float(b))
        assert float(gates.threshold_and(ta, tb)) == float(a & b)
        assert float(gates.threshold_or(ta, tb)) == float(a | b)
        assert float(gates.threshold_not(ta)) == float(1 - a)
python -m pytest packages/omnibias-boolean/tests/test_gates.py -k test_threshold_gates_hard_forward -q
Read the implementation
def threshold_and(a: Tensor, b: Tensor, beta: float = 10.0) -> Tensor:
    """Hard ``a AND b`` (fires iff ``a + b >= 2``); ``sigmoid(beta z)`` backward."""
    return binarize01(a + b - 1.5, beta)
Other public import paths
  • omnibias.boolean.torch.ops.threshold_and
  • omnibias.boolean.torch.ops.gates.threshold_and
FUNCTION

threshold_not​

omnibias.boolean.torch.ops.threshold_notSource ↗
threshold_not(a: Tensor, beta: float=10.0) -> Tensor

Hard NOT a (fires iff a < 0.5); sigmoid(beta z) backward.

Parameters

NameType & defaultContract
apositional-or-keywordTensorRequiredSee the operation contract and implementation below.
betapositional-or-keywordfloatDefault: 10.0See the operation contract and implementation below.
ReturnsTensor
Executed regression · threshold gates hard forward

Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.

def test_threshold_gates_hard_forward() -> None:
    for a, b in itertools.product((0, 1), repeat=2):
        ta, tb = torch.tensor(float(a)), torch.tensor(float(b))
        assert float(gates.threshold_and(ta, tb)) == float(a & b)
        assert float(gates.threshold_or(ta, tb)) == float(a | b)
        assert float(gates.threshold_not(ta)) == float(1 - a)
python -m pytest "packages/omnibias-boolean/tests/test_gates.py::test_threshold_gates_hard_forward" -q
Read the implementation
def threshold_not(a: Tensor, beta: float = 10.0) -> Tensor:
    """Hard ``NOT a`` (fires iff ``a < 0.5``); ``sigmoid(beta z)`` backward."""
    return binarize01(0.5 - a, beta)
Other public import paths
  • omnibias.boolean.torch.ops.threshold_not
  • omnibias.boolean.torch.ops.gates.threshold_not
FUNCTION

threshold_or​

omnibias.boolean.torch.ops.threshold_orSource ↗
threshold_or(a: Tensor, b: Tensor, beta: float=10.0) -> Tensor

Hard a OR b (fires iff a + b >= 1); sigmoid(beta z) backward.

Parameters

NameType & defaultContract
apositional-or-keywordTensorRequiredSee the operation contract and implementation below.
bpositional-or-keywordTensorRequiredSee the operation contract and implementation below.
betapositional-or-keywordfloatDefault: 10.0See the operation contract and implementation below.
ReturnsTensor
Executed regression · threshold gates hard forward

Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.

def test_threshold_gates_hard_forward() -> None:
    for a, b in itertools.product((0, 1), repeat=2):
        ta, tb = torch.tensor(float(a)), torch.tensor(float(b))
        assert float(gates.threshold_and(ta, tb)) == float(a & b)
        assert float(gates.threshold_or(ta, tb)) == float(a | b)
        assert float(gates.threshold_not(ta)) == float(1 - a)
python -m pytest "packages/omnibias-boolean/tests/test_gates.py::test_threshold_gates_hard_forward" -q
Read the implementation
def threshold_or(a: Tensor, b: Tensor, beta: float = 10.0) -> Tensor:
    """Hard ``a OR b`` (fires iff ``a + b >= 1``); ``sigmoid(beta z)`` backward."""
    return binarize01(a + b - 0.5, beta)
Other public import paths
  • omnibias.boolean.torch.ops.threshold_or
  • omnibias.boolean.torch.ops.gates.threshold_or