Skip to main content
Log in

Quantum Pattern Matching Fast on Average

  • Published:
Algorithmica Aims and scope Submit manuscript

Abstract

The d-dimensional pattern matching problem is to find an occurrence of a pattern of length \(m \times \dots \times m\) within a text of length \(n \times \dots \times n\), with \(n \ge m\). This task models various problems in text and image processing, among other application areas. This work describes a quantum algorithm which solves the pattern matching problem for random patterns and texts in time \(\widetilde{O}((n/m)^{d/2} 2^{O(d^{3/2}\sqrt{\log m})})\). For large m this is super-polynomially faster than the best possible classical algorithm, which requires time \(\widetilde{\Omega }( n^{d/2} + (n/m)^d)\). The algorithm is based on the use of a quantum subroutine for finding hidden shifts in d dimensions, which is a variant of algorithms proposed by Kuperberg.

This is a preview of subscription content, log in via an institution to check access.

Access this article

Price excludes VAT (USA)
Tax calculation will be finalised during checkout.

Instant access to the full article PDF.

Fig. 1
Fig. 2

Similar content being viewed by others

References

  1. Ambainis, A.: Quantum lower bounds by quantum arguments. J. Comput. Syst. Sci. 64, 750–767. quant-ph/0002066 (2002)

  2. Andoni, A., Hassanieh, H., Indyk, P., Katabi, D.: Shift finding in sub-linear time. In: Proceedings of 24th ACM-SIAM Symposium on Discrete Algorithms, pp. 457–465, (2013)

  3. Batu, T., Ergün, F., Kilian, J., Magen, A., Raskhodnikova, S., Rubinfeld, R., Sami, R.: A sublinear algorithm for weakly approximating edit distance. In: Proceedings of 35th Annual ACM Symposium on Theory of Computing, pp. 316–324, (2003)

  4. Bennett, C., Bernstein, E., Brassard, G., Vazirani, U.: Strengths and weaknesses of quantum computing. SIAM J. Comput. 26(5), 1510–1523. quant-ph/9701001 (1997)

  5. Boyer, R., Moore, S.: A fast string searching algorithm. Commun. ACM 20(10), 762–772 (1977)

    Article  MATH  Google Scholar 

  6. Brassard, G., Høyer, P., Mosca, M., Tapp, A.: Quantum Amplitude Amplification and Estimation. Quantum Computation and Quantum Information: A Millennium Volume, pp. 53–74. quant-ph/0005055 (2002)

  7. Chang, W., Lawler, E.: Sublinear approximate string matching and biological applications. Algorithmica 12(4–5), 327–344 (1994)

    Article  MathSciNet  MATH  Google Scholar 

  8. Childs, A., van Dam, W.: Quantum algorithm for a generalized hidden shift problem. In: Proceedings of 18th ACM-SIAM Symposium on Discrete Algorithms, pp. 1225–1232. quant-ph/0507190 (2007)

  9. Childs, A., Kothari, R., Ozols, M., Roetteler, M.: Easy and hard functions for the boolean hidden shift problem. In: Proceedings of 8th Conference on the Theory of Quantum Computation, Communication, and Cryptography (TQC’13), pp. 50–79. arXiv:1304.4642 (2013)

  10. Crochemore, M., Hancart, C., Lecroq, T.: Algorithms on Strings. Cambridge University Press, Cambridge (2007)

    Book  MATH  Google Scholar 

  11. Curtis, D., Meyer, D.: Towards quantum template matching. In: Proceedings of SPIE 5161, Quantum Communications and Quantum Imaging, pp. 134–141, (2004)

  12. Ettinger, M., Høyer, P., Knill, E.: The quantum query complexity of the hidden subgroup problem is polynomial. Inf. Process. Lett. 91, 43–48 (2004). quant-ph/0401083

    Article  MathSciNet  MATH  Google Scholar 

  13. Friedl, K., Ivanyos, G., Magniez, F., Santha, M., Sen, P.: Hidden translation and orbit coset in quantum computing. In: Proceedings of 35th Annual ACM Symposium on Theory of Computing, pp. 1–9. quant-ph/0211091 (2003)

  14. Gavinsky, D., Roetteler, M., Roland, J.: Quantum algorithm for the Boolean hidden shift problem. In: Proceedings of 17th International Computing & Combinatorics Conference (COCOON’11), pp. 158–167. arXiv:1103.3017 (2011)

  15. Gharibi, M.: Reduction from non-injective hidden shift problem to injective hidden shift problem. Quantum Inf. Comput. 13(3&4), 221–230 (2013). arXiv:1207.4537

    MathSciNet  Google Scholar 

  16. Giovannetti, V., Lloyd, S., Maccone, L.: Quantum random access memory. Phys. Rev. Lett. 100, 160501 (2008). arXiv:0708.1879

    Article  MathSciNet  MATH  Google Scholar 

  17. Grover, L.: Quantum mechanics helps in searching for a needle in a haystack. Phys. Rev. Lett. 79(2), 325–328 (1997). quant-ph/9706033

    Article  Google Scholar 

  18. Høyer, P., Mosca, M., de Wolf, R.: Quantum search on bounded-error inputs. In: Proceedings of 30th International Conference on Automata, Languages and Programming (ICALP’03), pp. 291–299. quant-ph/0304052 (2003)

  19. Kärkkäinen, J., Ukkonen, E.: Two- and higher-dimensional pattern matching in optimal expected time. SIAM J. Comput. 29(2), 571–589 (1999)

    Article  MathSciNet  MATH  Google Scholar 

  20. Knuth, D., Morris Jr, J., Pratt, V.: Fast pattern matching in strings. SIAM J. Comput. 6(2), 323–350 (1977)

    Article  MathSciNet  MATH  Google Scholar 

  21. Kuperberg, G.: A subexponential-time quantum algorithm for the dihedral hidden subgroup problem. SIAM J. Comput. 35(1), 170–188 (2005). quant-ph/0302112

  22. Kuperberg, G.: Another subexponential-time quantum algorithm for the dihedral hidden subgroup problem. In: Proceedings of 8th Conference on the Theory of Quantum Computation, Communication, and Cryptography (TQC’13), pp. 20–34. arXiv:1112.3333 (2013)

  23. Montanaro, A., de Wolf, R.: Quantum property testing. arXiv:1310.2035 (2013)

  24. Moore, C., Rockmore, D., Russell, A., Schulman, L.: The power of strong fourier sampling: quantum algorithms for affine groups and hidden shifts. SIAM J. Comput. 37(3), 938–958 (2005). quant-ph/0503095

    Article  MathSciNet  MATH  Google Scholar 

  25. Navarro, G.: A guided tour to approximate string matching. ACM Comput. Surv. 33(1), 31–88 (2001)

    Article  Google Scholar 

  26. Ozols, M., Roetteler, M., Roland, J.: Quantum rejection sampling. ACM Trans. Comput. Theory (TOCT) 5(3), 11:1–11:33 (2013). arXiv:1103.2774

    MathSciNet  MATH  Google Scholar 

  27. Ramesh, H., Vinay, V.: String matching in \(\widetilde{O}(\sqrt{n} + \sqrt{m})\) quantum time. J. Discrete Algorithms 1, 103–110 (2003). quant-ph/0011049

    Article  MathSciNet  MATH  Google Scholar 

  28. Regev, O.: A subexponential time algorithm for the dihedral hidden subgroup problem with polynomial space. quant-ph/0406151 (2004)

  29. Rötteler, M.: Quantum algorithms to solve the hidden shift problem for quadratics and for functions of large Gowers norm. In: Proceedings of 39th International Symposium on Mathematical Foundations of Computer Science (MFCS’09), LNCS vol. 5734, pp. 663–674. arXiv:0911.4724 (2009)

  30. Rötteler, M.: Quantum algorithms for highly non-linear Boolean functions. In: Proceedings of 21st ACM-SIAM Symposium on Discrete Algorithms, pp. 448–457. arXiv:0811.3208 (2010)

  31. Twamley, J.: A hidden shift quantum algorithm. J. Phys. A Math. Gen. 33, 8973 (2000)

    Article  MathSciNet  MATH  Google Scholar 

  32. van Dam, W., Hallgren, S., Ip, L.: Quantum algorithms for some hidden shift problems. SIAM J. Comput. 36, 763–778 (2006). quant-ph/0211140

    Article  MathSciNet  MATH  Google Scholar 

  33. Vishkin, U.: Deterministic sampling—a new technique for fast pattern matching. SIAM J. Comput. 20(1), 22–40 (1991)

    Article  MathSciNet  MATH  Google Scholar 

  34. Yao, A.: The complexity of pattern matching for a random string. SIAM J. Comput. 8(3), 368–387 (1979)

    Article  MathSciNet  MATH  Google Scholar 

Download references

Acknowledgments

I would like to thank Raphaël Clifford, Markus Jalsenius and Ben Sach for helpful discussions on the topic of this paper. I would also like to thank one pseudonymous and two anonymous referees for a number of detailed comments and suggestions which have significantly improved the paper. This work was supported by an EPSRC Early Career Fellowship (EP/L021005/1).

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Ashley Montanaro.

Appendix: Proof of Claim 14

Appendix: Proof of Claim 14

In this appendix, we analyse the algorithm of Sect. 5 for identifying hidden shifts. Let \(L_i = |\mathcal {L}_i|\) denote the number of states in the list at the start of the i’th stage. Our first task is to find a lower bound on \(L_{i+1}\) in terms of \(L_i\) which holds with high probability. For ease of analysis, we consider a different, “relaxed” process where any state is allowed to be combined with any other state, rather than being restricted to the same bin; and where we assume there are always an even number of states. We then relate the relaxed process to the real process followed by the algorithm by bounding the number of states that the real process would need to discard, given its restriction to only combining states in the same bin.

Let \(L_i^{(j)}\) denote the number of states we would have in the list \(\mathcal {L}_i\) after j steps of the i’th stage, based on following the relaxed process; note that \(L_i^{(0)} = L_i\). Also let \(S_i^{(j)}\) denote the number of successful pairings in the j’th step of the i’th stage (again allowing any pair of states to be combined). Finally let \(t_i\) denote the number of steps taken in the i’th stage (i.e. until the total number of states is at most \(n^2\)). Then

$$\begin{aligned} L_i^{(j)} = \frac{L_i^{(j-1)}}{2} - S_i^{(j)},\;\;\;\; L_{i+1} \ge \sum _{j=1}^{t_i} S_i^{(j)} - t_i 2^{db_i}, \end{aligned}$$
(1)

where the second inequality allows for the fact that at each step we may need to discard at most \(2^{db_i}\) states in the real process, as compared with the relaxed process. The probability that each combination operation applied to a pair succeeds is independent and equal to 1 / 2. Using a Chernoff bound, we have

$$\begin{aligned} \Pr [|S_i^{(j)} - L_i^{(j-1)}/4| \ge \ln n \sqrt{L_i^{(j-1)}/4} ] \le 2 e^{-(\ln n)^2 / 2} = 2n^{-(\ln n)/2}. \end{aligned}$$
(2)

As there will be \({{\mathrm{poly}}}(n)\) steps in total throughout the algorithm, this quantity is small enough that we can take a union bound over all steps and assume that this event never happens. Making this assumption, we have

$$\begin{aligned} L_i^{(j+1)} \ge \frac{L_i^{(j)}}{2} - \frac{L_i^{(j)}}{4} - \frac{\ln n}{2} \sqrt{L_i^{(j)}} = \frac{L_i^{(j)}}{4}\left( 1 - \frac{2\ln n}{\sqrt{L_i^{(j)}}} \right) . \end{aligned}$$
(3)

As we have \(L_i^{(j)} \ge n^2\) for all steps j, then

$$\begin{aligned} L_i^{(j+1)} \ge \frac{L_i^{(j)}}{4}\left( 1 - \frac{2\ln n}{n} \right) , \end{aligned}$$

implying

$$\begin{aligned} L_i^{(j)} \ge L_i \left( \frac{1 - (2\ln n)/n}{4} \right) ^j. \end{aligned}$$
(4)

By (1), (2) and (3),

$$\begin{aligned} L_{i+1}\ge & {} \sum _{j=1}^{t_i} S_i^{(j)} - t_i 2^{db_i} \ge \sum _{j=1}^{t_i} \frac{L_i^{(j-1)}}{4}\left( 1 - \frac{2\ln n}{\sqrt{L_i^{(j-1)}}}\right) \\&- t_i 2^{db_i} \ge \frac{1}{4} \sum _{j=1}^{t_i} L_i^{(j-1)} \left( 1 - \frac{2\ln n}{n}\right) - t_i 2^{db_i} \end{aligned}$$

and so by (4), writing \(\xi = (2\ln n)/n\),

$$\begin{aligned} L_{i+1}\ge & {} \frac{L_i}{4} (1-\xi ) \sum _{j=0}^{t_i-1} \left( \frac{1 - \xi }{4} \right) ^j - t_i 2^{db_i} = \frac{L_i}{4} (1 - \xi ) \frac{1 - ((1-\xi )/4)^{t_i} }{1 - (1-\xi )/4} - t_i 2^{db_i} \\= & {} L_i\frac{(1 - \xi ) (1 - ((1-\xi )/4)^{t_i}) }{3 + \xi } - t_i 2^{db_i}. \end{aligned}$$

A rough lower bound that follows from (3) for large enough n is that \(L_i^{(j+1)} \ge L_i^{(j)}/8\); and as \(L_i^{(j+1)} \le L_i^{(j)}/2\) always, we have \(1/8 \le L_i^{(j+1)}/L_i^{(j)} \le 1/2\). Using \(L_i^{(0)} = L_i\) and \(L_i^{(t_i)} \le n^2\) we obtain \(\log _2 (L_i / n^2) \le t_i \le 3\log _2 (L_i / n^2)\). So

$$\begin{aligned} ((1-\xi )/4)^{t_i} \le 4^{-t_i} \le n^4 / L_i^2. \end{aligned}$$

Assuming that \(L_i \ge n^3\) for all \(1 \le i \le S\), we have

$$\begin{aligned} \frac{(1 - \xi ) (1 - ((1-\xi )/4)^{t_i}) }{3+\xi } \ge \frac{(1- (2\ln n) / n)(1-1/n^2)}{3+ (2\ln n) / n} = \frac{1- O((\log n)/n)}{3}. \end{aligned}$$

Further assume that \(L_i = 2^{O(\sqrt{n})}\) for all i, implying \(t_i = O(\sqrt{n})\). Then

$$\begin{aligned} L_{i+1} \ge \frac{(1- O((\log n)/n))L_i}{3} - O(\sqrt{n}) 2^{db_i}. \end{aligned}$$

We now need to determine how large \(L_1\) needs to be such that \(L_{S+1}\) is still quite large. Working backwards, we can take

$$\begin{aligned} L_{i-1} \le 3(1+O((\log n)/n))L_i + O(\sqrt{n})2^{db_{i-1}}. \end{aligned}$$

Thus

$$\begin{aligned} L_1\le & {} \left( 3(1+O((\log n)/n)) \right) ^S L_{S+1} + \left( 3(1+O((\log n)/n)) \right) ^{S-1} O(\sqrt{n})2^{db_S} \\&+ \dots + O(\sqrt{n})2^{db_1}. \end{aligned}$$

Assuming that \(L_{S+1} = O(1)\), and \(S=O(\sqrt{n})\), we have

$$\begin{aligned} L_1 = O\left( \sqrt{n} \sum _{i=1}^S 3^i 2^{db_i}\right) \end{aligned}$$

We now need to pick values \(S,\, b_i\) for the number of stages and the number of bits zeroed at each stage, such that \(\sum _{i=1}^S b_i = n-1\), to minimise \(\sum _{i=1}^S 3^i 2^{d b_i}\). We choose these values to make all the above terms equal to \(2^{c \sqrt{n}}\) for some fixed c, i.e. \(b_i = (c\sqrt{n} - (\log _2 3)i)/d\) (for simplicity ignoring the fact that \(b_i\) has to be rounded to an integer). Relaxing to the constraint \(\sum _i b_i = n\) for simplicity, we obtain \(S c \sqrt{n} - (\log _2 3)S(S+1)/2 = dn\). Hence \(c = d\sqrt{n}/S + (\log _2 3)(S+1)/(2\sqrt{n})\). Minimising this over S, we get that the minimum is found at \(S = \sqrt{2 \log _3 2} \sqrt{dn}\), giving

$$\begin{aligned} c = \frac{\sqrt{d}}{\sqrt{2 \log _3 2}} + \frac{\log _2 3(\sqrt{2 \log _3 2} \sqrt{dn} + 1)}{2 \sqrt{n}} = \sqrt{(2\log _2 3)d} + O(1/\sqrt{n}). \end{aligned}$$

Thus

$$\begin{aligned} L_1 = O(\sqrt{n}\,S \, 2^{(\sqrt{2(\log _2 3)d}+O(1/\sqrt{n}))\sqrt{n}}) = O(n 2^{\sqrt{(2\log _2 3)dn}}) = O(n 2^{1.781\dots \sqrt{dn}}) \end{aligned}$$

as claimed.

Rights and permissions

Reprints and permissions

About this article

Check for updates. Verify currency and authenticity via CrossMark

Cite this article

Montanaro, A. Quantum Pattern Matching Fast on Average. Algorithmica 77, 16–39 (2017). https://doi.org/10.1007/s00453-015-0060-4

Download citation

  • Received:

  • Accepted:

  • Published:

  • Issue Date:

  • DOI: https://doi.org/10.1007/s00453-015-0060-4

Keywords

Navigation