boofun.core.query_model

Query Model for Boolean Functions.

This module formalizes the distinction between: 1. EXPLICIT functions - we have the full truth table in memory 2. QUERY-ACCESS functions - we can only evaluate f(x) on demand

This is CRITICAL for production safety. A user should be able to:

f = bf.create(massive_neural_network, n=1000) f.is_linear(num_tests=100) # SAFE: only 300 queries

Without the library trying to compute 2^1000 entries.

Design Philosophy:

“Never assume we can enumerate the domain”

The library should work correctly even if the function represents: - A billion-parameter neural network - A database lookup - An external API call - A physical measurement device

For such functions, only QUERY-BASED algorithms are valid.

Functions

check_query_safety(f, operation[, ...])

Check if an operation is safe to perform on this function.

get_access_type(f)

Determine how the function's values can be accessed.

safe_alternatives(operation)

Suggest query-safe alternative for an unsafe operation.

Classes

AccessType(value)

How can we access the function's values?

QueryModel(f[, max_queries])

Manages query complexity and safety for Boolean function operations.

Exceptions

ExplicitEnumerationError

Raised when trying to enumerate a query-access function.

QuerySafetyWarning

Warning when an operation may be unsafe for query-access functions.

class boofun.core.query_model.QueryModel(f: BooleanFunction, max_queries: int = 10000000)[source]

Manages query complexity and safety for Boolean function operations.

This class helps users understand and control the computational cost of operations on their functions.

Example

>>> f = bf.create(my_function, n=30)
>>> qm = QueryModel(f)
>>> qm.can_compute("fourier")
False
>>> qm.can_compute("is_linear", num_queries=100)
True
>>> qm.estimate_cost("influences")
{'queries': 32212254720, 'feasible': False}
__init__(f: BooleanFunction, max_queries: int = 10000000)[source]

Initialize query model for a function.

Parameters:
  • f – BooleanFunction to analyze

  • max_queries – Maximum acceptable query count

can_compute(operation: str, **kwargs) bool[source]

Check if operation is computationally feasible.

estimate_cost(operation: str, num_queries: int = 100) Dict[str, Any][source]

Estimate computational cost of an operation.

Returns:

  • queries: Estimated number of function evaluations

  • feasible: Whether this is computationally reasonable

  • time_estimate: Rough time estimate (assuming 1µs per query)

  • description: Human-readable description

Return type:

Dict with keys

summary() Dict[str, Dict[str, Any]][source]

Get cost summary for all operations.

print_summary()[source]

Print human-readable cost summary.

class boofun.core.query_model.AccessType(value)[source]

How can we access the function’s values?

EXPLICIT = 1
QUERY = 2
STREAMING = 3
SYMBOLIC = 4
boofun.core.query_model.get_access_type(f: BooleanFunction) AccessType[source]

Determine how the function’s values can be accessed.

Parameters:

f – BooleanFunction to check

Returns:

AccessType indicating safest access pattern

boofun.core.query_model.check_query_safety(f: BooleanFunction, operation: str, max_safe_n: int = 20, num_queries: int = 100, strict: bool = False) bool[source]

Check if an operation is safe to perform on this function.

Parameters:
  • f – BooleanFunction to check

  • operation – Name of operation (e.g., “fourier”, “is_linear”)

  • max_safe_n – Maximum n for which we allow unsafe operations

  • num_queries – Number of queries for safe operations

  • strict – If True, raise error instead of warning

Returns:

True if operation is safe to proceed

Raises:

ExplicitEnumerationError – If strict=True and operation is unsafe

Example

>>> f = bf.create(huge_function, n=100)
>>> check_query_safety(f, "fourier")  # Returns False, warns
>>> check_query_safety(f, "is_linear")  # Returns True
exception boofun.core.query_model.QuerySafetyWarning[source]

Warning when an operation may be unsafe for query-access functions.

exception boofun.core.query_model.ExplicitEnumerationError[source]

Raised when trying to enumerate a query-access function.

This protects users from accidentally trying to compute 2^n evaluations on a huge function.