The Weisfeiler-Leman Dimension of Conjunctive Queries

A graph parameter is a function f on graphs with the property that, for any pair of isomorphic graphs G1 and G2, f(G1)=f(G2). The Weisfeiler--Leman (WL) dimension of f is the minimum k such that, if G1 and G2 are indistinguishable by the k-dimensional WL-algorithm then f(G1)=f(G2). The WL-dimension of f is ∞ if no such k exists. We study the WL-dimension of graph parameters characterised by the number of answers from a fixed conjunctive query to the graph. Given a conjunctive query φ, we quantify the WL-dimension of the function that maps every graph G to the number of answers of φ in G. The works of Dvorak (J. Graph Theory 2010), Dell, Grohe, and Rattan (ICALP 2018), and Neuen (ArXiv 2023) have answered this question for full conjunctive queries, which are conjunctive queries without existentially quantified variables. For such queries φ, the WL-dimension is equal to the treewidth of the Gaifman graph of φ. In this work, we give a characterisation that applies to all conjunctive queries. Given any conjunctive query φ, we prove that its WL-dimension is equal to the semantic extension width sew(φ), a novel width measure that can be thought of as a combination of the treewidth of φ and its quantified star size, an invariant introduced by Durand and Mengel (ICDT 2013) describing how the existentially quantified variables of φ are connected with the free variables. Using the recently established equivalence between the WL-algorithm and higher-order Graph Neural Networks (GNNs) due to Morris et al. (AAAI 2019), we obtain as a consequence that the function counting answers to a conjunctive query φ cannot be computed by GNNs of order smaller than sew(φ). The majority of the paper is concerned with establishing a lower bound of the WL-dimension of a query. Given any conjunctive query φ with semantic extension width k, we consider a graph F of treewidth k obtained from the Gaifman graph of φ by repeatedly cloning the vertices corresponding to existentially quantified variables. Using a modification due to Furer (ICALP 2001) of the Cai-Fürer-Immerman construction (Combinatorica 1992), we then obtain a pair of graphs χ(F) and ^χ(F) that are indistinguishable by the (k-1)-dimensional WL-algorithm since F has treewidth k. Finally, in the technical heart of the paper, we show that φ has a different number of answers in χ(F) and ^χ(F). Thus, φ can distinguish two graphs that cannot be distinguished by the (k-1)-dimensional WL-algorithm, so the WL-dimension of φ is at least k.


Introduction
The Weisfeiler-Leman (WL) algorithm [41] and its higher dimensional generalisations [10] are amongst the most well-studied heuristics for graph isomorphism.This algorithm works as follows.For each positive integer k, the k-dimensional WL-algorithm iteratively maps k-tuples of vertices of a graph to multisets of colours.Two graphs G and G ′ are said to be k-WL-equivalent, denoted G ∼ = k G ′ , if this algorithm returns the same vertex colouring for G and G ′ , up to consistently renaming the colours.For the specific case k = 1 the WL-algorithm is equivalent to the colour-refinement algorithm, which is a widely used and efficiently-implementable heuristic for graph isomorphism (see e.g.[2,25]).
In addition to applications to graph isomorphism, recent works have shown that the expressiveness of Graph Neural Networks (GNNs) and their higher order generalisations is precisely characterised by the WL-algorithm [33,42].This result has sparked a flurry of research with the objective of determining which graph parameters are invariant on graphs that are indistinguishable by the WL-algorithm [32,13,4,3,34,28,8].We refer the reader to the survey by Grohe [24] for further reading.
Over the years, surprising alternative characterisations of k-WL-equivalence have been established.
(II) For each positive integer k, G ∼ = k G ′ if and only if there is no first-order formula with counting quantifiers that uses at most k + 1 variables and that can distinguish G and G ′ [26,10].
(III) For each positive integer k, G ∼ = k G ′ if and only if, for each graph H of treewidth at most k, the number of graph homomorphisms from H to G is equal to the number of graph homomorphisms from H to G ′ [21,16].This is the characterisation of k-WLequivalence that will be used in this work (see Definition 19).
The characterisation in (III) has ignited interest in studying the WL-dimension of counting graph homomorphisms and of counting related patterns [13,3,34,28,8].
A graph parameter f is a function from graphs that is invariant under isomorphisms.The WL-dimension of a graph parameter f is the minimum positive integer k such that f cannot distinguish k-WL-equivalent graphs (see Definition 20).Building upon the works of Dvorák [21], Dell, Grohe and Rattan [16], Roberson [36], and Seppelt [38], it has very recently been shown by Neuen [34] that the WL-dimension of the graph parameter that counts homomorphisms from a fixed graph H is exactly the treewidth of H.It is well-known that counting homomorphisms is equivalent to counting answers to conjunctive queries without existentially quantified variables (see e.g.[35]); such conjunctive queries are also called full conjunctive queries.In this work, we consider all conjunctive queries including those that have existentially quantified variables and we answer the fundamental question: What is the WL-dimension of the graph parameter that counts answers to fixed conjunctive queries?To state our results, we first introduce some central concepts.

Conjunctive Queries and Semantic Extension Width
A conjunctive query ϕ consists of a set of free variables X = {x 1 , . . ., x k } and a set of (existentially) quantified variables Y = {y 1 , . . ., y ℓ }, and is of the form ϕ(x 1 , . . ., x k ) = ∃y 1 , . . ., y ℓ : such that each A i is an atom R( z) where R is a relation symbol and z is a vector of variables in X ∪ Y .Since we focus in this work on undirected graphs without self-loops, in our setting there is only one binary relation symbol E, so all atoms are of the form E(z 1 , z 2 ).An answer to ϕ in a graph G is an assignment a from the free variables X to V (G) such there is an assignment h from X ∪ Y to V (G) which agrees with a on X and has the property that, for each atom E(z 1 , z 2 ), the image {h(z 1 ), h(z 2 )} is an edge of G.
As is common in the literature (see e.g.[35,11,12,17]), we can equivalently express the answers of ϕ in a graph G as partial homomorphisms to G. Let H be the graph with vertex set X ∪ Y that has as edges the pairs of variables in X ∪ Y that occur in a common atom.Then the answers of ϕ in G are the mappings a : X → V (G) that can be extended to a homomorphism from H to G.For this reason, following the notation of [17], we will from now an refer to a conjunctive query as a pair (H, X) where H is a graph and X is a subset of vertices of H corresponding to the free variables.We will say that (H, X) is connected if H is a connected graph.We will write Ans((H, X), G) for the set of answers of (H, X) in G; this is made formal in Section 2.1.The WL-dimension of a conjunctive query (H, X) is the WL-dimension of the graph parameter that maps every graph G to |Ans((H, X), G)|.
Semantic Extension Width Let (H, X) be a conjunctive query and let Y = V (H) \ X.The graph Γ(H, X) is obtained from H by adding an edge between each pair of vertices u = v in X if and only if there is a connected component in H[Y ] that is adjacent to both u and v.We then define the extension width of (H, X) as the treewidth of Γ(H, X); the definition of treewidth can be found in Section 2.2.
The semantic extension width of a conjunctive query (H, X), denoted by sew(H, X) is then the minimum extension width of any conjunctive query (H ′ , X ′ ) that is counting equivalent to (H, X), i.e., any conjunctive query (H ′ , X ′ ) such that, for every graph G, |Ans((H, X), G)| = |Ans((H ′ , X ′ ), G)|.A discussion of counting equivalence and counting minimal conjunctive queries can be found in Section 2.1.
Before stating our main result, we provide an example of a conjunctive query and its semantic extension width: Let (S k , X k ) be the k-star query: X k = {x 1 , . . ., x k } and S k has vertices X k ∪ {y} and edges {x i , y} for all i ∈ [k].Note that the answers of (S k , X k ) in a graph G are precisely the assignments from X k to V (G) such that the vertices all of the images of vertices in X k have a common neighbour.The k-star query is acyclic (i.e., S k has treewidth 1) and it has played an important role as a base case for complexity classifications regarding counting answers to conjunctive queries [11,17].The graph Γ(S k , X k ) is the (k + 1)-clique which has treewidth k.Since it is also minimal with respect to counting equivalence, we have sew(S k , X k ) = k.

Our Contributions
We now state our main result.
Theorem 1.Let (H, X) be a connected conjunctive query with X = ∅.Then the WLdimension of (H, X) is equal to its semantic extension width sew(H, X).
In Theorem 1, the WL-dimension of (H, x) is captured by its semantic extension width rather than by its extension width, which is the treewidth of Γ(H, X).This is because H[Y ] may contain a high-treewidth subgraph that does not influence the number of answers.
As an immediate corollary of Theorem 1, we obtain the following alternative characterisation of WL-equivalence.
As the following sections show, our classification of the WL-dimension of conjunctive queries has further strong consequences regarding the expressive power of graph neural networks (GNNs), the parameterised complexity of counting answers to conjunctive queries, and the WL-dimension of first-order formulas with universal quantifiers such as the formula corresponding to dominating sets.
GNNs and Conjunctive Queries Over the last decade, GNNs have received increasing attention due to their application to computations involving graph structured data (see [28]).Motivated by the fact that the number of occurrences of small patterns can capture interesting global information about graphs, and can therefore be used to compare graphs [31,1,27], researchers have studied the extent to which GNNs (and their higher order generalisations [33]) are able to count selected small patterns such as homomorphisms [28], subgraphs [8], and induced subgraphs [13].
Following [33] but simplifying the notation for our needs, we represent a t-layer order-k GNN N as a tuple N = (G, W 1 , . . ., W t , f 0 , . . ., f t ) where G is a graph, each W i is a set of weights, and each f i assigns a feature vector to each k-tuple of nodes.The GNN specifies how f i is computed from G, W 1 , . . ., W i−1 , f 0 , . . ., f i−1 .We use f N (G) to denote the final feature vector so f N (G) = f t .The feature vector f N (G) induces a partition on the k-tuples of vertices of G, which we call P N (G).
We next explain what we mean when we say that a GNN can "compute" a function on graphs.So far, this has been studied in a somewhat limited context.Namely, we say that a GNN can "compute" a function A N (G) if A N (G) can be computed in polynomial time from P N (G).Thus, when we say that a GNN can count small patterns, we mean that the number of such patterns can be efficiently computed from P N (G).We do not address the issue of whether the GNN can itself do the polynomial-computation that is needed to compute A N (G) from P N (G).Issues of dimension are also beyond the scope of this paper -in our setting the feature vector induces a partition on the k-tuples of vertices of G -for a brief discussion about how the dimension can be reduced see [33].
We say that a GNN N = (G, W 1 , . . ., W t , f 0 , . . ., f t ) is "fully refined" if there is no GNN ) such that P N ′ (G) strictly refines P N (G).In this setting, Morris et al. [33] established an equivalence between the expressive power of fully-refined order-k GNNs and the k-dimensional WL algorithm.For this, let N k be the set of fully-refined order-k GNNs.Propositions 3 and 4 of [33] give the following proposition.Proposition 3.For all N ∈ N k , P N (G) is exactly the the same as the partition P WL (G) on k-tuples of vertices that is computed by k-WL when it is run with input G and the initial partition induced by the initial feature vector f 0 of N .
Building upon Proposition 3, the works of Dvorák [21], Dell, Grohe and Rattan [16], and Lanzinger and Barcelo [28] determine the expressiveness of fully refined GNNs in the context of homomorphism counting.Essentially, order-k GNNs can count homomorphisms from a graph H if and only if the treewidth of H is at most k.The "if" direction has already been used implicitly in [21,16].It follows explicitly from [28,Theorem 6 and Lemma 7].The "only if" direction follows by combining Proposition 3 with the upper and lower bounds on the WL dimension of counting homomorphisms [21,16,36,28].Specifically, Lanzinger and Barcelo [28] show that homomorphisms from H to G can be efficiently computed from the vertex refinement produced when WL-k is run on input G starting from the partition in which each k-tuple is assigned a part based on the subgraph that induces.
Our classification (Theorem 1) provides a similar picture in the context of counting answers to conjunctive queries.First, we will show that if (H, X) is a conjunctive query with sew(H, X) = k then for all graphs G there is a fully refined GNN N ∈ N k with underlying graph G that computes |Ans((H, X), G)|.This follows from the following two observations.1.From [28,Theorem 6] and Proposition 3, for all k, all sequences F 1 , . . ., F n of graphs of treewidth at most k, all sequences µ 1 , . . ., µ k of rational numbers, and all graphs G there is a fully refined GNN N ∈ N k with underlying graph G such that n i=1 µ i |Hom(F i , G)| can be efficiently computed from P N (G).Observation 23), for all graphs G there is a finite sequence of graphs F 1 , . . ., F n of treewidth at most k, such that |Ans((H, X), G)| can be written as such as sum.

From our work (see
For the other direction we will show that if a fully refined GNN can compute the number of answers from (H, X) then the order of this GNN is at least sew(H, X).The proof is based on the following two observations.
(1) From Proposition 3, for all graphs G ′ and G ′′ such that G ′ ∼ = k G ′′ and all GNNs N ′ , N ′′ ∈ N k with underlying graphs G ′ and G ′′ , and any function (2) Let (H, X) be a conjunctive query with sew(H, X) = k.From our Theorem 1, there are graphs G and We can use these two facts to show that if a fully refined GNN can compute the number of answers from (H, X) then its order is at least sew(H, X).In particular, consider (H, X) with sew(H, X) = k.Suppose for contradiction that, for some j < k, some GNN

Parameterised counting of answers to conjunctive queries
The next consequence of our main result reveals a surprising connection between the complexity of counting answers to conjunctive queries and their WL-dimension.Given a class of conjunctive queries Ψ, the counting problem #CQ(Ψ) takes as input a pair consisting of a conjunctive query (H, X) ∈ Ψ and a graph G and outputs |Ans((H, X), G)|.We say that a class of conjunctive queries has bounded WL-dimension if there is a constant B that upper bounds the WL-dimension of all queries in the class.The assumption FPT = W [1] is the central (and widely accepted) hardness assumption in parameterised complexity theory (see e.g.[22]).We say that a conjunctive query is counting minimal if it is a minimal representative with respect to counting equivalence (see Definition 9).Theorem 1 implies Corollary 4.
Corollary 4. Let Ψ be a recursively enumerable class of counting minimal and connected conjunctive queries with at least one free variable.The problem #CQ(Ψ) is solvable in polynomial time if and only if the WL-dimension of Ψ is bounded; the "only if " is conditioned under the assumption FPT = W [1].
Quantum Queries and the WL dimension of counting dominating sets.Our main result also enables us to classify the WL-dimension of more complex queries including unions of conjunctive queries and conjunctive queries with disequalities and negations over the free variables.The statement of this classification requires the consideration of finite linear combinations of conjunctive queries (also known as quantum queries; see Definition 63).A quantum query is of the form The (H i , X i ) are connected and pairwise non-isomorphic conjunctive queries where each (H i , X i ) is counting minimal and X i = ∅.
It is well known [12,17] that unions of conjunctive queries, existential positive queries, and conjunctive queries with disequalities and negations over the free variables all have (unique) expressions as quantum queries, that is, the number of answers to those more complex queries can be computed by evaluation the respective quantum query according to the definition For this reason, understanding the WL-dimension of linear combination of conjunctive queries allows us to also understand the WL-dimension of more complex queries.
Defining the hereditary semantic extension width of a quantum query Q, denoted by hsew(Q), as the maximum semantic extension width of its terms, we obtain the following.

Corollary 5. The WL-dimension of a quantum query
As a final corollary of our main result we take a look at a concrete graph parameter, the WL-dimension of which was not known so far: the parameter that maps each graph G to the number of size-k dominating sets in G. Here, a dominating set of a graph G is a subset of vertices D of G such that each vertex of G is either contained in D or is adjacent to a vertex in D. With an easy argument, we show that counting dominating sets of size k can be expressed as a linear combination of the k-star queries (S k , X k ).Using Theorem 1 and Corollary 5, we obtain the following corollary.Corollary 6.For each positive integer k, the WL-dimension of the graph parameter that maps each graph G to the number of size-k dominating sets in G is equal to k.

Discussion and Outlook
We stated and proved our result for the case of connected conjunctive queries with at least one free variable over graphs.However, our result can easily be extended to the following.
(A) For disconnected queries, the WL-dimension will just be the maximum of the semantic extension widths of the connected components.
(B) If no variable is free, then counting answers of a conjunctive query becomes equivalent to deciding the existence of a homomorphism.The WL-dimension of the corresponding graph parameter is equal to the treewidth of the query modulo homomorphic equivalence, which for queries without free variables is the same as semantic extension width.This can be proved along the lines of the analysis of Roberson [36].
(C) Barceló et al. [4], and Lanzinger and Barceló [28] have shown very recently that the WL-algorithm (and the notions of WL-equivalence and WL-dimension) readily extend from graphs to knowledge graphs, i.e., directed graphs with vertex labels and edge labels; parallel edges with distinct labels are allowed, but self-loops are not allowed.It is not hard to see that our analysis applies to knowledge graphs as well.
Since the technical content of this paper is already quite extensive, we decided to defer the formal statement and proofs of (A)-(C) to a future journal version.Finally, extending our results from graphs to relational structures is more tricky, since it is not known yet whether and how WL-equivalence can be characterised via homomorphism indistinguishability from structures of higher arity. 1 However, recent works by Böker [7] and by Scheidt and Schweikardt [37] provide first evidence that homomorphism counts from hypergraphs of bounded generalised hypertreewidth might be the right answer.We leave this for future work.

Organisation of the Paper
We start by introducing further necessary notation and concepts in Section 2. Afterwards, we prove the upper bound of the WL-dimension in Section 3, and we prove the lower bound in Section 4. Those two sections can be read independently from each other and the majority of the conceptual and technical work is done in Section 4. Finally, we prove Theorem 1, as well as its consequences, in Section 5.

Preliminaries
Given a set S, we write Bij(S) for the set of all bijections from S to itself.Given a function f : A → B and a subset X ⊆ A, we write f | X : X → B for the restriction of f on X.We write π 1 for the projection of a pair to its first component, that is, π 1 (a, b) = a.Given a positive integer ℓ we set [ℓ] = {1, . . ., ℓ}.
All graphs in this paper are undirected and simple (without self-loops and without parallel edges).Given a graph G = (V, E), a vertex u ∈ V and a subset U of V , N (u) = {v ∈ V | {u, v} ∈ E} and N (U ) = ∪ u∈U N u .We say that a connected component C of a graph H is adjacent to a vertex v of H if there is a vertex u in C that is adjacent to v. Given a subset S of vertices of a graph G, we write G[S] for the graph induced by the vertices in S.
A homomorphism from a graph H to a graph G is a function h : V (H) → V (G) such that, for all edges {u, v} ∈ E(H), {h(u), h(v)} is an edge of G.We write Hom(H, G) for the set of all homomorphisms from H to G.An isomorphism from H to G is a bijection b : V (H) → V (G) such that, for all u, v ∈ V (H), {u, v} ∈ E(H) if and only if {h(u), h(v)} ∈ E(G).We say that H and G are isomorphic, denoted by H ∼ = G, if there is an isomorphism from H to G.An automorphism of a graph H is an isomorphism from H to itself, and we write Aut(H) for the set of all automorphisms of H.

Conjunctive Queries
As stated in the introduction, we focus on conjunctive queries on graphs.This allows us to follow the notation of [17].Definition 7. A conjunctive query is a pair (H, X) where H is the underlying graph and X is the set of free variables.When H and X are clear from context we will use Y to denote V (H) \ X.We say that a conjunctive query (H, X) is connected if H is connected.
It is well-known (see e.g.[11,12,17]) that the set of answers of a conjunctive query in a graph G is the set of assignments from the free variables to the vertices of G that can be extended to a homomorphism.Definition 8. Let (H, X) be a conjunctive query and let G be a graph.The set of answers of (H, X) in G is given by Ans We say that two conjunctive queries (H 1 , X 1 ) and (H 2 , X 2 ) are isomorphic, denoted by (H 1 , X 1 ) ∼ = (H 2 , X 2 ) if there is an isomorphism from H 1 to H 2 that maps X 1 to X 2 .
Throughout this work, we will focus on counting minimal conjunctive queries.It is known that all counting minimal conjunctive queries within a counting equivalence class are isomorphic [12,17].If a query has no existential variables so that X = V (H) then counting equivalence is the same as isomorphism.If all variables are quantified so that X = ∅ then counting equivalence is the same as homomorphic equivalence (also called semantic equivalence).

Treewidth and Extension Width
We start by introducing tree decompositions and treewidth.Definition 10.Let H be a graph.A tree decomposition of H is a pair consisting of a tree T and a collection of sets, called bags, B = {B t } t∈V (T ) , such that the following conditions are satisfied: (T3) For all e ∈ E(H), there is a bag B t with e ⊆ B t .
The width of (T, B) is max t∈V (T ) |B t |− 1 and a tree decomposition of minimum width is called optimal.The treewidth of H, denoted by tw(H), is the width of an optimal tree decomposition of H.The treewidth of a conjunctive query (H, X), denoted by tw(H, X), is the treewidth of H.
Next we introduce the extension width of a conjunctive query.
Definition 11 (Γ(H, X) and Extension Width).Let (H, X) be a conjunctive query.The extension Γ(H, X) of (H, X) is a graph with vertex set V (H) and edge set E(H) ∪ E ′ , where E ′ is the set of all {u, v} such that u, v ∈ X, u = v, and there is a connected component of H[Y ] which is adjacent to both u and v in H.The extension width of of a conjunctive query (H, X) is defined by ew(H, X) := tw(Γ(X, H)).
We will often restrict our analysis to counting minimal conjunctive queries.This requires us to lift the notion of extension width as follows.
Definition 12 (Semantic Extension Width).The semantic extension width of a conjunctive query (H, X), denoted by sew(H, X), is the extension width of a counting minimal conjunctive query (H ′ , X ′ ) with (H, X) ∼ (H ′ , X ′ ).
Note that the semantic extension width is well-defined since all counting minimal (H ′ , X ′ ) with (H, X) ∼ (H ′ , X ′ ) are isomorphic.

The ℓ-copy F ℓ (H, X)
One of the most central operations on conjunctive queries invoked in this work is a cloning operation on existentially quantified variables, defined as follows.
Definition 13 (F ℓ (H, X)).Let (H, X) be a conjunctive query and let ℓ be a positive integer.The ℓ-copy F ℓ (H, X) is defined as follows.The vertex set of There is a natural homomorphism from F ℓ (H, X) to H which we denote by γ[H, X, ℓ].Definition 14.Let (H, X) be a conjunctive query and let ℓ be a positive integer.Define γ[H, X, ℓ] : We will just write γ = γ[H, X, ℓ] if (H, X) and ℓ are clear from the context.Observation 15.The function γ is a homomorphism from F ℓ (H, X) to H.
Next, we relate the treewidth of the graph F ℓ (H, X) to the extension width of (H, X).Lemma 16.Let (H, X) be a conjunctive query and let ℓ be a positive integer.The treewidth of F ℓ (H, X) is at most ew(H, X).
Finally, construct a tree decomposition (T , B) of F ℓ (H, X) by identifying B X,i and B j i for each i ∈ [m] and j ∈ [ℓ].This tree decomposition shows that tw(F ℓ (H, X)) ≤ tw(Γ).
The following lemma follows implicitly from [5].We include a proof for completeness.

Proof. Choose any ℓ > |V (H)| + 1, and let
be an optimal tree decomposition of F ℓ (H, X).We prove the lemma by constructing a tree decomposition (T ′ , B ′ ) of Γ(X, H) with with width at most the width of (T, B).
We claim that (T ′ , B ′ ) is a tree-decomposition of Γ(H, X).This claim proves the lemma, since the width of (T ′ , B ′ ) is clearly at most the width of (T, B), and since the extension width of (H, X) is, by definition, the treewidth of Γ(H, X).Hence it remains to prove our claim by establishing (T1), (T2), and (T3) from Definition 10.In each case, for each v ∈ V (Γ(H, X)), (T2) Consider v ∈ V (Γ(H, X)) and let s and t be any pair of vertices of T such that v ∈ B ′ s and v ∈ B ′ t .We show that there is an s We distinguish between two cases.For the easy case, suppose that e ∈ E(H).Then that is adjacent to both v 1 and v 2 .Since there are ℓ copies of C in F ℓ (H, X), there are at least ℓ vertex disjoint paths from v 1 to v 2 in F ℓ (H, X).Now, using known separation properties of tree decompositions (see for instance [19,Lemma 5]), there is either a bag B t of (T, B) that contains v 1 and v 2 -in this case, we are done -or there is an edge e = {s, t} of T such that S := B s ∩ B t separates v 1 and v 2 , that is, S does not contain v 1 and v 2 , and every v 1 -v 2 -path of F ℓ (H, X) intersects S.This requires |S| ≥ ℓ and thus |B s | ≥ ℓ (and |B t | ≥ ℓ).Consequently, using the fact that (T, B) is optimal, the treewidth of F ℓ (H, X) is at least ℓ > |V (H)| + 1, which yields a contradiction, since by Lemma 16 we have Thus v 1 and v 2 must both be contained in some bag B t , and thus also in the bag With (T1)-(T3) established, the proof is concluded.
In combination, Lemmas 16 and 17 provide an alternative characterisation of the extension width, which we will be using for the remainder of the paper.
Proof.The corollary follows immediately from Lemmas 16 and 17.

Weisfeiler-Leman Equivalence, Invariance and Dimension
In order to make this work self-contained, we will use the characterisation of Weifeiler-Leman (from now on just "WL") equivalence via homomorphism indistinguishability due to Dvorák [21] and Dell, Grohe and Rattan [16].We recommend the survey of Arvind for a short but comprehensive introduction to the classical definition using the WL-algorithm [2].
Definition 19 (WL-Equivalence).Let k be a positive integer.Two graphs G and Thus, following the definition of Arvind et al. [3], we define the WL-dimension of a graph parameter f as the minimum k for which f is k-WL-invariant, if such a k exists, and ∞ otherwise.
Definition 20 (WL-dimension of conjunctive queries).Let (H, X) be a conjunctive query.The WL-dimension of (H, X) is the WL-dimension of the function G → |Ans((H, X), G)|.

Upper Bound on the WL-Dimension
The goal of this section is to prove the following upper bound.
Theorem 21.Let (H, X) be a conjunctive query.Then the WL-dimension of (H, X) is at most ew(H, X).
For the proof of Theorem 21 we will use the following interpolation argument.Lemma 22.Let (H, X) be a conjunctive query.Let G 1 and G 2 be graphs.Suppose that, for all positive integers ℓ, Proof.Let G be a graph and let σ : Let Ω be the set of functions from Y to V (G) and consider any Υ ⊆ Ω. Define Note that this yields a system of linear equations.For each positive integer ℓ, we have the equation n i=1 c i • i ℓ = 0 where The matrix corresponding to this system of equations is a Vandermonde matrix, so it is invertible.Thus c i = 0 for all i ∈ {1, . . ., n}.Therefore The proof of Lemma 22 immediately implies the following observation, Observation 23.Note that the graphs F ℓ (H, X) that are referred to in Lemma 22 have treewidth at most ew(H, X) by Lemma 16.In Observation 23 there are two possibilities.If we start with a query (H, X) that is counting minimal, we can apply directly the proof of Lemma 22. Otherwise, we apply the proof of Lemma 22 to a counting-equivalent counting-minimal query.
Observation 23.Let (H, X) be a conjunctive query of semantic extension width k and let G be a graph.There is a finite sequence of graphs F 1 , . . ., F n of treewidth at most k, such that |Ans((H, X), G)| can be computed via Gaussian elimination from the homomorphism counts |Hom(F ℓ , G)| for ℓ ∈ {1, . . ., n}.
Proof of Theorem 21.Let (H, X) be a conjunctive query.Let k = ew(H, X).We wish to show that the WL-dimension of (H, X) is at most k which is equivalent to showing that the function G → |Ans((H, X), G)| is k-WL invariant.To do this, we show that, for any pair of graphs G and Consider G and G ′ with G ∼ = k G ′ .This implies that for every graph From the definition of ew(H, X) and Corollary 18, for every positive integer ℓ, the treewidth of The claim then follows directly by Lemma 22.

Lower Bound on the WL-Dimension
The goal of this section, which is the technical heart of the paper, is the proof of the following lower bound.
Theorem 24.Let (H, X) be a counting minimal conjunctive query such that H is connected, and ∅ X V (H).Then the WL-dimension of (H, X) is at least ew(H, X).
In order to prove Theorem 24, we will find graphs G and G ′ such that G ∼ = k−1 G ′ , where k = ew(H, X), and |Ans((H, X), G)| = |Ans((H, X), G ′ )|.As explained in the introduction, we will rely on a recently developed version of the CFI graphs of Cai, Fürer and Immerman [10].The following subsection will provide a concise and self-contained explanation of the construction and properties of CFI graphs.

CFI Graphs
We start with a formal definition of a well-known version of CFI graphs [23] (see also [36]).
Definition 25 (CFI graphs, χ(G, W )). Let G be a graph and let W be a subset of V (G).For every vertex For any fixed G, the isomorphism class of χ(G, W ) depends only on the parity of |W |: Lemma 26 (Lemma 3.2 in [36]).Let G be a connected graph and let W, Neuen [34] established the following WL-equivalence result for χ(G, ∅) and χ(G, {w}).

Cloning Vertices in CFI Graphs
We will introduce some notions and properties of coloured graphs and of CFI graphs due to Roberson [36].First of all, since we will work with vertex colourings induced by homomorphisms throughout this section, we adopt the well-established notion of H-colourings of graphs.
Definition 28.We refer to a homomorphism from a graph G to a graph H as an H-colouring of G.
Recall that π 1 is the projection that maps a pair (a, b) to the first component a.

Observation 29 ([36]
).Let F be a graph and let W be a subset of V (F ).The function π 1 is a homomorphism from χ(F, W ) to F .
Technically, to get the F -colouring of χ(F, W ) in Observation 29, one should restrict π 1 to the domain V (χ(F, W )), but it will not be important to capture this in our notation.The following is an extension of a notion introduced in [36, Section 3.1] from CFI graphs to coloured graphs.Definition 30.Let H, G, and F be graphs, let c be a homomorphism from G to F , and let τ be a homomorphism from H to F .We define Hom Observation 31.Let H, G, and F be graphs and let c be a homomorphism from Theorem 32 (Theorem 3.6 in [36]).Let H be a graph, let F be a connected graph, let W ⊆ V (F ), and let τ ∈ Hom(H, F ).
Definition 33 (Cloning Colour-Blocks).Let G be a graph, let F be a connected graph, and let c be a homomorphism from G to F .Let k be a positive integer, let v = (v 1 , . . ., v k ) be a k-tuple of pairwise distinct vertices of F , and let z = (z 1 , . . ., z k ) be k-tuple of positive integers.The graph G(G, F, c, v, z) is obtained from G by cloning, for each i ∈ [k], the colour class of v i under c precisely z i − 1 times.Formally, for each v ∈ V (F ), let The We define a function Lemma 34.Let H and G be graphs and let F be a connected graph.Let c be a homomorphism from G to F and let τ be a homomorphism from H to F .Let v = (v 1 , . . ., v k ) be a k-tuple of distinct vertices of F and let z = (z 1 , . . ., z k ) be a k-tuple of positive integers.For all i ∈ [k], let d i be the number of vertices of H that are mapped by τ to v i , i.e., Proof.Let ρ : V (G ′ ) → V (G) be the function that maps cloned vertices to their original counterparts, that is Observe that ρ is a homomorphism from G ′ to G. We define an equivalence relation on the set Hom τ (H, 2 for an illustration.Consequently, we can represent each equivalence class of ∼ by a homomorphism ĥ ∈ Hom τ (H, G, F, C).
Finally, each equivalence class has size k i=1 z d i i , since for each i ∈ [k], there are z i possibilities for each of the Next we show that cloning colour-blocks in CFI graphs preserves WL-equivalence: |Hom τ (H, χ(F, W ), F, π 1 ).
Combining this with Theorem 32, we find that for all τ ∈ Hom(H, F ), Therefore where the quantities d 1 , . . ., d k depend on τ as in Lemma 34.This concludes the proof.

Reduction to the Colourful Case
Throughout this section, we fix a conjunctive query (H, X) such that H is connected, and with X = {x 1 , . . ., x k }.Fix also a positive integer ℓ.
Recall that the vertices of F are X ∪ (Y × [ℓ]).We start by extending the partitioning result from Section 4.2 from graphs to conjunctive queries.Moreover, recall the definition of the mapping γ : V (F ) → V (H) (Definition 14): Recall also that by Observation 15, the function γ is a homomorphism from F to H. We will adapt Lemma 34 to conjunctive queries in the following way.
Lemma 38.Let G be a graph and let c be an F -colouring of G, and assume that Proof.We encourage the reader to consider Figure 4 for keeping track of the mappings and homomorphisms used in the proof.Let ρ : V (G ′ ) → V (G) be the function that maps cloned vertices to their original counterparts, that is Observe that ρ is a homomorphism from G ′ to G. We define an equivalence relation on the set Ans τ ((H, X), (G, c)).Consequently, we can represent each equivalence class of ∼ by a homomorphism ĥ ∈ Ans τ ((H, X), (G, c)).
Finally, each equivalence class has size k i=1 z d i i , since for each i ∈ [k], there are z i possibilities for each of the Lemma 40 is the main technical lemma in this section, which is concerned with χ(F, ∅) and χ(F, {x 1 }).Recall that the projection π 1 is an F -colouring of χ(F, ∅) and χ(F, {x 1 }).Moreover γ is a homomorphism from F to H. Thus: Recall that Bij(X) is the set of all bijections from X to X.
Then there is a k-tuple z = (z 1 , . . ., z k ) of positive integers such that Suppose for contradiction that, for every k-tuple z of positive integers, |Ans((H, X), G 0 )| = |Ans((H, X), G 1 )|.By Observation 37 Then, treating the z i as variables, we can define a k-variate polynomial By (2), P (z 1 , . . ., z k ) = 0 for all k-tuples of positive integers (z 1 , . . ., z k ).We wish to apply polynomial interpolation, which requires us to collect coefficients for all monomials: To this end, observe first that all d τ i are bounded from above by k since |X| = k.Let A k be the set of all k-tuples of integers (a 1 , . . ., a k ) with 0 ≤ a i ≤ k for each i ∈ [k].Given a k-tuple a ∈ A k , we write coeff( a) for the coefficient of the monomial k i=1 z a i i , that is, By (2), P (z 1 , . . ., z k ) = 0 for all k-tuples of positive integers (z 1 , . . ., z k ).By multivariate polynomial interpolation, only the constant 0 polynomial can satisfy this conditions.Concretely, we obtain coeff(1, 1, . . ., 1) = 0.This yields the desired result since Bij(X) is the set of functions τ : X → V (H) with contradicting the assumption in the statement of the lemma, and therefore completing the proof.
Corollary 41.Suppose that the treewidth of F is t and that Then the WL-dimension of (H, X) is at least t.
Proof.Let v = (x 1 , . . ., x k ).By Lemma 35, for every k-tuple of positive integers z, From the definition of the cloning operation (Definition 33) and the fact that γ is the identity on X (see Definition 14) However, by Lemma 40, there is a k-tuple z = (z 1 , . . ., z k ) of positive integers such that This shows that the function G → |Ans((H, X), G)| can distinguish (t − 1)-WL invariant graphs and thus the WL-dimension of (H, X) at least t.
We will consider the set of partial automorphisms of a conjunctive query, defined as follows: Note that a mapping τ : X → X can only be extended to an automorphism of H if it is bijective.Thus: The following result will be very useful for the remainder of this section; it can be found in Corollary 54.4 in the full version [18] of [17].
Lemma 44 ( [18,17]).Let h be a homomosphism from H to H that surjectively maps X onto X.If (H, X) is counting minimal then h is an automorphism of H.
Lemma 46.Let G be a graph, let c be a homomorphism from G to H, and let τ ∈ Aut(H, X).
Proof.By Corollary 41 it suffices to establish the following.
By Observation 39, c is an H-colouring of both χ(F, ∅) and χ(F, {x 1 }).Thus we can use Lemmas 45 and 46, and obtain: The first factor is non-zero because Aut(H, X) is non-empty.The second factor is non-zero by the assumption in the statement of the corollary.

Proving the Lower Bound
We first set up some notation, following [17].
Definition 48 (Colour-prescribed Homomorphisms).Let H and G be graphs, let (H, X) be a conjunctive query, and let c be an Homomorphisms in cpHom(H, (G, c)) are said to be "colour-prescribed" with respect to c. Observation 49. cpAns((H, X), (G, c)) ⊆ Ans id ((H, X), (G, c)) Observation 49 follows directly from the definitions.Recall that Ans id ((H, X), X in the definition of Ans id ((H, X), (G, c)), while for all v ∈ V (H) in the definition of cpAns((H, X), (G, c)).Lemma 50 shows equivalence for counting minimal conjunctive queries.
Proof.Observation 49 proves one direction.For the other direction, consider a map a ∈ Ans id ((H, X), (G, c)).From the definition, there is a homomorphism h ∈ Hom(H, G) such that h| X = a and c(a(x)) = x for all x ∈ X.From the definitions of c and h, the function ϕ := c(h(•)) is a homomorphism from H to itself.Since ϕ maps X to X and (H, X) is counting minimal, Lemma 44 guarantees that ϕ is an automorphism of H. H, (G, c)).Since ϕ is the identity on X, the same is true of ϕ −1 .Thus, for all x ∈ X, h The following definition will be useful; it will provide a parity condition which is both sufficient and necessary for containment in the relevant set of answers.
Definition 51 (Extendable Assignments, E(X, F, W )). Let (H, X) be a conjunctive query with ∅ X = {x 1 , . . ., x k } V (H) and suppose that x 1 is adjacent to at least one vertex in Y = V (H) \ X. Suppose that H is connected and let ℓ be an odd positive integer.Let Let W be a subset of X and let ϕ be an assignment from X to V (χ(F, W )) such that, for all p ∈ [k], c(ϕ(x p )) = x p .Define S 1 , . . ., S k such that ϕ(x p ) = (x p , S p ) for all p ∈ [k].We say that ϕ is extendable if the following two conditions hold.
We will prove Lemmas 52, 53 and 54 in Section 4.4.1.
Lemma 52.Let (H, X) be a conjunctive query with ∅ X V (H) and suppose that x 1 is adjacent to at least one vertex in V (H) \ X. Suppose that H is connected and let ℓ be an odd positive integer.Let F = F ℓ (H, X) and let c = γ(π Lemma 53.Let (H, X) be a conjunctive query with ∅ X = {x 1 , . . ., x k } V (H) and suppose that x 1 is adjacent to at least one vertex in V (H) \ X. Suppose that H is connected and let ℓ be an odd positive integer.Let F = F ℓ (H, X) and let c = γ(π 1 (•)).Let W be a subset of X and let ϕ be an assignment from Lemma 54.Let (H, X) be a conjunctive query with ∅ X = {x 1 , . . ., x k } V (H) and suppose that x 1 is adjacent to at least one vertex in V (H) \ X. Suppose that H is connected and let ℓ be an odd positive integer.Let F = F ℓ (H, X) and let c = γ(π 1 (•)).Let W be a subset of X and let ϕ be an assignment from Lemma 55.Let (H, X) be a conjunctive query with ∅ X V (H) and suppose that x 1 is adjacent to at least one vertex in V (H) \ X. Suppose that H is connected and let ℓ be an odd positive integer.Let F = F ℓ (H, X) and let c = γ(π 1 (•)).Let W be a subset of X.Then cpAns((H, X), (χ(F, W ), c)) = E(X, F, W ).
Lemma 56.Let (H, X) be a conjunctive query with ∅ X V (H) and suppose that x 1 is adjacent to at least one vertex in V (H) \ X. Suppose that H is connected and let ℓ be an odd positive integer.Let Proof.The lemma follows immediately from Lemmas 52 and 55.
We can now state and prove Lemma 57, the main goal of this subsection, which will enable us to immediately infer Theorem 24.
Lemma 57.Let (H, X) be a counting minimal conjunctive query such that H is connected, and ∅ X V (H).Without loss of generality, suppose that x 1 is adjacent to at least one vertex in V (H) \ X.Let ℓ be an odd positive integer.Let F = F ℓ (H, X) and let c = γ(π Proof.The lemma follows directly from Lemma 50 and lemma 56.
We can now prove Theorem 24.
Theorem 24.Let (H, X) be a counting minimal conjunctive query such that H is connected, and ∅ X V (H).Then the WL-dimension of (H, X) is at least ew(H, X).

Proofs of Lemmas 52, 53 and 54
This section will use the following combinatorial lemma, Lemma 58, which seems to be folklore.
Lemma 58.Let G be a connected graph and let S ⊆ V (G) be a vertex-subset of even cardinality.Then there is an assignment Proof.The proof is by induction on n = |V (G)|.If n = 1, then E(G) = ∅ so necessarily S = ∅.By convention, the (empty) sum is 0 which is as desired since v / ∈ S. Assume for the induction hypothesis that the claim holds for n and let G = (V, E) be a graph with n + 1 vertices.Let v be a vertex of G such that G ′ := G \ {v} is connected.Let v 1 , . . ., v d be the neighbours of v.Note that d > 0 since G is connected.We consider two cases. Case Note that S ′ has even cardinality, since S \ v has odd cardinality.We can thus apply the induction hypothesis to G ′ and S ′ and obtain a function We apply the induction hypothesis to G ′ and S and obtain a function We finish by proving Lemmas 53, 54 and 52. and V j i as in Definition 51.If (E1) is not satisfied, then ϕ cannot be extended to a homomorphism from H to G. If (E2) is not satisfied then there is an Let y 1 , . . ., y t be the vertices of ) and that the vertices of G = χ(F, W ) are pairs (w, T ) where w is a vertex of F and T ⊆ N (w).So for a vertex v = y s of C i , γ(π 1 (h(y s ))) = y s implies that h(y s ) is of the form ((y s , j), T s ) for some j ∈ [ℓ].Moreover, since C i is a connected component of H[Y ] there is a single j such that, for all s ∈ [t], h(y s ) = ((y s , j), T s ).
We next define a t-by-(k + t) matrix M , indexed by vertices of F .The rows of M are indexed by (y 1 , j), . . ., (y t , j), and the columns of M are indexed x 1 , . . ., x k , (y 1 , j), . . ., (y t , j).The entries of M are defined as follows.For s From the definition of G = χ(F, W ), the size of T s is odd if (y s , j) ∈ W and even otherwise.Since W is a subset of X, every set T s has even cardinality.Thus every row of M has an even number of 1s, and therefore M has an even number of 1s.Let M X be the submatrix of M containing only the columns indexed by x 1 , . . ., x k .Let M Y be the submatrix of M containing only the columns indexed by (y 1 , j), . . ., (y t , j).
Note that M Y is a square matrix.We next show that it is symmetric.If M ((y s , j), (y s ′ , j)) = 1, then (y s ′ , j) ∈ T s , which implies that (y s , j) and (y s ′ , j) are adjacent in F and thus y s and y s ′ are adjacent in H. Suppose for contradiction that M ((y s ′ , j), (y s , j)) = 0. Then (y s , j) / ∈ T s ′ .So ((y s , j), T s ) and ((y s ′ , j), T s ′ ) are not adjacent in G = χ(F, W ), contradicting the fact that h is a homomorphism from H to G. Thus, our assumption was wrong, and M Y is symmetric.Since the diagonal of M Y contains only 0s (since H and F do not have self-loops), the number of 1s in M Y is even.
To finish the proof we will show that, for every p ∈ [k], the number of 1s in the column of M indexed by , which is odd by the choice of i.Thus, M X has an odd number of 1s and M Y has an even number of 1s, contradicting the fact that M has an even number of 1s.We conclude that our initial assumption for contradiction, that ϕ ∈ cpAns((H, X), (G, c)), is false, proving the lemma.
So to finish, fix p ∈ [k].We will show that the number of 1s in the column of M indexed by x p is |S p ∩ V j i |.Before considering the entries of this column of M , we establish a fact which we will use twice -if x p and (y s , j) are adjacent in F then, from the definition of F , x p and y s are adjacent in H. Since h is a homomorphism from H to G that extends ϕ, from the definition of S p , h(x p ) = ϕ(x p ) = (x p , S p ), so it follows that (x p , S p ) is adjacent to h(y s ) = ((y s , j), T s ) in G. Using this fact, we will show that the number of 1s in the column of M indexed by x k is |S p ∩ V j i |.First, consider any s such that that M ((y s , j), x p ) = 1.From the definition of M , x p ∈ T s so, from the definition of G = χ(F, W ), x p and (y s , j) are adjacent in F .From the fact, (x p , S p ) is adjacent to ((y s , j), T s ) in G. From the definition of G, (y s , j) ∈ S p .By construction, (y s , j) ∈ V j i , so (y s , j) ∈ S p ∩ V j i .Finally, consider any s such that that M ((y s , j), x p ) = 0. From the definition of M , x p / ∈ T s .There are two cases.Case 1: If x p and (y s , j) are not adjacent in F then, from the definition of S p , (y s , j) is not in S p so it is clearly not in S p ∩ V j i .Case 2. If x p and (y s , j) are adjacent in F but x p / ∈ T s then from the fact (x p , S p ) is adjacent to ((y s , j), T s ) in G.So from the definition of G, (y s , j) / ∈ S p so it is clearly not in S p ∩ V j i .As required, we thus obtain that the number of 1s in the column of M indexed by , define S p , C i , and V j i as in Definition 51.Condition (E1) ensures that, for every {x a , x b } ∈ E(H[X]), (x a , S a ) and (x b , S b ) are adjacent in G.By (E2), for every i ∈ [m] there is a j (G, c)), we will construct an h ∈ cpHom(H, (G, c)) with h| X = ϕ.To do this we will choose a value for h(y) for each y ∈ Y .For every Let Ω i = {y i,s : |T i,s,X | is odd}.We will show that |Ω i | is even.To see this, let M X be a matrix with rows indexed by (y i,1 , j i ), . . ., (y i,t i , j i ) and columns indexed by X. Define M (y i,s , j i ), x p ) to be 1 if x p ∈ T i,s,X and 0 otherwise.By construction, the number of 1s in the column of M indexed by x p is the number of vertices (y i,s , j i ) ∈ S p so this is |S p ∩ V j i i |.By the choice of j i , the number of 1s in M X is even.So Ω i , which contains the indices of rows with an odd number of 1s, has even cardinality.Apply Lemma 58 with graph C i and S = Ω i to obtain an assignment β : E(C i ) → {0, 1} such that, for all y i,s ∈ Ω i , y i,s ′ ∈N i (y i,s ) β({y i,s ′ , y i,s }) is odd and for all y i,s ∈ V (C i ) \ Ω i , this sum is even.Finally, let T i,s,Y = {(y i,s ′ , j i ) | y i,s ′ ∈ N i (y i,s ) and β({y i,s ′ , y i,s }) = 1} and let T i,s = T i,s,X ∪ T i,s,Y .We then define h(y i,s ) = ((y i,s , j i ), T i,s ).Our goal is to show that h ∈ cpHom(H, (G, c)).For this we require • Property 2: For all y i,s ∈ Y , c(h(y i,s )) = y i,s .(This follows immediately from the definition of h.) • Property 3: h is a homomorphism from H to G.That is, all three of the following hold.
-Property 3a: For every edge {x a , x b } of E(H[X]), {h(x a ), h(x b )} is an edge of G (this follows immediately from (E1) and the definition of G).
-Property 3b: For every edge {x p , y i,s } of H with x p ∈ X and y i,s ∈ C i , {h(x p ), h(y i,s )} is an edge of G.
-Property 3c: For every edge {y i,s , y i,s ′ } of C i , {h(y i,s ), h(y i,s ′ )} is an edge of G.
We start by showing Property 1 -that each h(y i,s ) is a vertex of G.For this, we need two constraints to be satisfied as follows.
• Constraint 1: Every vertex of T i,s must be a neighbour of (y i,s , j i ) in F .
• Constraint 2: |T i,s | must be even (this constraint comes from the definition of G since y i,s is not in W , which is a subset of X).
Constraint 1 follows directly from the definition of T i,s .To see that every x p ∈ T i,s,X is a neighbour of (y i,s , j i ) note that (y i,s , j i ) ∈ S p which implies that {x p , (y i,s , j i )} is an edge of F since (x p , S p ) is a vertex of G.It is immediate from the definition of T i,s,Y and F that every vertex in T i,s,Y is a neighbour of (y i,s , j i ) in F .Constraint 2 is by construction.Consider any pair (i, s).
We next consider Property 3b.Consider an edge {x p , y i,s } of H with x p ∈ X and y i,s ∈ C i .Then h(x p ) = (x p , S p ).If (y i,s , j i ) ∈ S p then x p ∈ T i,s,X so x p ∈ T i,s and h(y i,s ) = ((y i,s , j i ), T i,s ) is connected to (x p , S p ) in G by the definitions of F and G. Similarly, if (y i,s , j i ) / ∈ S p then x p / ∈ T i,s so again ((y i,s , j i ), T i,s ) is connected to (x p , S p ) in G.To finish the proof, we establish Property 3c.Consider an edge {y i,s , y i,s ′ } of C i .Then h(y i,s ) = ((y i,s , j i ), T i,s ) and h(y i,s ′ ) = ((y i,s ′ , j i ), T i,s ′ ) By construction, (y i,s ′ , j i ) ∈ T i,s iff (y i,s , j i ) ∈ T i,s ′ .Hence h(y i,s ) and h(y i,s ′ ) are connected in G.
Proof of Lemma 52.Let Y = V (H) \ X.For i ∈ [m] and j ∈ [ℓ], define C i and V j i as in Definition 51.For every subset W of X and every map ϕ : X → V (χ(F, W )) that satisfies c(ϕ(x p )) = x p for all p ∈ [k], let S 1 (ϕ), . . ., S k (ϕ) be the sets defined in Definition 51.For every ϕ ∈ E(X, F, W ), (E2) guarantees that for every i ∈ [m] there is a j i | is even.We will partition E(X, F, W ) into disjoint sets in terms of i and j i as follows.First we define E(X, F, W, 1) by fixing i = 1 and j i > 1.
For all i ∈ {2, . . ., m}, define We split M into two submatrices: M X consists of the columns indexed by vertices in X and M Y consists of the columns indexed by the remaining vertices of F .For every p ∈ [k], x p / ∈ S p since (x p , S p ) is a vertex of G and F has no self-loops.Also, M X is symmetric by (E1).So the number of 1s in M X is even.Now consider M Y .Since ϕ ∈ E(X, F, {x 1 }), (E2) implies that for every i ∈ [m] there is a For each i ∈ [m] and j ∈ [ℓ], let M j i be the submatrix of M Y containing only the columns indexed by the vertices in V j i .Then for all i ∈ [m], condition (C1) implies that M 1 i contains an even number of 1s and condition (C2) implies that for all j ∈ {2, . . ., ℓ}, M j i contains an odd number of 1s.Since ℓ is odd, the total number of 1s in the matrices M 1 i , . . ., M ℓ i is even.Consequently, the total number of 1s in M Y is even, contradicting the fact that M has an odd number of 1s and M X has an even number of 1s.

Main Result and Consequences
With upper and lower bounds established, we are now able to proof Theorem 1, which we restate for convenience.
Theorem 1.Let (H, X) be a connected conjunctive query with X = ∅.Then the WLdimension of (H, X) is equal to its semantic extension width sew(H, X).
Proof.We first consider the special case where (H, X) is a full conjunctive queries, that is, no variable of (H, X) is existentially quantified so X = V (H).In this case, (H, X) is counting minimal, since counting equivalence is the same as isomorphism in this case [17].Moreover, Γ(H, X) = H.Thus sew(H, X) = tw(H).Since Ans((H, X), G) = Hom(H, G) for X = V (H), counting answers to (H, X) is the same as counting homomorphisms from H, and the WL-dimension of counting homomorphisms is tw(H) as shown by Neuen [34].Now consider the case where X = V (H) and let (H ′ , X ′ ) be a counting minimal conjunctive query with (H ′ , X ′ ) ∼ (H, X).Then, |Ans((H, X), G)| = |Ans((H ′ , X ′ ), G)| for every graph G and thus (H, X) and (H ′ , X ′ ) have the same WL-dimension.Furthermore, since (H, X) is connected, so is (H ′ , X ′ ) -see [18,Section 6].Theorems 24 and 21 now state that the WL-dimension of (H ′ , X ′ ) is equal to ew(H ′ , X ′ ).Finally, by definition of semantic extension width, we have sew(H, X) = ew(H ′ , X ′ ), concluding the proof.

Homomorphism Indistinguishability and Conjunctive Queries
Given a class of graphs F, two graphs G and G ′ are called F-indistinguishable, denoted by If F is the class of all graphs, then a classical result of Lovász states that ∼ = F coincides with isomorphism (see e.g.Theorem 5.29 in [29]).Recent years have seen numerous exciting results on the structure of F-indistinguishability, depending on the class F: For example, Dvorák [21], and Dell, Grohe and Rattan [16] have shown that ∼ = F coincides with ∼ = k , i.e., with k-WL-equivalence, if F is the class of all graphs of treewidth at most k, and Mancinska and Roberson have shown that ∼ = F coincides with what is called quantum-isomorphism if F is the class of all planar graphs [30].
To state our first corollary, we extend the notion of homomorphism indistinguishability to conjunctive queries.Definition 59.Let Ψ be a class of conjunctive queries.Two graphs G and Then, using the notion of conjunctive query indistinguishability, we obtain a new characterisation of k-WL-equivalence.
Corollary 60 (Corollary 2, restated).Let k be a positive integer and let Ψ k be the set of all connected conjunctive queries with at least one free variable and with semantic extension width at most k.Then for any pair of graphs G and Proof.For the first direction, suppose that G ∼ = k G ′ and consider (H, X) ∈ Ψ k .Then sew(H, X) ≤ k and thus the WL-dimension of (H, X) is at most k by Theorem 1. Consequently, |Ans(( For the other direction, suppose that is the class of all conjunctive queries (H, V (H)) where H is a connected graph with treewidth at most k.Finally, we can remove the connectivity constraint as follows: Let F = F 1 ∪ F 2 be the disjoint union of two graphs F 1 and F 2 .If F has treewidth at most k, then both F 1 and F 2 also have treewidth at most k, since treewidth is monotone under taking subgraphs.Thus, where F is the class of all queries (H, V (H)) such that H is a graph with treewidth at most k and thus G ∼ = k G ′ .
In the following corollary, we show that the treewidth of a conjunctive query alone is insufficient for describing the WL-dimension.This is even the case for treewidth 1, i.e., for acyclic queries.
Corollary 61.The class of acyclic conjunctive queries have unbounded WL-dimension, that is, there is no k such that G ∼ = k G ′ if and only if G ∼ = T G ′ , where T is the class of all acyclic conjunctive queries.
Proof.The corollary follows immediately from Theorem 1 and the fact that acyclic conjunctive queries can have arbitrary high semantic extension width.Recall the k-star query (S k , X k ), where Clearly, (S k , X k ) is acyclic.Moreover, it is well-known that (s k , X k ) is counting minimal (see e.g.[17]).Finally, Γ(S k , X k ) is the k + 1-clique, and thus sew(S k , X k ) = tw(K k+1 ) = k.
Corollary 61 is in stark contrast to the quantifier-free case.The WL-dimension of any acyclic conjunctive query (H, V (H)) is equal to 1 since this case is equivalent to counting homomorphisms from acyclic graphs [21,16].Given Corollary 61 one might ask how powerful indistinguishability by acyclic conjunctive queries is: Is there any k > 1 such that T -indistinguishability is at least as powerful as k-WL-equivalence?We provide a negative answer.
Proof.For the disjoint union of two conjunctive queries (H 1 , X 1 ) ∪ (H 2 , X 2 ) and any graph Thus, it suffices to prove the observation for connected queries.
Let (H, X) be any connected acyclic conjunctive query.If X = ∅ then the observation is trivial since computing G → |Ans((H, X), G)| is equivalent to deciding whether there is a homomorphism from H to G.As H is acyclic, we thus have |Ans((H, X), Now assume that X = ∅.For the proof, we associate the query (H, X) with an edgeweighted tree T as follows: The vertices of T are X, and two (distinct) vertices x 1 , x 2 of T are adjacent if and only if there is a path from x 1 to x 2 in H, the intermediate vertices of which are all existentially quantified variables, i.e., contained in V (H) \ X.Note that there can be at most one such path since (H, X) is acyclic.The number of intermediate vertices on this path will become the weight of the edge {x 1 , x 2 } in T ; note that the weight is 0 if and only if x 1 and x 2 are adjacent in H. Let us write w : E(T ) → N for the weight function of the edges of T .Now it is easy to see that, for any graph G without isolated vertices, the elements of Ans((H, X), G) are precisely the mappings ϕ : V (T ) → V (G) such that for every e = {x 1 , x 2 } ∈ E(T ) there is a (not necessarily simple) walk from ϕ(x 1 ) to ϕ(x 2 ) with w(e) internal vertices.
• If ŵ = 0 then x must be mapped to a neighbour of the image of x ′ .Since both 2K3 and C 6 are 2-regular, there are precisely 2 options for each case.
• Otherwise ŵ is positive and even.Then This concludes the proof.
In other words, acyclic conjunctive queries cannot even distinguish 2K 3 and C 6 , which are the most common examples of graphs which are 1-WL-equivalent, but which are not 2-WL-equivalent.

WL-Dimension and the Complexity of Counting
In this section, we give a connection between WL-dimension and the parameterised complexity of counting answers to conjunctive queries.Recall that, given a class of conjunctive queries Ψ, the counting problem #CQ(Ψ) takes as input a pair consisting of a conjunctive query (H, X) ∈ Ψ and a graph G and outputs |Ans((H, X), G)|.
Recall that a class of conjunctive queries has bounded WL-dimension if there is a constant B that upper bounds the WL-dimension of all queries in the class.

Corollary 4. Let Ψ be a recursively enumerable class of counting minimal and connected conjunctive queries with at least one free variable. The problem #CQ(Ψ) is solvable in polynomial time if and only if the WL-dimension of Ψ is bounded; the "only if " is conditioned under the assumption
Proof.Given a conjunctive query (H, X), recall the definition of the graph Γ(H, X) from Definition 11.The contract of (H, X) is the induced graph subgraph Γ[X] (see [17,Definition 8]).
Using the notion of contract, Chen, Durand, and Mengel [20,11] and Dell, Roth and Wellnitz [17] have established an exhaustive complexity classification for #CQ(Ψ).For an explicit statement, Theorem 10 in [17] states that (conditioned on FPT = W [1]), #CQ(Ψ) is solvable in polynomial time if and only if both the treewidth of Ψ and the treewidth of the contracts of Ψ are bounded.
By Theorem 1, the WL-dimension of a connected counting minimal query with X = ∅ is equal to its extension width.It remains to show that the condition that both the treewidth of Ψ and the treewidth of the contracts of Ψ are bounded is the same as the condition that Ψ has bounded extension width.
First, suppose that Ψ has bounded extension width.Then for some quantity B, every query (H, X) in Ψ has extension width at most B. So tw(Γ(X, H)) ≤ B. Since H and Γ[X] are both subgraphs of Γ(X, H), they both have treewidth at most B, as required.
For the other direction, suppose that the treewidth of every query in Ψ and every contract of every query in Ψ is at most B. Consider a query (H, X) ∈ Ψ.Let t 1 ≤ B be the treewidth of H and let t 2 ≤ B be the treewidth of Γ[X].Let T = (T, B) be an optimal tree-decomposition of Γ[X].Let C 1 , . . ., C d be the connected components of H[Y ].For each i ∈ [d] let δ i be the subset of vertices in X that are adjacent to C i in H.By the definition of Γ, each δ i induces a clique in Γ, and thus in Γ[X].Therefore, there is a bag B i that contains δ i (see e.g.[6,Lemma 3]).Since the treewidth of Γ[X] is t 2 it follows that |δ i | ≤ t 2 + 1.
Next, for each i ∈ [d], let T i be an optimal tree decomposition of C i and note that the width of all T i is at most t 1 since all components C i are subgraphs of H.It is now straightforward to construct a tree-decomposition of width at most t 1 + t 2 of Γ(H, X).For every i ∈ [d], we add δ i to each bag of T i .Finally, we fix an arbitrary node v i of the tree T i of T and connect it to the node of T with bag B i .Clearly (T1)-(T3) are satisfied.

Linear Combinations of Conjunctive Queries
The study of linear combinations of homomorphism counts dates back to the work of Lovász (see the textbook [29]).Recently, staring with the work of Chen and Mengel [12] and of Curticapean, Dell and Marx [14], the study of these linear combinations has re-arisen in the context of parameterised counting complexity theory.Moreover, the works of Seppelt [38], Neuen [34], and Lanzinger and Barceló [28] have shown that the WL-dimension of a function evaluating a finite linear combination of homomorphism counts is equal to the maximum WLdimension of any term in the combination.Using Theorem 1, we establish a similar result (Corollary 5) for linear combinations of conjunctive queries.This gives a precise quantification of the WL-dimension of unions of conjunctive queries and of conjunctive queries with disequalities and negations over the free variables.Following Lovász's notion of a "quantum graph"[29, Chapter 6.1], we formalise our linear combinations as follows.
Definition 63 (Quantum Query).A quantum query Q is a formal finite linear combination of conjunctive queries Q = ℓ i=1 c i • (H i , X i ) such that, for all i ∈ [ℓ], c i ∈ Q \ {0} and (H i , X i ) is a connected and counting minimal conjunctive query with X i = 0.Moreover, the conjunctive queries (H i , X i ) are pairwise non-isomorphic.We call the queries (H Chen and Mengel [12], and Dell, Roth, and Wellnitz [17] have shown that for every union ϕ of (connected) conjunctive queries with at least one free variable there is a quantum query Q[ϕ] such that, for all graphs G, the number of answers of ϕ in G is equal to |Ans(Q[ϕ], G)|.Moreover, Q[ϕ] is unique up to reordering terms (and up to isomorphim of the constituents).They have also shown a similar result when ϕ is a conjunctive query with disequalities and negations.
Definition 64 (Hereditary Semantic Extension Width).The hereditary semantic extension width of a quantum query Q = ℓ i=1 c i • (H i , X i ) is hsew(Q) = max{sew(H i , X i ) | i ∈ [ℓ]}.We define the WL-dimension of a quantum query Q as the WL-dimension of the graph parameter G → |Ans(Q, G)|.The following lemma was shown by Seppelt [38] in the special case of homomorphisms, i.e., the case in which each constituent (H i , X i ) satisfies X i = V (H i ).The proof of the generalised version follows the same idea, combining Seppelt's approach and Theorem 1.
Corollary 5.The WL-dimension of a quantum query Q is equal to hsew(Q).
Proof.Let Q = ℓ i=1 c i • (H i , X i ) and k = hsew(Q).We first show that the WL-dimension of Q is upper bounded by k; this is the easy direction.For each i ∈ [ℓ], let k i = sew(H i , X i ).By the definition of hsew(Q), k = max i∈[ℓ] k i .
By Theorem 1, the WL-dimension of the function G → |Ans(H i , X i )| is k i .Let G and G ′ be graphs such that G ∼ = k G ′ (which means that for every i ∈ [ℓ], G ∼ = k i G ′ ).Then for every i ∈ [ℓ], |Ans((H i , X i ), G)| = |Ans((H i , X i ), G ′ )| so This means that the function G → |Ans(Q, G)| cannot distinguish k-WL-equivalent graphs so the WL-dimension of Q is at most k.
The more difficult direction is the lower bound.To this end, we will construct graphs F and F ′ such that F ∼ = k−1 F ′ and |Ans(Q, F )| = |Ans(Q, F ′ )|.This implies that the WLdimension of the graph parameter G → |Ans(Q, G)| is greater than k −1 so the WL-dimension of Q is at least k.
Assume without loss of generality that sew(H 1 , X 1 ) = k.By Theorem 1 the WL-dimension of (H 1 , X 1 ) is k so k is the minimum positive integer such that counting answers from (H 1 , X 1 ) cannot distinguish k-equivalent graphs.It was shown in [18,Lemma 34(iii)] that the matrix corresponding to this system has full rank.Therefore, for all i ∈ [ℓ], d i = 0.This implies |Ans((H 1 , X 1 ), G)| = |Ans((H 1 , X 1 ), G ′ )|, contradicting the choice of G and G ′ .Thus our assumption was wrong, and there is a graph the proof is concluded.

Star Queries and Dominating Sets
In this final section we use Theorem 1 to determine the WL-dimension of counting dominating sets (proving Corollary 6).
Definition 65 (Dominating Set).Let G be a graph and let k be a positive integer.A dominating set of G is a subset D ⊆ V (G) such that each vertex of G is either contained in D or adjacent to a vertex in D. The set ∆ k (G) contains all size-k dominating sets of G.
For analysing the WL-dimension of counting size-k dominating sets we will consider, as an intermediate step, the k-star-query.
Definition 66.Let k be a positive integer.The k-star is the conjunctive query (S k , X k ) where X k = {x 1 , . . ., x k }, V (S k ) = X ∪ {y}, and The k-star is often written in the more prominent form ϕ(x 1 , . . ., x k ) = ∃y : k i=1 E(x i , y).It is well-known that (S k , X k ) is counting minimal (see e.g.[17]).Moreover, Γ(S k , X k ) is the (k+1)-clique, which has treewidth k.Thus sew(S k , X k ) = k.Corollary 67 follows immediately from Theorem 1.
Corollary 67.The WL-dimension of (S k , X k ) is k.
We can now prove Corollary 6.For the upper bound, we have to show that F ∼ = k F ′ implies ∆ k (F ) = ∆ k (F ′ ), which is an immediate consequence of our previous analysis.Since the WL-dimension of G → |Inj((S k , X k ), G)| is equal to k, we have This concludes the proof.

Figure 1 :
Figure 1: Each homomorphism h from H to G induces a homomorphism τ from H to F by composing h with the F -colouring c of G.By partitioning Hom(H, G) along the induced homomorphisms to F , we obtain Observation 31.

Figure 2 :
Figure 2: Illustration for the proof of Lemma 34: G ′ = G(G, F, c, v z) is the graph obtained from G by cloning vertices (Definition 33), and ρ is the homomorphism from G ′ to G that maps each cloned vertex in G ′ to its primal vertex in G.Moreover, c is the F -colouring of G and c ′ = C(G, F, c, v, vz) is, by Definition 33, the composition of c and ρ, i.e., each cloned vertex is mapped by c ′ to the colour of its primal vertex.

Figure 3 :
Figure 3: Illustrations of the mappings used in Definition 36.

Figure 4 :
Figure 4: Overview of the mappings and homomorphisms used in the proof of Lemma 38.

Corollary 68 (
Corollary 6, restated).The WL-dimension of the function G → |∆ k (G)| is k.Proof.We start with the lower bound.To this end, given a graph G, and a conjunctive query (H, X), we set Inj((H, X), G) = {a ∈ Ans((H, X), G) | a is injective}.Let I = {(i, j) ∈ [k] 2 | i < j}and consider a subset J ⊆ I.The query (S k , X k )/J is obtained by identifying x i and x j if and only if (i, j) ∈ J. Observe that (S k , X k )/J ∼ = (S ℓ , X ℓ ) for some ℓ ≤ k.By the principle of inclusion and exclusion, for each graph G,|Inj((S k , X k ), G)| = J⊆I (−1) |J| • |Ans((S k , X k )/J, G)| = k i=1 c i • |Ans((S i , X i ), G)| ,wherec i = {J ⊆ I | (S k , X k )/J ∼ = (S i , X i )}.Thus, |Inj((S k , X k ), G)| computes the number of answers to the quantum query with constituents (S i , X i ) and coefficients c i .Moreover,c k = 1 since (S k , X k )/J ∼ = (S k , X k ) if and only if J = ∅. 2 By Corollary 5 and the fact that sew(S ℓ , X ℓ ) = ℓ for all ℓ ∈ [k], the WL-dimension of G → |Inj((S k , X k ), G)| is equal to k.Next observe that |Inj((S k , X k ), G)|/k! is the number of k-vertex subsets A of G such that there is a vertex y ∈ V (G) that is adjacent to all a ∈ A. Thus |V (G)| k − |Inj((S k , X k ), G)|/k! is equal to the size of the set D k (G) := {A ⊆ V (G) | |A| = k ∧ ∀y ∈ V (G) : ∃a ∈ A : {a, y} / ∈ E(G)}.Let G be the self-loop-free complement of G, that is, two distinct vertices u and v in V (G) = V (G) are adjacent in G if and only if they are not adjacent in G. Observe that {a, y} / ∈ E(G) if and only if a = y or {a, y} ∈ E(G).Therefore |D k (G)| = |∆ k (G)|.We are now ready to prove that the WL-dimension of the functionG → |∆ k (G)| is at least k.Suppose for contradiction that its WL-dimension is k ′ for some 1 ≤ k ′ < k.Then, for all G and G ′ with G ∼ = k ′ G ′ , |∆ k (G)| = |∆ k (G ′ )|.However, we know that the WL-dimension of G → |Inj((S k , X k ), G)| is equal to k.Thus there are graphs F and F ′ with F ∼ = k ′ F ′ and |Inj((S k , X k ), F )| = |Inj((S k , X k ), F ′ )|.It is well known (see e.g.Seppelt[38]) thatF ∼ = k ′ F ′ implies F ∼ = k ′ F ′ .Therefore |∆ k (F )| = |∆ k (F ′ )|.Let K 1 be the (treewidth 0) graph containing one isolated vertex.The number of homomomorphisms from K 1 to F determines the number of vertices of F so F ∼ = k ′ F ′ implies |V (F )| = |V (F ′ )|.Let n = |V (F )| = |V (F ′ )|.In summary, |Inj((S k , X k ), F )| = k! n k − |∆ k (F )| = k! n k − |∆ k (F ′ )| = |Inj((S k , X k ), F ′ )|,which contradicts the choice of F and F ′ and concludes the proof of the lower bound.
vertices contained in u∈V (F )\ v B u are called primal vertices and the vertices contained in i∈[k] (B v i × {1, . . ., z i }) are called cloned vertices.Two vertices x and y of G(G, F, c, v, z) are adjacent if and only if • x and y are primal vertices, and {x, y} ∈ E(G), or • x is primal, y is a clone, and {x, π 1 (y)} ∈ E(G), or • x is a clone, y is primal, and {π 1 (x), y} ∈ E(G), or • x and y are clones, and {π 1