{'fraction_non_alphanumeric': 0.057517492149192884, 'fraction_numerical': 0.008704754558977467, 'mean_word_length': 3.823037066560383, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 2, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
The Quantum Monad on Relational Structures

Homomorphisms between relational structures play a central role in finite model theory, constraint satisfaction and database theory. A central theme in quantum computation is to show how quantum resources can be used to gain advantage in information processing tasks. In particular, non-local games have been used to exhibit quantum advantage in boolean constraint satisfaction, and to obtain quantum versions of graph invariants such as the chromatic number. We show how quantum strategies for homomorphism games between relational structures can be viewed as Kleisli morphisms for a quantum monad on the (classical) category of relational structures and homomorphisms. We show a general connection between these notions and state-independent quantum realizations of strong contextuality in the Abramsky-Brandenburger formulation of contextuality. We use these results to exhibit a wide range of examples of contextuality-powered quantum advantage, and to unify several apparently diverse strands of previous work.


Introduction
Finite relational structures and the homomorphisms between them form a mathematical core common to finite model theory [24], constraint satisfaction [14], and relational database theory [23]. Moreover, much of graph theory can be formulated in terms of the existence of graph homomorphisms, as expounded e.g. in the influential text [17]. Thus, implicitly at least, the mathematical setting for all these works is categories of σ-structures and homomorphisms, for relational vocabularies σ.
What could it mean to quantize these structures? More precisely, with the advent of quantum computing, we can now consider the consequences of using quantum resources for carrying out various information-processing tasks. A major theme of current research is to delineate the scope of the quantum advantage which can be gained by the use of quantum resources. How can this be related to these fundamental structures?
Our starting point is the notion of quantum graph homomorphism introduced in [26] as a generalization of the notion of quantum chromatic number [11]. Consider the following game, played by Alice and Bob cooperating against a Verifier. Their goal is to establish the existence of a homomorphism G → H for given graphs G and H. Verifier provides vertices v 1 , v 2 ∈ V (G) to Alice and Bob respectively. They produce outputs w 1 , w 2 ∈ V (H) in response. No communication between Alice and Bob is permitted during the game. They win if the following conditions hold: v 1 = v 2 ⇒ w 1 = w 2 and v 1 ∼ v 2 ⇒ w 1 ∼ w 2 , where we write ∼ for the adjacency relation.
If only classical resources are permitted, then the existence of a perfect strategy for Alice and Bob -one in which they win with probability 1 -is equivalent to the existence of a graph homomorphism in the standard sense. However, using quantum resources, in the form of an entangled bipartite state where Alice and Bob can each perform measurements on their part, there are perfect strategies in cases where no classical homomorphism exists, thus exhibiting quantum advantage.
Alice-Bob games have also been studied for other tasks, notably for constraint systems. Consider the following system of linear equations over Z 2 : Of course, this system is not satisfiable in the standard sense, as we can see by summing over the left-and right-hand sides. Now consider the following Alice-Bob game. The Verifier sends Alice an equation, and Bob a variable. Alice returns an assignment to the variables in the equation, and Bob returns an assignment for his variable. They win if Bob's assignment agrees with Alice's, and moreover Alice's assignment satisfies the given equation. Classically, the existence of a perfect strategy is equivalent to the existence of a satisfying assignment for the whole system. Using quantum resources, there is a perfect strategy for the above system, which corresponds to Mermin's "magic square" construction [28]. This can be generalized to a notion of quantum perfect strategies for a broad class of constraint systems [13,12], which have strong connections both to the study of contextuality in quantum mechanics, and to a number of challenging mathematical questions [35,34]. Clearly, these games are analogous to those for graph homomorphisms. What is the precise relationship?
In [26], generalizing results in [11], the existence of a quantum perfect strategy for the homomorphism game from G to H is characterized in terms of the existence of a family {E vw } v∈V (G),w∈V (H) of projectors in d-dimensional Hilbert space for some d, subject to certain conditions. Analogous results for constraint systems are proved in [13]. This characterization eliminates the two-person aspect of the game, and the shared state, leaving a "projector-valued relation" as the witness for existence of a quantum perfect strategy. We shall henceforth call these witnesses quantum graph homomorphisms. An important further step is taken in [26]. A construction H → MH on graphs is introduced, such that the existence of a quantum graph homomorphism from G to H is equivalent to the existence of a standard graph homomorphism G → MH.
Our contribution begins at this point. We describe a general notion of non-local game for witnessing homomorphisms between structures for any relational signature. We show that the use of quantum resources in these games can be characterized by a notion of quantum homomorphism. Moreover, quantum homomorphisms can in turn be characterized as the Kleisli morphisms for a quantum monad on the (classical) category of relational structures and homomorphisms. This monad is graded [29] by the dimension of the Hilbert space.
Our account refines and generalizes the ideas from both [11,26] and [13]. We characterize quantum solutions for general constraint satisfaction problems, showing as a special case that these subsume the binary constraint systems of [13]. We also show how quantum witnesses for strong contextuality in the sense of [4] are characterized by quantum homomorphisms.
The precise relationship with the quantum graph homomorphisms of [26] turns out to be more subtle. We show that their notion is characterized by a quantum solution in our sense for a related boolean constraint system. Overall, we show that a wide range of notions of quantum advantage is captured in a uniform way by the quantum monad, applied directly to the standard classical structures.
For reasons of limited space, some background material on linear algebra and quantum mechanics and some proofs have been relegated to an Appendix.
We now consider the following game, played on finite structures A, B, in which Alice and Bob cooperate to convince a Verifier that there is a homomorphism from A to B: • Alice and Bob are separated, and not allowed to communicate (exchange classical information) while the game is played.
• In a play of the game, the Verifier sends Alice an index a, and a tuple x ∈ R A a ; and Bob an element x ∈ A.
• Alice returns a tuple y ∈ B ka , and Bob returns an element y ∈ B.
• Alice and Bob win that play if Alice and Bob may use probabilistic strategies. A perfect strategy is one in which they win with probability 1.
It is clear that if only classical resources are allowed, the existence of a perfect strategy is equivalent to the existence of a homomorphism from A to B. The actual strategy played by Alice and Bob may be pure or mixed, in the latter case using some shared randomness.
We now consider the use of quantum resources in the homomorphism game. We shall only consider the case of finite-dimensional resources in this paper. Such resources have the following general form: • There are finite-dimensional Hilbert spaces H and K, and a pure state ψ on H ⊗ K.
This state is shared between Alice and Bob. The separation between Alice and Bob is reflected in the fact that Alice can only perform operations on H, while Bob can only perform operations on K.
• For each a ∈ [p] and tuple x ∈ R A a , Alice has a POVM E a x = {E a x,y } y∈B ka . • For each x ∈ A, Bob has a POVM F x = {F x,y } y∈B These resources are used as follows: • Given a and x, Alice measures E a x on her part of ψ. • Given x, Bob measures F x on his part of ψ.
• They obtain the joint outcome (y, y) with probability ψ * (E a x,y ⊗ F x,y )ψ. If with probability 1 the outcome (y, y) satisfies the winning conditions, then this is a quantum perfect strategy.
We can write the winning conditions explicitly in terms of the quantum operations: A first remark is that our assumption of the bipartite structure of the state space does not in fact lose any generality. We could have asked simply that Bob's operators commute with those of Alice. However, since we are considering the finite-dimensional case, a result of Tsirelson [36,34] implies that this is equivalent to the tensor product formulation we have used.
Furthermore, using a pure state also does not lose any generality. Indeed, if we had a mixed state ρ = i p i ψ i ψ * i , with the trace replacing the inner products in (QS1) and (QS2), then the linearity of the trace implies that we could just as well use any of the pure states ψ i with the same measurements, and still satisfy the conditions.
We shall now show that in fact a quantum perfect strategy can without loss of generality be assumed to have a very special form, which will lead us to the equivalence with quantum homomorphisms. These results combine ingredients from [13] and [26,11]. The proofs are closest to those in [33], but are considerably simpler as well as more general.
For notational convenience, we shall focus on the case where the relational signature has a single k-ary relation R. Thus a quantum perfect strategy for the homomorphism game from A to B has the form (ψ, are POVMs satisfying the conditions (QS1) and (QS2).
Our first step is to show that without loss of generality, ψ can be taken to have full Schmidt rank.
Proof. Our proof will follow closely the first part of the proof of Theorem 6.5.1 in [33], so we omit detailed calculations. We write the Schmidt decomposition of It is straightforward to verify that P A P * A = I d = P B P * B . We have ψ := (P A ⊗ P B )ψ ′ , and ψ ′ = (P * A ⊗ P * B )ψ. Similarly, we define E x,y := P A E ′ x,y P * A , and F x,y := P A F ′ x,y P * B . Again, it is straightforward to verify that this yields well-defined POVMs, and moreover that the probabilities are preserved: The following simple general result will be useful.

Lemma 2. Let
A be a * -algebra, and a, b, d be self-adjoint elements of A, where d is also invertible. Suppose that ad = adb = db. Then a and b are projectors, and they both commute with d 2 . If A is a C * -algebra, then a = b.
Proof. First, a 2 d = adb = ad. Since d is invertible, this implies that a 2 = a, so a is a projector. Similarly, b 2 = b. Moreover, since a, b and d are self-adjoint, db = ad ⇐⇒ bd = da. Hence If A is a C * -algebra, then it is standard that d commutes with every element which commutes with d 2 [9,31], so da = ad = db, and since d is invertible, this yields a = b.
We shall now show that under the assumption of full Schmidt rank, the measurements are already remarkably constrained. We define E i x,y : be a quantum perfect strategy in which ψ has full Schmidt rank. Then for all x, i, y, E i x,y and F x,y are projectors, and E i x,y = 0. By the quantum strategy condition (QS1), E x,y DF T x,y = 0 when x = x i and y = y i . This in turn implies that E i x,y DF T x,y ′ = 0 when x = x i and y = y ′ . Now fix x and x = x i . Let A y := E i x,y , and B y := F T x,y . We have y A y = I = y B y , and A y DB y ′ = 0 when y = y ′ . Hence A y D = y ′ A y DB y ′ = A y DB y = y ′ A y ′ DB y = DB y . We can now apply Lemma 2, taking a = A y , d = D, and b = B y , to conclude that E i x,y and F x,y are projectors, and moreover that they commute with D 2 . We can use the last part of Lemma 2 to conclude that E i x,y = F T x,y whenever x = x i .
Finally, we show that the state can be chosen to be maximally entangled.
Proof. Let D ′ be the diagonal matrix associated with ψ ′ . Using Lemma 3, These conditions will be preserved by any state ψ whose diagonal matrix is full rank and commutes with the matrices F x,y . This holds in particular for the maximally entangled state, whose diagonal matrix has the form 1 We can now combine Lemmas 1, 3 and 4 to obtain the following result: Theorem 5. The existence of a quantum perfect strategy implies the existence of a strategy (ψ, {E x }, {F x }) with the following properties: • If x ∈ R A and y ∈ R B , then E x,y = 0.
It is worth noting that the procedure for obtaining the strategy in this special form has three steps: • In Step 1, the state and strategies are projected down to the subspace corresponding to the support of the Schmidt decomposition of the state. This step reduces the dimension of the Hilbert space, and preserves the probabilities for the strategy exactly.
• Step 2 does not change the strategy at all, but shows that it must already have strong properties.
• Step 3 changes the state but not the measurements. In general, the probabilities for the strategy will be changed, but the possibilities are preserved exactly.
Thus in passing to the special form, the dimension is reduced; the process by which we obtain projective measurements is not at all akin to dilation. This theorem shows that all the information determining the strategy is in Alice's operators. Moreover, Alice's operators must be chosen non-contextually, so that E i x,y is independent of the context x. This means that we can define projectors P x,y := E i x,y whenever x = x i . If x ′ ,y , so P x,y is well-defined. Now, recall the notion of joint measurability: a family of POVMs The following result is standard [16].
Proposition 6. A family of projective measurements is jointly measurable by a POVM if and only if they pairwise commute, and in this case the POVM is the product of the family, and hence projective.
For each x ∈ R A , the projective measurements {P x i } defined above are jointly measured by E x . Thus E x is the projective measurement P x = {P x,y } y given by P x,y := P x 1 ,y 1 · · · P x k ,y k .
We shall now introduce the notion of quantum homomorphism between relational structures A and B. A quantum homomorphism is a family of projectors {P x,y } x∈A,y∈B in Proj(d) for some d, satisfying the following conditions: where P x,y := P x 1 ,y 1 · · · P x k ,y k .
• (QH3) If x ∈ R A and y ∈ R B , then P x,y = 0.
Note that (QH1) implies that for any x, P x,y P x,y ′ = 0 whenever y = y ′ . We write A q → B for the existence of a quantum homomorphism from A to B.
Theorem 7. For finite structures A, B, the following are equivalent: 1. There is a quantum perfect strategy for the homomorphism game from A to B.
Proof. The implication from (1) to (2) follows directly from Theorem 5 and the subsequent discussion. For the converse, given a quantum homomorphism {P x,y } x∈A,y∈B , we can define E x,y := P x,y , F x,y := P T x,y , and use the maximally entangled state to obtain a quantum perfect strategy. It is straightforward to verify that the homomorphism conditions (QH1)-(QH3) imply the strategy conditions (QS1) and (QS2).
As a final remark, although we have focussed on a single relation to simplify the notation, our results go through for arbitrary relational signatures. Note that the general form of condition (QH2) is that P x,y and P x ′ ,y ′ must commute whenever x and x ′ are adjacent in the Gaifman graph of A -that is, they both occur in some tuple of some relation.

From quantum homomorphisms to the quantum monad
We now show how to characterize quantum homomorphisms as the Kleisli morphisms of a monad Q d on the category of relational structures. This monad is graded [29] by the dimension of the Hilbert space used as the quantum resource.
The monad will be defined on the category R(σ) of all σ-structures, since Q d A will always be infinite, even if A is finite. For the underlying universes of the structures, the construction can be seen as a quantum variant of the discrete distribution monad [19], widely used in coalgebra and semantics. It is well known that the distribution monad can be defined over any commutative semiring, with the non-negative reals being used for the standard case of probabilities [19,4]. Here we shall use the projectors Proj(d) with d ranging over the positive integers. For each d, Proj(d) is a partial commutative semiring, since we can only add projectors if they are orthogonal, and only multiply them if they commute. We also have the graded multiplication given by the tensor product: if P ∈ Proj(d) and Q ∈ Proj(d ′ ), then P ⊗ Q ∈ Proj(dd ′ ).
We fix a relational signature σ. For each positive integer d and σ-structure A, we shall define a σ-structure Q d A. The universe of this structure Q d A is the set of all functions p : A → Proj(d) satisfying the normalization condition: x∈A p(x) = I. Note that the normalization condition implies that the projectors {p(x)} x∈A are pairwise orthogonal. Since we are in finite dimension d, this in turn implies that p has finite support: p(x) = 0 for all but finitely many x. We can think of Q d A as the projector-valued distributions on A in dimension d.
For each relation R of arity k in σ, we define R Q d A to be the set of all tuples (p 1 , . . . , p k ) such that: Note that the first condition implies that the product of projectors in the second is a welldefined projector.
Proposition 8. Let A and B be finite σ-structures. There is a bijective correspondence between quantum homomorphisms {P x,y } x∈A,y∈B from A to B in dimension d and standard homomorphisms h : A → Q d B.
This correspondence is analogous to the familiar one between relations and set-valued functions, which shows that the category of relations is the Kleisli category of the powerset monad on Set.
Now we show that Q d extends to a functor on R(σ). Given a homomorphism h : Proof. The finite support and normalization conditions ensure that Q d h is well-defined. Functoriality is proved exactly as for the distribution monad. We verify that Q d h is a homomorphism. Suppose that (p 1 , . . . , p k ) ∈ R Q d A . By (QR1), this implies that [p i (x), p j (x ′ )] = 0 for all i, j ∈ [k], x, x ′ ∈ A. This implies that The unit of the monad η A : Verification that this is well-defined and yields a natural transformation is straightforward.
We also have the graded monad multiplication: . We prove that this gives a well-defined natural transformation in the Appendix. Thinking of ⊗ as the graded semiring multiplication on projectors, we can see the correspondence to the distribution monad.
We recall that given a category C, the endofunctor category [C, C] is monoidal; a monad on C is a monoid in this category [25]. Now let (M, ·, 1) be a monoid, which we can view as a discrete category with a strict monoidal structure. An M -graded monad [29] on C is a lax monoidal functor from M into [C, C]. Such a functor is given by the following data: an assignment m → T m of an endofunctor on C to each element of M ; a natural transformation η : Id . ✲ T 1 (the graded unit); and a natural transformation µ m,m ′ : T m T m ′ . ✲ T m·m ′ for all m, m ′ (the graded multiplication). These are subject to coherence conditions, which generalize the usual monad equations. We refer to [29] for details.
In our case, we use the monoid N + of positive integers under multiplication.
The proof of this result involves verifying a number of equations, and is fairly lengthy but straightforward. We provide details in the Appendix.
We are particularly interested in the Kleisli category for this graded monad. The objects of this category are the same as those of R(σ). A morphism from A to B is a homomorphism h : A → Q d B. By Proposition 8, we know that Kleisli morphisms correspond exactly to quantum homomorphisms.
The graded composition of Kleisli arrows h : An explicit description can be calculated from the graded monad structure given above: (k•h)(x)(z) = y∈B h(x)(y)⊗k(y)(z). If we write this in terms of the corresponding quantum homomorphisms {P x,y } x∈A,y∈B , {Q y,z } y∈B,z∈C , we obtain {R x,z } x∈A,z∈C given by the formula R x,z = y∈B P x,y ⊗ Q y,z . This generalizes the concrete definition for quantum graph homomorphisms from [26].

Quantum advantage via the quantum monad
We shall now show how the quantum monad provides a unified framework for expressing quantum advantage in a wide range of information processing tasks. We shall show equivalences between: • state-independent strong contextuality arguments • quantum advantage in constraint satisfaction • existence of quantum (but not classical) homomorphisms between relational structures.

Classical correspondences
We begin with the standard classical correspondence between constraint satisfaction problems and the existence of homomorphisms. A CSP instance has the form K = (V, D, C) where V is a set of variables, D is a domain of values 1 , and C is a set of constraints of the form (x, r), where for some k, x ∈ V k , and r ⊆ D k . We say that c = (x, r) is a k-ary constraint. A solution of the CSP is a function s : V → D such that, for all (x, r) ∈ C, s(x) ∈ r, where s(x) := (s(x 1 ), . . . , s(x k )).
Given K = (V, D, C) we define two structures over the signature with a k-ary relation symbol R c for each k-ary constraint c. First, B K has as universe D, and for each c = (x, r) ∈ C, R B K c = r. Secondly, A K has universe V , and for each c = (x, r) ∈ C, R A K c = {x}. The following is immediate: Proposition 11. There is a one-to-one correspondence between solutions for K and homomorphisms There is also a converse to this result. Given σ-structures A and B, we can define the CSP K AB = (V, D, C), where V = A, D = B, and C = {(a, R B ) | R ∈ σ, a ∈ R A }.

Proposition 12.
There is a one-to-one correspondence between homomorphisms A → B and solutions for K AB .
We will now look at empirical models over measurement scenarios, introduced in [4] as a general setting for studying contextuality, in quantum mechanics and beyond, with nonlocality as a special case.
A measurement scenario is a triple (X, M, O), where X is a set of measurement labels; M is a family of subsets of X, where we think of C ∈ M as a set of compatible measurements, or a context; and O is a set of measurement outcomes. An empirical model e : (X, M, O) for a scenario is a family e = {e C } C∈M of probability distributions e C ∈ Prob(O C ) on the joint outcomes of measuring all the variables in a context C. Such empirical models can arise from observational data (hence the name), or be generated by measuring a quantum state in contexts comprising jointly measurable observables. A hierarchy of notions of contextuality can be defined in this general setting [4,2]. We shall be concerned with strong contextuality. We say that e : (X, M, O) is strongly contextual if there is no global assignment g : X → O such that, for all C ∈ M, e C (g| C ) > 0. That is, there is no global assignment which is consistent with the model in the sense of yielding possible outcomes (non-zero probability) in all contexts. This form of contextuality is witnessed by the GHZ construction [15,27], as well as by Kochen-Specker paradoxes [21], and post-quantum devices such as the PR box [32].

Proposition 13.
There is a one-to-one correspondence between consistent global assignments for e and solutions of K e . Thus e is strongly contextual iff K e has no (classical) solution.
We thus have a three-way correspondence between CSPs, empirical models, and homomorphisms between relational structures.

Quantum solutions
We now consider how quantum resources enter the picture. Since we already have a notion of quantum homomorphism for general relational structures, the correspondences established in the previous subsection give us ready-made notions of quantum solution for CSPs and empirical models. We define a quantum solution for a CSP K = (V, D, C) to be a quantum homomorphism A K q → B K , i.e. a Kleisli morphism A K → Q d B K for some d. Similarly, we define a quantum solution for an empirical model e : (X, M, O) to be a quantum homomorphism A Ke q → B Ke . We shall now compare these notions to existing ones for empirical models and constraints. These will turn out to be special cases.
Note first that given an empirical model e, the corresponding CSP K e is determined purely by the supports of the probability distributions e C , i.e. the possibilistic content of the model. It is only this information which is relevant to strong contextuality. We can consider a fine-grained notion of realization of a probabilistic empirical model, as in [4]. However, if our concern is to give a quantum witness for strong contextuality of an empirical model e : (X, M, O), then this is given by a state ψ, and a PVM P x = {P x,o } o∈O for each x ∈ X, such that [P x,o , P x ′ ,o ′ ] = 0 whenever x and x ′ both occur in some C ∈ M. These must then satisfy, for all C ∈ M and s ∈ O C , e C (s) = 0 ⇒ ψ * P x,s(x) ψ = 0, where P x,s(x) = P x 1 ,s(x 1 ) · · · P x k ,s(x k ) .
This provides a quantum witness for strong contextuality if e is a strongly contextual empirical model. An example is provided by the GHZ state, using X and Y measurements for each party [4]. An infinite family of such examples using three-qubit states is described in [1].
We can also consider a stronger notion. A state-independent quantum witness for e : (X, M, O) is given by a family of PVMs {P x } x∈X which, for any state ψ, yield a quantum witness for e. The Mermin magic square and pentagram [28], and Kochen-Specker constructions [21,10], provide examples of state-independent quantum realizations of strong contextuality. Note that in the state-independent case, we have the condition: e C (s) = 0 ⇒ P x,o = 0. An interesting point arising from this result, taken together with the results from Section 2, is that state-independent strong contextuality proofs can always be underwritten by nonlocality arguments. This can be seen as a general form of constructions for turning Kochen-Specker contextuality proofs into Bell non-locality arguments [18]. Indeed, the rôle of the entangled state and of Bob in the non-local game is to provide an operational or physical underpinning for the compatibility or generalized no-signalling assumption which is made for empirical models [4]. Can we find a similar underpinning in the state-dependent case? We shall return to this point in the final section.
We now consider binary constraint systems (BCS), which have been extensively studied [13,12,35,20]. We shall follow the account in [13]. A BCS (V, C) is simply a boolean CSP (V, {0, 1}, C). In this case, constraints can be written in the form c = (x, b c ), where b c : {0, 1} k → {0, 1} is a boolean function. Our general notion of quantum solution yields in this case a family of PVMs P x = {P x,o } o∈{0,1} for x ∈ V , such that [P x,o , P x ′ ,o ′ ] = 0 whenever x and x ′ both occur in some constraint, and P x, In [13], a notion of operator solution for a BCS is defined. This is an assignment of a self-adjoint operator (aka observable) A x to each variable x, such that: (1) each A x is binary, i.e. A 2 x = I; (2) [A x , A x ′ ] = 0 when x and x ′ both occur in the same constraint. To express constraints, the representation of boolean values b → (−1) b , b ∈ {0, 1}, is used. It is standard that each boolean function {−1, +1} k → {−1, +1} can be uniquely represented by a real multilinear polynomial p(X 1 , . . . , X k ) [30]. Moreover, if the corresponding boolean function in the {0, 1}-representation is b : It is also standard that if we substitute pairwise commuting self-adjoint operators A 1 , . . . , A k for the variables X 1 , . . . , X k , we obtain a self-adjoint operator p(A 1 , . . . , A k ). The condition for an operator solution to satisfy the constraints is then expressed as follows: for each constraint c = (x, b c ), where b c is represented by the polynomial p c (X 1 , . . . , X k ), we must have p c (A 1 , . . . , A k ) = −I.
To relate this notion of operator solution to our quantum solution, note that a binary observable A x with A 2 x = I has a spectral decomposition A x = P x,0 − P x,1 , where P x,0 , P x,1 are projectors. Commutation of the observables for variables occurring in the same context is equivalent to commutation of the corresponding projectors. Given a constraint c = ((x 1 , . . . , x k ), p c ), since the observables A x 1 , . . . , A x k pairwise commute, we obtain a resolution of the identity o∈{0,1} k P x,o = I. Multiplying p c (A 1 , . . . , A k ) by this expression yields It follows that p c (A 1 , . . . , A k ) = −I iff for all o with b c (o) = 0, P x,o = 0. As an immediate consequence, we have: Proposition 15. Given a BCS (V, C), there is a one-to-one correspondence between operator solutions of the BCS and quantum solutions of the corresponding CSP.

Graphs
The results we have seen thus far show that our notion of quantum homomorphism subsumes a number of existing notions in contextuality and non-local games. However, as we shall now see, the situation in the setting which provided the original motivation for our approach, namely graph homomorphisms, is somewhat more subtle. Graphs arise as structures for the signature with a single binary relation. Simple graphs are those where the relation is symmetric and irreflexive. If we specialize our definition of quantum homomorphism to the case G q → H between graphs G and H, this gives a family {P x,y } x∈V (G),y∈V (H) of projectors satisfying the following conditions: • for all x, x ′ ∈ V (G) and y, y ′ ∈ V (H) with x ∼ x ′ , [P x,y , P x ′ ,y ′ ] = 0; • for all x, x ′ ∈ V (G) and y, y ′ ∈ V (H) with x ∼ x ′ and y ∼ y ′ , P x,y P x ′ ,y ′ = 0.
This definition differs from that introduced by Mančinska and Roberson [26] as a generalization of quantum graph colouring [11], in that the latter does not impose the first condition forcing commutativity between the PVMs corresponding to adjacent vertices of G. We refer to this as an MR quantum graph homomorphism in order to distinguish it from our notion. This reflects a difference in the Alice-Bob game used to motivate each definition. In the game we consider, Alice receives an ordered edge of G as an input and Bob a vertex of G, and we require that Alice answers with an edge of H and Bob with a vertex of H in a fashion consistent with Alice's choice. By contrast, Mančinska and Roberson consider a symmetric Alice-Bob game, where each player receives a vertex of G as input and outputs a vertex of H, and their outputs are required to be the same when they receive the same vertex, and required to form an edge of H whenever their inputs form an edge of G.
It is clear that a quantum homomorphism between graphs in the sense of this paper is also an MR quantum graph homomorphism. But the precise relationship between the two notions is yet to be understood in general: in particular, whether the existence of an MR quantum graph homomorphism implies the existence of a quantum homomorphism in our sense. However, by adapting a construction due to Ji [20], we can capture the existence of MR quantum graph homomorphisms in terms of quantum homomorphisms of relational structures, via a BCS.
Given graphs G and H, we define V = {r xy | x ∈ V (G), y ∈ V (H)}. For each x ∈ V (G), we have a boolean constraint y r xy ; a constraint ¬(r xy ∧ r xy ′ ) when y = y ′ ; and a constraint ¬(r xy ∧ r x ′ y ′ ) whenever x ∼ x ′ and y ∼ y ′ . This defines a BCS (V, C).
Theorem 16. Given graphs G and H, there is a one-to-one correspondence between MR quantum graph homomorphisms from G to H and quantum homomorphisms A K q → B K for the associated CSP K = (V, {0, 1}, C).
Conversely, given a quantum homomorphism {Q xy,o }, we define P x,y := Q xy,1 . (MR2) follows from (QH3), while using (QH1), we can reverse the reasoning in the previous paragraph to show that (QH4) implies (MR1). These passages are clearly mutually inverse, so the result follows.
It is noteworthy that our approach allows us to avoid ad hoc coding of constraints by polynomials, as in [13,20]. Instead, we quantize the standard classical notions in a uniform way, using the quantum monad.

Outlook
This work suggests a number of directions for further study. We list a few: • A notion of quantum graph isomorphism, with an equivalent characterization via an Alice-Bob game, has been studied in [8]. This can be generalized to relational structures. How does this fit in our quantum monad framework?
• Our approach captures quantum advantage provided by state-independent strong contextuality. Does state-dependent contextuality admit a similar treatment?
• Any strategy for an Alice-Bob game has a winning probability, which is related to the contextual fraction [3]. Can our approach be adapted to deal with quantitative aspects?
• Homomorphisms are intimately related with the existential positive fragment. Can this be extended to provide a notion of quantum validity for first-order formulae?
• Can other concepts from finite model theory, such as pebble games, which admit a comonadic formulation [7], be similarly quantized?
• The algebras of the quantum monad can be described as convex structures with mixing weighted by projectors rather than just numbers in [0, 1]. Is this viewpoint useful?
, {α i } and {β i } are orthonormal sets of vectors in C d A and C d B respectively, and λ i > 0 for all i. This follows directly from Singular Value Decomposition. We refer to d as the Schmidt rank of ψ.
The following result is standard, but we did not find an explicit reference so we include a proof.

Quantum monads
is a well-defined homomorphism, and yields a natural transformation.
Proof. Firstly, µ d,d ′ A is a well-defined function, by finiteness of support. To show that it is a homomorphism, consider (P 1 , ..., P k ) ∈ R Q d Q d ′ A . We must show firstly that for all z, z ′ ∈ C, and i, j, . Using linearity, this reduces to showing that P i (p) ⊗ p(z) commutes with P j (p ′ ) ⊗ p ′ (z ′ ) for all p, p ′ ∈ Q d ′ . Applying (QR1) to P i and P j , we have that P i (p) commutes with P j (p ′ ). If p(z) commutes with p ′ (z ′ ), we are done. If not, then we know that p cannot be adjacent to p ′ in the Gaifman graph of Q d ′ . Hence for any expansion p = (p 1 , . . . , p k ) with p i = p, p j = p ′ , p ∈ R Q d ′ A , so by (QR2) we must have P 1 (p 1 ) · · · P k (p k ) = 0. Using normalization, we have P i (p)P j (p ′ ) = p i =p,p j =p ′ P 1 (p 1 ) · · · P k (p k ) = 0, and so (P i (p) ⊗ p(z))(P j (p ′ ) ⊗ p ′ (z ′ )) = 0 = (P j (p ′ ) ⊗ p(z ′ ))(P i (p) ⊗ p(z)). Now let q i = µ d,d ′ A (P i ), i = 1, . . . , k. To show that (QR2) holds for (q 1 , . . . , q k ) reduces similarly to showing that, if x ∈ R A , then P 1 (p 1 ) · · · P k (p k ) ⊗ p 1 (x 1 ) · · · p k (x k ) = 0 for all p 1 , . . . , p k . If p 1 (x 1 ) · · · p k (x k ) = 0 we are done; otherwise, since x ∈ R A , we must have p ∈ R Q d ′ A , and applying (QR2) to (P 1 , ..., P k ), we must have P 1 (p 1 ) · · · P k (p k ) = 0.
Naturality is commutativity of the following square.
This is the following calculation: The second step uses the fact that Q d ′ f (p) = q ⇐⇒ q(y) = f (x)=y p(x).
The unit η : Id Lemma 19. Let A be a structure and d ∈ N + . Then, the following diagram commutes: Proof. Let p ∈ Q d A and x ∈ A. The claim is that (µ 1,d . The left-hand side of this equation expands to and the right-hand side to where δ x : A → {0, 1} is defined by δ x (x ′ ) := δ x,x ′ for all x ′ ∈ A.

Additional material on the quantum monad
We shall now show that the quantum monad is monoidal (or commutative) and affine [22]. This continues the analogy with the distribution monad, which is well known to have these properties [19]. The category R(σ) has finite products, given by the usual cartesian product of structures. The terminal object ⊤ is the one-element structure, with each relation interpreted as the universal relation. Because of normalization, the following is immediate: Proposition 22. This is a well-defined homomorphism, and the family {m d,d ′ A,B } defines a graded natural transformation satisfying the monoidal coherence conditions, thus witnessing a commutative strength.