Exact and Practical Pattern Matching for Quantum Circuit Optimization

Raban Iten, Romain Moyard, Tony Metger, David Sutter, Stefan Woerner

TL;DR

This paper introduces a novel, provably correct algorithm for finding all "maximal matches" of a smaller quantum circuit (pattern) within a larger quantum circuit. This is a crucial building block for optimizing quantum circuits, and their algorithm is shown to be practical and efficient, outperforming theoretical worst-case complexity.

What Problem Does This Paper Address?

Quantum computers, especially those in the near-term (NISQ era), are limited by the number of qubits and the presence of noise. To maximize their utility, quantum computations must be compiled into efficient quantum circuits, minimizing the number of gates. A key technique for this is "pattern matching," where a smaller circuit (pattern) is searched for within a larger circuit. If found, the pattern can be replaced by a more optimized equivalent circuit. The challenge lies in the fact that quantum gates don't always commute, meaning their order can be changed, which complicates the search for matches. Existing methods often rely on heuristics that might miss optimal solutions or are computationally too expensive. This paper addresses the need for an exact yet practical algorithm for this pattern matching problem, considering gate commutation.

Methods

The core contribution is a new classical algorithm called PatternMatch. It operates on a canonical form of quantum circuits, which is a directed acyclic graph representation that is independent of gate ordering due to commutation. The algorithm works in two main stages:

  1. ForwardMatch: This subroutine greedily finds matches for the "forward part" of the pattern. It's proven that a greedy strategy is optimal here.
  2. BackwardMatch: This more complex subroutine handles the "backward part" of the pattern, deciding which gates to include in the match while potentially needing to "unmatch" some previously found forward matches. It uses a tree-based search to explore different scenarios and find the optimal trade-off.

The algorithm also incorporates heuristics to speed up the process by making informed choices about qubit assignments and pruning the search tree, albeit at the cost of potentially missing some maximal matches.

Datasets

The authors evaluated their algorithm using:

  • Random quantum circuits: These were generated with varying numbers of gates and qubits, and with X, C-NOT, and Toffoli gates chosen uniformly at random.
  • Benchmark circuits: These are circuits representing relevant computations for quantum algorithms, which were already optimized using state-of-the-art techniques.

Key Insights and Ideas

  • Canonical Form is Key: Representing circuits in a canonical form, independent of gate ordering, simplifies the problem of handling commutation.
  • Greedy Forward, Strategic Backward: The algorithm cleverly combines a greedy approach for the initial part of the match with a more exhaustive search for the remaining part, balancing efficiency and completeness.
  • Practicality Over Theoretical Worst-Case: While the theoretical worst-case complexity is high, the authors demonstrate through numerical experiments that their algorithm scales much better in practice, making it viable for near-term quantum devices.
  • Heuristics Offer a Trade-off: The introduced heuristics provide a tunable way to balance runtime and the guarantee of finding all maximal matches, making the algorithm more flexible for different use cases.

Key Results

  • Provably Correct: The algorithm is proven to find all maximal matches of a pattern in a quantum circuit, up to equivalent matches.
  • Practical Efficiency: Numerical experiments show that the algorithm scales significantly better than its theoretical worst-case complexity, often exhibiting an almost quadratic scaling with circuit size for fixed pattern size.
  • Circuit Optimization: When used for template matching, the algorithm achieved an approximate 30% reduction in gate count for random circuits.
  • Beyond State-of-the-Art: The algorithm was able to further optimize benchmark circuits that were already considered highly optimized by existing methods.
  • Heuristics Improve Runtime: The implemented heuristics provide a constant-factor improvement in runtime, with minimal impact on the scaling behavior.

Limitations and Future Work

  • Heuristic Trade-offs: The heuristics, while speeding up the algorithm, can lead to missing some matches. The paper notes that for the pattern in Figure 7a, the qubit heuristics caused missing ~39% of matches longer than half the pattern.
  • Complexity Scaling: The worst-case complexity still scales exponentially with the pattern size, making it impractical for very large patterns.
  • Future Work:
    • Developing more sophisticated heuristics for qubit assignment and BackwardMatch.
  • Exploring optimizations that consider gate commutation beyond pairwise interactions.
  • Investigating templates that don't require fixed qubit assignments.
  • Extending the approach to more general subgraph isomorphism problems with different rewriting rules.
  • Implementing a high-performance version in languages like C++ and leveraging parallelization.

Summary updated

Abstract

Quantum computations are typically compiled into a circuit of basic quantum gates. Just like for classical circuits, a quantum compiler should optimize the quantum circuit, e.g. by minimizing the number of required gates. Optimizing quantum circuits is not only relevant for improving the runtime of quantum algorithms in the long term, but is also particularly important for near-term quantum devices that can only implement a small number of quantum gates before noise renders the computation useless. An important building block for many quantum circuit optimization techniques is pattern matching, where given a large and a small quantum circuit, we are interested in finding all maximal matches of the small circuit, called pattern, in the large circuit, considering pairwise commutation of quantum gates. In this work, we present a classical algorithm for pattern matching that provably finds all maximal matches in time polynomial in the circuit size (for a fixed pattern size). Our algorithm works for both quantum and reversible classical circuits. We demonstrate numerically that our algorithm, implemented in the open-source library Qiskit, scales considerably better than suggested by the theoretical worst-case complexity and is practical to use for circuit sizes typical for near-term quantum devices. Using our pattern matching algorithm as the basis for known circuit optimization techniques such as template matching and peephole optimization, we demonstrate a significant (~30%) reduction in gate count for random quantum circuits, and are able to further improve practically relevant quantum circuits that were already optimized with state-of-the-art techniques.
BibTeX
@article{iten2022exact,
  author = {Raban Iten and Romain Moyard and Tony Metger and David Sutter and Stefan Woerner},
  title = {{Exact and Practical Pattern Matching for Quantum Circuit Optimization}},
  journal = {ACM Trans. Quantum Comput},
  year = {2022},
  doi = {10.1145/3498325},
}
Figure