omnibias.boolean.torch.ops.design
Boolean truth tables, multilinear extensions, algebraic spectra, and finite-temperature gate systems.
import omnibias.boolean.torch.ops.designModule contract & mathematical conventions
Differentiable Boolean-function design losses (torch).
Spectral objectives for *shaping* a Boolean function during optimization, all
built on the differentiable ~omnibias.boolean.torch.ops.spectrum engine:
degree_penalty-- penalize high-order spectral energy (push toward low-degree / low-complexity functions, e.g. for noise-stable or easy-to-learn targets);influence_penalty-- penalize total influence (average sensitivity);target_spectrum_loss-- match a prescribed Walsh or Mobius spectrum (spectral design / function approximation).
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.
degree_penalty
omnibias.boolean.torch.ops.degree_penaltySource ↗degree_penalty(values: Sequence[float] | Tensor) -> Tensorsum_S |S| * hat f(S)^2 -- spectral energy weighted by monomial order.
Parameters
| Name | Type & default | Contract |
|---|---|---|
valuespositional-or-keyword | Sequence[float] | TensorRequired | See the operation contract and implementation below. |
TensorExecuted regression · design loss parity
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_design_loss_parity() -> None:
vals = [0.1, 0.7, 0.3, 0.9]
t = float(tb.degree_penalty(torch.tensor(vals, dtype=torch.float64)))
j = float(jb.degree_penalty(jnp.asarray(vals, dtype=jnp.float64)))
assert t == pytest.approx(j, rel=1e-9, abs=1e-11)python -m pytest "packages/omnibias-boolean/tests/test_cross_backend.py::test_design_loss_parity" -qRegression example · degree penalty orders functions
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_degree_penalty_orders_functions() -> None:
# A degree-1 dictator has less high-order energy than degree-2 XOR.
dictator = torch.tensor(pm1_values((0, 0, 1, 1)), dtype=torch.float64) # f = x1
xor = torch.tensor(pm1_values((0, 1, 1, 0)), dtype=torch.float64)
assert float(design.degree_penalty(dictator)) < float(design.degree_penalty(xor))python -m pytest packages/omnibias-boolean/tests/test_design.py -k test_degree_penalty_orders_functions -qRead the implementation
def degree_penalty(values: Sequence[float] | Tensor) -> Tensor:
"""``sum_S |S| * hat f(S)^2`` -- spectral energy weighted by monomial order."""
w = walsh_coeffs(values)
n = w.shape[0].bit_length() - 1
orders = torch.tensor(
[bin(mask).count("1") for mask in range(1 << n)],
dtype=w.dtype,
device=w.device,
)
return (orders * w**2).sum()Other public import paths
omnibias.boolean.torch.ops.degree_penaltyomnibias.boolean.torch.ops.design.degree_penalty
influence_penalty
omnibias.boolean.torch.ops.influence_penaltySource ↗influence_penalty(values: Sequence[float] | Tensor) -> TensorTotal influence sum_i Inf_i (average sensitivity), differentiable.
Parameters
| Name | Type & default | Contract |
|---|---|---|
valuespositional-or-keyword | Sequence[float] | TensorRequired | See the operation contract and implementation below. |
TensorExecuted regression · influence penalty 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_influence_penalty_is_differentiable() -> None:
vals = torch.rand(8, dtype=torch.float64, requires_grad=True)
design.influence_penalty(vals).backward()
assert vals.grad is not Nonepython -m pytest "packages/omnibias-boolean/tests/test_design.py::test_influence_penalty_is_differentiable" -qRead the implementation
def influence_penalty(values: Sequence[float] | Tensor) -> Tensor:
"""Total influence ``sum_i Inf_i`` (average sensitivity), differentiable."""
return influences_diff(values).sum()Other public import paths
omnibias.boolean.torch.ops.influence_penaltyomnibias.boolean.torch.ops.design.influence_penalty
target_spectrum_loss
omnibias.boolean.torch.ops.target_spectrum_lossSource ↗target_spectrum_loss(values: Sequence[float] | Tensor, target: Sequence[float] | Tensor, basis: str='walsh') -> TensorMean-squared error between the function's spectrum and a target spectrum.
basis is "walsh" (hat f(S)) or "mobius" (m_S); target is
indexed by subset mask, matching the spectrum-engine outputs.
Parameters
| Name | Type & default | Contract |
|---|---|---|
valuespositional-or-keyword | Sequence[float] | TensorRequired | See the operation contract and implementation below. |
targetpositional-or-keyword | Sequence[float] | TensorRequired | See the operation contract and implementation below. |
basispositional-or-keyword | strDefault: 'walsh' | See the operation contract and implementation below. |
TensorExecuted regression · target spectrum loss decreases
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_target_spectrum_loss_decreases() -> None:
# Drive a free 2-bit function toward the Walsh spectrum of XOR.
target = spectrum.walsh_coeffs(
torch.tensor(pm1_values((0, 1, 1, 0)), dtype=torch.float64)
).detach()
vals = torch.zeros(4, dtype=torch.float64, requires_grad=True)
opt = torch.optim.Adam([vals], lr=0.2)
first = None
last = None
for _ in range(200):
opt.zero_grad()
loss = design.target_spectrum_loss(vals, target, basis="walsh")
loss.backward()
opt.step()
last = float(loss.detach())
if first is None:
first = last
assert last is not None and first is not None
assert last < first
assert last < 1e-3python -m pytest "packages/omnibias-boolean/tests/test_design.py::test_target_spectrum_loss_decreases" -qRead the implementation
def target_spectrum_loss(
values: Sequence[float] | Tensor,
target: Sequence[float] | Tensor,
basis: str = "walsh",
) -> Tensor:
"""Mean-squared error between the function's spectrum and a target spectrum.
``basis`` is ``"walsh"`` (``hat f(S)``) or ``"mobius"`` (``m_S``); ``target`` is
indexed by subset mask, matching the spectrum-engine outputs.
"""
if basis == "walsh":
coeffs = walsh_coeffs(values)
elif basis == "mobius":
from omnibias.boolean.torch.ops.spectrum import mobius_coeffs
coeffs = mobius_coeffs(values)
else:
raise ValueError(f"basis must be 'walsh' or 'mobius', got {basis!r}")
target_t = torch.as_tensor(target, dtype=coeffs.dtype, device=coeffs.device)
return ((coeffs - target_t) ** 2).mean()Other public import paths
omnibias.boolean.torch.ops.target_spectrum_lossomnibias.boolean.torch.ops.design.target_spectrum_loss