 # Quantum Dynamic Programming Algorithm for DAGs. Applications for AND-OR DAG Evaluation and DAG's Diameter Search

In this paper, we present Quantum Dynamic Programming approach for problems on directed acycling graphs (DAGs). The algorithm has time complexity O(√(n̂m)n̂) comparing to a deterministic one that has time complexity O(n+m). Here n is a number of vertexes, n̂ is a number of vertexes with at least one outgoing edge; and m is a number of edges. We show that we can solve problems that have OR, AND, NAND, MAX and MIN functions as the main transition step. The approach is useful for a couple of problems. One of them is computing Boolean formula that represented by DAG with AND and OR boolean operations in vertexes. Another one is DAG's diameter search.

## Authors

##### This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

## 1 Introduction

Dynamic programming approach is one of the most useful ways to solve problems in computer science [CLRS01]. The main idea of the method is solving a problem using precomputed solutions of the same problem, but with smaller parameters. One class of problems that uses dynamic programming is problems on directed acycling graphs (DAGs). Example of such problems can be computing diameter of a DAG (length of longest path). Another example is computing Boolean formula that can be represented in DAG with conjunction (AND) or disjunction (OR) in vertexes, and inversion (NOT) on edges.

The best known deterministic algorithm of dynamic Programming on DAGs use Depth-first search algorithm (DFS) as subroutine. Thus, this algorithm has at list Depth-first search algorithm’s time complexity, that is , where is a number of edges and is a number of vertexes.

We suggest a quantum algorithm with time complexity , where is a number of vertexes with non-zero outgoing degree. It can solve problems that have dynamic programming algorithm with OR, AND, NAND, MAX or MIN functions as transition step. We use Grover’s search [Gro96, BBHT98] and Dürr and Høyer maximum search [DH96] algorithms to speed up our search. A similar approach has been applied by Dürr et al. [DHHM04, DHHM06]; Ambainis and Špalek [AŠ06]; Dörn [Dör09, Dör08] to different graph problems.

We apply this main approach to two problems. First of them is computing boolean formula. It is known that any Boolean function can be represented as Disjunctive normal form (DNF). Such formula can be represented as AND-OR tree or AND-OR directed acycling graph. In such graphs, leaves are associated with boolean variables and other vertexes are associated with conjunction (AND) or disjunction (OR); edges can be associated with inversion (NOT) function. Boolean circuits can be considered as an example of such representation. Quantum algorithms for computing AND-OR trees were considered by Ambainis et al. [ACR10, Amb07, Amb10]. Authors presented algorithm with time complexity . On the one hand, this algorithm has better time complexity than the algorithm in this paper. On the other hand, Ambainis’ algorithm is applicable only to trees, but not to DAGs. This is the significant restriction because converting DAGs to a tree can lead to exponential increasing of a vertexes number. In that case, the algorithm for DAGs has exponential benefit comparing to the algorithm for trees.

The second problem is computing a diameter for a DAG. A diameter is a length of a path between two most far vertexes of a graph. As other problems that can be solved by dynamic programming on DAGs, this problem has deterministic time complexity . At the same time, our algorithm has time complexity .

The paper is organized in the following way. We present definitions in Section 2. Section 3 contains a general description of the algorithm. An application to AND-OR DAG evaluation is in Section 4. Section 5 contains a solution for DAG’s diameter search problem.

## 2 Preliminaries

Let us present definitions and notation from graph theory, that we use in this paper. You can read more about graphs and algorithms in [CLRS01].

Graph is a pair where is a set of vertexes, is a set of edges, an edge is a pair of vertexes , for .

Graph is directed if all edges

are ordered pairs. In that case, an edge

leads from vertex to vertex . Graph is acycling if there is no path that starts and finishes in the same vertex. In the paper, we consider directed acycling graphs (DAGs).

Let be a list of vertexes that can be reached from a vertex in one step, for . Let be a degree of the vertex .

Let be a set of indexes of vertexes that have not outgoing edges. Formally, . Let .

For DAGs we require two additional properties:

• if there is an edge , then ;

• last vertexes belong to , formally , for .

Our algorithms use some quantum algorithms as subroutine and other part is classical. As quantum algorithms we use query model algorithms. This algorithms can do query to black-box that has access to graph structure and stored data. We use an adjacency list model as model for graph representation. The input specified by arrays , for . We suggest [NC10] as good book about quantum computing.

## 3 Quantum Dynamic Programming Algorithm for DAGs

Let us describe an algorithm in general case.

Let us consider some problem on directed acycling graph . Suppose that we have a dynamic programming algorithm for or we can say that there is a solution of the problem that is equivalent to computing a function for each vertex.

As a function we consider only functions from a set with following properties:

• .

• Result set can be real numbers , or integers , for some integer .

• if then , where functions are such that ;

is a set of vectors of at most

elements from ; .

• if then is classically computable in constant time.

Suppose that the function has a quantum algorithm with time complexity , where is a length of the argument for the function . Then we can suggest the following algorithm:

Algorithm 1.

Let we have an array where we will store results of the function . Let be a function such that , if ; , if . Note that means .

The algorithm is following.

for from to

, where

Let us discuss time complexity of Algorithm 1.

###### Lemma 1

Suppose that time complexity of quantum algorithms are , where is a length of an argument. Then time complexity of Algorithm 1 is .

###### Proof

Note, that when we compute we already have computed or can compute in constant time , because for all we have .

A time complexity of a processing for a vertex is , .

The algorithm process vertexes one by one, therefore to compute time complexity , we should sum time complexities of the processing of each vertex. Hence

Note, that quantum algorithms have a probabilistic behavior. That’s why we should compute a probability of error for the algorithm. Let us compute a probability of error for Algorithm 1.

###### Lemma 2

Let the quantum algorithm for the function has an error probability , where . Then the probability of error for Algorithm 1 is at most .

###### Proof

Let us compute a probability of a success work of Algorithm 1. Suppose that all vertexes are computed without error. The probability of this event is , because we invoke algorithms for vertexes from and error of each invocation is independent event.

Therefore, the probability of an error for Algorithm 1 is at most .

For some functions and algorithms, we have not a requirement that all arguments of should be computed without error. In that case, we will get better error probability. This situation is discussed in Section 3.1.1.

### 3.1 Examples of Functions

We can choose the following functions as a function .

• Conjunction (AND function). For computing this function, we can use a Grover search algorithm [Gro96, BBHT98] for searching among arguments. If the element that we found is , then the result is . If the element is , then there is no s, and the result is .

• Disjunction (OR function). We can use the same approach, but here we search s. If we found , then the result is ; and , otherwise.

• NAND function. We can use the same approach as for AND function, but here we search s. If we found then the result is ; and , otherwise.

• Maximum function. We can use Dürr and Høyer maximum search algorithm [DH96].

• Minimum function. We can use the same algorithm, as for maximum.

• other functions that have quantum algorithms.

As we discussed before, AND, OR and NAND functions can be computed using Grover search algorithm. Therefore algorithm for these functions on vertex has an error and time complexity , for . Hence . The similar situation with computing maximum and minimum functions. Dürr-Høyer algorithm give us an error .

If we develop a dynamic programming solution, based on AND, OR, NAND, MAX and MIN functions and directly apply Lemma 2 then we get error of the algorithm . This is very big error if we have at lest two vertexes with more than one outgoing edges. So we can repeat invoking algorithm for reduce error probability. This result is presented in the following lemma.

###### Lemma 3

If functions then there is a quantum dynamic programming algorithm for the problem that has time complexity and error probability .

###### Proof

Let we have a quantum algorithm and a deterministic algorithm for a function , where . In the case of NAND function, we use the deterministic algorithm for OR function as .

It is easy to construct a deterministic algorithm for a function that is work in linear time.

Then we present the following algorithm for the problem :

Algorithm 2.

Let we have an array where we will store results of the function . Additionally, we have a temporary array . Let be a function such that , if ; , if .

The algorithm is following.

for from to

for from to

, where

The difference between Algorithm 1 and Algorithm 2 is the following. Instead of invoking algorithm once, we invoke it times. Then we apply the same function to results that the algorithm returns. In the case of NAND function, we apply OR function to results that the algorithm returns.

Let us consider all functions one by one and show that the Algorithm 2 is right. Additionally, we discuss the error probability.

Firstly, let us focus on OR function. The algorithm for OR function searches s. We use Grover search algorithm for this procedure. If the algorithm finds the index of , then the result is . If the algorithm finds the index of , then the result is .

Suppose that there is no s; then the algorithm returns index of always. Suppose that there is at list one ; then the algorithm returns the index of with probability at most . Therefore, we have one side error.

Let us consider , where are results that the algorithm returns. The result of processing of vertex is wrong only if all are s but result should be . Therefore, a probability of error of the algorithm is at most .

Secondly, let us consider AND function. By the similar way we can show that the algorithm always returns right answer in a case of the result should be ; and returns in place of with probability at most . If we consider as a result of processing of a vertex ; then a probability of error of the algorithm is at most .

Thirdly, let us consider NAND function. By the similar way, we can show that the algorithm always returns right answer in a case of the result should be ; and returns in place of with probability at most . If we consider as a result of processing of vertex ; then a probability of error of the algorithm is at most .

Finally, let us consider MAX function (MIN function has exactly the same analysis). The algorithm returns index of maximal element from . We use Dürr-Høyer algorithm for this procedure. Let be the index of maximal element. The algorithm returns wrong index with probability at most . If we consider , then iff all invocations of the algorithm have errors. Probability of such event is at most .

Therefore, the probability of error is at most . Note that

 lim^n→∞ε(^n)1/n=lim^n→∞1−(1−1^n2)^n1/n=1;

Hence, .

Let us discuss time complexity of Algorithm 2. By Lemma 1, time complexity is

 T1=∑i∈{1,…,n}∖LT(di)≤∑i∈{1,…,n}∖LO(√dilog^n)=O⎛⎝(log2^n)⋅∑i∈{1,…,n}∖L√di⎞⎠.

Due to the Cauchy-Bunyakovsky-Schwarz inequality, we have

 ∑i∈{1,…,n}∖L√di≤√^n∑i∈{1,…,n}∖Ldi

Note that , for . Therefore, , because is total number of edges. Hence

 √^n∑i∈{1,…,n}∖Ldi=√^n∑i∈{1,…,n}di=√^nm.

Therefore, .

#### 3.1.1 Dynamic Programming Based on Minimum and Maximum functions

If we consider only MIN and MAX functions as , then we can get better result than for Algorithm 2. Let us present Algorithm 3, that do less repetition of Dürr-Høyer algorithm and has better time complexity. Suppose a solution of the problem is a value of , for some .

Algorithm 3.

Let we have an array where we will store results of the function . Additionally, we have a temporary array . Let be the longest path from the vertex to one of vertexes . Let be a function such that , if ; , if .

The algorithm is following.

for from to

for from to

, where

Let us discuss properties of Algorithm 3.

###### Lemma 4

Suppose that functions and a solution of the problem is a value of , for some . Then there is a quantum dynamic programming algorithm for the problem that has time complexity and error probability .

###### Proof

we consider Algorithm 3 as the algorithm for .

Let us consider a vertex for . On processing we should compute MAX or MIN among . Without limit of generalization we can say that we compute MAX function. Let be an index of maximal element. It is required to have not error for computing . At the same time, if we have an error on processing , ; then we get value . In that case, we still have . Therefore, an error can be on processing of any vertex .

Let us focus on the vertex . For computing without an error, we should compute without an error. Here such that maximum is reached on . For computing without an error, we should compute without an error. Here such that maximum is reached on and so on. Hence, for solving problem without error, we should process only at most vertexes without an error.

Therefore, the probability of error for Algorithm 3 is

 1−(1−(12)2logq)q=O(1q) because limq→∞1−(1−1q2)q1/q=1.

## 4 Quantum Algorithm for AND-OR DAGs Evolution

Let us apply ideas of quantum dynamic programming algorithms on directed acycling graphs to AND-OR DAGs

It is known that any Boolean function can be presented as Disjunction normal form (DNF) or formula using AND, OR and NOT function (conjunction, disjunction and inversion) [Yab89]. Any such formula can be presented as directed acycling graph with following properties:

• If a vertex such that ; then the vertex labeled with a variable. We call these vertexes “variable-vertexes”.

• There is no vertexes such that .

• If a vertex such that ; then the vertex labeled with Conjunction or Disjunction. We call these vertexes “function-vertexes”.

• Any edge is labeled with or .

• There is one especial root vertex .

The graph represents a formula that can be evaluated in the following way. We associate a value with a vertex , for . If is a variable-vertex then is a value of a corresponding variable. If is a function-vertex labeled by a function then , where , , is a label of an edge . Here for any boolean variable , we say that and . The result of the evolution is .

Let us consider an example. Let formula

 F(x1,x2,x3,x4,x5)=x1∨x2∨¬(x4∧x5∧¬x3)∨((¬x4∧x5∧(x1∨x2∨x3))

The DAG representation of the formula is presented in Figure 1. Figure 1: DAG representation for the formula F. Black edges are labeled by 0, white edges are labeled by 1. s=1.

An AND-OR DAG can be evaluated using the following algorithm that is a modification of Algorithm 2:

Algorithm 4.

Let we have an array where we will store results of functions . Additionally, we have a temporary array . Let a variable-vertex is labeled by , for all . Let be a quantum algorithm for ; and let be a deterministic algorithm for . Let be a function such that , if ; , if .

The algorithm is following.

for from to

for from to

, where , .

Algorithm 4 has the following property:

###### Theorem 4.1

Algorithm 4 evolutes the AND-OR DAG ; has time complexity and error probability .

###### Proof

Algorithm 4 evolutes the AND-OR DAG by the definition of AND-OR DAG for formula .

Algorithm 4 is almost the same as Algorithm 2. The difference in labels of edges. At the same time, the Oracle gets information on edge in constant time. Therefore, time complexity and error probability of does not change. Hence, using similar proof we can show that time complexity and error probability .

### 4.1 NAND DAGs Evaluation

Another way to represent a Boolean function is a NAND directed acycling graph. A Boolean function can be presented as NAND-formula [Yab89]. We can present NAND formula as DAG with similar properties as AND-OR DAG, but function-vertexes has only NAND labels. Additionally, we can say, that edges in the graph have not labels, because we do not need NOT functions in the formula. At the same time, if we want to use more operations then we can consider NAND-NOT DAGs and NAND-AND-OR-NOT DAGs. The following theorem claims results similar to the result from Theorem 5.1.

###### Theorem 4.2

Algorithm 4 evaluates a NAND-AND-OR-NOT DAG and a NAND-NOT DAG. Algorithm 2 evolutes a NAND DAG. Both algorithms have time complexity and error probability .

###### Proof

The proof is similar to proofs of Lemma 3 and Theorem 5.1.

### 4.2 Discussion

Let us compare the algorithm for AND-OR DAGs evaluation with existing ones. Deterministic algorithm for the problem has Depth-first search [CLRS01] as subroutine and should observe all edges. So, time complexity of deterministic algorithm is . Our quantum algorithm has benefit, if , so . A simple example of Boolean function that has quantum speed-up is . Time complexity of quantum algorithm is , but time complexity of deterministic algorithm is , because .

Let us compare our quantum algorithm with existing ones. Quantum algorithms for computing AND-OR trees and NAND-trees were considered by Ambainis et al. [ACR10, Amb07, Amb10]. Authors presented algorithms that has time complexity .

If we apply our algorithm to a tree, then time complexity is , because a tree is DAG and number of edges is . Therefore, we get worse time complexity, even if we compare with a deterministic algorithm for a tree that has complexity .

At the same time, the algorithm of Ambainis et al. cannot be applied to DAGs, but only to trees. Note, that many formulas have big trees, but small DAGs. The difference between tree and DAG sizes can be up to exponential. In such case, our algorithm gives significant benefit comparing to the algorithm for a tree.

Let us present an example for such formula. . DAG and tree representations of parity in Figure 2. Figure 2: DAG (left) and tree (right) representations of F2=x1⊕x2⊕x3⊕x4. Black edges are labeled by 0, white edges are labeled by 1. s=1.

The third example is a Boolean function , for positive integers and , such that . The function has AND-OR tree with size because of parity function. If we want to prove it we can use the idea from [Smo93]. If we consider AND-OR DAG then and , because we have only one vertex for each conjunction . So we have following time complexities for this boolean formula:

• Algorithm 4: .

• Deterministic algorithm that can compute XOR function and deterministic algorithm for AND-OR DAG evaluation: , because we should test all variables .

• The quantum algorithm for AND-OR tree evaluation .

## 5 Quantum Algorithm for DAG’s Diameter Search Problem

A diameter of a directed acycling graph is the length of the shortest path between two the most far vertexes. The length of a path is a number of edges in this path. Formally, let be the length of the shortest path between vertexes and . If there is no path between and then . Then , where .

It is easy to see that diameter has the following property, but we present the proof for completeness of presentation.

###### Lemma 5

For any directed acycling graph , there are vertexes and such that there is no ingoing edges for , and the length of the shortest path between and equals to the diameter .

###### Proof

Assume that there is and such that , but there is ingoing edge for or .

Assume that has an ingoing edge. Therefore, there is such that an edge . Hence, there is a path between vertexes and such that . It is a contradiction with definition of diameter.

Assume that has an outgoing edge. Therefore, there is such that an edge . Hence, there is a path between vertexes and such that . It is a contradiction with definition of diameter.

So, we assumed that the claim of the lemma is wrong and got the contradiction.

Assume that we have a restriction for diameter: for some positive integer . If we have not a restriction, then we use . So, we can present an algorithm for diameter finding:

Algorithm 5.

Suppose we have an array where we will store a length of the shortest path from a vertex to most far vertex . Let be the Dürr-Høyer quantum algorithm for function. Let be a function such that , if ; , if .

The algorithm is following.

for from to

for from to

, where , .

if then

if then

else

Algorithm 5 has the following property:

###### Theorem 5.1

Algorithm 5 computes a diameter of the graph ; has time complexity and error probability .

###### Proof

It is easy to see that . Note that the inner loop by is just a repetition of the algorithm for decreasing the error. Therefore, by definition of diameter. Hence, Algorithm 5 computes diameter.

Time complexity of computing is and error is . Time complexity and error of main part of the algorithm can be computed by the similar way as in Lemma 4, because the difference between main parts of Algorithm 3 and Algorithm 5 only in explicit realizations of MAX function and function on vertexes from . Note that , because .

## 6 Conclusion

We suggest the quantum dynamic programming algorithm for some problems on directed acycling graphs. Any deterministic dynamic programming algorithm on DAGs can be converted to quantum one if it uses MAX, MIN, OR, AND, NAND functions or any other functions that have quantum speed-up.

As examples, we suggested quantum algorithms for evaluating Boolean function that presented by AND-OR and NAND directed acycling graphs; and computing diameter of a DAG.

Considering quantum dynamic programming algorithms for other problems is interesting.

##### Acknowledgements.

This work was supported by ERC Advanced Grant MQC. The work is performed according to the Russian Government Program of Competitive Growth of Kazan Federal University. I thank Andris Ambainis and Alexander Rivosh from University of Latvia for helpful discussions.

## References

• [ACR10] Andris Ambainis, Andrew M Childs, Ben W Reichardt, Robert Špalek, and Shengyu Zhang. Any and-or formula of size n can be evaluated in time on a quantum computer. SIAM Journal on Computing, 39(6):2513–2530, 2010.
• [Amb07] Andris Ambainis. A nearly optimal discrete query quantum algorithm for evaluating nand formulas. arXiv preprint arXiv:0704.3628, 2007.
• [Amb10] Andris Ambainis. Quantum algorithms for formula evaluation. https://arxiv.org/abs/1006.3651, 2010.
• [AŠ06] Andris Ambainis and Robert Špalek. Quantum algorithms for matching and network flows. In Annual Symposium on Theoretical Aspects of Computer Science, pages 172–183. Springer, 2006.
• [BBHT98] Michel Boyer, Gilles Brassard, Peter Høyer, and Alain Tapp. Tight bounds on quantum searching. Fortschritte der Physik, 46(4-5):493–505, 1998.
• [CLRS01] Thomas H Cormen, Charles E Leiserson, Ronald L Rivest, and Clifford Stein. Introduction to Algorithms-Secund Edition. McGraw-Hill, 2001.
• [DH96] Christoph Durr and Peter Høyer. A quantum algorithm for finding the minimum. arXiv preprint quant-ph/9607014, 1996.
• [DHHM04] Christoph Dürr, Mark Heiligman, Peter Høyer, and Mehdi Mhalla. Quantum query complexity of some graph problems. In International Colloquium on Automata, Languages, and Programming, pages 481–493. Springer, 2004.
• [DHHM06] Christoph Dürr, Mark Heiligman, Peter Høyer, and Mehdi Mhalla. Quantum query complexity of some graph problems. SIAM Journal on Computing, 35(6):1310–1328, 2006.
• [Dör08] Sebastian Dörn. Quantum complexity of graph and algebraic problems. PhD thesis, Universität Ulm, 2008.
• [Dör09] Sebastian Dörn. Quantum algorithms for matching problems. Theory of Computing Systems, 45(3):613–628, 2009.
• [Gro96] Lov K Grover. A fast quantum mechanical algorithm for database search. In Proceedings of the twenty-eighth annual ACM symposium on Theory of computing, pages 212–219. ACM, 1996.
• [NC10] Michael A Nielsen and Isaac L Chuang. Quantum computation and quantum information. Cambridge university press, 2010.
• [Smo93] Roman Smolensky. On representations by low-degree polynomials. In Foundations of Computer Science, 1993. Proceedings., 34th Annual Symposium on, pages 130–138. IEEE, 1993.
• [Yab89] Sergey Vsevolodovich Yablonsky. Introduction to Discrete Mathematics: Textbook for Higher Schools. Mir Publishers, 1989.