cqlib_core/ir/mod.rs
1// This code is part of Cqlib.
2//
3// (C) Copyright China Telecom Quantum Group 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//! # Quantum Circuit Intermediate Representation (IR)
14//!
15//! This module provides parsers and serializers for various quantum circuit formats,
16//! enabling interoperability between Cqlib and other quantum computing tools.
17//!
18//! ## Supported Formats
19//!
20//! | Format | Description | Load Module | Dump Module |
21//! |--------|-------------|--------------|-------------|
22//! | OpenQASM 2.0 | IBM's quantum assembly language | [`qasm2::load`] | [`qasm2::dump`] |
23//! | OpenQASM 3.0 | OpenQASM 3 circuit language | [`qasm3::load`] | [`qasm3::dump`] |
24//! | QCIS | Telecom Quantum's intermediate format | [`qcis::load`] | [`qcis::dump`] |
25//!
26//! ## Error Types
27//!
28//! Each format keeps concrete error types so callers can match format-specific
29//! failures without downcasting:
30//!
31//! | Format | Load Error | Dump Error |
32//! |--------|------------|------------|
33//! | OpenQASM 2.0 | [`qasm2::load::QasmParseError`] | [`qasm2::dump::QasmDumpError`] |
34//! | OpenQASM 3.0 | [`qasm3::load::Qasm3ParseError`] | [`qasm3::dump::Qasm3DumpError`] |
35//! | QCIS | [`qcis::load::QcisParseError`] | [`qcis::dump::QcisDumpError`] |
36//!
37//! File-based entry points preserve the original I/O error through
38//! [`std::error::Error::source`]. The crate does not currently expose a shared
39//! `Format` trait or umbrella IR error enum; use the explicit per-format modules
40//! or crate-root aliases below.
41//!
42//! ## Quick Start
43//!
44//! ### Loading from OpenQASM
45//!
46//! ```rust
47//! use cqlib_core::ir::qasm2_loads;
48//!
49//! let qasm = r#"
50//! OPENQASM 2.0;
51//! include "qelib1.inc";
52//! qreg q[2];
53//! h q[0];
54//! cx q[0], q[1];
55//! "#;
56//!
57//! let circuit = qasm2_loads(qasm).unwrap();
58//! ```
59//!
60//! ### Loading from QCIS
61//!
62//! ```rust
63//! use cqlib_core::ir::qcis_loads;
64//!
65//! let qcis = r#"
66//! H Q0
67//! CZ Q0 Q1
68//! M Q0 Q1
69//! "#;
70//!
71//! let circuit = qcis_loads(qcis).unwrap();
72//! ```
73//!
74//! ### Dumping to Format
75//!
76//! ```rust
77//! use cqlib_core::circuit::{Circuit, Qubit};
78//! use cqlib_core::ir::{qasm2_dumps, qasm3_dumps, qcis_dumps};
79//!
80//! let mut circuit = Circuit::new(2);
81//! circuit.h(Qubit::new(0)).unwrap();
82//! circuit.cz(Qubit::new(0), Qubit::new(1)).unwrap();
83//!
84//! let qasm = qasm2_dumps(&circuit).unwrap();
85//! let qasm3 = qasm3_dumps(&circuit).unwrap();
86//! let qcis = qcis_dumps(&circuit).unwrap();
87//! ```
88
89pub mod qasm2;
90pub mod qasm3;
91pub mod qcis;
92
93pub use qasm2::dump::{dump as qasm2_dump, dumps as qasm2_dumps};
94pub use qasm2::load::{load as qasm2_load, loads as qasm2_loads};
95pub use qasm3::dump::{dump as qasm3_dump, dumps as qasm3_dumps};
96pub use qasm3::load::{load as qasm3_load, loads as qasm3_loads};
97pub use qcis::dump::{dump as qcis_dump, dumps as qcis_dumps};
98pub use qcis::load::{load as qcis_load, loads as qcis_loads};
99
100pub(crate) fn io_errors_equivalent(lhs: &std::io::Error, rhs: &std::io::Error) -> bool {
101 lhs.kind() == rhs.kind() && lhs.to_string() == rhs.to_string()
102}