API Reference

class klay.Circuit(*args, **kwargs)

Circuits are the main class added by KLay, and require no arguments to construct.

circuit = klay.Circuit()

nb_nodes(self) int

Number of nodes in the circuit.

nb_root_nodes(self) int

Number of root nodes in the circuit.

true_node(self) klay.NodePtr

Adds a true node to the circuit, and returns a pointer to this node.

false_node(self) klay.NodePtr

Adds a false node to the circuit, and returns a pointer to this node.

literal_node(self, literal: int) klay.NodePtr

Adds a literal node to the circuit, and returns a pointer to this node.

and_node(self, children: collections.abc.Sequence[klay.NodePtr]) klay.NodePtr

Adds an and node to the circuit, and returns a pointer to this node.

or_node(self, children: collections.abc.Sequence[klay.NodePtr]) klay.NodePtr

Adds an or node to the circuit, and returns a pointer to this node.

set_root(self, root: klay.NodePtr) None
Marks a node pointer as root. The order in which nodes are set as root determines the order of the output tensor.

Note

Only use this when manually constructing a circuit, when loading in a NNF/SDD its root is automatically set as root.

remove_unused_nodes(self) None
Removes unused nodes from the circuit. Root nodes are always considered used.

Warning

Invalidates any NodePtr referring to an unused node (i.e., a node not connected to a root node).

add_sdd(sdd: SddNode, true_lits: Sequence[int] = (), false_lits: Sequence[int] = ()) NodePtr

Add an SDD to the Circuit.

Parameters:
  • sdd – PySDD SDDNode to be added.

  • true_lits – List of literals that are always true and should get propagated away.

  • false_lits – List of literals that are always false and should get propagated away.

add_sdd_from_file(self, filename: str, true_lits: collections.abc.Sequence[int] = [], false_lits: collections.abc.Sequence[int] = []) klay.NodePtr

Add a sentential decision diagram (SDD) from file.

Parameters:
  • filename – Path to the .sdd file on disk.

  • true_lits – List of literals that are always true and should get propagated away.

  • false_lits – List of literals that are always false and should get propagated away.

add_d4_from_file(self, filename: str, true_lits: collections.abc.Sequence[int] = [], false_lits: collections.abc.Sequence[int] = []) klay.NodePtr

Add an NNF circuit in the D4 format from file.

Parameters:
  • filename – Path to the .nnf file on disk.

  • true_lits – List of literals that are always true and should get propagated away.

  • false_lits – List of literals that are always false and should get propagated away.

to_torch_module(semiring: str = 'log', probabilistic: bool = False, eps: float = 0)

Convert the circuit into a PyTorch module.

Parameters:
  • semiring – The semiring in which the circuit should be evaluated. Supported options are "log", "real", "mpe", or "godel".

  • probabilistic – If enabled, construct a probabilistic circuit instead of an arithmetic circuit. This means the inputs to a sum node are multiplied by a probability, and we can interpret sum nodes as latent Categorical variables.

  • eps – Epsilon used by log semiring for numerical stability.

to_jax_function(semiring: str = 'log')

Convert the circuit into a Jax function.

Parameters:

semiring – The semiring in which the circuit should be evaluated. Supported options are "log", "real", "mpe", or "godel".