Generalized XOR non-locality games with graph description on a square lattice

We propose a family of non-locality unique games for 2 parties based on a square lattice on an arbitrary surface. We show that, due to structural similarities with error correction codes of Kitaev for fault tolerant quantum computation, the games have classical values computable in polynomial time for $d=2$ measurement outcomes. By representing games in their graph form, for arbitrary $d$ and underlying surface we provide their classification into equivalence classes with respect to relabeling of measurement outcomes, for a selected set of permutations which define the winning conditions. A case study of games with periodic boundary conditions is presented in order to verify their impact on classical and quantum values of the family of games. It suggests that quantum values suffer independently from presence of different winning conditions that can be imposed due to periodicity, as long as no local restrictions are in place.


I. INTRODUCTION
The fact that physical theories simultaneously aim at explaining phenomena already observed, and need to be, at least in principle, experimentally falsifiable, puts measurement process in the centre of attention. Statistics of single measurement outcomes, and the nature of correlations between them, depends on the physical theory describing the process of generating measurement outcomes.
It cannot be a surprise that access to systems exhibiting richer statistics of measurement outcomes enables one to improve performance in different tasks related to information processing. For example, the non-existence of a hidden-variable model for observed measurement outcomes, attested by violation CHSH inequality, is a sufficient condition for a string of outcomes to be intrinsically random, i.e. to exhibit randomness that cannot be explained by our lack of knowledge about the system [1]. Intrinsic randomness of quantum correlations can be used for secure establishment of a cryptographic key [2], even in a situation when one relies solely on the measurement statistics, without a need to trust that a system has been prepared, and measurement performed in a specific way [3]. Furthermore, a gap of effectiveness for usage of quantum and classical resources is present in communication complexity tasks [4].
The qualitative difference between classical and quantum systems, through the notion of Bell non-locality [5], can be quantified in two equivalent frameworks: Bell inequalities and non-locality games. In the game setting, the correlators present in the Bell inequality may be assigned some desired values, and the task for the physical system may be set to satisfy all the given constraints with the highest probability, with respect to a previously known probability distribution of measurement settings. This is interpreted as questions to parties of the physical system. There exist games with optimal classical and quantum strategies proven to yield different values of winning probability. The most famous example is the game associated with the CHSH Bell inequality.
Taking this into account, it is of vital importance to calculate quantum and classical values achievable for a given non-locality game. In this paper, we propose a family of non-locality unique games for two parties, defined on a square lattice on an arbitrary surface. The games show a lot of similarities with error correction codes proposed for fault tolerant quantum computation by Kiteav [6]. We use them to calculate their classical values in a polynomial time (while it is an NP-hard problem in general), for number of measurement outcomes d = 2. Furthermore, due to these geometrical properties we are also able to establish a classification of games into equivalence classes with respect to local relabeling of outcomes of measurements, and to study the role which periodic boundary conditions can play in setting of classical and quantum values of these games. We assume that the periodic lattice has even number of cells in order to allow for bipartite structure needed for non-locality game; otherwise, the construction can be used to define a contextuality game. We also point out important differences between Kitaev codes and our games.
The above is achieved through representation of nonlocality games in a graph form. The graph description of non-locality games, its basic properties, as well as general notation and calculation of games classical and quantum values, are introduced in Section II. Sections III, IV, V are devoted to classification of games for gradually incising number of possible measurement outcomes, provided that the winning conditions are described by a specific group of permutations. Section VI contains a description of the procedure for calculation of classical values for d = 2, and discussion of possible extensions to higher dimensions, one being a generalization of the method for d = 2, while the second one based on an unique representation of each game with respect to a chosen maximal spanning tree. Section VII is devoted to analytic and numerical studies of the role which periodic boundary conditions can play in setting classical and quantum values for the family of games. We conclude with Section VIII.

II. PRELIMINARIES
A. Non-locality games for 2 parties The setting of the game is the following: a referee asks a question x from the set A = {A 1 , . . . , A |A| } to one part of the spatially separated system, Alice, and a question y ∈ B = {1, . . . , B |B| } to another party, Bob. Then Alice and Bob return answers a ∈ {0, . . . , d − 1} and b ∈ {0, . . . , d − 1}, respectively. Case of different number of possible answers for Alice and Bob can also be described in this way, with a subset of answers not used by one party. The parties cannot communicate after receiving the questions, and they return answers such that the probability of winning the game is maximized, with uniform probability of different pairs of questions to appear. The winning conditions (i.e., a set of accepted answers for given questions), are known to both parties. The choice of answers can be based on a pre-established classical strategy (then the maximal winning probability is called a classical value of the game ) or outcomes of measurements on a shared quantum state (with maximal winning probability called quantum value of the game).
The dimension of underlying quantum systems and measurement operators can be in principle arbitrary. This makes it calculating the quantum value difficult. It is still not known if this is possible for an arbitrary game, although semidefinite programs can be used to compute upper bounds [7]. Therefore, one is interested in a special class of non-locality games, called unique games. They are defined by the property that for every pair of questions and an answer by Alice: (x, y, a), there exists exactly one answer b that satisfies the winning condition of the game. In other words, for each pair of questions the constraint which the winning answers must satisfy is defined by pre-established function π : A A → A B . The players win iff b = π(a). For this class of games, it has been showed that the quantum value can be approximated to a constant factor in polynomial time [8]. Furthermore, in the scenario of XOR game, where A A = A B = 2 and winning conditions depend solely on a ⊕ b for a given (x, y), the quantum value can be computed exactly in polynomial time [9,10] due to Tsirelson theorem [11].
On the other hand, calculating the classical value of a non-locality game is a vertex labeling problem, and as such, for every positive constant δ, there is always A A = A B high enough such that it is NP-hard even to decide whether a given game has a classical strategy satisfying all winning conditions, or there is no strategy satisfying more than δ fraction of the winning conditions [12][13][14]. If the Unique Games Conjecture [15] is true, then the above applies as well to unique non-locality games, with the modification that the task is to distinguish between existence of a strategy satisfying almost all winning conditions, and a non-existence of strategy satisfying more than a small fraction of winning conditions. For unique games in general, and XOR games in particular, it is known that calculating their exact classical values is an NP-hard problem [16].
In this paper we propose a class of two party nonlocality unique games for arbitrary local dimensions d = A A = A B . This class is defined on bipartite graphs, with vertices of the graph corresponding to questions asked by the referee, and edges labeled by permutations between measurement outcomes that satisfy the winning condition.

B. Graph description
Below we introduce basic notions associated with graph representation of non-locality games. In this representation, the vertices of a graph correspond to questions asked by the referee (measurements). Two vertices are connected by an edge iff both corresponding measurements can be performed simultaneously. A function K : E(G) → S d assigns to each edge a permutation of the set of d elements. These permutations represent the desired correlations between measurement outcomes. If the graph is connected and bipartite, then each of the two independent sets of vertices corresponds to measurements performed by a distinct party. Then the labeled graph directly corresponds to a non-locality game. The classification provided in further chapters applies to generalized XOR games, which have been investigated in detail in [17]. XOR games are characterized by binary measurement outcomes (A A = A B = 2) with correlations/anticorrelations demanded between outcomes of selected parties. In the generalized version of a XOR game, we allow for A A = A B = d possible outcomes from the set {0, ..., d − 1}.
Constraints on the edges connecting vertices u and v are defined by permutations S n of the set {0, ..., n − 1}. We will focus on games in which these permutations belong to the set L n = {π i :π i (x) = i − x mod n} or L n = {σ i :σ i (x) = x + i mod n}. A graph together with an edge labeling will be referred to as a labeled graph. If all permutations assigned to the edges are equal to their inverse, we will talk about an undirected labeled graph. In this paper, we will be interested in connected bipartite graphs defined on a cubit lattice, but many of the results presented here do not depend on the type of the connected bipartite graph.
We will start with characterization of amount of classicality for a XOR game d = 2. For this we will use a notion of consistency. An assignment k : V (G) → {0, . . . , d − 1} of measurement outcome values to vertices is consistent if it has no contradiction on any edge of the graph, i.e. for every edge uv the relation between outcomes on its vertices is given by a permutation labeling this edge, k(v) = π(k(u)). A connected labeled graph can have no more than d consistent assignments, as assigning a value to one vertex determines the values of all its neighbors. We will say that a labeled graph (or its subgraph) is good if it has d consistent vertex-assignments and bad if no assignment is consistent. If the number of consistent assignments is larger than 0 but less than d, we say that the graph is ugly (see Fig. 1). Every consistent assignment defines a deterministic strategy for a given game, which allows the players to win with probability 1. If a labeled graph has no consistent assignment, then no such strategy exists for the game. Thus, good and ugly graphs will describe games that can be won by strategies in which a state of the system is purely classical, and proper measurements just reveal the properly correlated values, whereas in games represented by bad graphs one can expect that quantum strategies may outperform classical ones.

C. Equivalence of labeled graphs
The notion of equivalence between two games has to be properly defined in the language of their graph representation. We will say that two games are equivalent iff the corresponding labeled graphs are equivalent. We say that two labeled graphs are equivalent iff one can be obtained from the other through: 1. an isomorphism of the underlying graphs 2. changing the direction of an edge and replacing the permutation on this edge with its inverse 3. switching operations s(v, σ), which changes the labels on all edges incident with the vertex v as follows: Each of the above operations can be interpreted as renaming the inputs and/or outputs. It follows that equivalent games have equal classical and quantum winning probabilities.
In this paper, however, we will largely focus on the equivalence between different labelings on the same graph. We say that two labelings of a graph are equivalent iff one can be obtained from the other through switches. It is clear that any two games defined on the same graph with equivalent labelings must be equivalent. For games represented by a labeled graph on a planar grid, their classification will depend only on a local structure of the graph. Speaking more precisely, the equivalence of two labelings of such a graph will be determined by sets and type of cycles defined on cells of the grid. By a cell we mean here a cycle that does not contain any other cycles (In a square lattice, this is a cycle with four edges). A bad cell will be referred to as a defect.
For grids on surfaces other than the plane (eg. a torus), in order to classify corresponding non-locality games we will have to take into account cycles arising from the topological structure. This is similar to the way in which classes of homology of error paths have to be taken into account in topological error correction codes in order to describe a logical state of a code, and we will comment on observed similarities and differences between the two.
The group of permutations of two elements does not have any non-trivial subgroups and consists only of identity and transposition operations: S 2 = {Id, (01)}. This group is the example of a permutation group defined by L d , that, along the group L d , will be subjected to a more detailed analysis for d > 2 in the following chapters. Proofs of theorems will be based on a concept of a canonical representation of a graph, used in [18] to prove equivalent statements for signed graphs (which are functionally identical to labeled graphs with d = 2 outcomes). Later, we generalize this line of reasoning to the case of higher d.
A spanning tree of a graph is a subgraph containing all of its vertices and some of the edges such that there is exactly one path connecting each two vertices of the graph. We use this to define the canonical representation of a game. The canonical representation of a game (G, K) with respect to the spanning tree T is a game on the same graph with a labeling equivalent to K such that the I permutation is assigned to all edges of T . This is similar to a concept introduced in [18] for signed graphs. Later, we shall use a generalized version for graphs labeled with S d for an arbitrary d. It is clear from the definitions that one can always obtain a canonical representation of any game through switches (see Fig. 3). For d = 2, the equivalence classes of graphs are uniquely determined by their canonical representation: Proposition 1. Two labelings of the same graph with d = 2 outcomes are equivalent iff the corresponding games have the same canonical representation. The canonical representation can be defined with respect to any spanning tree. Before we move to the proof, let us stress that Proposition 1 is valid for games defined on surfaces with and without boundary conditions, as in both these cases it is possible to transform a game to its canonical form.
Proof. (⇐ part). The same canonical representation of two games implies that one can be transformed into the other by performing switches that bring one of them to the common canonical form, and then transform the canonical form into the other game. Therefore, the games are equivalent.
(⇒ part). It follows from the fact that, for d = 2, a game has only one canonical representation with respect to a given spanning tree. To see this, let us notice that S 2 = L 2 = L 2 , and show how the structure of labelings changes due to local permutations. Let − → uv be an edge originally labeled with a permutation σ (σ(u) = v, where we abuse the notation and denote by u, v labelings of the vertices), and we apply a switch, then σ changes to πσ for the switch s(v, π), and to σπ −1 for s(u, π) (see Fig.  4). According to this rule, any switch applied on one vertex belonging to an edge outside the spanning tree and aimed at changing the permutation assigned to this edge, would have to be accompanied by an inverse transformation on neighboring vertices that are connected through the spanning tree. But the inverse of (01) is (01). Therefore, we have to apply the same permutation on every other vertex of the spanning tree when we construct the new canonical representation of the game, so that the Id permutations on the spanning tree remain unaffected. But this would imply performing a switch on both ends of every edge not in the spanning tree, and as permutations belonging to S 2 commute, we have (01)σ(01) −1 = σ, and permutations σ ∈ {Id, (01)} assigned to all such edges remain unchanged. Thus the canonical representation will remain the same.
It follows that two labelings of a graph with S 2 are equivalent if and only if they have a shared canonical representation with respect to an arbitrary spanning tree.
Notice that the first part of the proof does not depend on the number of outputs. Hence we have the following result.
Corollary 2. If two games for any d have the same canonical representations (on an arbitrarily selected spanning tree), then they are equivalent.
For d = 2, the following holds as well Theorem 1. Two labelings of a graph with S 2 are equivalent iff they have the same set of bad cycles.
Note that the above is the definition of equivalence for signed graphs in [19]. In the simple case of d = 2, every bad cycle contains odd number of transpositions.
(⇐ part). It follows from Corollary 2 that if two labelings are not equivalent, then their canonical representations with respect to the same spanning tree are different. Different canonical representations imply different sets of bad cycles, because one can always find a differentiating cycle. Let e be an edge which differs between the two canonical representations. Any cycle consisting of e and some edges of the spanning tree is good in one of the games and bad in the other (see Fig. 5).
Bad cycles in a grid can have two origins -they can arise as a result of the existence of defects, or can have a non-local character, i.e. are not a function of defects in the graph. These two origins have distinctive implications in the classification of games, therefore we will present this classification separately for planar games and games with on surfaces other than the plane. A. Planar graphs for d = 2 We will show that for a planar graph for d = 2 all bad cycles arise from defects. Let us note that for d = 2 the cycles can have only two classes: good and bad. A good cycle is a cycle with defect class I (or 0) and a bad cycle is a cycle with defect class (01) (or 1).
Proposition 15 provides an easy way of finding the defect class of a larger cycle based on the classes of the cells contained within. For a labeling with d = 2 outcomes, the class of every cell is either 0 or 1, which implies the following.
Corollary 3. For a planar graph labeled with S 2 , the cycle is bad if it contains an even number of bad cells, otherwise it is good.
By Theorem 1 we know that for d = 2 two labelings are equivalent iff they have the same set of bad cycles. From Proposition 15 we see that for a planar graph the set of bad cycles is uniquely associated with the set of defects. Therefore, we have Corollary 4. Two labelings of a planar graph with S 2 are equivalent iff they have the same set of defects.

FIG. 7:
A network with cyclic boundary conditions on left/right and bottom/up boundaries, and a graph for d = 2. σ1, σ2 -bad cycles stemming from defects. Bad cycles of red permutations along green and blue lines -two cycles characterizing the same equivalence class of the game (transition between the cycles can be performed throughσ1 switches applied to vertices denoted by 'X'). A bad cycle along the purple line, characterizing different equivalence class.
plane, then bad cycles can arise due to lines of (01) permutations in the dual lattice, that give rise to no defects (see Fig. 7). Fig. 8 shows how the periodicity of boundary conditions creates opportunity for non-local paths of errors to arise -they constitute paths from the center to the exterior of the continuously deformed dual graph.
The effect of taking into account periodic boundary conditions is depicted in Fig. 9. If there are no boundary conditions, the edges of the graph can be divided into a spanning tree (brown) and a remaining set of black edges. Corollary 4 states that two games on a plane for d = 2 are equivalent, iff they have the same set of defects. If the graph is driven to a canonical representation with respect to the spanning tree depicted, defects are determined only by the black edges.
However, introducing a periodic boundary condition (in one or more possible directions) implies an addition of a column/row of additional edges. Naturally, labelings of these new edges will potentially give rise to new defects. Furthermore, as these new edges bypass the spanning tree of the graph, they can all be labeled by (01) permutations without creating a single defect. Such chains of permutations cannot be removed or contracted to a point by switches. We will refer to them as loops.
Pairs of defects are also typically connected by similar chains of (01) permutations. We can think of those chains of permutations as paths and cycles in the dual lattice. They can be seen as an analogue of paths describing logical operators in a Kitaev code on a torus (see Fig. 10). However, in the case of the planar grid there is a notable difference between the game and the code defined by the same labeled graph. In Kitaev codes it is not possible to remove a path of errors on the sharp boundary of the code (because the removal here can be performed by application of stabilizer operators, and there are no stabilizer operators that act on a single qubit as in this setting this would violate the demand for the stabilizers to commute FIG. 8: Periodicity of boundary conditions (a) enables continuous transformation of a game to a form (b), (d). Both red and blue sets of (01) permutations lead to no defects. While blue permutations can be erased by applying switches on vertices labeled with X (b), which contracts the associated path in a dual lattice to a point (c), red permutations remain joining the center of the dual lattice with its exterior (e) -applying a selected choice of switches (d) gives rise to an orange line (e), with the same end points as the initial, red line.
FIG. 9: Due to boundary conditions applied along directions marked by yellow/green arrows, the graph structure can be divided into the spanning tree T (brown), black edges within the extended tree T L and yellow/green edges joining the boundaries and bypassing the spanning tree. Cells with black and brown edges contribute to the equivalence class of the graph by experiencing defects. Defects can be obtained on cells with yellow/green edges as well, but furthermore yellow/green edges can be collectively labeled by (01) permutations without creating a defect, despite the fact that such a labeling is not equivalent to labeling all edges with I. with each other). On the other hand, on the graph associated with a d = 2 non-local game it is possible to remove such a path. The possibility of destroying a path connecting two boundaries without periodicity condition is already a result of the existence of a spanning tree that joins all the boundaries without periodic conditions (cf. Fig. 9). Let us focus on equivalence conditions for two nonplanar games. Each of the games can be characterized by a set of (01) permutations labeling the edges. These correspond to a set of paths in the dual lattice and we will call them error paths, for the analogy with quantum error correction codes. We will prove the following: Theorem 2. Two labelings K 1 , K 2 of a graph are equivalent iff the labeling K defined as K(e) = K 1 (e)K 2 (e) −1 for all edges is equivalent to K I defined as K I (e) = I for all e.
Proof. Let K(e) = K 1 (e)K 2 (e) −1 be equivalent to K I . By the definition of equivalent labelings, one can transform K into K I = KK −1 through switches. If the same switches are applied to K 1 , it is transformed into Now assume that the labelings are equivalent. Then K 2 can be obtained from K 1 through switches. Applying the same set of switches to the labeling K transforms it into K I .
An easy way to check that the labeling K is equivalent to K I is the following.
Observation 5. Let P be the set of error paths of a labeling K with S 2 . The labeling is equivalent to K I iff the number of loops in each homology class is even and all other error paths are contractible to a point.
Proof. A pair of loops within the same homology class can be annihilated through switches by moving one onto the other. Obviously, an error path is considered contractible if and only if it can be removed by switches.
On the other hand, an unpaired loop cannot be removed by switches and thus a labeling with an odd number of loops in some homology class is not equivalent to K I .
From the above we see that a necessary condition for two games two be equivalent is that they have the same set of defects. Otherwise, error paths in P will be unconctractible to a point. The remaining mechanism to generate paths in P that are uncontractible is solely associated with the topology of the surface on which the graph is defined. Namely, a lattice (e.g. on a cylinder or a torus) can allow for construction of paths that cannot be contracted to a point by a continuous transformation -a set of paths with this property, that can be transformed into each other, is called a homology class. Therefore, games with error paths belonging to different homology classes are not equivalent. From the definition of equivalence of labelings we see that, as local transformations do not change homology class of the paths, nor the position of defects, they have to be the same for games belonging to the same equivalence class. Therefore Proposition 6. Two labelings of a K 1 , K 2 of a grid are equivalent iff they have the same set of defects and the labeling K = K 1 K −1 2 contains no loops which cannot be annihilated by switches.
The role of the topological properties of the surface on which the graph is defined in the equivalence between two games is based on the fact that switches which transform equivalent graphs into each other can deform error paths only in a continuous manner. Therefore, two equivalent games cannot have different sets of loops within each of the homology classes admitted by the geometry. This property does not depend on d, and therefore will be crucial for classification of games for higher number outcomes.
For games with d = 3 possible measurement outcomes (and the same number of different correlations that can be demanded for outcomes of a pair of measurements), the group S 3 can be generated by Before going to classification of these games, we prove some useful statements about graphs labeled by permutations from L d and L d . Proof. Because the graph is bipartite, we can divide its vertices into two disjoint sets. Applying switches with permutations from L d to vertices from one of these sets will transform permutations from L d (L d ) that label edges into permutations from L d (L d ).
For games that can be described by labeled graphs on square lattices, we take into account situations where all edges are labeled with permutations from L d , or equivalently from L d . This implies that the defect class of each cell is a permutation from L d . Nevertheless, many of the results can be easily generalized to different types of lattices or to all planar graphs. Furthermore, Observation 7 implies that the results can also be (indirectly) applied to graphs labeled with L d .
In the case of d > 2, because the size of the group L d is larger than L 2 , each cell can have one of d different defect classes. It follows from the proof of Proposition 15 that two graphs labeled by L d have the same sets of defects of each class then they have the same sets of cycles of each class.
The lemmas 16 and 17 from the Appendix applied to d = 3 are useful to derive the theorem for equivalence of games with correlations defined by permutations from L 3 or L 3 .
Theorem 3. Two labelings with L 3 are equivalent iff either the defect class of each cell is the same for both labelngs or the set of defects of class x for one labeling is the set of defects of class −x for the other labeling, for every x ∈ {0, 1, 2}.
Proof. (⇒ part). Two labelings are equivalent if we can obtain one from the other through switches. Select a cell of defect class x and let v 0 denote the starting vertex of the characteristic permutation. We apply a switch s(v, π): 1. If v = v 0 , then the defect class of the cell remains unchanged, due to Lemma 16 and Lemma 17.
2. If v = v 0 and π =σ i ∈ L 3 , then the class of the cell remains unchanged, due to Lemma 16.
3. If v = v 0 and π =π i ∈ L 3 , then the defect class of the cell after the switch is changed into −x, due to Lemma 17.
Since S 3 = L 3 ∪L 3 , a switch can only change the defect class of a cell from x to −x.
If a switch by π ∈ L 3 is applied to some vertex v of G, we obtain a labeling in which some edges are labeled with L 3 . In order to return to L 3 we must switch all vertices adjacent to v by some permutation π ∈ L 3 . This shows that if we switch a vertex by some π ∈ L n , we must also apply such switches to all vertices of the graph. Hence, the defect class of every cell is changed from x to −x. Otherwise we only switch by permutations σ ∈ L 3 and and the classes of all cells remain unchanged.
Proof. (⇐ part) Same as in the proof of 4, which can be found in the Appendix. In Theorem 4 we present a generalization of Theorem 3 to a graph with an arbitrary number of outcomes and labeled with L d .
Theorem 4. Two labelings K, L of a connected planar graph with permutations from L d are equivalent iff there exists a permutation π ∈ S d such that for every cell c of a graph we have cl(c, L) = πcl(c, K)π −1 , where cl(c, L) is a defect class of a cell c in labeling L.
The proof of this theorem can be found in the Appendix.
From the above we see that any transformation which connects two equivalent labelings with L d has to be composed from switches s(v, σ 1 (v)πσ 2 (v)), such that σ 1 , σ 2 ∈ L d and may depend on the vertex v, while π is the same for all vertices and in general does not have to belong to L d .
In Section III B we provided conditions for equivalence of labelings with L 2 on different surfaces. Here we generalise these results to arbitrary d. A switch s(v,σ i ∈ L d ) on an arbitrary vertex of the network continously deforms the labelings, in a sense that, in a convention in which permutations on the adjacent edges point from the vertex the the exterior, every permutationσ x on an edge is shifted:σ x →σ iσx =σ i+x . Let us define an operation of adding labelings L, K from L d of a given graph G = (V, E). The sum M = K + L is a labeling in which M (e) = K(e)L(e) for every e ∈ E. The difference K − L will denote the sum K + L −1 .
Lemma 8. Let K s : E → L d be a labeling obtained from K I through a single switch s(v, π). Then the labeling M = K + K s is equivalent to K : E → L d and adding K s to a labeling has the same effect as performing the switch s(v, π).
Proof. The labeling K s assigns I to all edges not incident with the vertex v, π to all edges vu coming out of v an π −1 to all edges uv going into v. It follows that Since the permutations in L d commute, K(e)π −1 = π −1 K(e) and thus M is the labeling obtained from K through the switch s(v, π).
From this, we have the following. Corollary 9. Let K 1 , K 2 : E → L n be two labelings of a graph. K 2 can be obtained from K I through switching by permutations from L d iff K 1 + K 2 is equivalent to K 1 .
Therefore, we arrive with the following equivalence criterion for all planar games with permutations from L d for arbitrary d: Theorem 5. Let K 1 and K 2 be two labelings of a graph with L d . The labelings are equivalent iff there exists a labeling M and permutation π such that K 1 = π(K 2 )+M, where M is equivalent to K I and and π(K) denotes the labeling obtained from K by applying the switches s(v, π) to all vertices v ∈ V.
Proof. (⇒) Let K 1 and K 2 be two equivalent labelings. It follows from Corollary 18 that there exists a permutation π such that K 1 and π(K 2 ) have the same set of defects of each class. We can now transform π(K 2 ) into K 1 using only switches from L d . Thus, by Corollary 9, we have K 1 = π(K 2 ) + M for some labeling M equivalent to K I .
(⇐) Now assume that K 1 = π(K 2 ) + M, where M is a labeling obtained from K I through a switches with permutations from L d . This means that π(K 2 ) can be transformed into K 1 through the same set of switches. Thus, π(K 2 ) is equivalent to K 1 . Since π(K 2 ) is equivalent to K 2 , it means that K 1 and K 2 are equivalent.
From the above we can obtain yet another equivalence condition, analogous to the one given in Theorem 2 for labelings with d = 2.
Corollary 10. Two labelings K 1 , K 2 : E(G) → L d are equivalent iff, there exists a permutation π ∈ S d such that the labeling K = K 1 − π(K 2 ) is equivalent to K I .
In order to characterize equivalence classes of labelings with L d permutations on a surface of genus 2 or more, we make use of graph topological properties. First we define the enlarged spanning tree T L as follows: Definition 1. For a graph G, let us enlarge a set of edges T 0 forming a spanning tree T (G) by adding an edge which belongs to a cell such that this edge is the only member of the cell not belonging to T . Update the set T 0 with this enlarged set. The above procedure is to be applied unless there is no cell that contains only one edge not belonging to T 0 . Such a set: T L = T 0 will be called the enlarged spanning tree of T .
Note that if the graph G is planar, then T L = G. On a torus/surface with a higher genus some edges of the graph remain outside of T L . Lemma 11. The complement G − T L of the enlarged spanning tree T L is composed of all edges of a graph that do not belong to T and belong to some nontrivial loops. G − T L contains exactly one nontrivial loop in each homology class.
As an analogue to a notion of the class of a cell, we define the class of a loop defined on the complement of T L to be equal to the index of a permutation from the setσ 0 ,σ 1 ,σ 2 , . . . ,σ d−1 that labels the edges belonging to the loop. Theorem 6. Equivalence class of a game of permutations from L d , defined on a graph with a spanning tree T on a surface with genus g, is completely characterized by: • sets of cells of classes cl = 1, 2, . . . , d − 1, • sets of loops on the complement of T L of classes cl = 1, 2, . . . , d − 1, where all the above sets are described modulo a chosen permutation π ∈ S d and π / ∈ L d that maps permutations from L d into L d . Such permutation maps defect classes and permutations on loops belonging to the complement of T L in the same way: cl → πclπ −1 ,σ i → πσ i π −1 . Number of the non-equivalent loops of permutations is equal to d 2g .
In the proof of the above Theorem we will use the fact that canonical form of a L d game associated with a given spanning tree T depends on permutations that belong to T L , but not to T (they are fixed, up to permutation π applied to every vertex, by classes of defects), as well as by permutations that belong to the complement of T L (they are partially fixed, up to permutation π applied to every vertex, by classes of defects, but also provide some additional characteristic of a game arising from periodic boundary conditions).

VI. CLASSICAL VALUES FROM DEFECTS AND LOOPS
In this section we discuss some methods for calculating the classical value of a game based on the properties of the corresponding labeled graph. On planar graphs, the classical value is given by error correction algorithm in Kitaev code. A modification of the algorithm is necessary when periodic boundary conditions are enforced. Since the sets of defects and loops of each class are enough to define a game, one could use these properties to calculate its classical and quantum values.
The values are winning probabilities given by the formula p win = max V (ab|xy)p(ab|xy).
(2) Above, V (ab|xy) is the winning condition, taking value 1 iff π xy (a) = b, where π xy is a permutation between the responses for a pair of questions, and fixed by the unique game, p(ab|xy) is the conditional probability of obtaining a pair of answers (a, b) given questions (x, y), and p(x, y) is the distribution of questions. Below, we take it uniform. Maximization is performed over families of conditional probabilities. In classical case, the optimization can be performed over deteministic local hidden variable models, i.e. where we have b p(ab|xy) = p(a|x) for every x and y, and that p(a |x) = 1 for a selected response a [20,21] (the same applies to Bob responses). Therefore for the games investigated in this paper, classical values can be calculated through search over all possible classical assignments of values [0, . . . , d − 1] to nodes of the corresponding graph. In a case of optimal labeling, the classical value is going to be p win,cl = |E(G)|−β C |E(G)| , where |E(G)| is the number of pairs of questions (number of edges of the corresponding graph), and β C is the number of contradictions, i.e. number of winning conditions π xy that are not satisfied by the optimal labeling.
Here we show that in the case of d = 2 outputs the algorithm for calculating the classical value can be efficient. The method is similar to ones used in error correction and relies on connecting pairs of defects with the shortest possible paths in the dual lattice to minimize the number of contradictions. We also consider the case with 3 or more outputs and propose an analogous algorithm. In this case defects may have to be gathered into sets of more than two and connected with minimum Steiner trees in the dual lattice instead of shortest paths. A Steiner tree for a given set S, as defined in [22], is a tree which connects all vertices in S. Figure 11 provides examples of such paths and trees.
First notice that the labeling K I : E → L d which assigns I to all edges of the graph gives rise to no contradictions. In fact, every labeling with no contradiction is equivalent to K I . We can use this fact to prove the following lemma.
Lemma 12. For every labeling K of a graph G there exists an equivalent labeling K o such that K o (e) = I for exactly β C (G, K) edges. No labeling equivalent to K assigns I to more than |E(G)| − β C (G, K) edges.
Proof. Notice that a labeling K has no contradictions if and only if it is equivalent to K I . It follows that β C (G, K) = k if and only if changing the labels of a certain set S of k edges results in a labeling K equivalent to K I . The labeling K I can be obtained from K through a specific set of switches. Applying the same set of switches to K results in a labeling K o which assigns I to an edge e if and only if e / ∈ S. Obviously any labeling in which identity is assigned to more than |E(G)| − β C (G, K) edges has fewer than β C (G, K) contradictions and thus it is not equivalent to K.
We will call K o an optimal labeling and many of our methods will involve finding an optimal labeling for a given configuration of defects and loops.
The simplest case is a torus (or higher genus surface) with no defects. In this case for each loop of length k the graph contains a set of k disjoint bad cycles, each containing one edge of the loop. Even if two loops intersect, the cycles associated with those loops are still edge-disjoint. Thus, the contradiction number is the total length of all loops. This does not depend on the number of outputs or the classes of the loops. Now we consider a game with d = 2 outputs and no loops. In this type of game optimal labelings are defined by sets of paths in the dual lattice G * such that a) each path connects two defects and b) the total number of edges in the set of paths is as small as possible. Such a set is equivalent to a perfect matching with maximum weight in a weighted graph G defined as follows.
1. G is a complete graph and V (G ) is the set of defects of (G, K).

The weight of an edge
is the length of a shortest path in the dual lattice connecting the defects u and v.
In short, the method consists of the following steps: 1. Find the distance (i.e. the length of the shortest path) between each pair of defects; 2. Choose the pairs so that: a) every defect has a pair and b) the total length of the paths is minimized.
Finding a maximum weight perfect matching is a well studied problem and a polynomial time algorithm for solving it can be found in [23]. It is a generalization of a method described in [24] and [25]. The method relies on Berge's lemma [26], which allows us to increase the size of a non-maximum matching using augmenting paths, as well as a method known as blossom shrinking. A blossom is defined as an odd cycle in G with a maximum matching. Shrinking all blossoms in the graph, i.e. replacing them with single vertices whose neighborhood is the same as the neighborhood of the blossom, allows us to apply the algorithm for bipartite graphs to all graphs.
In the case of labelings on a non-planar grid, where loops are a possibility, some additional steps are needed to determine the classical value. From the above method we obtain a minimal labeling M with the smallest set of non-identity edges possible for a given configuration of defects. However, it is still possible that M is not equivalent to the original labeling K. In this case, we have K − M = L, where L is a labeling with no defects, but containing one or more non-trivial loops. Thus, to find an optimal labeling equivalent to K, we must add the necessary loops to M. In order to ensure that the resulting labeling is indeed optimal, we must add a set S of loops in such a way that: This way we obtain a labeling K o , which is equivalent to K. We now show that this labeling is indeed optimal.
Suppose some labeling K o is equivalent to K and has fewer non-identity edges than K o . Such a labeling is obviously also equivalent to K o and, as such, differs from A generalization of the above method can most likely be used for d ≥ 3. In this case the defects are not paired, as they were for d = 2, but grouped into sets of up to d elements such that the classes of defects in each set add up to 0 mod d. Here the steps are: 1. Find all minimal sets of defects which add up to 0 mod d; 2. For every such set, calculate the minimum number of edges in a tree connecting them in the dual lattice; 3. Choose the sets so that: a) every defect is in exactly one set and b) the total length of the trees is minimized.
Note that the above generalization resembles generalization of decoding algorithm for qubit surface codes into qudit systems [27]. However, first step of our algorithm is applied globally to the whole structure of the graph, in constrast with grouping defects into local clusters of increasing size, which in [27] is performed in spirit of renormalization group approach [28]. The final step of the algorithm is equivalent to finding a maximum weight perfect matching in the weighted hypergraph G defined as follows:

The weight of an edge is
where St(S) is the minimum length of a Steiner tree for the set S and T (G * ) is the number of edges in the spanning tree of the dual lattice.
By a perfect matching in a hypergraph we mean a set M of edges such that each vertex belongs to exactly one edge in M . One problem with such a generalization is properly defining the hypergraph equivalent of a blossom. The problem of Steiner trees is also NP-hard in general, which may increase the complexity of the algorithm for large numbers of outputs. Nevertheless, an interesting implication is the fact that the hypergraph G is guaranteed to have a perfect matching.

C. Spanning tree approach
Another approach, which may be better for games with a large number of outputs, relies on the following result.
Lemma 13. Every optimal labeling K o of a grid G is a canonical representation for some spanning tree of G.
Proof. Let K o be an optimal labeling of a grid G. First, remove any edges from G which define a loop in K o . Next, remove an arbitrary set of edges which defines a loop in each direction in which there is no loop in K o . The remaining graph H is a planar grid.
The set S of edges e ∈ E(H) such that K o (e) = I corresponds to a certain set S * of edges in the dual lattice H * of H. The set S * is a subset of the edge set of a spanning tree T of H * . If we remove all edges in H corresponding to edges of T , what remains is a spanning tree T 1 of H such that K o assigns I to all of its edges. Since any spanning tree of H is also a spanning tree of G, It follows that K o is the canonical representation with respect to the spanning tree T 1 .
It is easy to see that a game has only one canonical representation for any given spanning tree. This representation is easy to obtain from the tree itself using the tree enlarging procedure from section V. Unfortunately, not every canonical representation is an optimal labeling for a given set of defects and loops. Therefore to find the optimal labeling we need to compare canonical representations with respect to different spanning trees. The number of spanning trees grows exponentially with respect to the number of vertices in the grid, so this method appears to be NP-hard. However, unlike the algorithm based on matchings, its complexity does not depend on the number of outputs.

VII. EXAMPLES
Below we show how local and non-local conditions, associated with topology of a given game, can influence maximal probabilities of winning the games, with classical and quantum resources provided. Classical values can be calculated exactly using methods described in the previous section. In the quantum case, the optimization in (2) is performed over all states |Ψ in a Hilbert space and sets of ortogonal projective measurements on Alice and Bob subsystems such that p(ab|xy) = Ψ|M a x M B y |Ψ , and [M a x , M b y ] = 0. Due to the fact that dimension of Hilbert space is not fixed, every POVM measurement can be described in the above form. The task of finding the optimal protocol saturating the quantum value is equivalent to finding a Bell operator S = a,b,x,y V (ab|xy)M a x M b y with maximal norm. Upper bound on a quantum value can be found by solving a semidefinite problem resulting from relaxing the conditions for set of quantum probabilities aimed at satisfying restrictions of a particular game. An infinite series of these relaxations, in a form of the so called NPA hierarchy [29] of semidefinite programs, was shown to give values converging to quantum value as defined above. Here, we will calculate the upper bound Q ↑ on quantum values by solving first level problems of the hierarchy, i.e. by optimizing (2) under the restriction that the matrix Γ with entries

y is composed of single orthogonal projective operators (and Identity).
A lower bound Q ↓ is calculated on the basis of the so-called see-saw algorithm [30]. The algorithm, for a given dimension of Hilbert space, is based on a sequence of semidefinite programs that aim at looking for an optimal Bell operator S based on linear structure of S with respect to measurement of Alice when measurements of Bobs are fixed (and vice-versa). It starts from randomly chosen measurement operators for Alice and Bob and an initial state shared between them, and then performs 3 steps: for given measurement operators, finds a state that leads to maximal winning probability; ii) optimizes measurement operators for Alice given quantum state and measurement operators for Bob; iii) optimizes measurement operators for Bob given quantum state and measurements operators for Alice. At each step, an SDP program is invoked to solve the optimization problem. For a given initial random configuration, this 3 step process is repeated 20 times, and a lower bound on quantum value presented here is calculated as maximum over 20 independent runs of the program.
While it is not even known if, for a fixed dimension of the Hilbert space, the see-saw algorithm allows for convergence into the global maximum, for a qubit case we obtain exact quantum values of the games, as both upper and lower bounds coincide. We found that the analytical upper bound on quantum value of a d-outcome XOR game [31] coincides with the one calculated by an SDP in the first level of the NPA hierarchy (for games with periodic boundary conditions), or exceeds it (for games without periodic boundary conditions). Therefore, its values are not reported.
We start with the problem of boundary conditions, for a XOR game (n=2). In Fig. 12 the grids a) and b) without boundary conditions are equivalent. Since a) is equivalent to a grid with identity assigned to all edges, it contains no contradictions. Thus, both classical and quantum winning probability for this graph is equal to 1.

No boundary conditions
FIG. 12: A 4×4 grid with and without periodic boundary conditions. Blue edges are associated with I permutations, while red edges are associated with a permutation (01). No defects present. k+1 homologically non-trivial bad cycles cross red edges (d).
On the torus a loop can occur which cannot be removed by switches. Hence the graphs in Fig. 12 c) and d) are not equivalent. In the case of a k × k torus with one loop and no defects β C = k, regardless of the number of outputs or the class of the loop. This is because the graph contains a set of k disjoint chordless bad cycles, one for each edge of the loop. These cycles behave like defects and cannot be removed by switches. Since the cycles are disjoint, one edge needs to be changed in order to make each cycle good. Hence, the classical winning probability for this game is p = |E|−β C |E| = 2k 2 −k 2k 2 = 2k−1 2k . In general, if we consider a torus with two possible loops of permutations σ x , σ y = Id (Fig. 13), we obtain games for which classical values do not depend on a particular type of non-trivial permutation, but solely on its presence (see Table I). For such games, an upper quantum bound calculated based on first level of the NPA hierarchy, and lower than 1 due to periodic boundary conditions, coincide with an analytic quantum bound introduced in [31]. However, for non-periodic boundaries the latter bound exceeds 1. Classical values, as well as both bounds on quantum values, show an additive behavior: introduction of a new path connecting the boundaries leads to their decrease by a value that does not depend on presence of other paths of permutations. For example, if by F (n, x, y) = C(n, x, y), Q ↑ (n, x, y), Q ↓ (n, x, y) we denote a function representing a classical value of a  Table I. Nodes corresponding to different questions Ai and Bj, i, j = 1, . . . , 8 are marked on the smaller graph.  2  2  3  3  4  4  5  5  6  6  7  7  8  8   1  1  2  2  3  3  4  4  5  5  6  6  7  7  8  8   Torus  No boundary conditions   1  1  2  2  3  3  4  4  5  5  6  6  7  7  8  8  9  9  10  10  11  11  12  12  13   1  1  2  2  3  3  4  4  5  5  6  6  7  7  8  8  9  9  10  10  11  11  12 12 13 FIG. 14: The grids from Fig. 12 depicted as bipartite graphs game or one of its quantum bounds, specified in Table I, we have 1 − F (n, 1, 1) = 1 − F (n, 1, 0) + 1 − F (n, 0, 1), which does not depend on the size of the lattice.
At the end, we show that the non-additive behavior of Q ↑ is present for games with periodic boundary conditions even when it comes to adding a loop of permutations to a game already hosting a nontrivial permutation on one of the edges (see Fig. 15b ). Table III shows  It is also visible from the above results that games that are equivalent according to Theorem 6 are characterized by the same bounds on quantum values. According to Theorem 6, all games with permutation types, both local and in form of non-contractible loops, modified by the same permutation π ∈ S d and π / ∈ L d , are equivalent.

VIII. DISCUSSION AND CONCLUSIONS
Similarity between properties of the presented family of non-locality games and Kitaev error correction codes is not complete: as was shown for a d = 2 case and a square lattice on a plane, a chain of anticorrelations in the dual lattice, which join two opposite boundaries, cannot be removed by local operations (i. e. by multiplying it by generators of stabilizer group), yet its analogue can be erased in the game setting by local relabelings of measurement outcomes.
Nevertheless, for d = 2 and in a case of non-periodic boundary conditions of the lattice, the same algorithm can be used for calculating most probable chain of errors and classical value, in the quantum error correction code and non-locality games, respectively. Periodic boundary conditions do not impose any significant challenges, nor in game classification, nor in the computation of classical value of a game, and the problems can be addressed with modification of the algorithm used for non-periodic scenario.
The non-contractible loops of permutations appear to have independent impacts on quantum values, as exact quantum values (for d = 2) and both bounds on quantum values (for d = 3) show additive behavior. This resembles commuting relation between logical operators acting in the codespace of the stabilizer error correction code, and enables us to conjecture that this is indeed property of a quantum set of correlations in the described setting. The non-additive behavior emerges only in presence of local features of the game -defects, which, in the associated error correction picture, correspond to excitation of a state of the system out of the codespace, and happening due to local errors.
Due to game constructions on a square lattice, for a fixed surface its classical value cannot go to zero in the asymptotic limit of the number of questions asked, and it is lower bounded by |T (G)| |E(G)| , with |E(G)| being number of edges of a graph representing the game, and |T (G)| number of edges belonging to its maximal spanning tree. Nevertheless, by constructing games with relatively short maximal spanning trees, i.e. by utilizing surfaces with high genus and small minimal lengths of curves within each homotopy class, one could try to minimize classical value, and obtain high quantum/classical gap in high d, provided quantum values increase with number of possible outcomes. of the project, and Anubhav Chaturvedi for helpful remarks and sharing his expertise in semidefinite programming. This work was supported by National Science Centre, Poland, grant OPUS 9. 2015/17/B/ST2/01945.

IX. APPENDIX
In this paper we attempt to classify the labelings of a grid graph with permutations from the set L d = {σ i : σ i (x) = x + i mod d}. We say that two labeled graphs (G 1 , K 1 ) and (G 2 , K 2 ) areequivalent if one can be obtained from the other by means of the following operations.
1. Isomorphism between G 1 and G 2 , 2. In a directed graph, replacing an arc − → uv labeled with π with an arc − → vu labeled with π −1 , 3. Switching operations s(v, σ) for any vertex v ∈ V G1 and permutation σ ∈ S d , defined as follows. For every vertex u ∈ N G (v): However, in this paper we mostly consider an equivalence between different labelings of the same graph. We understand two labelings of a graph G to be equivalent iff one can be obtained from the other by means of a series of switches s(v, σ). It is clear that if the labelings K 1 , K 2 of a graph G are equivalent, then the labeled graphs (G, K 1 ) and (G, K 2 ) are also equivalent.
Definition 2. A cell of a lattice G is a cycle which does not contain any other cycles within it. In the square lattice specifically, a cell is a cycle with four edges. By a defect we mean a cell which is a bad cycle, i.e. has no consistent vertex assignment. Proof. Changing the starting point used in the definition of the defect class will cause the change of the order of permutationsσ i ∈ L d that cl(C, K) is composed of, but this will not affect cl(c, K), because all permutations in L d commute with each other.
Proposition 15. In a planar graph labeled with L d , the class of a cycle is the sum of the classes of all cells within the cycle.
Proof. Let C and C be two cycles in a planar graph which have two vertices a and b in common (see Fig.  16). We denote the compositions of all permutations on the a − b path not belonging to C as π 1 , on the b − a path not belonging to C as π 3 , and on the b − a path belonging to both cycles as π 2 ,. Then we have Π C = π 2 π 1 and Π C = π 3 π −1 2 . The composition of permutations on the outer cycleĈ (containing both C and C ) can be written as ΠĈ = π 3 π 1 = π 3 π −1 2 π 2 π 1 = Π C Π C . If the classes of C and C are x and y, respectively, we havẽ σ yσx (a) = y + (x + a) =σ x+y (a). Thus, if the cycle is formed on the boundary of two cycles of class x and y that share a common edge, its class will be x + y. Definition 4. By a (nontrivial) loop we understand a series of edges in a grid such that the corresponding edges in the dual lattice form a cycle which is not contractible to a point. Typically we will refer to loops consisting of non-identity edges.
Any loop of length k which exists in a grid with no defects defines a set of k bad cycles in the graph, all of which have the same defect class. We will refer to the defect class of those cycles as the class Cl(L) of the loop.
On a higher genus surface two labelings K 1 and K 2 with the same sets of defects of each class may be nonequivalent. In such cases the labeling K 1 − K 2 contains no defect, but it has at least one loop.
It would be convenient to define the equivalence classes based solely on the configuration of defects and loops within a labeling. However, unlike defects, loops may not actually be an inherent property of a given labeling. It is possible for K 1 − K 2 to contain a loop even if no loop can be identified in either K 1 or K 2 .
Which labeling in this case can be said to possess a loop? This is necessarily a matter of convention. For every possible configuration of defects one must choose one default labeling D with the minimum number of nonidentity edges. We will say that this labeling has no loops. Any labeling K with the same configuration of defects as D will be said to have a loop of class x iff the labeling K − D has such a loop.
Note that K − D is a labeling with no defects. Thus, it is equivalent to a labeling in which all non-identity edges form unambigous loops.

A. Classification
Lemma 16. If, by applying local permutations from L d one can transform into each other two graphs labeled by permutations from L d , then these two labeled graphs have the same set of defects of each type, i.e. every cell has the same class in both graphs.
Proof. Let Π =σ x ∈ L d be the defect class of a selected cell. A switch s(v,σ y ) changes the class of the cell Π → σ yσxσ Lemma 17. Applying switch s(v,π y ), whereπ y ∈ L d to a vertex of a cell with defect class x and labeled with L d will change the defect class of the cell into −x, if the switch was applied to the starting vertex , and will not affect the defect class otherwise.

Proof of Theorem 4. (⇒)
If two labelings are equivalent, then one can be transformed into the other by switches. Assume without loss of generality that a permutation σ 1 ∈ L d on the edge e = v 1 v 2 is transformed into σ 2 ∈ L d by switches s(v 1 , η) and s(v 2 , π): σ 2 = πσ 1 η −1 . Such a transformation changes the defect class of the cell c 2 with starting point v 2 from cl(c 2 , K) into πcl(c 2 , K)π −1 , whereas the defect class of the cell c 1 with starting point v 1 is changed from cl(c 1 , K) into ηcl(c 1 , K)η −1 . Notice that η = σ −1 2 πσ 1 , where σ 1 , σ −1 2 ∈ L d and as such, they do not change the class of the cell. Now we see that the defect class of the cell c 1 changes into πcl(c 1 , K)π −1 . This implies cl(c, L) = πcl(c, K)π −1 for all cells.
(⇐) Let K, L : E → L d be labelings such for every cell c, we have cl(c, K) = πcl(c, L)π −1 , where π ∈ S d is the same for all cells. When we start from the labeling K, and apply permutations π on all vertices, we obtain an equivalent labeling π(K) such that cl(c, π(K)) = cl(c, L) for every cell c.
For every labeling with L d , we can obtain a canonical representation using only switches with permutations from L d . Such switches do not change the defect class of any cell. Furthermore, for a given spanning tree there is only one labeling with a given set of defects and all edges of the spanning tree labeled with identity. Thus, the labelings π(K) and L have a shared canonical representation, and are therefore equivalent.
Note that the (⇒) part of the proof does not depend on the graph being planar. Hence, we have the following corollary.
Corollary 18. Let K and L be two labelings of the same graph. If the labelings are equivalent, then there exists a permutation π such that cl(c, K) = π −1 cl(c, L)π for every cell c of the graph.
Proof of Lemma 11. It is obvious that all edges of G−T L belong to G − L, as T is a subgraph of T L . Now let us consider the dual lattice G * . The graph G * −T L * has no vertices of degree 1, as they were all absorbed into T L * during the enlarging procedure. It follows that every edge of G * −T L * belongs to a cycle and thus, every edge of G − T L belongs to a loop.
Any contractible loop in G divides its edge set into two disconnected components. Since T L is a connected graph, all loops contained in G − T L must be noncontractible.
The spanning tree T contains no cycle. It follows that there is a loop in every homology class with no edges in T. Since every cell belonging to such a loop has at least two edges not belonging to T , the loop dose not get absorbed into T L . Thus, G − T L contains a loop from every homology class.
Finally, two loops belonging to the same homology class can be continuously transformed into one another. But in this case these two loops would be separating the graph into two non-connected subgraphs. This, however, is impossible, since T L is connected. Therefore, for a given spanning tree T and genus g of the surface, there are 2g unique closed loops in the complement of T L .
In the proof of Theorem 6 we will use the fact that canonical form of a L d game associated with a given spanning tree T depends on permutations that belong to T L , but not to T (they are fixed, up to permutation π applied to every vertex, by classes of defects), as well as by permutations that belong to the complement of T L (they are partially fixed, up to permutation π applied to every vertex, by classes of defects, but also provide some additional characteristic of a game arising from periodic boundary conditions).
Proof of Theorem 6. First assume that there exists a permutation π ∈ S d such that cl(c, K 1 ) = πcl(c, K 2 )π −1 for every cell c of the grid and Cl(l, K 1 ) = πCl(l, K 2 )π −1 for every loop l. Let T be a spanning tree of the graph. There is clearly only one way to label the enlarged tree T L such that all edges of T are assigned the identity and the classes of all cells are preserved. The permutations assigned to the edges of G − T L must also be the same. If they were not, the classes of loops in the two labelings would be different. Thus K 1 and π(K 2 ) have a shared canonical representation, which means that they are equivalent. Since π(K 2 ) is equivalent to K 2 , it follows that K 1 and K 2 are equivalent. Now assume that K 1 and K 2 are equivalent. As shown before, K 2 can be obtained from K 1 by applying a switch S(v, σ v π) to each vertex, where σ v ∈ L d and π ∈ S d − L d is the same for all vertices.
As shown before, the switch s(v, σ v π) changes the defect class of a cycle from σ to π −1 σπ if v is the starting point of the cycle and leaves it unchanged otherwise. This applies to contractible cycles on the surface, which arise from defects, as well as to noncontractible cycles, which give rise to nontrivial loops.
Combined with the definition of the class of a loop, this shows that the set of switches which transforms K 1 into K 2 changes the classes of defects and loops in exactly the same way, namely cl(c, K 1 ) = πcl(c, K 2 )π −1 for every cell c and Cl(l, K 1 ) = πCl(l, K 2 )π −1 for every loop l.