cqlib_core/compile/commutation/mod.rs
1// This code is part of Cqlib.
2//
3// (C) Copyright China Telecom Quantum Group 2025-2026
4//
5// This code is licensed under the Apache License, Version 2.0. You may
6// obtain a copy of this license in the LICENSE.txt file in the root directory
7// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
8//
9// Any modifications or derivative works of this code must retain this
10// copyright notice, and modified files need to carry a notice indicating
11// that they have been altered from the originals.
12
13//! # Gate Commutation
14//!
15//! This module proves when two concrete instruction applications can be
16//! exchanged by compiler passes without changing circuit semantics. A
17//! successful query returns a [`Commutation`] proof, while `None` means only
18//! that the available oracles could not prove commutation.
19//!
20//! The checker combines several conservative proof sources:
21//!
22//! - fast structural facts such as identity, global phase, disjoint support,
23//! and identical applications;
24//! - algebraic rules for diagonal gates, Pauli-axis strings, controlled-axis
25//! gates, and selected symmetric two-qubit families;
26//! - explicit `A; B -> B; A` rules from the compiler knowledge library;
27//! - an optional small local-matrix fallback for concrete parameters.
28//!
29//! # Examples
30//!
31//! ```rust
32//! use cqlib_core::circuit::{Instruction, Parameter, Qubit, StandardGate};
33//! use cqlib_core::compile::commutation::{check_commutation, Commutation};
34//!
35//! let result = check_commutation(
36//! &Instruction::Standard(StandardGate::RZ),
37//! &[Qubit::new(0)],
38//! &[Parameter::symbol("a")],
39//! &Instruction::Standard(StandardGate::RZ),
40//! &[Qubit::new(0)],
41//! &[Parameter::symbol("b")],
42//! );
43//!
44//! assert_eq!(result, Some(Commutation::Exact));
45//! ```
46
47pub mod algebra;
48pub mod checker;
49pub mod matrix;
50pub mod rules;
51
52pub use checker::{
53 Commutation, CommutationChecker, CommutationConfig, CommutationResult, check_commutation,
54};
55
56pub use algebra::algebraic_commutation;
57
58#[cfg(test)]
59#[path = "./commutation_test.rs"]
60mod commutation_test;