Addition and Differentiation of ZX-diagrams

The ZX-calculus is a powerful framework for reasoning in quantum computing. It provides in particular a compact representation of matrices of interests. A peculiar property of the ZX-calculus is the absence of a formal sum allowing the linear combinations of arbitrary ZX-diagrams. The universality of the formalism guarantees however that for any two ZX-diagrams, the sum of their interpretations can be represented by a ZX-diagram. We introduce a general, inductive definition of the addition of ZX-diagrams, relying on the construction of controlled diagrams. Based on this addition technique, we provide an inductive differentiation of ZX-diagrams. Indeed, given a ZX-diagram with variables in the description of its angles, one can differentiate the diagram according to one of these variables. Differentiation is ubiquitous in quantum mechanics and quantum computing (e.g. for solving optimization problems). Technically, differentiation of ZX-diagrams is strongly related to summation as witnessed by the product rules. We also introduce an alternative, non inductive, differentiation technique rather based on the isolation of the variables. Finally, we apply our results to deduce a diagram for an Ising Hamiltonian.


Introduction
ZX-calculus, originally introduced in [CD11], is a graphical language that allows reasoning about quantum computing.In this language complex computations on qubits are represented with diagrams made out of elementary generators 1 .Each diagram corresponds to a linear transformation between Hilbert spaces of qubit states.A compact set of rewrite rules allows to transform diagrams into equivalent ones.The notable advantage of ZX-calculus compared to other representations (e.g.linear maps, circuits, and tensor networks) is that in this language the computations may be done entirely graphically.A general introduction to the language alongside the overview of the main applications is available in [vdW20].
Due to its flexibility, ZX-calculus is widely used to address different problems of quantum computing.For instance, ZX-calculus allowed the derivation of important results in the field of measurement-based quantum computing (MBQC) [DP10,KvdW19].It was also successfully applied to circuit optimization [CDD + 20, dBBW20, KvdW20, DKPvdW20], design and verification of error-correction color codes [GD18], and to the analysis and compilation of surface codes [Hor11,dBH20,HEMN20].
However, the applications of the ZX-calculus to the rapidly growing field of variational algorithms [CAB + 21] such as Quantum Approximate Optimization Algorithm (QAOA) [FGG14], Variational Quantum Eigensolver (VQE) [PMS + 14] and variational quantum machine learning are so far limited.Nevertheless, as variational algorithms do not require error correction, the incoming emergence of NISQ devices makes them an object of particular attention [Pre18].We believe that the reason why they are still unexplored with the means of ZX-calculus is the absence of a convenient way to differentiate parametrized diagrams.Indeed, the basic building blocks of variational algorithms are parametrized circuits, and the search for optimal parameter values is a crucial part of these algorithms.In theory, finding optimal parameters may be NP-hard [BK21].In practice, the search is usually done by classical numerical optimization methods and most of them use derivatives [GS17].
The main difficulty for differentiation of ZX-diagrams comes from the product rule: ∂f g = ∂f g + f ∂g that involves adding two terms.This rule is crucial to evaluate the derivative of the sequential composition and the tensor product.As ZX-diagrams are defined inductively with these two compositions, the derivative of a complex diagram could be computed out of the derivatives of its parts if we were allowed to use the product rule, and therefore sum diagrams.
The works [ZG21,TYd21] use explicit sums of diagrams (usually referred to as bags of diagrams) to represent the derivative of diagrams with multiple occurrences of the parameter.The major disadvantage of this approach is that there are no rules to manipulate sums of ZX-diagrams.Therefore, we can't fully exploit the power of graphical computation (while it's still possible to use rewrite rules on summands [SH22]).In this work, we suggest an approach where the derivative of a parametrized ZX-diagram is another ZX-diagram.Hence we avoid the extension of the signature with formal sums.In order to tackle sums that appear in the product rule, we introduce an original technique to perform the addition of diagrams entirely in the ZX-calculus.For this purpose, we use special diagrams called controlled states [JPV19].We suggest an inductive way to represent every ZX-diagram by such a state.As we know how to sum controlled states [JPV19] the addition for arbitrary diagrams follows.An inductive definition of the derivative is obtained by an explicit diagrammatic representation of the product rules.
In an attempt to give a ready-to-use toolbox for differentiation, we provide an easy and convenient way to compute the derivative for the family of linear diagrams ZX(β) [JPV19], that is diagrams where angles might depend linearly (with integer coefficients) on one parameter β.Most of the circuits that are used for variational algorithms belong to ZX(β) and we believe that our formulas will make their analysis much simpler.
A definition for a derivative similar to our formulas was obtained in the independent work [WY22].This work uses W-spiders [Had15] to handle product rules.In contrast to our result, the diagrammatic differentiation presented in [WY22] maps to a ZX-diagram a diagram from another language called algebraic ZX-calculus [Wan20].The algebraic ZX-calculus is convenient to represent arbitrary complex numbers, therefore their differentiation procedure can handle more general families of diagrams than ZX(β).This advantage comes with the cost of abandoning the legacy of the vanilla ZX-calculus which is by far the most popular graphical calculus for quantum computing.
In the end, we show how our result together with the Stone's theorem [Sto32] allows finding a ZX-diagram representing an Ising Hamiltonian.In variational algorithms Ising Hamiltonians are typically used in the definition of the loss function for parameter optimization.In practical applications Hamiltonians are usually given as a sum of local terms H = k i=1 H i where each H i acts only on a limited number of qubits.Small individual terms H i can be relatively easily represented as diagrams.For instance, for the Hamiltonian H = u,v Z u Z v diagrams for individual terms are trivial: H u,v = π π .However, in the traditional framework there is no graphical procedure that constructs the overall sum H out of such elementary blocks.
The ability to perform addition directly leads to a procedure for differentiation.On the other direction, with Stone's theorem some specific kinds of sums might be straightforwardly expressed as diagrams if we can compute derivatives.We demonstrate how to combine our formula for the derivatives and the theorem to find a diagrammatic representation for example Hamiltonian.
Structure of the paper.In Section 2, we give a brief introduction to the ZX-calculus followed by some useful lemmas.In Section 3, we provide a general overview of addition and differentiation in quantum computing.In particular, we show how addition may be represented in circuit notation.We continue by giving an overview of the potential benefits coming from using ZX-diagrams to reason about derivatives.This section, alongside additional examples and explanations, constitutes the major extension of the current work with respect to the previous version [JPV22].
In the next Section 4 we introduce an inductive procedure for the transformation of a diagram into a controlled form.We show how this procedure leads to an algorithm for the addition of ZX-diagrams.In Section 5, we provide two approaches for the differentiation, including an inductive definition (directly inspired by the product rules) and compact formulas for derivatives of linear diagrams ZX(β) where β is a vector of parameters.In Section 6, we show how to apply our formulas to obtain a diagram for an Ising Hamiltonian.As in most works on ZX-calculus, an empty spider denotes the spider with α = 0.A diagram with no input/output is called a scalar.To compactly write scalar factors, we introduce syntactic sugar [−] ⊗n .For any scalar d : 0 → 0 the notation

ZX-calculus
Semantically, ZX-diagrams are standardly interpreted as linear maps, and thus they can be used to represent quantum evolutions.Definition 2.2.For any ZX-diagram D : n → m, let D ∈ M 2 m ,2 n (C) be inductively defined as: |ij⟩⟨ji| where bra-ket notations are used: Example 2.3.Complex numbers can be represented with diagrams: Sometimes it is meaningful to consider diagrams with angles from a restricted sub-group G of R/2πZ.Such restrictions lead to fragments of the language, denoted ZX G -calculus [JPV19].The standard interpretation associates to each In particular, the π 2 -(resp.π-) fragment2 , also called Clifford (resp.real Clifford) fragment, enjoys nice properties [Bac14,DP14] but is not universal for quantum computing, even approximately.Furthermore, any quantum computation that can be expressed in this fragment can be efficiently simulated on a classical computer.As soon as the group contains the angle π 4 , the corresponding fragment is approximatively universal for quantum computing: any 2 n × 2 n unitary transformation can be approximated by a ZX-diagram from this fragment with arbitrary precision.In particular the π 4 -fragment, also called 'Clifford+T' fragment has be extensively studied [JPV18a,JPVW17,NW18].Other finitely generated fragments have been considered in [JPV19].
Notice that for any sub-group G of R/2πZ that contains π 4 , ZX G -diagrams are universal [JPV19] in the sense that for any matrix 2.2.The calculus.Two ZX-diagrams may have the same interpretation, as a consequence the language is equipped with a set of rewrite rules (Fig. 1) that allows to transform diagrams.
In addition, ZX-diagrams can be deformed at will: all wires may be bent in any manner that keeps intact the order of inputs and outputs.It is also allowed to arbitrarily change the order of wires for greed and red spiders and the Hadamard.Corresponding transformation rules are aggregated under the paradigm Only topology matters: As a direct consequence of the axiom (S1), these rules also hold for non-empty spiders.They also imply that whenever a wire has no free ends, we can draw it in any way, f.e. as a horizontal line.
We denote ZX ⊢ D 1 = D 2 if D 1 may be transformed to D 2 by local application of rewriting rules.
The ZX-calculus is sound, i.e. the rules preserve the semantics: if ZX ⊢ D 1 = D 2 then D 1 = D 2 .The converse property is called completeness.The set of rules (Fig. 1) was proven complete for the π 4 -fragment [JPV19], and a single extra-rule makes the language complete for arbitrary diagrams [JPV18b].Notice that alternative sets of rules have been shown to be complete for general ZX-diagrams [HNW18,Vil19].We choose to consider the rules of Fig. 1 as they have been used to study diagrams with parameters in [JPV18b]. . . .
We also introduce some new lemmas that will be useful in the following sections.The proofs serve as an illustration of the graphical computation process.
Lemma 2.23.(2.3) Proof (lemma 2.25).The left hand side is: For the right hand side we get: The lemma condition follows from the completeness of ZX-calculus.
2.4.Linear diagrams.In ZX-diagrams the angles inside spiders are real numbers or elements of some group G.However, in many equations such as (S1), (K), (H), and (SUP) we used symbols α and β instead of numbers.The symbolic notation was extremely handy to define families of equalities, i.e. equalities that hold for any real values of angles.By explicitly keeping symbols inside spiders we can draw parameterized diagrams.A parameterized diagram with n different symbols can be interpreted as a function that associates to each element of the space R n a well-defined ZX-diagram.
, , , , , , combined by tensor product and composition such that α is of the form i n i β i + c with n i ∈ Z and c ∈ L.
In other words, in a parameterized linear diagram D = D(β 1 , . . ., β k ) each angle α of a (red/green) spider is of the form i n i β i + c for some integer n i and c ∈ L It was shown in [JPV18b] that for L = { nπ 4 } n∈Z the Clifford+T axiomatization (Fig. 1) is complete for linear diagrams.
The family of linear diagrams may appear restricted compared to ZX-diagrams that allow angles from a more general class of functions.It is, however, sufficient for applications in variational quantum algorithms as they use circuits where parameters appear in a linear fashion [Pre18].More importantly, for this family we are able to demonstrate simple formulas for the derivative.We believe that such formulas are not obtainable even for a slightly more general fragment ZX An where angles belong to the group of affine functions where coefficients are real numbers).Intuitively, the difficulty comes from the absence of a simple representation of a general matrix over real numbers in terms of spiders.This restriction is removed in the algebraic ZX-calculus [Wan20] at the cost of an extended set of generators.

Related work
In this section we provide a broader context for addition and differentiation in quantum computing.In particular, we discuss how the addition of two unitary matrices may appear in quantum applications and how it can be expressed in the circuit notation [CW12].We also review other attempts to manipulate sums of ZX-diagrams.
The differentiation in quantum computing appears in the context of variational algorithms.We review how derivatives of ZX-diagrams may be helpful for the analysis of these algorithms, in particular in the detection of barren plateaus and in the design of parameter shift rules.These ideas point out possible directions for future work.
3.1.Addition in quantum computing.Addition with circuits.In pure qubit quantum mechanics computations are performed by unitary transformations.Unitary transformations form a group with respect to multiplication, but the sum of two unitary transformations U a and U b is not unitary.
However, in some context it is meaningful to consider transformations of the form shown that such an approach typically outperforms the traditional technique for simulation based on Trotterization.
The protocol for the addition of the unitaries U a and U b in [CW12] is: where q a is an ancilla qubit initialized in a zero-state, and we have access to the controlledversion of the unitary gates U a and U b .
The protocol (3.1) can be easily verified.Starting in the state |0⟩|ψ⟩ we obtain: If the ancilla qubit is measured in the |0⟩ state, the input state |ψ⟩ was successfully transformed We remark that protocol (3.1) uses an ancilla qubit and post-selection on the measurement output and, therefore, exceeds the framework of pure qubit computations.We also emphasize that the circuit involves controlled versions of unitary transformations.In order to be executed on a quantum computer, the controlled unitaries have to be compiled into a sequence of elementary gates.An efficient compilation is non-trivial even for a simple controlled-controlled-not gate usually called Toffoli gate.Interestingly, the use of graphical representations may enhance the compilation of controlled gates [KvdWK19].
Addition with the ZX-Calculus.In the context of the ZX-calculus, there is no natural way to perform a linear combination of arbitrary ZX-diagrams.Fundamentally, this deficiency is due to the absence of a proper physical interpretation for a sum of diagrams.Indeed, ZX-calculus is a language for a process theory representation of quantum computing -i.e. each diagram is a process [CK17].The composition of processes can be interpreted as their sequential application, the tensor product corresponds to the parallel application, but the addition doesn't have a meaningful physical interpretation.
Several attempts were made to address this issue.The first option selected in [TYd21,SH22] consists in an extension of the formalism from diagrams to bags of diagrams, i.e. formal sums of diagrams.The use of formal sums means computations and diagram simplifications cannot be done purely in the context of diagrams, but also require basic calculus.
Independently from our result, [YWS] suggested an alternative protocol for the addition of two ZX-diagrams.This article uses the algebraic ZXW-calculus instead of vanilla ZX-calculus.
There are some similarities with our work, in the sense that their result can also be seen as based on a translation to a controlled form.However, their definition of a controlled form is not equivalent to ours.In addition, instead of inductive procedure, they rely on a decomposition of a diagram on elementary matrices that have an exponential complexity in the worst-case.A more detailed comparison of the two approaches is presented in Section 4.3.

Differentiation in variational algorithms.
In quantum computing a variational algorithm (VQA) is defined by a parameterized quantum circuit (PQC) (also called ansatz ) and a loss function that helps to discriminate between the different values of the parameters.Usually, the loss function is given as an expectation of an energy operator in the state prepared by PQC.Such expectation can be represented as a scalar ZX-diagram with some angles depending on the parameters.
Many state-of-the-art methods for the optimization of the loss function such as Quantum Analytic Descent [KB22] and meta-learning optimizers [WSW + 21] use derivatives [GS17].For efficient training it is important to detect deficiencies in the parameter landscape.In particular, gradient-based methods fail to coverge to an optimal solution in the presence of so-called barren plateaus -zones with exponentially vanishing gradient values.It was shown that some specific structure of the cost function [CSV + 21] or, alternatively, the structure of the ansatz itself [MBS + 18] may lead to a landscape containing such zones.
As was demonstrated in [ZG21], the diagrammatic representation is beneficial in the analysis of the barren plateau phenomena.Yet the work [ZG21] pointed out a crucial obstacle limiting the application of ZX-calculus to variational algorithms -notably the absence of convenient tools for the addition and differentiation of parametrized diagrams.Due to this obstacle, in [ZG21] the analysis was limited to diagrams with only two occurrences of the parameter.In [TYd21] an arbitrary number of parameter occurrences is tackled with explicitly represented product rules leading to bags of diagrams.
Other than in the analysis of the barren plateau, diagrammatic representation can be used to design parameter shift rules.
Parameter shift rules.Parameter shift rules appeared as a solution to the problem of gradient evaluation.Indeed, the gradient of the loss function ∂⟨ψ 0 |U † (α)OU (α)|ψ 0 ⟩ ∂α i (where O is the energy operator) in general can't be directly evaluated on quantum hardware.However, in some cases the gradient can be expressed as a linear combination of the loss function values at different points: where The values L(α k ) can be computed with stochastic approximation.
Decompositions of the form (3.4) are called parameter shift rules.We remark that the parameter shift rules lead to an unbiased estimator for the gradient compared to the finite difference approximation ∂L ∂α (α 0 ) = L(α 0 +δ)−L(α 0 ) δ which, in turn, enhances the parameter optimization process [MBK21].
The first shift rule was derived for the case U (α) = W e iαH V where W and V don't depend on α, while H (called generator ) has two eigenvalues +1 and −1 [MNKF18].It was further extended to generators with arbitrary symmetric eigenvalues +r and −r [SBG + 19]: For more complex U (α) we can decompose U (α) on a product of elementary matrices and use product rules.Alternatively, more sophisticated techniques [WIWL22, KE21, ILY21] involve a spectral decomposition of the generator H.These advanced rules imply a smaller number of elements in the linear combination (3.4) compared to the decomposition, leading to an economy in the number of calls made to the quantum computer.
We remark that simply having a ZX-diagram for the derivative of the loss function is not enough to design new shift rules.We need to further extend the toolbox of ZX-calculus, for instance, we may need a procedure that compiles a diagram into a linear combination of other diagrams corresponding to valid quantum expectations.This extension constitutes one of the possible directions for future work.

Addition of ZX-diagrams
The addition is a natural operation on matrices from a Hilbert space.However, it doesn't correspond to a physical process, hence it is not reflected in the standard ZX-calculus [CK17].
It is not hard to show that it is not possible directly, from two diagrams D 1 and D 2 corresponding to two matrices M 1 and M 2 , to obtain a diagram corresponding to the sum M 1 + M 2 by just plugging D 1 and D 2 into a bigger diagram.Indeed, any diagram that contains a subdiagram representing the 0 matrix also represents a 0 matrix.So if D 1 represents a 0 matrix and D 2 represents a nonzero matrix, then plugging D 1 and D 2 into a bigger diagram will give a zero matrix, and therefore will not represent the sum of the two matrices.
On the other hand, for any two diagrams D 1 , D 2 : n → m, the universality of the ZX-calculus guarantees that there exists a diagram We provide in this section a general construction for such a diagram.
The key ingredient is to use controlled versions of the original diagrams, rather than the original diagrams.As pointed out in [JPV19] Intuitively, a controlled state is a way to encode the state C |1⟩.A controlled state with no outputs is called controlled scalar.For instance, the scalar 0 could be encoded by the controlled scalar C 0 = . Indeed, we can explicitly check that C 0 |0⟩ = 1 and It will be more useful in what follows to perform graphical proofs instead of matrix computation to manipulate controlled states.With these rules, we verify the condition Controlled states have nice properties that allows to perform element-wise addition and tensor product of corresponding vectors: Lemma 4.3 (Sum and tensor product [JPV19]).For any controlled states C 1 , C 2 : 1 → n and C 3 : 1 → m the diagrams: The top part of the diagram can be transformed as follows: (4.7) The statement follows from (S2) and (S1).
For the second equality we have: The lemma follows from the definition of the controlled state.
We follow in this work the definition of a controlled state in [JPV19].There exist other definitions, see e.g.[YWS], which essentially differ in the output of the zero-case.4.2.Controlizer.The lemma 4.3 provides a way to obtain the sum of two diagrams in a controlled state form.
The definition and the lemma technically allow only to solve the problem for states rather than arbitrary matrices, but notice that going from a matrix n → m to a state 0 → n + m is just a matter of bending wires.We remark that bending wires alone can't lead to a procedure for addition, as there is no obvious way to directly sum state diagrams without controlled versions.
What remains to be done is to explain how to compute algorithmically, from a state (diagram) D, a controlled version of the same state (diagram).
We introduce for this the notion of a controlizer -a map that associates diagrams with the corresponding controlled states.Several maps satisfy the definition of the controlizer.For instance, the map presented in [JPV19] that associates to each diagram its normal form is in fact a controlizer.In [YWS] an alternative controlled state form is constructed from the 2 n coefficients of the algebraic normal form.
In our approach the controlizer is defined by induction over the composition and the tensor product and doesn't rely on normal forms.It means that the shape of the controlizer will follow closely the shape of the original diagram.In terms of complexity, this also implies that the size of the controlizer output is polynomial in the size of the diagram.• For the generators β , , , , , : can be decomposed as follows using the above generators: We remark that the output of the inductive controlizer is not unique as it depends on the decomposition order.For instance, two diagrams that are equivalent up to deformation (the topology matters rule) will produce different outputs, that are not equivalent up to deformation.However, the semantics is preserved: both representations will of course represent the same matrix.
We now verify that our inductive controlizer 4.7 satisfies the definition 4.6: is a direct consequence of the axiom (S1).First, we inductively prove that for all diagrams D the diagram C(D) is controlled state.
• Composition: let's assume that C(D 1 ) and C(D 2 ) are controlled states.We show that C(D 1 • D 2 ) is also a controlled state: Next we show that the map C satisfy the property (4.9), i.e. it correctly encodes the input diagram.
Proof (theorem 4.8).The theorem follows from the definition of the controlizer and the lemma 4.3.
We illustrate the diagrammatic addition with a simple example.(4.27) In this example we simplified the diagram with graphical rewriting to obtain a known result: 4.3.Discussion.The previous example shows that our inductive procedure leads to large diagrams, even when the initial diagrams are fairly simple.The resulting diagrams roughly follow the shape of the initial diagrams, but are still quite different.This defect is however consistent with intuition.Indeed, contrary to sequential and parallel compositions, the addition is not a physical operation, hence it is not surprising that can't be easily incorporated into the framework of ZX-diagrams.On the positive side, we can rely on the powerful equation theory of the ZX-calculus to simplify, when it is possible, the diagrams representing the sum of diagrams.An alternative approach for the addition in the algebraic ZXW-calculus was presented in [YWS].It also relies on the controlled forms of the diagram, but their controlled forms are different from ours.Notably, contrary to our approach that accepts diagrams with an arbitrary number of input and output wires, the controlled forms in [YWS] are defined only for two families of diagrams: state diagrams ZX[0, n] and diagrams ZX[m, m] that correspond to square matrices.Controlled matrices [YWS] are built from the decomposition of the initial diagram D on a product of diagrams for so-called elementary matrices [WY21].In the general case, the decomposition on elementary matrices seems to require an explicit computation of the matrix corresponding to the diagram.Therefore, although for given controlled forms the addition from [YWS] is done diagrammatically with the help of W-spider, the computation of the controlled forms uses the semantics.In contrast, in our inductive procedure the entire process is maintained in the graphical framework.
However, in some special cases of practical importance the approach suggested in [YWS] is beneficial.For instance, as was shown in [YWS] that for local Hamiltonians made out of Pauli tensors the decomposition on elementary matrices is straightforward.As a consequence, the controlled versions are easy to compute and the overall procedure leads to better-looking diagrams.

Differentiation of ZX-diagrams
A basic definition of the derivative is the derivative for a smooth function f : R → R: In traditional calculus the derivative usually is computed from the decomposition of the function f : R → R on a sum, product, and composition of elementary functions.Derivatives for compositions of function functions are computed using linearity and the product rules: and similarly for the tensor product.
A parameterized ZX-diagram D(β) can be understood as a function f : R → ZX that associates to each real value β ∈ R a ZX-diagram.In the context of variational algorithms, we consider the derivatives over the parameters.
In order to keep the notations simple in what follows we restrict our attention to the set of linear diagrams over one variable, denoted by ZX(β).We denote the corresponding matrices by M(β).The elements of matrices in M(β) belong to the ring generated by complex numbers and the smooth functions e iA where A = {kβ | k ∈ Z} is the group of affine functions with integer coefficients.We remark that even if we restrict the consideration to diagrams over one variable, all results are easily extended to the case of partial derivatives ∂ β i for linear diagrams with an arbitrary number of variables mutatis mutandis.
The sum for the matrices in M(β) is naturally defined by entry-wise addition.Therefore, we can define the derivative in M(β) by specifying the general definition for monoidal categories with sums given in [TYd21]: is a linear unary operator on M(β) that associates to a matrix L ∈ M(β), L : n → m another matrix L ′ ∈ M(β), L ′ : n → m of the same dimensions.It satisfies the following axioms: Rather than defining the derivative as a limit of finite difference and deriving the product rules, the work [TYd21] suggests an axiomatic definition that proclaims product rules as the fundamental property of the derivative operator.We remark that the entry-wise differentiation of matrix elements satisfies this definition.
Although our theorem 4.8 provides a fully diagrammatic way for the addition of diagrams, we avoid extending the language with a formal introduction of a sum operator in order to circumvent unnecessary complications.Therefore, the general definition from [TYd21] doesn't apply.Instead, we suggest an alternative semantics for the derivative in the ZXcalculus requiring the coherence between the derivative of a diagram and the derivative of the corresponding linear map: m] is a unary operator that commutes with the standard interpretation: where ∂ M is an (arbitrary) fixed derivative in M(β) satisfying the definition (5.1).
The property (5.2) is called diagrammatic differentiation.

Diagrammatic differentiation with controlizers.
The derivative in M(β) is defined through product rules that involve sums that will be translated to the diagrammatic framework using controlizers.• Generators: For parametrized spiders: For all other generators g : n → m that do not contain an instance of the variable β, we define ∆(g) = . . .n+m .
(5.5) Similarly to controlizers, a step-by-step application of the map ∆ may lead to different diagrams depending on the order of decomposition on tensor products and compositions.However, all possible outputs are semantically equivalent and, by the completeness of ZX-calculus, are equivalent as diagrams.
By combining C-derivatives and controlizers we can reproduce the semantics of product rules in a graphical representation: Proof (theorem 5.6).We give the proof by induction over tensor product and composition.
• Generators: First, we show that Indeed, we have: we verify the property of diagrammatic differentiation: For a generator g : n → m that doesn't depend on β: π ⊗ . . .= 0 n×m (5.12)

=
(5.17) The claim follows from the induction assumption and the definition of controlizer.
• Composition: Following the same reasoning as for the tensor product we obtain for where the diagram T is exactly the same as in (5.15).On more time, by distributivity of the sum in M(β) over the composition we obtain (5.21) (5.22) The claim follows from the definition of controlizer an the induction assumption.We consider a simple example to illustrate how to perform the inductive procedure.
Example 5.7.We apply the definition 5.5 to the simple scalar diagram .By definition, ∆ 5.2.Formula for derivatives in ZX(β).Although perfectly correct, the differentiation procedure described above leads to very puzzling output even for small diagrams (see example 5.7).In this section we provide a simpler approach to obtain the derivative of a diagram in ZX(β) written in a special form.We formalize it in definitions ∂ ZX and ∂ P of unary operators that satisfy the property of diagrammatic differentiation (5.2).First, we remark that even in the inductive procedure the effort can be significantly reduced if we separate the parts that depend on the parameter from the rest of the diagram that is constant on β.Indeed, for constant diagrams A : k → l and B : m → k that do not depend on β, the C-derivative is the controlled version of the zero diagrams C k+l 0 and C m+k 0 .Therefore, for a parameterized diagram D(β) : n → m the lemma 4.4 implies :

⊗2m
(5.24)These expressions are significantly simpler that the general case (5.4) where both parts of the compositions depend on β.
We want to further explore this property to find simplified formulas for the derivative.
Let's denote by X β (n, m) the family of diagrams β . . .
Claim 5.8.Using the rules of ZX-calculus, each diagram D(β) from ZX(β) may be transformed into the form where n, m are some integer numbers and D 1 , D 2 are constant with respect to β.We call diagrams in this form β-factored.
A rigorous demonstration of claim 5.8 may be found in [JPV18b].It essentially follows from the (H) rule to transform any green spider into a red spider, the (S1) rule to separate the β part from the rest of the diagram, and the paradigm Only topology matters to rearrange the diagram.
The diagram X β (n, m) has a particularly regular form, so we expect it to have a nicelooking derivative.We would like to develop the intuition from the semantics ∂ M X β (n, m) .5.2.1.Intuition.Due to the form obtained in the previous claim, it remains to find the derivative of X β (n, m) to obtain the desired results.
For reasons that will become more clear later, we first focus on a variant of X β (n, m) with green spiders instead of red spiders.By applying entrywise differentiation to the vector |β(n, m)⟩, we obtain the following result: Therefore, obtaining the derivative of (|β(n, m)⟩) requires the ability to produce the matrix M ∆ .
The first step is finding a diagram for the controlled-triangle D λ : 2 → 2. Semantically, we want the diagram D λ to apply the triangle to the second input if the first one is in the |1⟩ state and do nothing otherwise.We denote the matrix corresponding to D λ by coefficient α x is:  (5.39) We remark that according to the definition 5.2 the derivative D ′ : n → m of a diagram D : n → m that is constant on β is such that D ′ = ∂ M D = (0) n×m .Therefore, theorem 5.10 is a direct consequence of the following lemma: Lemma 5.11.For any n, m: (5.40) Proof.We prove the lemma by induction.We provide the demonstration for n = n + 1, the proof for m = m + 1 is directly obtainable in the same way.
Base: We already know that ∂ M β = ie iβ |−⟩.Thus, for the case n = 1, m = 0 the lemma statement follows from: (5.41) Step: By induction, we assume that the equation (5.40) holds for some n and m.We show that under this assumption In order to prove the induction we will proceed in three steps.Firstly we apply the product rule to the matrix ∂ M X β (n + 1, m) : (5.42) where the second equality follows from the inductive assumption.Secondly, we show the last sum in the equation (5.42) can be represented diagrammatically with a controlled state, i.e. the following claim holds: Claim 5.12.We can find a controlled state X : 1 → n + 1 + m and a constant scalar Finally, we will show that we can transform the diagram with a sequence of graphical rewrites: Claim 5.13.
As the rewrite rules preserve the semantics, it follows from the claims above that: (5.45) 10:35 Proofs.While proving the claims 5.12 and 5.13 we will repeatedly use the following lemmas: Lemma 5.14. (5.47) are controlled states such that  claim 5.12.We introduce the diagram: where C ±β and C (n,m) β are defined in the previous lemma (5.15).From these definitions and the lemma (4.3) follows that (5.50) Therefore the claim holds for the controlled state (5.49) and the scalar c =  We suggest a more compact formula for diagrams in what we call pair-factored form: Lemma 5.16.The diagram: (5.53) The lemma (5.16) can be proven by applying the same approach as in the proof of the formula (5.11) for individual spiders.We leave the exact proof to the reader.
It is possible to extend 5.16 to find the derivative for X β (n, m) when n ̸ = m.Indeed, it is always possible to transform a diagram into an equivalent diagram that has the same number of occurrences of β and −β, using the fact that ±β = .For instance, if n > m: where σ is some wire permutation and (5.54) Example 5.17.We apply 5.16 to the same diagram as in 5.7:

⊗2
(5.55) We observe that using the formula for the pair-factored form (5.53) we obtain a much more compact result than with the inductive procedure (see the example 5.7).Even if both approaches lead to diagrams with the same semantics, in practice the diagrams obtained with the formulas (5.53) and (5.38) are less verbose.For this reason they are easier to manipulate.5.4.Discussion.Contrary to derivatives defined in [TYd21] and [ZG21] in our approaches a derivative of a ZX-diagram is another ZX-diagram.
In the inductive approach we proceed by integration of the product rules using controlizers.We also observe that as the derivative of a constant diagram is trivial, it is beneficial to "factor-out" the part that depends on the parameter.We adopted this approach in formulas for diagrams in β-factored form and pair-factored forms.To derive the formulas, we got the intuition in the desired semantics of the diagrams.Thenceforth, our formulas were rigorously proven by induction.
A result similar to our simplified formula for β-factored forms was independently derived in [WY22].The major difference between our formula and the method shown in [WY22] is the considered language.Indeed, in our work we operate ZX-diagrams while Wang and Yeung consider diagrams from the more expressive algebraic ZX-calculus.
The difficulty to represent derivatives for non-linear diagrams follows from the fact that there is no simple way to represent real numbers in the vanilla ZX-calculus.In the algebraic ZX-calculus this restriction is removed.As a consequence, when an algebraic ZX-diagram is parameterized by an arbitrary derivable function f (x), the differentiated algebraic ZX-diagram is parametrized by f ′ (x).

Diagrammatic representation of Ising Hamiltonians
The addition of diagrams naturally appears in the representation of Hamiltonians.Usually, Hamiltonians are given as a weighted sum of Pauli tensors (see [Ves22] for an introduction to Hamiltonians): Pauli tensors have a particularly simple decomposition on elementary matrices.Therefore, the procedure described in [YWS] is well-suited for the construction of diagrammatic representation for such Hamiltonians.
In addition, the work [YWS] also suggests a way to exponentiate diagrams representing Hamiltonians.The exponentiate of a Hamiltonian H is a unitary e iθH .The exponentiation is done with the aid of the Cayley-Hamilton theorem.The Cayley-Hamilton theorem allows to decompose the exponent e iθH on a linear combination of We remark that in a particular case when H = i H i is a sum of commuting terms it is usually straightforward to obtain e iθH as a product of individual e iθH i .However, if some local terms in H don't commute the product decomposition doesn't apply.For instance, this is the case for modified mixer Hamiltonians used in the extension of QAOA to constrained optimization problems [HWO + 19].
In this section, we will also consider an inverse problem.Using Stone's theorem, we demonstrate how to obtain a diagram for H given the derivative of the diagram for e iθH .
The Stone theorem relating the derivative of a unitary group e iγH to its generator H was used before in [TYd21].However, in [TYd21] the derivatives were defined as formal sums of diagrams.As a consequence, the work [TYd21] didn't suggest a single diagram representation for a Hamiltonian but rather a representation as a sum of diagrams.
We remark that for an Ising Hamiltonian H the diagram D U (β) of the linear map U (β) = e iβH is easy to find.For Hamiltonians with integer coefficients the matrix U (β) = e iβH belongs to M(β).It satisfies the definition of a strongly continuous one-parameter unitary group: Definition 6.1 (Unitary group [TYd21]).A one-parameter unitary group is a unitary matrix U : n → n in M(β) with U (0) = id n and U (β)U (β ′ ) = U (β + β ′ ) for all β, β ′ ∈ R. It is strongly continuous when lim β→β 0 U (β) = U (β 0 ) for all β 0 ∈ R.

Theorem 6.2 (Stone ([Sto32]))
. There is a one-to-one correspondence between strongly continuous one-parameter unitary groups U : n → n in M(β) and self-adjoint matrices H : n → n in M. The bijection is given explicitly by U (β) = e iβH and H = −i(∂ M U )(0).
We use the bijection from Stone's theorem to find the diagram h ∈ ZX R such that h = H.Using the property U (0) = id n we obtain: where the third equality is due to the fact that the evaluation commutes with the standard interpretation.
We give an example of a diagram for an Ising Hamiltonian obtained via our approach.It is of course possible to obtain a diagram for H using the addition procedure we obtained earlier, rather than going through computing its exponential and then differentiating.However, in the case when H is Ising, the exponential of H is typically easier to obtain than H itself.

Discussions
In this work, we have introduced an inductive definition for addition of ZX-diagrams, that we have then used to introduce an inductive definition of the differentiation of ZX-diagrams.Addition and differentiation are essential tools for the development and the study of quantum algorithms, but, as a matter of fact, both of them are leading to large diagrams, even when the initial diagrams are fairly simple.
In sections 5.2 and 5.3, we have shown that instead of simplifying the resulting diagrams a posteriori, one can a priori put the initial diagrams in an appropriate form.While this approach is not inductive anymore, it seems to ease the differentiation of diagrams in practice.
As an application we have shown that our result allows the construction of diagrams for Ising Hamiltonians and for derivatives of parametrized circuits.Therefore, it becomes possible to study variational algorithms entirely within the ZX-calculus.In particular, we can use .We believe that it will lead to a better understanding of the potential of variational algorithms and of their applications to real-world problems.

2. 1 .
Syntax and Semantics.The ZX-diagrams are generated by green spiders α where both kinds of spiders have an arbitrary number of inputs/outputs and are decorated with angles.ZX-diagrams are also made of wires: the identity , the swap and also the possibility to bend wires with a cup and a cap .A wire can connect two nodes or, alternatively, have free ends that point either up or down.Wires with free ends pointing towards the top (towards the bottom) of the diagram are called inputs (outputs).We denote by D : n → m a diagram with n inputs and m outputs.Finally, the empty diagram is denoted with .Definition 2.1.ZX-diagrams are inductively defined as follows: for n, m ∈ N and α ∈ R/2πZ, diagrams, and for any ZX-diagrams D 0 : a → b, D 1 : b → c, and D 2 : c → d, D 1 • D 0 : a → c and D 0 ⊗ D 2 : a + c → b + d are ZX-diagrams.Pictorially:

Figure 1 .
Figure1.Axioms for ZX as presented in[JPV19].All rules stay true flipped upside down and with inverted colors.Families of equations are given using 'dots': . . .means any number of wires, . . .means at least one wire.
, one can inductively define the addition on 'controlled' versions of the diagrams.A controlled version of a diagram D 0 is roughly speaking a diagram with an extra input such that when this extra input is set to |1⟩ the diagram behaves as D 0 and when it is set to |0⟩ the diagram behaves as a neutral diagram.Roughly speaking, the addition procedure puts controlled versions of two diagrams aside and plugs a diagram corresponding to const(|10⟩ + |01⟩) to the inputs.To build a controlled version of a diagram our construction uses controlled states originally introduced in [JPV19].4.1.Controlled states. Definition

n.
The diagram corresponding to the encoded state is simply π C . . .D . ..= .Example 4.2 (Proven in[JPV19]).Diagrams C 1/ The previous lemma can be used to add two controlled states.More precisely, if one of the controlled states encodes a state diagram D 1 : 0 → n and the second controlled state encodes D 2 : 0 → n the diagram π C+ . . .D+ . . .= correspond to the vector D 1 + D 2 .Moreover, as the construction returns a controlled state we can directly proceed to the addition of new 10:15 terms: for N controlled versions C 1 , . . ., C N of D 1 , . . ., D N the diagram: stateD = D 1 + • • • + D N .We remark that for each number of output wires n the zero-elementC n 0 : 1 → n is a controlled state . . .n. Indeed, we can prove the following lemma:Lemma 4.4.For any controlled state C : 1 → n We start by proving the first equality.The left-hand side is: Notation 4.5.ZX[n, m] denotes the family of all ZX-diagrams with n inputs and m outputs.The formal definition of the controlizer is: Definition 4.6 (Controlizer).We say that a map C : ZX[n, m] → ZX[1, n + m] that associates to every diagram D : n → m a diagram C(D) : 1 → n + m is a controlizer if the following conditions hold for any ZX-diagram D: • C(D) is a controlled state • The state represents D up to some scalars in the sense that: Definition 4.7 (Inductive controlizer).We define the map C : ZX[n, m] → ZX[1, n + m] that associates to each diagram D : n → m a diagram C(D) : 1 → n + m as follows: We prove the claim for the generators β ,

••
Composition: Let's assume that the property (4.9) holds for diagrams D 1 : n → m and D 2 : m → k.The controller for their composition D 1 • D 2 is given by: Tensor product: Let's assume that the property (4.9) holds for diagrams D 1 : n → m and D 2 : k → l.The controller for their tensor D 1 • D 2 is given by: assumption.To give a flavor of our computation process we show how to obtain a controlled version of π : Addition of arbitrary diagrams.The controlizer allows to map every two ZX-diagrams D 1 : n → m and D 2 : n → m to controlled states C 1 : 1 → n + m and C 2 : 1 → n + m.The lemma 4.3 provides a way to obtain a sum of resulting controlled states.By combining this two results we can recover the diagram for addition of D 1 and D 2 .The addition protocol is resumed in the following theorem: Theorem 4.8.For diagrams D 1 : n → m and D 2 : n → m the diagram Example 4.9.Using 4.8, we construct a diagram D corresponding to the addition of and π .

Notation 5. 3 .
In what follows we denote by C any map that satisfies the definition 4.6 of controlizer, for instance the one of 4.7.Definition 5.4.Given a controlizer C, the C-derivative is the map ∆ : ZX(β) → ZX(β) that associates to a parameterized diagram D : n → m another diagram ∆ (D) : 1 → n + m defined as follows: clear from the definition, the C-derivative of a diagram is not derivative of a diagram.Rather, it corresponds to a controlled-version of the derivative.A simple verification shows that for any generator G the C-derivative ∆ (G) is a controlled state.From lemma 4.3 it follows that for every diagram D : n → m the C-derivative ∆ (D) : 1 → n + m is a controlled state.Moreover, as was proven in lemma 4.4 the Cderivative . . .n+m of generators that are independent on β is precisely the zero-element C n+m 0 for the addition of controlled states.The same lemma implies, in particular, that the C-derivative of any diagram D : n → m that is independent on β is . . .n+m .
input wires are used only in control, the output of such a procedure is α x |b 0 , . . ., b n+m ⟩.This procedure can be directly translated in a diagram D λ ; the derivative of X β (n, m) = β . . .β −β . . .−β can be obtained by conjugating every input with Hadamard boxes: 35) 10:33 By applying the rule (H) to the final we obtain ∂ M X β (n, m) = Hadamard boxes from D λ have disappeared in the expression of ∂ M X β (n, m) , which explains why we chose initially red spiders instead of green spiders.5.2.2.Diagrammatic differentiation of β-factored forms.Motivated by the simplicity of the result (5.36), we suggest an alternative non-inductive definition for derivatives of diagrams in β-factored forms: Definition 5.9.Given a diagram D is inspired by the intuitive reflection presented above, we provide a rigorous diagrammatic demonstration of the soundness of our definition.More precisely, we prove the following theorem: Theorem 5.10.The operator ∂ ZX [−] defined at (5.9) satisfies the property of diagrammatic differentiation: for any diagram D(β) ∈ ZX(β) in β-factored form, ∂ ZX D(β) = ∂ M D(β) 5.48) Proof.For the diagram C πβ the claim follows directly from the definition.For the diagram C (n,m) βwe check two cases: