Synthesizing and tuning stochastic chemical reaction networks with specified behaviours

Methods from stochastic dynamical systems theory have been instrumental in understanding the behaviours of chemical reaction networks (CRNs) arising in natural systems. However, considerably less attention has been given to the inverse problem of synthesizing CRNs with a specified behaviour, which is important for the forward engineering of biological systems. Here, we present a method for generating discrete-state stochastic CRNs from functional specifications, which combines synthesis of reactions using satisfiability modulo theories and parameter optimization using Markov chain Monte Carlo. First, we identify candidate CRNs that have the possibility to produce correct computations for a given finite set of inputs. We then optimize the parameters of each CRN, using a combination of stochastic search techniques applied to the chemical master equation, to improve the probability of correct behaviour and rule out spurious solutions. In addition, we use techniques from continuous-time Markov chain theory to analyse the expected termination time for each CRN. We illustrate our approach by synthesizing CRNs for probabilistically computing majority, maximum and division, producing both known and previously unknown networks, including a novel CRN for probabilistically computing the maximum of two species. In future, synthesis techniques such as these could be used to automate the design of engineered biological circuits and chemical systems.


Introduction
A central goal of synthetic biology is to implement specified behaviours in biological systems. Chemical reaction networks (CRNs) have been used extensively to model a broad range of biological systems, gene regulatory networks [1], synthetic logic circuits [2] and molecular programs built from DNA [3].
Extensive theoretical understanding exists about the behaviour of a multitude of CRNs, and the behaviour of some networks has been exhaustively explored [4]. Methods also exist to convert CRNs into equivalent physical implementations, based on DNA strand displacement [3,5]. CRNs also provide a common language for expressing problems studied in computer science theory, including Petri nets and population protocols [6], as well as control theory and engineering [7]. The computational power of CRNs has been extensively studied [6], and it has been shown that error-free stably computing CRNs [8] compute exactly the class of semi-linear functions [9,10]. If the stability restriction is relaxed and we allow the CRN to sometimes compute the wrong answer, then it is possible to implement a register machine, meaning that CRNs are equivalent in power to Turing machines, up to a finite error [6,11,12]. Therefore, given the expressive power of CRNs and their direct correspondence to biological implementations, we sought to develop a methodology for synthesizing candidate CRNs that exhibit a specified behaviour.
Although there are procedures to generate CRNs for semilinear predicates [10] and functions [9], primitive recursive functions [6], ordinary differential equations (ODEs) [13] or Turing machines [6,12], the proposal of practical CRNs that (either stably or probabilistically) compute a given function has hitherto mostly been a manual effort. The most obvious practical consideration is that the number of components should not be too large. As an example, for synthetic gene circuits, each component must be introduced into an organism, characterized and checked for orthogonality with other engineered components, which presents a strong preference towards smaller circuits. Furthermore, each component brings a burden to the host cell resources, the extent of which is a complex relationship between the strength of the promoter and ribosome binding sites and the availability of ribosomes, amino acids, nucleoside triphosphate, etc. Accounting for such complications is difficult to automate, though some effort has been afforded recently for the construction of genetic logic circuits [14].
Two of the most common semantics for CRNs are continuous deterministic and discrete stochastic. In continuous deterministic semantics, the reactions are interpreted as a system of differential equations, on a (continuous) real-valued vector of species concentrations, which describe the evolution of the system according to mass action kinetics over time. In discrete stochastic semantics, the reactions are interpreted as state transitions in a discrete state-space composed of non-negative integer-valued molecule counts. Each state transition occurs at a time drawn from an exponential distribution that is a function of the stoichiometry of the system. The deterministic semantics approximate the mean of the stochastic semantics, but are only accurate at high molecule numbers [15]. There are also continuous stochastic semantics, which incorporate measures of variability, such as the linear noise approximation (LNA) and, more generally, moment closure techniques [16]. In this paper, we are interested in systems with low molecule numbers (40 or fewer) so the term 'CRN' intends the discrete stochastic semantics unless stated otherwise. Moreover, because we are interested in computation performed by a stochastic system, we consider probabilistic computation, where the correct answer is only produced with some probability. If a CRN (with rates) satisfies a specification or problem with non-zero probability we say that it (probabilistically) computes that specification. If a CRN satisfies a specification or problem with probability 1, we say it stably computes the specification.
In this study, we propose the first method to automate the synthesis of discrete stochastic CRNs, by formally specifying a desired input-output relation and automatically identifying CRNs that satisfy this behaviour with high probability. 1 This is in contrast to other methods where CRNs are generated from other formal systems [9,12,13] or to reproduce a time series [18]. First, we identify CRNs that have the capacity to produce correct, finite computations for a given finite set of inputs. The synthesis problem is more challenging than verification, where the goal is to determine the correctness of an existing CRN [19]. We express CRN synthesis as a satisfiability modulo theory (SMT) problem, which can be addressed using solvers such as Z3 [20]. This allows us to generate a number of candidate CRNs of a given size, in terms of the numbers of reactions, species and computation lengths, that satisfy the design constraints, or to prove that no such CRN exists. However, while the existence of correct computations is guaranteed for each generated CRN, the probability of these computations occurring might be low.
To determine whether correct computations can occur with high probability, we next optimize the reaction rates of each generated CRN. To solve the optimization problem, we combine stochastic search strategies based on Markov chain Monte Carlo (MCMC) with numerical integration of the chemical master equation (CME). This part of the problem was recently addressed in [21,22], though applied only to a single input. We specifically focus on uniform CRNs, which have the desirable property that the number of species and reactions does not depend on the input value. We restrict our attention to bimolecular CRNs with precisely two reactants and two products in every reaction. Bimolecular CRNs (with all rates equal to 1) are equivalent to population protocols (PPs) [8] and also guarantee that mass is conserved in the system.
An alternative synthesis strategy was proposed in [18], which used a defined sketch of possible reactions and species of the desired CRN and a specification of the desired temporal behaviour as constraints for an SMT solver. In addition, the LNA was used to search for optimal (in species numbers and accuracy) CRNs and parameters. While the use of such an approximation allows for more efficient synthesis, it assumes that the stochastic mean is identical to the solution of the deterministic rate equations. As a result, many interesting systems that make use of stochasticity at low molecule numbers will not be identified. For example, the division CRN in §3. 3 does not work at all when approximated with the LNA.
More generally, the application of SMT solvers together with sketching approaches, as in [18] for CRN synthesis, have also proven successful in the context of programme synthesis. For example, in [23] a syntax-guided framework was developed for the synthesis of input-output functions, Gulwani et al. [24] focused on the problem of synthesizing loop-free programmes from a library of components, and Bloem et al. [25] considered the synthesis of distributed, fault-tolerant systems. While our approach does not rely on user-specified sketches, we restrict the search space by considering only CRNs with a given number of reactions and species. Furthermore, we allow for probabilistic solutions by identifying systems that are capable of producing correct computation paths for a range of inputs, rather than requiring that all executions for all inputs satisfy the specification as in programme synthesis. A similar strategy has also been used for the synthesis of biological programmes in [26,27], where the goal is to guarantee that certain experimentally observed executions of a system can be reproduced. While Z3 is also used as the backend SMT solver in [26,27], the focus there is on qualitative models of genetic regulatory networks. By contrast, we consider stochastic CRNs as models of biochemical systems, which makes the overall approach and encoding details different and necessitates a rate optimization step in addition to the reaction network synthesis.
SMT solvers such as Z3 support rich combinations of theories that could be used to extend the approach proposed here. For example, stochastic CRNs, rather than their nondeterministic abstractions, can be encoded using the theory of reals or approximated using bit-vectors [28]. However, addressing CRN synthesis while considering probability of correctness requires reasoning over sets of paths, which makes the problem more challenging. The SMT of reals could also be used to reason about reachability in rate-independent CRNs with continuous semantics [29], as an alternative to the use of specialized ODE solvers as in [18].
In this work, our aim is to synthesize CRNs that exploit stochasticity to probabilistically compute a specified function.
The target application is to suggest CRNs that might be implemented in some programmable chemistry such as DNA computing or genetic circuits. For this scenario, we chose to generate and optimize circuits that would be as accurate as possible in a predefined input range and in a predefined time period (enabling the experimenter to indicate how long they are willing to wait). Our method also has applications as a tool for theorists to explore CRNs that compute specified functions.
We first applied our approach to synthesize CRNs capable of solving majority decision-making, a problem well studied in the literature [30][31][32][33][34][35][36]. Our method identified known CRNs that give probabilistic solutions in optimal time [31][32][33] and also revealed a novel asymmetric solution ( §3.1.2). Next we considered the maximum function for which the known stable CRN [9] has four reactions and (in bimolecular form) uses eight species. Our method identified a smaller CRN that to our knowledge is a novel approximation of the maximum function (see §3.2). These examples illustrate the potential for automatically determining CRNs with specified behaviour. Finally, we applied our method to Euclidean division, a non-semi-linear function for which it is believed there are no stable, stochastic CRNs. All CRNs that we enumerated automatically were unable to compute Euclidean division with high probability, though we showed that a larger circuit, compatible with our framework, is a good probabilistic solution. Overall, we show that it is possible to enumerate and optimize CRNs that probabilistically compute a variety of functions.

Preliminaries
A CRN is a tuple C ¼ (L,R), where L ¼ fs 0 , . . ., s N g and R ¼ {r 0 , . . . ,r M } denote the finite sets of species and reactions, respectively. A CRN C 0 ¼ (L 0 ,R 0 ), such that L 0 #L and R 0 # R, is called a subnetwork of C. A reaction is a tuple r ¼ (r r , p r , k r ), where r r and p r are the reactant and product stoichiometry vectors (r r s [ N 0 and p r s [ N 0 denote the stoichiometry of each species s[L in r r and p r , respectively), k r [ R !0 denotes the rate constant of r and k denotes the vector of all reaction rates. Given a reaction r ¼ (r r , p r , k r ), the set of reactants of r is fs[L j r r s . 0g and the set of products of r is fs[L j p r s . 0g. In this paper, we focus on the class of bimolecular CRNs, where P s[L r r s ¼ 2 and P s[L p r s ¼ 2, for all reactions r [ R. The dynamical behaviour of bimolecular CRNs can be understood as follows. The set of all possible system states is 0 represents the number of molecules of each species. We denote the number of molecules of species s[L in state x by x s . Given a reaction r [ R where r r s ¼ 2 for some s[L, the propensity 2 of r on x is k r x ¼ k r . x s . (x s 2 1)/2. If, on the other hand, r r s ¼ r r s 0 ¼ 1 for some species s, s 0 , the propensity of r is k r x ¼ k r . x s . x s 0 . The time at which reaction r would fire, once the system enters state x[X, is stochastic and follows an exponential distribution with a rate determined by the reaction's propensity k r x . Assuming that reaction r is the first one to fire, the state of the system is updated as x 0 s ¼ x s 2 r r s þ p r s for all s[L, where x and x 0 are the current and next states, respectively.
An abstraction of CRNs that preserves reachability but does not consider reaction rates or time is given by the transition system T C ¼ (X,T), where C ¼ (L,R) and the transition relation T is defined as In other words, the choice between reactions from R is non-deterministic but enough molecules of each reactant must be present in state x for the reaction to fire. The transition between states x and x 0 happens when any reaction r [ R fires and the number of molecules is updated accordingly. A path x 0 , x 1 , . . . of T C satisfies T(x i , x iþ1 ) for i ¼ 0, 1, . . . and, given an initial state x 0 , we call state x f reachable from x 0 in T C if there exists a path x 0 , . . ., x f .
Given a CRN C, let X 0 # X denote a finite set of initial states and X r # X denote the set of states reachable from X 0 . C can be represented as a continuous-time Markov chain (CTMC) that preserves information about the transition probabilities and rates that determine the stochastic behaviour of the system and the expected execution times. We define a CTMC to be a tuple M ¼ (X r , p 0 , Q), where X r is a finite set of states, p 0 : X r ! R is the initial distribution of molecule copy numbers of all species, and Q : X r Â X r ! R is a matrix of transition propensities. While the set of initial states is not represented explicitly, it is captured through the initial distribution, i.e. X 0 ¼ fx[X r j p 0 (x) . 0g. A CTMC M C is constructed from a CRN C by first determining the set of reachable states, and then evaluating the propensities of each reaction. The (i, j )th entry of Q, q ij , represents a transition from state x i to state x j . Accordingly, q ii is the remaining probability mass, equal to À P i=j q ij . The transient probability vector p t evolves according to dp t =dt ¼ p t Q, which is known as the chemical master equation (CME).
While the dynamics of the CME depend on the rate constants of the CRN (k), the state graph of the associated CTMC does not. Therefore, when attempting to find CRNs with specific behaviours, it is possible to separate reachability properties from dynamic behaviours, by considering changes in k or not. Eventually, we consider finding rate constants k that are optimal with respect to the desired behaviour. Following [21,37], we therefore introduce the notion of a parametric CTMC ( pCTMC), which is a CTMC with transition rates parametrized by k. More formally, if we denote by P a parameter space, P : R P !0 , then k is instantiated by a parameter point p [ P. Accordingly, given a pCTMC M and parameter space P, an instantiated pCTMC M p ¼ (X, p 0 , Q p ) is an evaluation at point p [ P.

Example
An example CRN C AM defined by consists of the set of species fA, B, Xg. Reaction (2.2a) has two reactants, A and B, two products, X and X, and occurs at rate r 1 . The pCTMC M C AM p generated from the CRN C AM and initial state f2A, 2Bg can be seen in figure 1. The corresponding transition system T C AM is identical to the CTMC except that the rates of the transitions are ignored. Here, the parameter point p is the set of reaction rates fr 1 , r 2 , r 3 g.
In the state graph (figure 1), each transition is obtained by applying a reaction to a source state, which identifies a target state through updating the molecule counts of the source state. The rate of the transition is obtained as the rate of the reaction multiplied by the number of ways in which the reaction can be applied. For example, from the initial state f2A, 2Bg there is a single transition obtained by applying reaction (2.2a), which consumes one copy of species A and one copy of species B, and produces two copies of species X, resulting in the state fA, B, 2Xg. This reaction has rate r 1 and can be applied in four ways, since each of the two copies of species A can interact with each of the two copies of species B, resulting in a transition rate of 4r 1 . There exists one path between f2A, 2Bg and f4Xg, which is the sequence of states through the intermediate state fA, B, 2Xg.

Problem formulation
The main problem we consider in this paper is the identification of CRNs that satisfy given properties. Specifically, we are interested in finite reachability properties, which capture a range of interesting CRN behaviours.
Let C ¼ (L,R) be a given CRN and T C ¼ (X, T) and M C ¼ (X r , p 0 , Q) denote its transition system abstraction and CTMC representation, respectively, as discussed in §2.1. Let f : X ! f0, 1g denote a state predicate; for example, f(x) ¼ x s . 5 specifies that there must be more than five copies of species s at state x (see appendix A for a formal definition).
In this paper, we consider path predicates c ¼ (f 0 , f F ), which are expressed using two state predicates that must be satisfied at the initial (f 0 ) and at some final (f F ) state of a path. Let K denote the number of steps we consider. Definition 2.1. Given a finite path r : x 0 . . . x K of T C we say that r satisfies path predicate c ¼ (f 0 , f F ), denoted as r o c, if and only if f 0 (x 0 )^f F (x K ) evaluates to true and no reactions are enabled in x K (i.e. x K is a terminal state).
We define the probability of c, denoted P c , using M C as follows. Let X 0 ¼ fx[X j f 0 (x)g denote the set of states that satisfy the initial state predicate. We initialize M C with a uniform sample from the states x that satisfy f 0 , which defines p 0 (x, k) as a function of the rate parameters k as where t F denotes the maximal time we consider and p t F is the probability vector at time t F computed using the CME introduced in §2.1. In other words, we define P c (k) as the average probability of the states satisfying f F at time t F , for rate parameters k.
If 0 , P C (k) , 1, we say the CRN computes or probabilistically computes the specification. If P C (k) ¼ 1, we say it stably computes the specification. By extension, we also refer to a CRN (stably or probabilistically) computing a problem where the CRN satisfies path predicates beyond the set of path predicates used in the generation phase. This is a more restrictive notion of computation than is usually used in the literature. Firstly, we require finite paths which end in a terminal state, instead of the more usual either finite or infinite paths (e.g. [8 -10]). Secondly, our specifications only consider a finite set of input configurations rather than an infinite input set. We informally check the generalizability of a CRN for a specification by evaluating it on inputs beyond the initial set of path predicates.
It is important to note that definition 2.2 does not reward circuits that reach a high probability before the final time. While in principle it is possible to optimize for both speed and accuracy by, for example, defining an integral performance metric (e.g. the integral of P C over time), a compromise would arise between early accuracy and final accuracy. As such, the choice of t F in the integral performance metric would implicitly control the importance of early versus final accuracy. Owing to this complication, we have decided to only consider final accuracy in this article. While we are still forced to choose t F arbitrarily, we vary the rate constants over ranges that enable most circuit simulations to equilibrate by this time.
We are now in a position to formally define the problem being solved in this article.
where I # X is the set of input configurations, find a bimolecular CRN C and a vector of rate parameters k ¼ k opt such that (1) for each c i , there exists a path r i of T C , such that r i oc i , and (2) k opt maximizes the averaged probability of path predicates

Synthesis and tuning of chemical reaction networks
We solve problem 2.3 by addressing each of the two subproblems separately. First, we generate a number of CRNs that satisfy the specifications from problem 2.  Figure 1. The example CRN C AM and the CTMC generated from C AM with initial state f2A, 2Bg.
rsif.royalsocietypublishing.org J. R. Soc. Interface 15: 20180283 capable of producing a path that satisfies each path predicate, which addresses problem 2.3(1), but they might also include incorrect paths and the probability of correct computations might be low. Therefore, we tune the reaction rates of these CRNs in order to maximize the average probability (discussed in §2.4.2), which addresses problem 2.3(2).

Satisfiability modulo theory-based synthesis
Here, we present our approach to finding a bimolecular CRN C that satisfies a specification expressed as path predicates C ( problem 2.3(1)). We address this problem by encoding T C symbolically for any possible bimolecular CRN C ¼ (L,R), where jRj ¼ M and jLj ¼ N (i.e. the number of species and reactions is given), together with the specification C for some finite number of steps K, as an SMT problem. We then use the SMT solver Z3 [20] to enumerate bimolecular CRNs that satisfy the specification or prove that no such CRNs exist for the given N, M and K. Finally, we apply an incremental procedure to search for CRNs of increasing complexity (larger N and M) or to provide more complete results by increasing K.
In addition, we introduce the following constraints.
-We label a subset of the species L I # L as inputs and assert that V no reactions are possible due to insufficient molecules of at least one reactant.
The parameter K specifies the maximal trajectory length that is considered. The BMC approach is conservative, since computations that require more than K steps (reaction firings) to reach a state satisfying f F will not be identified. Increasing K leads to a more complete search, and indeed the approach becomes complete for a sufficiently large K determined by the diameter of a system, but also increases the computational burden. 3 To alleviate this, we follow an approach from [19] and consider stutter transitions (corresponding to multiple firings of the same reaction in a single step) by using the following modified transition relation definition T st (as opposed to T from equation (2.1)): For any enabled reaction r (x s !r r s ), T st allows r to fire up to n times in the stutter transition. n is limited by the consumption and production of the species needed for the reaction to fire (x s ! n . (r r s 2 p r s )). In many cases, stutter transitions dramatically reduce the required trajectory lengths (K s denotes the number of stutter transitions in a trajectory), since multiple copies of the same species can react simultaneously. However, this is not restrictive, since for n ¼ 1 the original definition of T is recovered. In addition to such stutter transitions, T st allows self-loops at terminal states, and therefore computations that require fewer than K s steps to reach a state satisfying f F can also be identified.
The encoding strategy described so far allows us to represent CRN synthesis as an SMT problem and apply an SMT solver such as Z3 [20] to produce a CRN that satisfies the specification or prove that no such CRN exists for the choice of M, N and K (or K s ). More specifically, a solution CRN C is represented through the valuation of r and p, which are extracted from the model returned by Z3.
In general, we are interested in enumerating many (or all possible) CRNs for the given class (defined by M, N and K or K s ), which ensures that no valid solutions are omitted at that stage. To do so, we apply an incremental SMT-based procedure, where at each step we assert a uniqueness constraint guaranteeing that no previously discovered CRNs are generated. Given a concrete, previously generated CRN C 0 ¼ (L,R 0 ) and the new symbolic CRN C ¼ (L,R) we are searching for (both of which are defined using the same species L), we define the constraint The new CRN C cannot simply be a permutation of the same reactions. 4 We start by generating a solution C 0 (if one exists), asserting the constraint DifferentFrom(C 0 ), and repeating this procedure until the constraints become unsatisfiable, which corresponds to a proof that no additional CRNs exist for the given N, M and K.

Tuning chemical reaction networks with parameter optimization
Here, we present our approach to optimizing the reaction rates for CRNs satisfying C. This becomes a parameter synthesis problem over a set of pCTMCs, analogous to parameter synthesis for a single pCTMC, as studied in [21,37]. In contrast to this work, we aggregate over the multiple input combinations, as specified in problem 2.3 (2). To obtain solutions for the probability at a specified time p t , we used numerical integration of the CME. Specifically, we used the Visual GEC software [39] to encode the CRNs and then integrate the CME for each combination of inputs.
To solve the maximization problem, we used an MCMC method, as implemented in the Filzbach software [40]. Filzbach uses a variation of the Metropolis-Hastings (MH) algorithm to perform Bayesian parameter inference. The MH algorithm is used to approximate the posterior probability of a parameter set from a hypothesized model taking on certain values, based on a likelihood function that rewards parameter sets that enable the model to reproduce observation data. The probability of each parameter value is approximated by constructing a Markov chain of sampled parameter sets, such that a proposed parameter set is accepted with some probability, based on the ratio of the likelihood function evaluated at current and proposed parameter sets. For more information on MCMC methods, see [41]. MCMC methods, such as simulated annealing, have also been shown to efficiently find solutions to combinatorial optimization problems [42], taking a stochastic search approach similar to the MH algorithm. Stochastic search can provide benefits over gradient-based optimizers by maintaining a non-zero probability of making uphill moves, protecting against getting stuck in poor local optima. To use Filzbach for optimizing CRN parameters, we use the rsif.royalsocietypublishing.org J. R. Soc. Interface 15: 20180283 argument of problem 2.3(2) as the likelihood function, but rescale so that a proposed parameter set will be accepted at probability 0.25 if it is 1% worse than the current parameter set. Subsequently, we generate MCMC chains with suitably many burn-in iterations and samples to obtain an approximate optimizing parameter set k.

Results
One of the main contributions of this paper is a method for the identification of CRNs that satisfy given properties ( §2.4). The method addresses problem 2.3, allowing us to search for CRNs for which there exists a computation path that satisfies the set of path predicates ( problem 2.3(1)), then optimize the rate parameters to maximize the probability that the behaviour is satisfied ( problem 2.3(1)). In the following, we illustrate the method by identifying CRNs that probabilistically compute a range of specifications.

Probabilistic majority computation with symmetric and asymmetric chemical reaction networks
The majority problem (also known as the binary consensus problem) is one of the most analysed functions in distributed computing [36,43]. To solve the majority problem (Maj), a system must convert the smaller of two given finite populations into the larger population. We specify the majority problem with path predicates For both synthesis and rate optimization, we used the specification as instantiated with inputs A ¼ a and where X denotes the set of states in the CTMC and By including several input combinations, we are able to increase the penalty for incorrect computation, however each input configuration also increases the computation time for optimization, which relies on calculating the chemical master equation once per input configuration. Therefore, all input sets are selected to strike a balance between diversity of input-output behaviours and computational cost. For Maj, we specifically excluded the input combination (a, b) ¼ (1, 1), a pathological input that precludes finding some interesting CRNs, in particular Maj 3,3 #36 (figure 3a). We applied the SMT approach to identify all CRNs with two, three or four species and two, three or four reactions that satisfy C Maj , using K s 10 stutter steps (defined formally in §2.4.1). We then applied our optimization procedure with final time t F ¼ 1000 and ranked the CRNs by the optimized value of P C Maj ( figure 2). Overall, this revealed that there are no CRNs (with N 4, M 4 and K s 10) that can solve the majority problem stably, that is, regardless of rates P C Maj ¼ 1 on all computation paths; however, there were several CRNs that could compute solutions with P C Maj close to 1.
In the following subsections, we consider the results for N ¼ M ¼ 3 and N ¼ M ¼ 4, and analyse the majority performance of selected CRNs. We also analyse the expected time until termination, using the procedure in §4 (right-hand panels of figure 3).

Majority computation: three species and three reactions
Out of 59 640 possible CRNs with three species and three reactions, the SMT solver found 39 CRNs where C Maj was 28 36 14 12 0 6 17 16 8 26 30 10 5 24 19 1 25 39 34 29   rsif.royalsocietypublishing.org J. R. Soc. Interface 15: 20180283 satisfied, 22 of which were unique. 5 Of these, two satisfied the predicate with a probability exceeding 0.9 after optimization ( figure 2a). A score of 0.982 was reached by Maj 3,3 #28 (figure 3a) and a score of 0.936 was achieved by Maj 3,3 #36 (figure 3b). Maj 3,3 #36 is the three-reaction analogue [3] of the known four-reaction approximate majority algorithm [31,32] (we will later identify this as Maj 4,4 #3750). This three-reaction analogue does not satisfy the formal specification C Maj if the input configuration (a, b) ¼ (1, 1) is included, as in this case the network terminates in the state (A ¼ 0, B ¼ 0, X ¼ 2) and fails to make a decision. Accordingly, in figure 3a, we see that it scores a 0 on inputs A ¼ 1, B ¼ 1. The optimization of Maj 3,3 #36 led to unequal reaction rate values, with the A þ B ! X þ X reaction being faster than the other two. To understand how this influences convergence to one of the two decision states for computations involving a large number of molecules, we prepared phase portraits of the rate equations of the continuous deterministic CRN semantics. This revealed that the optimization moved the saddle point to lower values of A and B and therefore to higher concentrations of X ( figure 4). Analogously, analysis of the discrete stochastic CRN semantics revealed longer (and therefore slower) trajectories, and thus an accuracy-time trade-off    Figure 3. Response of probabilistic majority algorithms to varied inputs. For each input combination, specified as initial copies of species A and species B, the probability that both have the correct molecule count after 1000 time units is reported. Results are shown for a variety of networks that performed well following optimization (figure 2). The performance of each CRN is compared both before optimization (all rates equal to 1.0; left panels) and after optimization (central panels). The grey boxes show the input ranges used for both generation and optimization. The expected time until the CTMC reaches a terminal state is calculated for varying total molecule counts (n) (right panels). These times consider rates scaled as if occurring in a volume n (see §4). The completion times for three alternative initial configurations (initial copies of A were 10%, 60% and 90% of n, respectively) were calculated, illustrating minor differences in circuit completion times (cross symbols mark systems using optimized rates and filled circles mark systems using 1.0 for all rates). The red and blue lines are plots of logn and n, respectively, and give an indication of running time.
rsif.royalsocietypublishing.org J. R. Soc. Interface 15: 20180283 (figure 3a). Nevertheless, while final accuracy is improved at the cost of slower computation, there is still a logarithmic relationship with total molecule counts.
To the best of our knowledge, the better performing Maj 3,3 #28 (figure 3b) has not been analysed previously. Most likely, this is due to this network proposing an asymmetric solution to a symmetric problem, which at first appears counterintuitive. The reactions used in #28 are not isomorphic to switching the A and B species, and consequently the performance of this network with unit rates is poor and asymmetric (P C Maj ¼ 0.763; figure 3b, centre panel). However, optimization selects for compensatory rate values that enable surprisingly good performance in the range of inputs considered (P C Maj ¼ 0.982; figure 3b, right panel). Analysis of the deterministic phase portrait reveals that optimization moves the saddle point close to (A, B) ¼ (0, 0), and produces a separatrix that almost exactly follows the line A ¼ B. However, this accuracy comes at the cost of speed, in cases where A and B are close together the expected run times start to follow a linear trend (figure 3b) rather than logarithmic.

Majority computation: four species and four reactions
We next considered CRNs with four species, to explore how many reactions are required to stably compute the majority specification. There is a known CRN (or population protocol) with four species that stably computes the majority problem with three reactions and four species [44][45][46]. Indeed, it has been shown that four species are necessary [35,46] to stably compute majority. However, this known CRN does not satisfy C Maj . This is because the decision is encoded by all molecules belonging to the subset fA, Xg, or all molecules belonging to the subset fB, Yg (for some applications this is more practical, e.g. [11]). As such, the decision states represent a collection of terminal states of the associated CTMC, whereas our definition involves a path predicated on a single state. However, it is easy to see that by adding two further reactions, we obtain an Maj 4,5 CRN that probabilistically computes C Maj with probability 0.946 at rate 1. In this section, we use our method to provide evidence that more than four reactions (with four species [35]) are necessary to stably compute majority (C Maj ).
By applying the SMT solver to four species and four reactions, we found 6486 networks (of which, 1678 are unique)  The well-studied, optimal (in terms of reaction firings), approximate majority circuit [31,32], Maj 4,4 #3750 (figure 3c), scored only 0.841 before optimization and 0.879 after optimization. Curiously, this was worse than the smaller CRN Maj 3,3 #36 described above, which produced an unoptimized score of 0.926. Seemingly, optimizing for final accuracy led to asymmetric rate constants and therefore asymmetric accuracy with respect to the input values ( figure 3c, right panel).
A large number of Maj 4,4 networks produced an optimized score that exceeded 0.9. This is largely due to being able to append a dummy reaction to either of the high-performing Maj 3,3 networks. Therefore, we categorized the Maj 4,4 networks according to whether they contained Maj 3,3 #28 or Maj 3,3 #36 as a subnetwork (figure 2c). Of the Maj 4,4 CRNs with Maj 3,3 #36 as a subnetwork, only three (#6408, #4777, #3860; see §A.1) managed to improve on the accuracy score of that network by more than 0.5%. Of these, CRN Maj 4,4 #3860 was an interesting case as it also has the top scoring asymmetric CRN Maj 3,3 # 28 as a subnetwork. Other networks with Maj 3,3 #28 as a subnetwork optimized well with most scoring over 0.98. The first (Maj 4,4 #6408 at 0.997) and third (Maj 4,4 #4777) best-scoring CRNs were versions of Maj 3,3 #36 that expanded one reaction into two reactions. This increased accuracy comes at the expense of time however, as we observe a linear relationship between absorption time and total molecule counts (figure 3d).
We note that in our exhaustive search of the Maj 4,4 search space there are no CRNs that stably compute majority on our input set with K s 10 stutter steps.

Numerical evaluation times
The numerical evaluation times of our procedure depend on the size of the circuit (M and N ), length of considered computations (K) and exact specification F (including the number of given path predicates). We illustrate the computation times required for the SMT-based synthesis part of our approach with the majority decision-making CRNs (figure 5).
To determine how the numerical evaluation of the CME used in our method scales with molecular copy numbers, we first ran calculations for the established three-reaction probabilistic CRN (system Maj 3,3 #36) for majority. As increasing the copy number decreases the simulation time interval over which there are transient dynamics, we integrated the CME over the time interval [0, 100/n], where n is the total copy number. The calculation was initialized with 0.6n copies of A and 0.4n copies of B, and all rates were set to 1. We calculated transient probabilities at 500 output points, with n[ [10,1000]. This led to state spaces of varying size, up to 10 6 , with all calculations completing within 7200 s (2 h; figure 6). Smaller examples took only a few seconds.
We can approximate the total CPU run-time for parameter tuning as a function of the number of iterations of the MCMC algorithm and the number of input combinations assessed. For example, doing 200 iterations over 10 input combinations, which all have fewer than 30 total

3.2.
Computing the maximum copy number of two species

Exact maximum computation
There is a simple bimolecular CRN to compute the minimum where W is a waste species, whereas finding the maximum (Y ¼ max (A, B)) with a CRN (so far) seems to require three extra reactions. A stably computing CRN found in [9] can be converted into a bimolecular reaction by adding a 'fuel' species F (where x F !x A þ x B ), which uses four reactions and eight species.

Probabilistic maximum computation
Motivated by the stably computing circuit for maximum, we applied our technique to determine whether there exist smaller, bimolecular CRNs that probabilistically compute maximum. We specify the maximum problem using path For both synthesis and rate optimization, we used the specification instantiated with input configurations Using SMT, we identified all CRNs with three or four species and three or four reactions that satisfy C Max for K s 10 stutter steps. We found no CRNs for three species with three or four reactions. For four species and three reactions, we found 128 CRNs, 66 of which were unique under specification isomorphism.
We then optimized the reaction rates for final time t F ¼ 1000 and ranked the solutions by the value of P C Max for each. Before optimization (all rates equal to 1), none of the CRNs scored above 0.5. After optimization, three CRNs reached a score of P C Max ¼ 0.999, while the fourth best scored only 0.646 ( figure 7). The top three CRNs, Max 4,3 0, 1 and 15, are all variations of the same basic reaction schema, The F's in the above reaction are substituted with combinations from the set fA, B, Y g (see §A.2). To the best of our knowledge, this is a novel CRN for probabilistically computing maximum. The reaction system (3.1) uses a mixture of 'fast' and 'slow' reactions. A best case computation for this algorithm relies on the input species pairing off to produce the output species (using the first fast reaction), before any slow reactions occur. Following this, the remainder of the more abundant species is converted to the output species, using the final two reactions, which results in the correct answer. If the final two rules fire before the first rule is completed, there is a chance that the computation will not result in the correct answer. As such, the firing of the first reaction should be as frequent as possible to reduce the chance of an incorrect computation. Conversely, the final two reactions should be as slow as possible to prevent them interfering in the first part of the algorithm. This leads to an accuracy-time trade-off (figure 8). As the ratio between the slow and fast reactions decreases, accuracy decreases (figure 8a), but expected hitting times increase logarithmically (figure 8b). Thus, it is possible to optimize the accuracy and time trade-off for a finite input range; however, beyond this range accuracy will drop rapidly to 0 (figure 8c) as the total number of molecules n increases. At the same time, the halting time increases according to Q(n).

Euclidean division
So far, all of the target problems introduced in this paper are semi-linear functions, and so stably computing CRNs exist for them [9]. Division by a pre-specified constant is also a semi-linear problem, and, as such, division by 2 can be stably computed by the single-reaction CRN: Applying our technique to division by 2 resulted in CRNs that contain the known reaction with some additional 'helper' reactions. These extra reactions decreased the expected time by an additive constant; however, this minor speed up came at the cost of accuracy, with maximum accuracy only when they do not fire at all. As such, we omitted graphically showing the results for the synthesis of CRNs that compute division by 2.
To challenge our CRN synthesis method, we applied our technique to compute a function that is not semi-linear, the problem of general Euclidean division. That is, for input quantities a (the dividend) and b (the divisor), we seek a computation of the function a4b ¼ b a / b c. To formally specify the division problem, we use path predicates c i [C Div for input configuration i (see §2.1) with To give a diverse selection of responses, we used the specification instantiated with input configurations C Div ¼ (3,2), (4,2), (11,2), (14,2), (2, 3), (7, 3), (11,3), (12,3), (15,3), (2,4), (4,4), (8,4), (9,4), (12,4), (1,5), (5,5), (10,5), (15,5), (16, 5)} for both optimization and synthesis. This input set aims to balance the time/memory costs of optimization against the diversity of the result of the division. The list has five instances for each divisor 2, 3, 4 and 5. Without a diverse input set, we found that CRNs implementing simpler division functions could achieve high scores in optimization. We applied our method to search for CRNs satisfying C Div for N 5 species, M 6 reactions and K s 10 stutter steps ( figure 9). While the search over some combinations (N, M, K s ) terminated, some larger combinations did not terminate within three months, and were subsequently halted. For N ! 3 and M! 4 none of the processes terminated, illustrating a drawback in our approach to satisfying path predicates over combinatorially increasing reaction sets (6.3 Â 10 9 CRNs with four species and five reactions; 6.3 Â 10 11 CRNs with five species and five reactions; 2.6 Â 10 13 CRNs with six species and five reactions). Indeed, three processes out of four in the range N[f4, 5g, M[f5, 6g did not output a single CRN and never fully explored K s ¼ 1. However, for smaller bounds on N and M, we were able to generate over 10 6 candidate CRNs that satisfied the specification encoded by C Div .
We then applied our optimization procedure with final time t F ¼ 1000 and ranked the CRNs by the optimized value of P C Div . As mentioned above, we chose the set of inputs I to provide a diverse set of responses that would direct synthesized networks to be general dividers by including a variety of different dividend and divisor values in the input set I . Nevertheless, we found that CRNs implementing the simpler function f(x) ¼ bx/3c were able to achieve a score of 0.579 on C Div . For example, Div 4,4 #79523 scored 0.696 after optimization, but analysis of this CRN revealed that it produced correct outputs with high probability when the divisor was 3 (for appropriately chosen rates), but other divisors led to correct outputs with low probability (figure 10b). Consequently, several CRNs were optimized to use rates that make them excellent at dividing by 3. Indeed, the best 28 CRNs satisfying C Div all have their highest scores in the division by 3 row. Analogously, many CRNs instead optimized for approximating computing division by 4, for example the 28th best CRN, Div 4,4 #61586 (score 0.655; figure 10a). The best CRN identified in our partial search is Div 5,4 #751168 (figure 10), which scored 0.748 after optimization. In conclusion, in the optimization phase, it is important to have a diversity of inputs that will direct the parameter search towards a region that solves the general problem and to avoid minima where solutions score well but do not solve the problem in general. This must be balanced against the memory and time cost of stoichiometries .10.
While we were unable to automatically synthesize CRNs that compute Euclidean division with high probability, the function is known to be computable by register machines, which can be simulated probabilistically by CRNs [6,11,12]. A large CRN for Euclidean division is also sketched in [47]. Based on this, we hand-constructed a CRN with 10 species and seven reactions that takes advantage of fast (approx. 100) and slow (approx. 0.01) reaction speeds (figure 10d). The CRN probabilistically computes Euclidean division by counting the number of times the denominator species can be subtracted from the numerator species, and performs well, scoring P C Div ¼ 0.998 following optimization. Only when dividing by 1 does the optimized CRN perform  Figure 8. Our probabilistic circuit for maximum exhibits an accuracy -time trade-off. We parametrized the probabilistic maximum circuit Max 4,3 #1 as: poorly, but this is only because the computation time is limited to t f ¼ 1000, and division by 1 was not included in C Div , and so rates were not optimized for this condition. The hand-made Div 10,7 CRN satisfies C Div , and makes use of leader 6 molecules Q 1 , Q 2 , Q 3 , Q 4 to control phases of the computation. Finally, it is important to note that an alternative method to divide two numbers with CRNs is where the mean of the stationary distribution of species X is exactly i A / i B , where i A and i B are the initial values of A and B in initial configuration i [48]. However, because we constrain our CRN synthesis to CRNs that have reached a terminal state (see §2.4.2), the path predicates c [ C Div are not satisfied by this CRN. That is, we only consider CRNs that halt in the correct state and so the above division CRN is not specifiable in our scheme. While relaxing the requirement for termination is possible in principle, it is not possible to evaluate the stationary distribution in the SMT phase of our method without supplying parameter values during this phase. This would remove the benefit of filtering CRNs that do not contain a satisfying path, which we have shown to be highly stringent. The alternative would be to optimize the stationary distribution for all CRNs.

Discussion
In this paper, we presented a computational approach for the synthesis and parameter tuning of CRNs, given a specification of the system's correctness. We focused on the sub-class of bimolecular CRNs due to their importance as representations of various molecular algorithms and population protocols. However, our approach is more general and could also be applied directly to the synthesis of CRNs from other classes (e.g. unimolecular, trimolecular, etc.), which are defined through different stoichiometry constraints. The CRNs we synthesize can be converted into equivalent physical implementations, for example using DNA strand displacement (DSD) [3,5]. Our approach could also be applied to synthesize DSD directly, by explicitly  Figure 9. (a-p) Search and optimization of CRNs that probabilistically compute Euclidean division M4N. CRNs were tested for their ability to probabilistically compute M4N for input sets M[f2, 3, 4, 5g and N[f3, 4, 5, 6g. Each panel shows the number of CRNs found, the highest observed value of P C Div after optimization, and the number of stutter steps K s that Z3 was considering when the computation was halted after three months. K max ¼ 11 indicates that the computation halted and we have a complete list of CRNs up to and including K s ¼ 10.
taking into account the desired implementation strategy, such as encoding the CRN species as single-stranded DNA and the reactions as double-stranded complexes [3,5]. This could lead to simpler designs than the ones obtained through direct translation of CRNs. We considered simple reachability properties defined in terms of predicates on the initial and final states of a computation which are sufficient to express various logical and arithmetic functions and operations. More general specifications, for example where intermediate states along computations are specified, are also currently possible within our approach but extensions to more expressive languages, such as the probabilistic temporal logics used with other methods [37], remain a direction for future work.    Figure 10. Response of selected Euclidean division algorithms to a range of inputs. For each input combination, specified as initial copies of species A and B, the probability that the molecule count of X is b A / B c after 1000 time units is reported. (d ) is a 'hand-made' 10-species seven-reaction CRN that satisfies C Div . rsif.royalsocietypublishing.org J. R. Soc. Interface 15: 20180283 We first introduced our methodology for synthesizing CRNs using SMT in a conference publication [17], but this has been considerably expanded in this article. Previously, we showed that it was possible to rediscover known CRNs for majority computation, but also some unknown CRNs that are structurally asymmetric but through careful selection of their rate constants [17]. Here, we showed that the separatrices of these asymmetric majority networks approximately lie along the diagonal [ 4). We have also shown new CRNs for majority computation that use four species, but none were able to solve the majority problem exactly (figure 2). In our first publication using this method, we also introduced attempts to find CRNs that probabilistically compute Euclidean division [17]. Here, we explored larger CRNs (figure 9), and showed that the solutions found could not provide good solutions for all divisors ( figure 10). We also analysed a hand-made CRN comprising 10 species and seven reactions that is an excellent approximator for Euclidean division (figure 10d), but was beyond the computational limit for the SMT step of our method. Finally, this article introduces new results for approximating the maximum function between two species (figure 7). We showed that CRNs could make use of a combination of fast and slow reaction rates to probabilistically compute the maximum using fewer species and reactions than the previously known, stably computing, CRN [9]. Owing to the differences in rate constants, an accuracy-time trade-off arose in the computation by our CRNs (figure 8). Nevertheless, without considering such networks, biological computation schemes are likely to be more complex than might be necessary; a (bounded) loss of accuracy might be a preferable trade-off. We suggest that designing CRNs with variable rates would be difficult to achieve without automated synthesis tools.
An alternative approach to the problem of realizing arbitrary behaviour in biochemical systems is to use directed evolution [49,50]. In silico evolutionary search strategies might scale to larger CRNs and address the synthesis and parameter optimization sub-problems using a single, combined procedure. However, this comes at the cost of completeness, where the absence of a solution does not mean a solution does not exist. For many applications, elements of our method could be complementary to evolutionary algorithms. For example, the exact CTMC methods we use to assess the probability of correct computations in a given CRN could provide a useful fitness function for evolutionary search, compared with alternative approximate methods based on stochastic simulation. In contrast to evolutionary search strategies, our method addresses the existence and optimization sub-problems separately and uses the SMT solver and theorem prover Z3 to identify CRNs that satisfy a given specification (kinetics are ignored at this first stage). Since the results provided by Z3 are complete (up to K or K s ), the termination of the procedure with no solutions is a 'proof' that no CRNs exist for a given specification (up to K or K s ). Thus, besides providing a practical tool for the identification of CRNs with given behaviour, the completeness property means our approach could also help explore the theoretical limits of CRN computation (e.g. no CRNs with fewer than M species and N reactions that compute a given function exist). It is also important to choose a K (K s ) that is large enough to accommodate the likely lengths of (stutter) trajectories needed to satisfy the specification for a given set of path predicates. However, currently large K or K s values are costly to compute and so this limits the range of inputs we can choose for path predicates.
The fully automated generation of CRNs with discrete stochastic semantics that satisfy specified behaviours is a challenging problem and certain scalability limitations of our current method must be addressed to provide a more complete solution. Firstly, the SMT-based synthesis procedure we propose may represent large or infinite state spaces and handle systems with large molecule numbers. However, currently this method is limited to relatively small CRNs with few reactions and species, and short computation paths. Secondly, the CTMC methods we apply require an explicit representation of the state space, which must be finite (which is always the case for bimolecular CRNs initialized with a finite number of molecules). As the number of states grows exponentially in the number of species, the method is practically suitable only for systems involving relatively few species and numbers of molecules. To circumvent the need for an explicit representation of the state space, stochastic dynamical behaviour could be approximated by averaging multiple trajectories from Gillespie's stochastic simulation algorithm [51]. Instead, one could solve a synthesis problem for alternative semantics of the CRN, such as the continuous-state fluid or central-limit approximations [52], or the continuous-state deterministic rate equations. Depending on the specification, and the nature of the CRN, some of these approaches might be appropriate, but none are free of their own documented limitations. Finally, while the synthesis stage of our approach is a powerful filter on the space of all CRNs (e.g. C Maj filters out all but 6486 CRNs out of 134 810 340 possible four-species four-reaction networks, approx. 0.005%). However, as the number of reactions grows, so does the number of spurious CRNs that satisfy the specification in an uninteresting way. For example, we find many CRNs that are simple birth-death processes that can reach all possible final states. Currently, we filter these after the parameter tuning phase, which requires substantial additional computation. This indicates that additional constraints describing more accurately the structure and dynamics of 'good' solutions could improve the method.
We consider terminating computations by enforcing that no reactions are enabled at the state that satisfies f F . Alternative strategies possible within our approach could consider reaching a fixed-point (i.e. the firing of any enabled reaction does not cause a transition to a different state), or reaching a cycle along which f F is satisfied, to guarantee that the correct output is eventually computed and remains unchanged by any subsequent reactions.
For tuning reaction rates, alternative cost functions could be used that reward solutions that are 'nearly' correct, e.g. using a mean-squared error. This would be most appropriate in high copy number situations, where a precise number of molecules is not integral. Our approach is focused on discrete-state stochastic semantics, and therefore most appropriate for systems operating at low copy numbers, offering an exact characterization of the probability that a specific predicate is satisfied. Our results were shown for calculations at t F ¼ 1000 time units, a transient probability, rather than at the stationary distribution. While the selection of t F is subjective, it allows a circuit programmer to specify how long they are willing to wait for a computation, and provide rate constants that produce correct computations with high probability within the desired time. Circuits that reach high probability at t . t F will not be rewarded more than the probability score accumulated so far by t ¼ t F . However, a natural extension to the presented method would be to reward circuits that reach high probability at t , t F , both imposing an upper bound on time and optimizing within that range. This could be achieved by integrating our metric over the interval [0, t F ].
Automating the search for CRNs that compute the solution for a specified behaviour could be beneficial to both theoretical and experimental molecular programmers. Our method can be used to show the existence or the absence of CRNs of a certain size and also suggest CRNs that can be tuned for a specific input range, and so become candidate designs for experimental construction. Prior to construction, more in-depth analysis of the candidate CRNs produced is beneficial, including parameter sensitivity/robustness analysis and bifurcation analysis (where appropriate). Future work could also incorporate notions of robustness into the proposed method, for example by using interval-based methods [37]. Nevertheless, our results provide a starting point for synthesis of CRNs with discrete-state stochastic semantics, and illustrate the potential of this approach for computing arbitrary discrete-state behaviours.