1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// This code is part of Cqlib.
//
// (C) Copyright China Telecom Quantum Group 2025-2026
//
// This code is licensed under the Apache License, Version 2.0. You may
// obtain a copy of this license in the LICENSE.txt file in the root directory
// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
//
// Any modifications or derivative works of this code must retain this
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.
//! Rule DSL parser, loader, and dumper for the cqlib compiler.
//!
//! This module provides a small domain-specific language (DSL) for describing
//! peephole optimization rules. A rule consists of:
//! - A **match** block: a sequence of gate patterns to match in the circuit.
//! - An optional **require** block: algebraic conditions that must hold for the
//! variables appearing in the match.
//! - A **rewrite** block: the replacement gates (may be empty, meaning deletion).
//!
//! # Grammar (EBNF)
//!
//! ```ebnf
//! rule_file ::= rule*
//!
//! rule ::= "rule" ident "{"
//! "match" "{" gate_patterns "}"
//! ("require" "{" conditions "}")?
//! "rewrite" "{" gate_patterns "}"
//! "}"
//!
//! gate_patterns ::= [gate_pattern (","? gate_pattern)*]
//!
//! gate_pattern ::= gate_spec ["(" param_list ")"] qubit_list
//! gate_spec ::= ident | "MC" ident "[" number "]"
//! param_list ::= expr ("," expr)*
//! qubit_list ::= number+
//!
//! conditions ::= [condition (","? condition)*]
//! condition ::= expr "==" expr ["mod" expr]
//! expr ::= mathematical expression (parsed by `Parameter::try_from`)
//! ```
//!
//! # Key constraints
//!
//! - **Qubits must appear outside parentheses**: write `RX(a) 0`, not `RX(0, a)`.
//! - **Qubit numbers are rule-local logical labels**, not fixed physical qubit
//! indices. The same label refers to the same matched qubit within one rule.
//! Rewrite operations may only reference labels that appeared in the match block.
//! - **Multiple patterns on the same line must be separated by commas**;
//! commas are optional between line breaks.
//! - **Line comments** starting with `//` are supported.
//! - The mathematical constant `π` is automatically resolved to `f64::consts::PI`
//! when expressions are evaluated.
//!
//! # Example
//!
//! ```text
//! rule merge_rz {
//! match {
//! RZ(a) 0
//! RZ(b) 0
//! }
//! rewrite {
//! RZ(a + b) 0
//! }
//! }
//! ```
//!
//! # Module overview
//!
//! - [`lexer`] – Tokenizer that
//! produces span-based tokens borrowing from the input string.
//! - [`parser`] – Recursive-descent
//! parser that extracts raw expression text and delegates evaluation to
//! [`crate::circuit::Parameter::try_from`].
//! - [`ast`] – AST definitions (`RuleDef`, `GatePattern`).
//! - [`lower`] – Lowering logic from
//! AST to the runtime `Rule` type.
//! - [`load`] – Convenience helpers
//! for loading rules from files or strings.
//! - [`dump`] – Serialization helpers
//! for writing `Rule` or `RuleDef` back to the DSL text format.
pub use ;
pub use ;
pub use ;
pub use ;
pub use LowerError;
pub use ;