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. These port labels greatly simplify pattern matching. A pre-requisite for graph rewriting is the ability to find subgraphs of the input that match known graph identities: the pattern matching problem. 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. The runtime is bound by the maximum width $w$ and depth $d$ of the patterns, as well as the input graph size $|G|$ as $O(|G| \cdot c^w / w^{1/2} \cdot d)$ with $c = 6.75$. This offers a significant advantage over existing solutions for use cases where patterns have low width and the set of patterns is large and fixed ahead of time. In the context of quantum circuits, pattern width can be limited to qubit number. Quantum superoptimisers may use thousands of rewrite rules on circuits with less than 5 qubits, making them an ideal use case. We provide benchmarks showing that our algorithm offers a 20x speedup over current implementations on a dataset of 10'000 real world patterns describing quantum circuits.
翻译:图重写是在编译器、机器学习和量子计算等领域优化和修改图表达的一种流行工具。其底层数据结构通常是端口图——在边端点带有标签的图。这些端口标签极大简化了模式匹配。图重写的前提是具备在输入图中查找与已知图恒等式匹配的子图的能力,即模式匹配问题。我们提出了一种针对端口图模式匹配的新解决方案。其创新之处在于使用预计算的数据结构,使得模式匹配的运行时复杂度与模式数量无关。运行时复杂度受限于模式的最大宽度$w$和深度$d$,以及输入图大小$|G|$,表示为$O(|G| \cdot c^w / w^{1/2} \cdot d)$,其中$c = 6.75$。对于模式宽度较低且模式集较大且预先固定的应用场景,这相比现有解决方案具有显著优势。在量子电路的语境中,模式宽度可限制为量子比特数。量子超优化器可能在少于5个量子比特的电路上使用数千条重写规则,使其成为理想的应用场景。我们提供的基准测试显示,在描述量子电路的10,000个真实世界模式数据集上,我们的算法相比现有实现实现了20倍的加速。