Tensor Network Rewriting Strategies for Satisfiability and Counting

by   Niel de Beaudrap, et al.
University of Oxford

We provide a graphical treatment of SAT and #SAT on equal footing. Instances of #SAT can be represented as tensor networks in a standard way. These tensor networks are interpreted by diagrams of the ZH-calculus: a system to reason about tensors over ℂ in terms of diagrams built from simple generators, in which computation may be carried out by transformations of diagrams alone. In general, nodes of ZH diagrams take parameters over ℂ which determine the tensor coefficients; for the standard representation of #SAT instances, the coefficients take the value 0 or 1. Then, by choosing the coefficients of a diagram to range over 𝔹, we represent the corresponding instance of SAT. Thus, by interpreting a diagram either over the boolean semiring or the complex numbers, we instantiate either the decision or counting version of the problem. We find that for classes known to be in P, such as 2SAT and #XORSAT, the existence of appropriate rewrite rules allows for efficient simplification of the diagram, producing the solution in polynomial time. In contrast, for classes known to be NP-complete, such as 3SAT, or #P-complete, such as #2SAT, the corresponding rewrite rules introduce hyperedges to the diagrams, in numbers which are not easily bounded above by a polynomial. This diagrammatic approach unifies the diagnosis of the complexity of CSPs and #CSPs and shows promise in aiding tensor network contraction-based algorithms.



There are no comments yet.


page 1

page 2

page 3

page 4


Classifying Complexity with the ZX-Calculus: Jones Polynomials and Potts Partition Functions

The ZX-calculus is a graphical language which allows for reasoning about...

Fast counting with tensor networks

We introduce tensor network contraction algorithms for the counting of s...

PBS-calculus: A Graphical Language for Quantum-Controlled Computations

We introduce the PBS-calculus to represent and reason on quantum computa...

Revisiting Decision Diagrams for SAT

Symbolic variants of clause distribution using decision diagrams to elim...

The Impact of Heterogeneity and Geometry on the Proof Complexity of Random Satisfiability

Satisfiability is considered the canonical NP-complete problem and is us...

Coend Calculus and Open Diagrams

Morphisms in a monoidal category are usually interpreted as processes or...

Improved Job sequencing Bounds from Decision Diagrams

We introduce a general method for relaxing decision diagrams that allows...
This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

1 Introduction

The Boolean satisfiability problem, or SAT, and its variants, is central to theoretical computer science and complexity theory, with many practical real-world applications. The formula defining the problem can always be given in conjunctive normal form (CNF) comprising of an AND of OR-constraints. In a boolean formula on variables , a literal is either some variable or its negation , for some .

Problem 1.1.

CNFSAT (or simply SAT)

[labelwidth=3.5em, labelindent=1em, itemsep=0.25ex, font=]


, where .



The formula consists of a conjunction of clauses for , each being a disjunction of a number of literals. SAT is the decision problem of determining whether there exists an assignment of the variables satisfying all clauses. Given a formula in CNF form, we write for the instance of SAT to evaluate , and for its answer.

Famously, SAT is NP-complete. At the heart of this work is the family of special cases SAT, where for every clause . For we have that SAT P. However, SAT NP-complete, and so are all cases for  [10.5555/2086753, cook, levin]. An important special case of SAT (in the more general formulation of determining whether a boolean formula of some form is satisfiable) is XORSAT, where the OR clauses are replaced with XORs, i.e. replacing in Def. 1.1. This special case is significantly easier, in that XORSAT P.

For each decision problem, , the corresponding counting problem asks for the number of satisfying assignments to a Boolean formula. In particular, #SAT is given as follows:

Problem 1.2.

#CNFSAT (or simply #SAT)

[labelwidth=3.5em, labelindent=1em, itemsep=0.25ex, font=]


, where .



In general, #SAT #P-complete [valiant1979complexity]. The counting versions of the special cases defined above are denoted similarly #XORSAT, #SAT. Given a formula in CNF form, we write for the instance of #SAT described by , and for its solution. Note that #XORSAT P and that #SAT #P-complete for . In this sense, #SAT is significantly harder than its decision version.

This work is motivated by the will to understand the structural origin of the easiness or hardness of these problems. What is it that makes counting the solutions to XORSAT, and consequently deciding if there is at least one, easy? Why is SAT easy to decide but hard to count? What changes when we turn to SAT and both deciding and counting are hard? In the following, we investigate these questions by employing graphical methods.

2 Counting with ZH rewrites

Instances of #SAT may be represented as tensor networks [10.21468/SciPostPhys.7.5.060, garcia-saez2011] and the solution is returned by full tensor contraction, a #P-hard problem in general [Damm2002]. Here, we describe problem instances in terms of a specific variety of tensor network, i.e. diagrams of the ZH-calculus [backens2018zh, wetering2019completeness] — a formal system to analyse complex-valued tensor networks of bond-dimension (all indices ranging over ) by transformations of diagrams. This provides a means of considering the complexity of these problems by tensor-based techniques, while nevertheless avoiding explicit tensor contractions.

2.1 Tensor networks from the ZH calculus

The ZH calculus is a diagrammatic notation for tensor networks, which allows computations to be done with these diagrams in lieu of matrix computations. These diagrams consist of graphs with different kinds of nodes: Z-spiders, which are represented as white dots with a specific number of wires, and H-boxes, which are represented as white boxes labelled with a complex number . These generators are interpreted as follows, where denotes the map from diagrams to matrices:

The -spider is a high-dimensional Kronecker delta-function whose matrix elements are all zeros except for the two entries indexed by all zeros or all ones which are . An -box represents an all-ones matrix with the exception of the entry indexed by all ones which is set to . If not denoted, by convention . Straight and curved wires have the interpretations:

Diagram juxtaposition corresponds to tensor product and composition to matrix product:

Expressions in the ZH calculus also use two derived generators, called X-spiders, represented by a gray dot with a number of wires, and NOT.


With these definitions,  

  acts on computational basis states as XOR, i.e. one wire caries the parity of the other wires, and [style=gray phase dot]¬ acts as NOT (or Pauli ). We will write a X-spider labelled by a ‘

’ to represent an X-spider with a NOT applied to any of its legs. Concretely, the correspond to the even- and odd-parity tensors:

These nodes suffice to express any tensor of any rank with bond-dimension over

, as we now show. For a vector

, we define the short-hand notation

Then, given such a tensor , with indices, we may construct the ZH diagram representing it, called the ZH normal form for , using one box for each of its coefficients :


For any coefficient , the corresponding coefficient gadget may be omitted — we call a ZH diagram with these omitted coefficient gadgets a condensed normal form. These normal forms will in many cases not be the most efficient way to represent a tensor by ZH diagrams; for our purposes it suffices that they exist as a means to represent any particular diagram. Thus, the ZH calculus forms a way to represent tensors with diagrams assembled from simple generators.

2.2 Representing #SAT instances as diagrams

Instances may be represented by ZH tensor networks in a straightforward way.

Theorem 2.1.

Any instance can be represented as a closed ZH diagram, i.e. with no open wires, which evaluates to , composed of the following nodes:


We construct a bijection between CNF formulae and ZH diagrams, keeping consistent with the notation in Defs. 1.1, 1.2. The construction is of the form of a circuit (preparation, process, measurement) with postselection.

Variables are initialised as “states” to all their possible assignments. Let be the number of clauses in which or its negation participates. Each is represented by a -ary Z-spider so that it is copied enough times to participate the clauses, after possibly going through a negation, as dictated by . The states then enter the -gates, which play the role of the “processes”. Let be the number of variables that are involved in clause . Finally, an -ary -gate accepts accepting as inputs all outputs of the -gates. Post-selecting this -gate on , implements an “effect” and represents the satisfiability requirement, which is equivalent to postselecting all outputs of the -gates to , by the property of the -gate. A postselected-on- -gate is an -constraint and is a tensor with entries the OR truth table. This tensor is represented by an all-negated -box. Diagrammatically we have:

Thus, any in CNF is expressed as a bipartite tensor network by connecting -spiders with all-negated -boxes. We can capture the fact that a variable appears negated in a given clause by removing a negation from that wire, since two negations cancel out.

In this way, we can exactly capture the instance as a closed ZH-diagram, with one Z-spider for each variable, one box for each clause, and wires (possibly with NOTs) connecting them. ∎

Note that the -box can be viewed as an -constraint, in the sense that its tensor elements are given by . Also, the postselected-circuit construction above returns the tensor network representations of CNFSAT of [10.21468/SciPostPhys.7.5.060].

Example 2.2.

Consider the following CNF formula:


Its corresponding tensor network is:

2.3 Evaluating tensor networks with the ZH calculus

The calculus is not just a notation for tensor networks, but a sound system of diagrammatic transformations that preserve semantics, i.e. the tensor represented by the network.

The most basic diagram transformations, or “rewrites”, allowed in the ZH calculus are ways that sub-networks of ZH generators may be transformed while preserving semantics. For , the rewrite rules are not only sound, but also complete — meaning that any two networks using generators which are equivalent, may be proven to be equivalent using only diagram transformations. In particular, any closed tensor network (e.g. the ZH-diagram representing ) expressed as a ZH diagram corresponds to a scalar (e.g. ). By completeness, it is always possible to transform a closed ZH-diagram into an efficient representation of that scalar, such as a disconnected collection of arity-0 generators. We call full simplification of a closed diagram a sequence of rewrites that removes all wires from the diagram.

Thus, as an alternative to tensor network contraction in the conventional way, we may in principle evaluate using transformations of ZH diagrams. Taking this approach, the complexity of the computation is governed by the size and complexity of the network as it is transformed as well as the number of transformation steps taken. This is in contrast to full tensor contraction, in which the network becomes simpler (in terms of edge count) throughout the evaluation. In this case, the complexity is in terms of the cost of storing the entries of the nodes which increases exponentially with their rank, which in turn is upper bounded by the number of incident wires. One may elaborate on this by performing SVDs on the tensor nodes to limit the bond-dimension as much as possible [levin2007a, gray2020, verstraete2004]. Naturally, the different approach of the ZH calculus does not ensure that contraction can be performed efficiently — the number of rewrites required to perform a full simplification may be exponential in the number of nodes of the input tensor, and the diagrams constructed in doing so may also in principle become exponentially complex — but because the nodes range over a simple set, it is possible in some cases to use relationships between the generators and their types to identify special cases which may be evaluated efficiently.

In either case — for full tensor contraction, or full simplification by ZH rewrites — one generally expects to avoid mounting complexity in special cases, either to avoid mounting complexity of the tensor nodes (for tensor contraction) or mounting complexity of networks or difficulty in determining how to transform the networks (for ZH diagrams). In the following sections, we consider the complexity of ZH simplification strategies for , , as well as their corresponding decision problems.

3 Satisfiability with ZH rewrites

One may ask whether or not the decision problem may similarly be represented by ZH diagrams. The decision problem corresponds to determining whether or not the answer to a counting problem is non-zero.

3.1 Matrices over semirings

The complex numbers , natural numbers and the booleans are all commutative semirings: sets equipped with two commutative and associative binary operations , with identity element , and , with identity element . Operation distributes over and is absorbing for so that . In , the operations and are just the usual addition and multiplication. For , we take and , treating disjunction as addition and conjunction as multiplication. We may also write as a product , when .

A homomorphism of semi-rings is a function which preserves and . The most important semi-ring homomorphisms for our purposes are the inclusion and the projection . In both cases, these maps are uniquely defined, due to the property of being homomorphisms. Notably, we can see as a quotient of , where we additionally impose the (seemingly nonsensical) equation . In this case, is the quotient map.

For any semi-ring, we can define a monoidal category of matrices of , whose objects are natural numbers, morphisms are matrices, composition is given by matrix multiplication, and by tensor product. That is, for and , is a matrix whose elements are . In particular, we can regard a morphism as a -tensor.

We can lift a semiring homomorphism to a functor simply by applying to each of the elements of a matrix. In this way, we can obtain a faithful functor from and a full functor from .

3.2 From counting to deciding by change of semiring

Counting and deciding may be interpreted as asking essentially the same question of “counting”, but relative to different semirings: either in for the counting problem, or in for the decision problem. Therefore, a closed diagram representing a in CNF evaluates to by interpreting the diagram as a tensor network over and to when the diagram is viewed as a tensor with coefficients in . However, graph-partitioning based optimisers for the tensor contraction path are expected to exhibit similar performance regardless of the choice of semiring over which the tensor network’s coefficients take values [10.21468/SciPostPhys.7.5.060]. This is the case even for easy problem families such as or . We present an approach to reasoning about boolean tensor networks using the ZH calculus which allows us to recover the efficient solvability of said easy problems.

3.3 Boolean tensor rewrites from the ZH calculus

Consider approaching the idea of rewriting boolean tensor diagrams, from the direction of taking an existing diagrammatic calculus, which is well suited to describe matrices over , such as the calculus, and “projecting” it down to the boolean semiring . Subtly, this cannot be simply done by changing the semiring from which the node parameters are chosen. This is because there is no corresponding element to in the booleans ( has no additive inverse) whereas -boxes play a prominent role in the rewrites. Put another way, there is no semiring homomorphism (or even ). Despite this, we can still reason soundly about boolean tensor networks with the calculus. However, while is defined for generators taking parameters in , among these generators are ones taking parameters in . Diagrams which are composed exclusively of such generators will represent tensors over . Furthermore, some gadgets of the ZH calculus — in particular, the -spider and -dot of Eqn. (1) — represent matrices over .

Any tensor network involving the -valued generators together with these gadgets will also represent tensors over . We may then consider ways to reason about boolean tensors through the use of these diagrams. Note that -valued ZH generators, together with NOT-dots, suffice to represent the ZH normal forms (and condensed normal forms) of any -valued tensor with indices of dimension , as described in Eqn. (2). This motivates the following definition:

Definition 3.1.

A NatZH diagram is a diagram generated from

Some of the basic rewrites may be interpreted as rewrites of diagrams, but not all. Notably, any rule involving an -box cannot be realised on diagrams, and as the definitions of gray nodes in Eqn. (1) involve these, rewrites involving them also pose problems. However, just as it is possible to reason about real polynomials by making use of the complex numbers, any theorem of which describes the equivalence of diagrams is still usable to reason about diagrams, regardless of whether the intermediate steps preserve the set of diagrams. This is essentially re-stating the fact that embeds faithfully in the larger category .

Having defined a sub-theory of which maps onto , we can approach the subject of boolean tensors via the functor which projects -matrices down to -matrices. This projection will preserve any equation we proved between -matrices, e.g. those proven with the ZH-calculus. But also, more rules become true. Notably, since in booleans , -boxes are the same as -boxes, which will have a dramatic effect in Section 4.3.

4 Identifying Efficiently Simplifiable Instances

In this section we study the above introduced paradigmatic counting and decision problem families whose complexity is well studied in the literature. We will observe that application of rewrites that result in variable elimination, or killing -spiders, is an efficient diagrammatic technique for solving the problem. In contrast, (potentially) hard instances are those whose analogous rewrites either block further simplification or exponentially grow the diagram. We will illustrate precisely what we mean by this using the examples in this section.


We begin with the easy case of parity constraints. Since P, then it trivially follows that P. Interestingly however, this problem shows rich behaviour. When one tries to anneal to a solution, one encounters a glassy landscape which makes this approach inefficient [RicciTersenghi1639, patil2019obstacles].

As in Proof. 2.2, an -constraint is a postselected-on- -gate. Since the -gate is the -spider, a -constraint is a -spider. A gray dot can absorb a number of -dots and by double negation elimination it is an -spider if it absorbs an even number of negations and a -spider if odd. Also, a -dot gets copied by a white dot:

Therefore, any #XORSAT instance diagram is represented in terms of:

Consider Example 2.2, but with the -constraints replaced by -constraints. Then one gets an example of a formula and its representative tensor network:

The -spider, and by consequence the -spider satisfy the bialgebra law [wetering2019completeness]. Also, a looping wire evaluates to if the wire is negated and to if the wire is naked. In the boolean case a naked looping wire can be trivially removed since :

The graph defining the problem is bipartite; every wire connects a white dot with a gray dot (either -spider or -spider). After one application of the bialgebra rewrite rule, we can always fuse dots of the same colour:

Iteratively using the bialgebra rewrite, we can eliminate variables by introducing only polynomially many edges. In fact, the number of wires introduced during this dance between gray and white spiders is quadratically upper-bounded by the number of nodes in the network. In the case when the solution is not zero, full simplification returns , resulting from the number of disconnected components of the network after the iterated application of the bialgebra rule. For the decision problem, exactly the same procedure returns

4.2 #2sat

Now that we’ve seen that can be efficiently solved by eliminating variables, we turn to the seemingly simple but actually hard problem and attempt a similar strategy. An instance is represented by a diagram generated by variable tensors, binary -boxes, and negations. The latter two generators compose the binary -constraint:


We locally arrange the wires around a variable so to separate those that go through a negation from the naked wires. To eliminate a variable, we use the following theorem.

Theorem 4.1.

The following rule holds in the ZH-calculus, for all :


By completeness of the ZH-calculus, it suffices to show the matrices of the LHS and RHS are equal. In each case, the matrix has a single 2 in the top-left corner, 1’s in the rest of the first row and column, and 0’s elsewhere.

We can evaluate matrix elements by pre- and post-composing with computational basis states and their adjoints and simplifying using the rules of the ZH-calculus. In particular, we use the following rules:


and split into 3 cases: (i) If all the inputs are plugged with basis-0 and outputs with basis-0-adj, the LHS simplifies to a single Z-spider with no inputs and outputs and the RHS to a single H-box labelled by . In both cases, this equals the scalar . (ii) If at least 1 input is basis-1 and 1 output is basis-1-adj, the LHS simplifies to a diagram containing at least 1 copy of and RHS contains an box with no inputs and outputs. Hence both sides go to . (iii) Otherwise the LHS and the RHS both simplify to many copies of or , which goes to . ∎

In addition to the matrix derivation given in the proof above, equation (5) has a logical interpretation as well, in light of the representation of CNFs as ZH-diagrams from Theorem 2.1. Propositional resolution, i.e. reducing a pair of clauses of the form and to a clause , is a form of modus ponens which is particularly well-suited to CNFs. If we just focus on the -boxes in equation (5), we see that this transformation replaces all of the clauses containing a single variable with all of the possible resolutions not containing :


Indeed the conclusion of this implication is satisfiable if and only if its premise is.

However, a lingering question is: what is going on with the extra on the RHS of (5)? This captures precisely the fact that, for every satisfying assignment to the conclusion of (7) where all the are true, the premise has satisfying assignments, one where and one where .

The extra -box in the RHS of (5) prevents us from begin able to iteratively apply this rule to a CNF instance to obtain a solution in polynomial time. Indeed we should expect to find some obstruction to any polynomial-time strategy, since #P-complete. However, we will now see what happens when we switch to a category where .

4.3 2sat

We now look at the decision version of the above problem which is in P. The ZH-diagram of an instance is generated just as in Theorem 2.1, but it is evaluated as a tensor network over the Booleans, rather than the natural numbers. In particular, the parameter on the RHS of equation (5) becomes in the case of Booleans. So, the extra -box on RHS vanishes: 2sat-simp

Hence, for the booleans, we get a new, simpler rule:


This rule no longer introduces H-boxes that would obstruct further application of the rule to its neighbours. Hence, any 2SAT instance can be fully simplified as follows. Every application of (8) eliminates one variable, and after every variable elimination we perform white-dot fusions, as in the case of , since again the network is bipartite, every wire, either naked or negated, connects a -box with a white-dot.

We observe that all variables can be eliminated by iterative application of the above boolean-spider killing rule (8), followed by a small amount of ‘tidying up’. That is, we can remove ‘duplicate’ boxes, which correspond to repeated clauses, and ‘self-loops’, which correspond to a variable occurring twice in a clause, using the following rules:

In the case of the first 2 kinds of loops, we end up with some basis states, so we are not a CNF-like ZH-diagram. However, applying the rules (6) from the proof of Theorem 4.1 will either get back to CNF or produce a (i.e. ‘unsatisfiable’).

With every white dot removed, we introduce a number of wires going through -boxes which is quadratic in the arity of that white dot. Since we can remove repeated -boxes at every step, the arity of a given white dot is at most linear in the number of variables remaining. We thus recover the polynomial cost of full simplification of SAT instances, and we witness this purely in terms of diagram rewriting.

4.4 #SAT and SAT

For the generic case, which is #P-complete, and the corresponding case, which is NP-complete, the variable elimination rewrites introduced above for and have analogues that introduce boxes connected to more than just pairs of white dots. In particular, for #SAT we have that killing a white spider results in:

This rule is straightforward to prove from the #2SAT version (8) and the basic ZH-calculus rules.

Again, passing to SAT, we can use to obtain a simpler rule:


which we can indeed apply iteratively to solve a SAT instance. However, comparing the 2SAT simplification rule to the SAT version, we notice one crucial difference. In equation (8), the arity of the -boxes remains unchanged (at 2). Hence, we can argue that are at most polynomially many of them at each step. However, if the CNF contains clauses of 3 or more variables, iterating (9) will cause the arities of -boxes to grow arbitrarily large. Hence, in the worst case, we will obtain exponentially many distinct -boxes, and the efficiency of our procedure is destroyed.

5 Conclusions and Future Work

We have imported rewrite rules from the sound and complete graphical calculus in order to reason about tensor networks that represent instances of and . In this way, we have identified that tensors of instances of problem families known to be in P, such as and , can be efficiently rewritten to a scalar encoding the solution to the problem.

In particular, in order to be able to reason about boolean tensor networks encoding instances of , we observed that tensors encoding instances of take values over the natural numbers. The calculus is complete for since it is complete for , and so for counting problems we simple used the available rewrites of . In order to switch from counting to decision, we interpreted decision as counting but over the boolean semiring. Interestingly, projecting from to gave rise to rewrite rules which made apparent the difference in complexity between and .

For further work, we aim to complete ongoing work with preliminary results on defining a sound and complete graphical calculus for boolean tensor networks. Such a calculus is motivated by the boolean projection we have used here but is an independent calculus from . Notably, there exists highly relevant work on the calculus [colecomfort], which provides a sound and complete framework for both natural number tensors as well as for boolean tensors. It is inspired by the calculus [Coecke2011] and adds the and gates as primitive generators.

Finally, we note that the Python library PyZX [kissinger2019pyzx], developed to support automated rewriting for ZX-diagrams supports the ZH-calculus as well. Building on this library, we aim to develop a useful tool both for classical and quantum many-body problems. It would be interesting to benchmark such a library against existing techniques for counting and decision problems, as well as aid existing tensor contraction methods by equipping them with rewrite strategies and create hybrid algorithms for counting problems.

6 Acknowledgments

N.dB. is supported by a Fellowship funded by a gift from Tencent Holdings (tencent.com). A.K. would like to acknowledge support from AFOSR grant FA2386-18-1-4028. K.M. is supported by an 1851 Research Fellowship (royalcommission1851.org) and by Cambridge Quantum Computing Ltd.. K.M. acknowledges Stefanos Kourtis for inspiring discussions.

Appendix A ZH rewrite rules

Rewrite rules for the complete ZH-calculus in Fig. 1.

(ZS1) Z-spider-rule (HS1) H-spider-rule
(ZS2) Z-special (HS2) H-identity
(BA1) ZX-bialgebra (BA2) ZH-bialgebra
(M) multiply-rule (U) unit-rule
(A) average-rule (I) intro-rule
(O) ortho-rule
Figure 1: The rules of the ZH-calculus. are nonnegative integers and are arbitrary complex numbers. The right-hand sides of both bialgebra rules (BA1) and (BA2) are complete bipartite graphs on vertices, with an additional input or output for each vertex.