{'fraction_non_alphanumeric': 0.033709037779169275, 'fraction_numerical': 0.00469630557294928, 'mean_word_length': 4.650058962264151, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Belief propagation in monoidal categories

We discuss a categorical version of the celebrated belief propagation algorithm. This provides a way to prove that some algorithms which are known or suspected to be analogous, are actually identical when formulated generically. It also highlights the computational point of view in monoidal categories.


Introduction
We discuss a categorical version of the celebrated belief propagation algorithm [26]. This provides a way to prove that some algorithms which are known or suspected to be analogous, are actually identical when formulated generically. It also highlights the computational point of view in monoidal categories. The approach could also make possible software implementations that use a categorical formulation of algorithms to enable generic programming.
The setting for this algorithm, and for many computational questions concerning monoidal categories, is a diagram (Def. 2.2). By a diagram we mean an equivalence class of monoidal words over a finite tensor scheme, usually with certain additional properties. An interpretation [27] assigns values to each of the variables in the tensor scheme in a functorial way. Basic questions to ask of such a diagram, once interpreted in a particular category, include 1. compute a (possibly partial) contraction, 2. solve the word problem (are two diagrams equivalent, i.e. do they have the same interpretation) or compute a normal form for a diagram, 3. solve the implementability problem (construct a word equivalent to a target using a library of allowed morphisms), and 4. choose morphisms in a diagram to best approximate a more general diagram (possibly allowing the approximating diagram itself to vary).
Many practical questions are instances of one of these problems including computational challenges in probabilistic graphical models, quantum programming and logic [4,12], the tensor network state approach to quantum condensed matter and quantum chemistry, parts of computational complexity theory including constraint satisfaction and counting constraint satisfaction problems, and many database operations.
For general monoidal categories, these problems are difficult: at least #P-hard (1), undecidable (2), undecidable (3), and NP-hard (4) respectively. The news is similarly bad for approximate versions of each. Nevertheless, given their practical significance, many tractable special cases, approximate algorithms, and heuristics exist to solve these problems in restricted cases.
Perhaps most prominent among such algorithms is the belief propagation algorithm [26], and its many extensions and analogs. From the categorical point of view, these extensions and analogs should just be the same abstract algorithm operating in different categories (e.g. probabilistic graphical models vs. sets and relations). These analogies have been drawn explicitly in many areas. For example, the connection between belief propagation and turbo coding theory was described in [20] and the connection to survey propagation for SAT problems is explored in [18].
We outline a general categorical form of the belief propagation algorithm for Problem 1 and look at how it specializes. We categorified another class of algorithms for Problem 1 in previous work [25]. We also touch briefly on Problems 2 and 3. Approaches to Problem 4 often require a solution to Problem 1 as a component.
By describing algorithms in terms of monoidal categories, the common structure of problems can be understood and computational knowledge can be more readily shared across disciplines. Creating general tools that work for any category with suitable properties, and can be specialized automatically once the monoidal category interface of a domain was specified, would be a significant advance. Given the rapidly expanding universe of applied problems given categorical interpretations, such an abstraction has the potential to be as useful as convex programming or numerical linear algebra. We only take a tiny step in this direction in the present work.

Tensor schemes and word problems
We assume the reader is familiar with monoidal categories [15]. We mainly consider the strict version here. The monoidal language T ⊗,• generates the free monoidal category over T . It consists of all valid morphism words that can be formed from Mor(T ), and identity morphisms. Constructively, T ⊗,• is described as follows.

Given words
Given a word in a monoidal language, we can attach an interpretation by considering it as defining a morphism in a monoidal category. By the universal property, we can safely consider the word in the free monoidal category, which already imposes some equivalences such as id Two words are equivalent if they represent the same morphism in the free monoidal category.

Definition 2.2.
An equivalence class of words in the free monoidal category over a tensor scheme is called a diagram.
Further notions of equivalence arise if we add additional relations. So far we have no normal form for words; for example Because there is a coherent graphical language for the free monoidal category over a tensor scheme, the word problem is not too difficult. Adding adjectives (special types of monoidal categories) and relations, or fixing values, so that the category is no longer free may make it easier or harder. Proposition 2.3. The word problem and implementability problem in a monoidal category over a finite tensor scheme are undecidable.
To prove this proposition one just needs to embed a known undecidable problem in some non-free monoidal category; see [24] for an example of how to do this. Proposition 2.3 puts us at a distinct disadvantage as compared to computational commutative algebra, where the word problem for polynomials is always at least computable with Gröbner bases [3]. We call a monoidal category decidable if its word problem is decidable.
The existence of coherent graphical languages for some types of monoidal categories means that the word problem can be reduced to graph isomorphism [7]. Hence the word problem for the free closed category and free compact closed category over a finite tensor scheme are in LOGSPACE and P [17] respectively. Normal forms for such graphs were explored in [8,2], see also [21]. One could produce normal forms for words in X-categories (for some adjective X such as "traced" or "dagger") with coherent graphical languages indirectly by this method, by transforming a word to normal form graph and then to a word again by some deterministic scheme.
It would be preferable for some purposes to have a confluent terminating rewriting system that attached a direction to the equalities of the X-category.
Term rewriting and computing normal forms in monoidal categories is a field in its infancy; see [13,23] for some of what is known.
A final method for the word problem is to apply a functor to a category which is complete with respect to the category of interest but might have an easier word problem. Finite dimensional vector spaces over a field of characteristic zero are complete for traced symmetric monoidal categories [10] and finite dimensional Hilbert spaces are complete for dagger compact closed categories [28]. Thus we can potentially certify inequality of words once we have bounded dimension, for example obtaining numerical methods for the word problem in categories by assigning random morphisms in the category of finite-dimensional vector spaces and linear transformations.
We assume our category is small, so the set of all morphisms from A to B is a hom set denoted Mor(A, B). We can specialize the word problem to particular hom sets such as Mor(I, I). In this case it is sometimes called a contraction problem (Problem 1). For example, in semiringed categories, the word problem for morphisms in Mor(I, I) generalizes counting constraint satisfaction problems [25].
An important word problem for current purposes is determining equality of I-valued points, i.e. morphisms of type Mor(I, A) for some object A. The reason for this is as follows.
We want to generalize algorithms such as belief propagation that work over the category of vector spaces and linear transformations (or a probabilistic version thereof). In the generalization, we can no longer assume that objects A are sets with points (such as probability distributions in the classical belief propagation algorithm). However, messages are still morphisms of type Mor(I, A) for each object A and we still express the belief propagation equations in terms of equality of morphisms in each Mor(I, A). Deciding if two vectors are equal up to numerical tolerance becomes deciding a word problem in Mor(I, A). These messages must also be stored somehow.
Thus for our algorithm to run efficiently, we need the word problem for I-valued points to be efficiently decidable and their representation to be efficient. Thus, we will generally assume that I-valued points can be stored and compared efficiently. This can be made precise by introducing a size function.
In the classical setting for belief propagation there is a monoid homomorphism, size : Ob(T ) ⊗ → N, from the free monoid generated by the objects of our tensor scheme to the natural numbers. This sends monoidal product to multiplication (or to the addition of log dimensions if we prefer to do something closer to counting wires). For example the dimension of a vector space fits this description. Then for each object A, words in Mor(I, A) require O(size(A)) storage and the word problem for I-valued points of A is linear in the size of A.

Compact closed, spidered, and dungeon categories
A compact closed category is a monoidal category with duals for objects and a compatible symmetric braiding, and string diagrams define the free compact closed category over a tensor scheme [11].

Definition 2.4.
A spidered category is a strict symmetric monoidal category equipped with a special commutative Frobenius structure [5] (A, m, u, δ , ε, σ F ) on each object A.
Note that the morphisms of a spidered category need not be Frobenius, or even monoid or comonoid homomorphisms (in fact requiring this trivializes the structure).
To such a spidered category we now add duals for objects to obtain a compact closed category with additional structure. We call a compact closed category which is a spidered category in a compatible way a dungeon category.

Definition 2.5.
A dungeon category is a compact closed category (C , σ C , i, e) such that (i) Each object has a special commutative Frobenius structure (A, m, u, δ , ε, σ F ) with σ F A ( * ) ,A ( * ) = σ C A ( * ) ,A ( * ) , and (ii) Any two morphisms f , g which are constructed from the identity id A , the symmetric braiding σ A,A , the Frobenius morphisms, and the dualizing cup and cap morphisms i A , e A for A, and have the same domain (tensor product of zero or more or copies of A and A * ) and the same codomain (another such tensor product), and are connected as a graph, are equal.
In other words, a "directed spider" morphism as in (ii) depends only on the number of inward and outward directed arrows, which way they point, and their order, with the second condition dropped up to application of σ A,A .
Dungeon categories are a good setting for generalized belief propagation because we can bend wires and have spiders that play the role of variables in the probabilistic setting for belief propagation.

Sum-product and belief propagation for contraction
The contraction problem in semiringed categories is #P-hard. There are many cases however where the problem becomes tractable. The main examples include trees (or diagrams where sections are merged to yield a tree) and categories for which a categorical generalization of holographic algorithms can be made to hold [25].
Most abstractly, the sum product algorithm [14] is simply the observation that when a diagram is a tree, one can perform contraction according to the tree. If the given diagram is not a tree, we can group nodes into a tree decomposition [9] to force it to be a tree, and then run sum-product. This is known as the junction tree algorithm [16] and has also been extended to the quantum case [19].
For actual computations, we can improve on the abstract sum-product algorithm by using an optimized message-passing version, which among other benefits permits parallelization.

Belief propagation in factor graphs
First we review BP in probabilistic factor graph models with discrete variables (see e.g. [22,Ch. 14]). The algorithm operates on a factor graph, a bipartite graph with one part discrete random variables v ∈ V and one part factors u ∈ U . Each factor assigns a real number to each combination of states of the variables it is connected to. Multiplying factors and normalizing if needed gives a joint probability distribution.
Belief propagation is a message passing algorithm. Each message is a probability distribution over the states one variable v can take, so a vector in the associated vector space V v . Each factor f U at node u is a tensor in ⊗ v∈nbhd(u) V v . Thus a factor defines | nbhd(u)| linear maps f u,v : ⊗ i∈nbhd(u)\v V i → V v , one for each v ∈ nbhd(u). First we describe how to compute messages locally at each node in the factor graph, then how to assemble these into a complete algorithm.
Messages at variables. Suppose we have designated an edge e incident on the variable as output and the rest as input. Compute the pointwise (Hadamard) product of the incoming messages, and output it as the outgoing message along e. Since we are in a probabilistic category, this Hadamard product includes a rescaling so that the message is a probability distribution. If there are no incoming messages, output the uniform message.
Messages at factors. Suppose we have designated one of the edges (u, v) connected to a variable v incident on the factor u as output and the rest as input. Compute the tensor product of the incoming messages, apply f u,v : ⊗ i∈nbhd(u)\v V i → V v , and output the result as the outgoing message along the edge to v.
Resulting algorithm. This defines a system of BP equations describing the fixed points of the update rules. The initial messages can chosen to be uniform distributions. When applied to a factor graph which is a tree, the algorithm converges after iterating a number of times equal to the diameter of the tree. Choosing a root, this can be completed in two "passes," leaves to root then root to leaves, updating messages only as they change. More generally we consider a convergence threshold for the BP equations. It is a theorem that belief propagation is exact on trees, and it can work surprisingly well even when this is not satisfied.

Categorical Belief Propagation
In order for our generalized BP algorithm to operate, the category to which it is applied will need a few basic features; in particular it must be reshapable.
Suppose we have a morphism f : dom( f ) → cod( f ) and decompositions of its domain and codomain into monoidal products of other objects: dom( f ) = A 1 ⊗ · · · ⊗ A n and cod( f ) = A n+1 ⊗ · · · ⊗ A m . Suppose I and J are ordered disjoint subsets of [m] := {1, 2, . . . , m} whose union is [m]. These fix an alternate decomposition of A 1 ⊗ · · · ⊗ A m into two objects (with arbitrary reordering), D = ⊗ i∈I A i , C = ⊗ j∈J A j . We require that there exists a unique morphism r I,J ( f ) : D → C called the reshaping of f from I to J determined from this data. The reshaping does not depend on the route, that is for any I ′ , J ′ we have r I,J r I ′ J ′ = r I,J . If this works for all morphisms, we say the category is reshapable.
We now assume that we are working in a dungeon category (which is therefore reshapable) in which I-valued points can be stored and compared efficiently (e.g. space and time complexity linear in some "size" monoid homomorphism). This will guarantee the efficiency of belief propagation by an argument analogous to the classical case.

Bipartite with one part spiders
Assume we have a dungeon category (a strict compact closed category equipped with a compatible special commutative Frobenius structure on each object). This allows for the special spider morphisms to generalize and axiomatize the role played by variables in the traditional belief propagation algorithm for factor graphs. Consider a bipartite diagram with one part consisting of spiders (generalized variables) and the other arbitrary morphisms (generalized factors). Both spiders and factors are reshaped before composition. The reshaping of the spiders simply leads to a Hadamard (elementwise) product in the case of vector spaces and linear transformations.
The BP algorithm is expressed in terms of messages. When objects are sets, messages are elements of the set. For example, if objects are vector spaces, the messages will be vectors. In general messages for object A are I-valued points (elements of Mor(I, A)). For example any probability distribution can be expressed this way as a stochastic matrix applied to the unit Frobenius morphism (the unit "creates" a variable with a uniform distribution).
Using the compact closed structure to reshape morphisms, we still have that each "factor" morphism at node u defines | nbhd(u)| different morphisms f : ⊗ i∈nbhd(u)\v V i → V v , one for each v ∈ nbhd(u). First we describe how to compute messages locally at each node, then how to assemble these into a complete algorithm.
Messages at spiders. Apply the reshaped spider to incoming messages, and output the result as the outgoing message. If there are no incoming messages, treat the spider as a Frobenius unit.
Messages at "factor" morphisms. Suppose we have designated one of the wires incident on the factor f as output and the rest as input. Compute the monoidal product of the incoming messages, apply the reshaped f , and output the result as the outgoing message.
The system of BP equations are now equalities of I-valued morphisms, describing the fixed points of the update rules. The initial messages can be chosen to be units at the spiders. The nice behavior of the algorithm on diagrams which are trees should be preserved, once suitable definitions are made so that we can talk about convergence.
A spider is just a special kind of morphism. To get the general bipartite version, replace the message procedure at spiders with another copy of the factor message procedure.

Examples
In the category BoolRel each object is a two element set or a monoidal (here Cartesian) product of such. Morphisms are relations. Categorical belief propagation becomes the survey propagation algorithm for solving constraint satisfaction problems.
Augmenting the category FinRel of finite sets and relations with a positive integer multiplying each element of a relation yields the semiringed category NFinRel (discussed in the context of counting constraint satisfaction problems such as computing partition functions in [25]). This category also corresponds approximately to sufficient statistics in the analysis of contingency tables [1].
We can add additional flexibility to FinRel to obtain database categories [29]. Then categorical belief propagation becomes a query planning algorithm.