Table of Contents
Fetching ...

Scalable Pattern Matching in Computation Graphs

Luca Mondada, Pablo Andrés-Martínez

TL;DR

The paper tackles pattern matching in port graphs for graph rewriting by introducing a pre-computed data structure that encodes a large set of patterns, enabling a single query to identify all embeddings without depending on the number of patterns. The core idea is to select anchor sets for each pattern and build a canonical tree (CT) representation via split graphs, turning pattern matching into tree-inclusion problems and string-prefix matching on encoded CTs. The authors derive theoretical complexity bounds showing exponential growth only in the pattern width w, with a pre-computation cost of O((d·ℓ)^w·ℓ + ℓ·w^2·d) and a per-subgraph query cost of O(|G|·(c^w)/w^{1/2}·d) where c=6.75, plus a practical Rust implementation that achieves up to a 20x speedup over state-of-the-art tools on datasets of thousands of quantum circuit patterns. Empirically, the approach scales well for large pattern sets and small widths, offering a promising avenue for scalable graph rewriting in computation graphs and quantum circuit optimization. The work provides both solid theoretical guarantees and a concrete open-source tool to accelerate pattern-based optimisations in real-world domains.

Abstract

Graph rewriting is a popular tool for the optimisation and modification of graph expressions in domains such as compilers, machine learning and quantum computing. The underlying data structures are often port graphs - graphs with labels at edge endpoints. A pre-requisite for graph rewriting is the ability to find graph patterns. We propose a new solution to pattern matching in port graphs. Its novelty lies in the use of a pre-computed data structure that makes the pattern matching runtime complexity independent of the number of patterns. This offers a significant advantage over existing solutions for use cases with large sets of small patterns. Our approach is particularly well-suited for quantum superoptimisation. We provide an implementation and benchmarks showing that our algorithm offers a 20x speedup over current implementations on a dataset of 10000 real world patterns describing quantum circuits.

Scalable Pattern Matching in Computation Graphs

TL;DR

The paper tackles pattern matching in port graphs for graph rewriting by introducing a pre-computed data structure that encodes a large set of patterns, enabling a single query to identify all embeddings without depending on the number of patterns. The core idea is to select anchor sets for each pattern and build a canonical tree (CT) representation via split graphs, turning pattern matching into tree-inclusion problems and string-prefix matching on encoded CTs. The authors derive theoretical complexity bounds showing exponential growth only in the pattern width w, with a pre-computation cost of O((d·ℓ)^w·ℓ + ℓ·w^2·d) and a per-subgraph query cost of O(|G|·(c^w)/w^{1/2}·d) where c=6.75, plus a practical Rust implementation that achieves up to a 20x speedup over state-of-the-art tools on datasets of thousands of quantum circuit patterns. Empirically, the approach scales well for large pattern sets and small widths, offering a promising avenue for scalable graph rewriting in computation graphs and quantum circuit optimization. The work provides both solid theoretical guarantees and a concrete open-source tool to accelerate pattern-based optimisations in real-world domains.

Abstract

Graph rewriting is a popular tool for the optimisation and modification of graph expressions in domains such as compilers, machine learning and quantum computing. The underlying data structures are often port graphs - graphs with labels at edge endpoints. A pre-requisite for graph rewriting is the ability to find graph patterns. We propose a new solution to pattern matching in port graphs. Its novelty lies in the use of a pre-computed data structure that makes the pattern matching runtime complexity independent of the number of patterns. This offers a significant advantage over existing solutions for use cases with large sets of small patterns. Our approach is particularly well-suited for quantum superoptimisation. We provide an implementation and benchmarks showing that our algorithm offers a 20x speedup over current implementations on a dataset of 10000 real world patterns describing quantum circuits.
Paper Structure (42 sections, 19 theorems, 24 equations, 5 figures)

This paper contains 42 sections, 19 theorems, 24 equations, 5 figures.

Key Result

Theorem 0

Let $P_1, \dots, P_\ell$ be patterns with at most width $w$ and depth $d$. The pre-computation runs in time and space complexity For any subject port graph $G$, the pre-computed prefix tree can be used to find all pattern embeddings $P_i \to G$ in time where $c = 6.75$ is a constant.

Figures (5)

  • Figure 1: Pattern matching on a port graph is reduced to the problem of matching on trees. A subset of vertices are chosen as anchor sets (left). A neighbourhood of the anchors is extracted and represented as a tree (middle). Finally, pattern matches are found by searching for matching subtrees (right).
  • Figure 2: A port graph and its linear path decomposition (coloured edges) on the left. On the right, the split graph resulting from the choice of anchors $X = \{A, D\}$. We use the circuits convention, i.e. port labels are partitioned into linear paths using the relation $i_k \sim o_k$.
  • Figure 3: The split graph of \ref{['fig:splitgraph']}, represented by 6 strings obtained from its 3 linear paths.
  • Figure 4: Runtime of pattern matching for $\ell = 0\dots 10^4$ patterns on 2, 3 and 4 qubit quantum circuits from the Quartz ECC dataset, for our implementation (Portmatching) and the Quartz project. All $\ell = 1954$ two qubit circuits were used, whereas for 3 and 4 qubit circuits, $\ell = 10^4$ random samples were drawn.
  • Figure 5: Runtime of our pattern matching for random quantum circuits with up to 10 qubits.

Theorems & Definitions (28)

  • Theorem 0
  • Proposition 1
  • Proposition 2
  • Proposition 3
  • Corollary 4
  • Proposition 5
  • proof
  • Proposition 6
  • Proposition 7
  • Proposition 8: Correctness of AllAnchors
  • ...and 18 more