omnibias.boolean.torch.ops.gates
Boolean truth tables, multilinear extensions, algebraic spectra, and finite-temperature gate systems.
import omnibias.boolean.torch.ops.gatesModule 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}^kand 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 smoothsigmoid(beta z)surrogate backward fromomnibias.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.
linear_threshold
omnibias.boolean.torch.ops.linear_thresholdSource ↗linear_threshold(inputs: Sequence[Tensor], weights: Sequence[float], bias: float, beta: float=10.0) -> TensorHard linear-threshold gate H(sum_k w_k x_k + bias) with surrogate backward.
Parameters
| Name | Type & default | Contract |
|---|---|---|
inputspositional-or-keyword | Sequence[Tensor]Required | See the operation contract and implementation below. |
weightspositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
biaspositional-or-keyword | floatRequired | See the operation contract and implementation below. |
betapositional-or-keyword | floatDefault: 10.0 | See the operation contract and implementation below. |
TensorExplicit validation & failure cases
- When
len(inputs) != len(weights), raisesValueError('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_thresholdomnibias.boolean.torch.ops.gates.linear_threshold
soft_and
omnibias.boolean.torch.ops.soft_andSource ↗soft_and(a: Tensor, b: Tensor) -> Tensora AND b = a * b (product t-norm).
Parameters
| Name | Type & default | Contract |
|---|---|---|
apositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
bpositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
TensorExecuted 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>]" -qRead the implementation
def soft_and(a: Tensor, b: Tensor) -> Tensor:
"""``a AND b = a * b`` (product t-norm)."""
return a * bOther public import paths
omnibias.boolean.torch.ops.soft_andomnibias.boolean.torch.ops.gates.soft_and
soft_implies
omnibias.boolean.torch.ops.soft_impliesSource ↗soft_implies(a: Tensor, b: Tensor) -> Tensora -> b = 1 - a + a*b.
Parameters
| Name | Type & default | Contract |
|---|---|---|
apositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
bpositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
TensorExecuted 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>]" -qRead the implementation
def soft_implies(a: Tensor, b: Tensor) -> Tensor:
"""``a -> b = 1 - a + a*b``."""
return 1.0 - a + a * bOther public import paths
omnibias.boolean.torch.ops.soft_impliesomnibias.boolean.torch.ops.gates.soft_implies
soft_majority3
omnibias.boolean.torch.ops.soft_majority3Source ↗soft_majority3(a: Tensor, b: Tensor, c: Tensor) -> Tensor3-input majority ab + bc + ca - 2abc (exact on vertices).
Parameters
| Name | Type & default | Contract |
|---|---|---|
apositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
bpositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
cpositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
TensorExecuted 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" -qRead 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 * cOther public import paths
omnibias.boolean.torch.ops.soft_majority3omnibias.boolean.torch.ops.gates.soft_majority3
soft_nand
omnibias.boolean.torch.ops.soft_nandSource ↗soft_nand(a: Tensor, b: Tensor) -> Tensora NAND b = 1 - a*b.
Parameters
| Name | Type & default | Contract |
|---|---|---|
apositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
bpositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
TensorExecuted 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>]" -qRead the implementation
def soft_nand(a: Tensor, b: Tensor) -> Tensor:
"""``a NAND b = 1 - a*b``."""
return 1.0 - a * bOther public import paths
omnibias.boolean.torch.ops.soft_nandomnibias.boolean.torch.ops.gates.soft_nand
soft_nor
omnibias.boolean.torch.ops.soft_norSource ↗soft_nor(a: Tensor, b: Tensor) -> Tensora NOR b = 1 - (a + b - a*b).
Parameters
| Name | Type & default | Contract |
|---|---|---|
apositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
bpositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
TensorExecuted 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>]" -qRead 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_noromnibias.boolean.torch.ops.gates.soft_nor
soft_not
omnibias.boolean.torch.ops.soft_notSource ↗soft_not(a: Tensor) -> TensorNOT a = 1 - a.
Parameters
| Name | Type & default | Contract |
|---|---|---|
apositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
TensorExecuted 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" -qRead the implementation
def soft_not(a: Tensor) -> Tensor:
"""``NOT a = 1 - a``."""
return 1.0 - aOther public import paths
omnibias.boolean.torch.ops.soft_notomnibias.boolean.torch.ops.gates.soft_not
soft_or
omnibias.boolean.torch.ops.soft_orSource ↗soft_or(a: Tensor, b: Tensor) -> Tensora OR b = a + b - a*b (probabilistic sum).
Parameters
| Name | Type & default | Contract |
|---|---|---|
apositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
bpositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
TensorExecuted 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>]" -qRead the implementation
def soft_or(a: Tensor, b: Tensor) -> Tensor:
"""``a OR b = a + b - a*b`` (probabilistic sum)."""
return a + b - a * bOther public import paths
omnibias.boolean.torch.ops.soft_oromnibias.boolean.torch.ops.gates.soft_or
soft_xnor
omnibias.boolean.torch.ops.soft_xnorSource ↗soft_xnor(a: Tensor, b: Tensor) -> Tensora XNOR b = 1 - (a + b - 2*a*b).
Parameters
| Name | Type & default | Contract |
|---|---|---|
apositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
bpositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
TensorExecuted 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>]" -qRead 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_xnoromnibias.boolean.torch.ops.gates.soft_xnor
soft_xor
omnibias.boolean.torch.ops.soft_xorSource ↗soft_xor(a: Tensor, b: Tensor) -> Tensora XOR b = a + b - 2*a*b.
Parameters
| Name | Type & default | Contract |
|---|---|---|
apositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
bpositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
TensorExecuted 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 Nonepython -m pytest "packages/omnibias-boolean/tests/test_gates.py::test_soft_gate_is_differentiable" -qRead the implementation
def soft_xor(a: Tensor, b: Tensor) -> Tensor:
"""``a XOR b = a + b - 2*a*b``."""
return a + b - 2.0 * a * bOther public import paths
omnibias.boolean.torch.ops.soft_xoromnibias.boolean.torch.ops.gates.soft_xor
threshold_and
omnibias.boolean.torch.ops.threshold_andSource ↗threshold_and(a: Tensor, b: Tensor, beta: float=10.0) -> TensorHard a AND b (fires iff a + b >= 2); sigmoid(beta z) backward.
Parameters
| Name | Type & default | Contract |
|---|---|---|
apositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
bpositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
betapositional-or-keyword | floatDefault: 10.0 | See the operation contract and implementation below. |
TensorExecuted 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.0python -m pytest "packages/omnibias-boolean/tests/test_gates.py::test_threshold_gate_surrogate_gradient_nonzero" -qRegression 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 -qRead 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_andomnibias.boolean.torch.ops.gates.threshold_and
threshold_not
omnibias.boolean.torch.ops.threshold_notSource ↗threshold_not(a: Tensor, beta: float=10.0) -> TensorHard NOT a (fires iff a < 0.5); sigmoid(beta z) backward.
Parameters
| Name | Type & default | Contract |
|---|---|---|
apositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
betapositional-or-keyword | floatDefault: 10.0 | See the operation contract and implementation below. |
TensorExecuted 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" -qRead 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_notomnibias.boolean.torch.ops.gates.threshold_not
threshold_or
omnibias.boolean.torch.ops.threshold_orSource ↗threshold_or(a: Tensor, b: Tensor, beta: float=10.0) -> TensorHard a OR b (fires iff a + b >= 1); sigmoid(beta z) backward.
Parameters
| Name | Type & default | Contract |
|---|---|---|
apositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
bpositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
betapositional-or-keyword | floatDefault: 10.0 | See the operation contract and implementation below. |
TensorExecuted 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" -qRead 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_oromnibias.boolean.torch.ops.gates.threshold_or