1 Introduction
The Boolean satisfiability problem, or SAT, and its variants, is central to theoretical computer science and complexity theory, with many practical realworld applications. The formula defining the problem can always be given in conjunctive normal form (CNF) comprising of an AND of ORconstraints. 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=]
 Input:

, where .
 Output:

.
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 NPcomplete. 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 NPcomplete, 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=]
 Input:

, where .
 Output:

.
In general, #SAT #Pcomplete [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 #Pcomplete 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, garciasaez2011] and the solution is returned by full tensor contraction, a #Phard problem in general [Damm2002]. Here, we describe problem instances in terms of a specific variety of tensor network, i.e. diagrams of the ZHcalculus [backens2018zh, wetering2019completeness] — a formal system to analyse complexvalued tensor networks of bonddimension (all indices ranging over ) by transformations of diagrams. This provides a means of considering the complexity of these problems by tensorbased 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: Zspiders, which are represented as white dots with a specific number of wires, and Hboxes, 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 highdimensional Kronecker deltafunction whose matrix elements are all zeros except for the two entries indexed by all zeros or all ones which are . An box represents an allones 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 Xspiders, represented by a gray dot with a number of wires, and NOT.
(1) 
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 Xspider labelled by a ‘
’ to represent an Xspider with a NOT applied to any of its legs. Concretely, the correspond to the even and oddparity tensors:
These nodes suffice to express any tensor of any rank with bonddimension over
, as we now show. For a vector
, we define the shorthand notationThen, 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 :
(2) 
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:
Proof.
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 Zspider 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. Postselecting 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 postselectedon gate is an constraint and is a tensor with entries the OR truth table. This tensor is represented by an allnegated box. Diagrammatically we have:
Thus, any in CNF is expressed as a bipartite tensor network by connecting spiders with allnegated 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 ZHdiagram, with one Zspider 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 postselectedcircuit construction above returns the tensor network representations of CNFSAT of [10.21468/SciPostPhys.7.5.060].
Example 2.2.
Consider the following CNF formula:
(3) 
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 subnetworks 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 ZHdiagram representing ) expressed as a ZH diagram corresponds to a scalar (e.g. ). By completeness, it is always possible to transform a closed ZHdiagram into an efficient representation of that scalar, such as a disconnected collection of arity0 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 bonddimension 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 nonzero.
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 semirings is a function which preserves and . The most important semiring 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 semiring, 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, graphpartitioning 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 NOTdots, 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 restating the fact that embeds faithfully in the larger category .
Having defined a subtheory 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 ZHcalculus. 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.
4.1 #XORSAT and XORSAT
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 postselectedon 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 upperbounded 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:
(4) 
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 ZHcalculus, for all :
(5) 
Proof.
By completeness of the ZHcalculus, it suffices to show the matrices of the LHS and RHS are equal. In each case, the matrix has a single 2 in the topleft corner, 1’s in the rest of the first row and column, and 0’s elsewhere.
We can evaluate matrix elements by pre and postcomposing with computational basis states and their adjoints and simplifying using the rules of the ZHcalculus. In particular, we use the following rules:
(6) 
and split into 3 cases: (i) If all the inputs are plugged with basis0 and outputs with basis0adj, the LHS simplifies to a single Zspider with no inputs and outputs and the RHS to a single Hbox labelled by . In both cases, this equals the scalar . (ii) If at least 1 input is basis1 and 1 output is basis1adj, 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 ZHdiagrams 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 wellsuited 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 :
(7) 
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 polynomialtime strategy, since #Pcomplete. 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 ZHdiagram 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: 2satsimp
Hence, for the booleans, we get a new, simpler rule:
(8) 
This rule no longer introduces Hboxes 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 whitedot fusions, as in the case of , since again the network is bipartite, every wire, either naked or negated, connects a box with a whitedot.
We observe that all variables can be eliminated by iterative application of the above booleanspider killing rule (8), followed by a small amount of ‘tidying up’. That is, we can remove ‘duplicate’ boxes, which correspond to repeated clauses, and ‘selfloops’, 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 CNFlike ZHdiagram. 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 #Pcomplete, and the corresponding case, which is NPcomplete, 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 ZHcalculus rules.
Again, passing to SAT, we can use to obtain a simpler rule:
(9) 
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 ZXdiagrams supports the ZHcalculus as well. Building on this library, we aim to develop a useful tool both for classical and quantum manybody 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 FA23861814028. 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 ZHcalculus in Fig. 1.
(ZS1)  Zspiderrule  (HS1)  Hspiderrule 
(ZS2)  Zspecial  (HS2)  Hidentity 
(BA1)  ZXbialgebra  (BA2)  ZHbialgebra 
(M)  multiplyrule  (U)  unitrule 
(A)  averagerule  (I)  introrule 
(O)  orthorule 
Comments
There are no comments yet.