cqlib_core/compile/resource/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.
6// You may obtain a copy of this license in the LICENSE.txt file in
7// the root directory of this source tree or at
8// http://www.apache.org/licenses/LICENSE-2.0.
9//
10// Any modifications or derivative works of this code must retain this
11// copyright notice, and modified files need to carry a notice indicating
12// that they have been altered from the originals.
13
14//! Shared logical-qubit ancillary-resource management for compiler stages.
15//!
16//! This module is compiler infrastructure rather than MCX-specific synthesis
17//! code. It tracks which logical qubits compiler transforms may use as
18//! ancillary resources and keeps allocation policy separate from algorithms
19//! that consume those resources.
20//!
21//! The main entry point is [`ResourceManager`]. A compiler planner asks the
22//! manager for a side-effect-free [`ResourcePlan`] with
23//! [`ResourceManager::preview`], compares feasible algorithm candidates, and
24//! commits only the selected plan with [`ResourceManager::commit`]. The returned
25//! [`ResourceLease`] reserves its qubits until the caller restores their
26//! required state and calls [`ResourceManager::release`].
27//!
28//! Clean and dirty resources are usage contracts, not facts inferred by
29//! simulating quantum state. A clean ancillary qubit must enter an algorithm in
30//! `|0>` and be restored to `|0>` before release. A dirty ancillary qubit may
31//! enter in an unknown, possibly entangled state, so the consuming algorithm
32//! must separately prove that it restores the complete input state before
33//! release.
34//!
35//! Input qubits are not automatically clean ancillary resources. Although a
36//! circuit's input qubits conventionally start in `|0>`, the compiler does not
37//! currently analyze intermediate quantum state and therefore cannot establish
38//! that an input qubit is still clean at an arbitrary transform boundary.
39//!
40//! The compiler may create clean logical ancillary qubits before layout. After
41//! [`ResourceManager::enter_post_layout`] is called, requests may reuse
42//! registered resources but may not create logical qubits without physical
43//! mappings. Activating unused physical capacity after layout requires
44//! orchestration outside this module.
45//!
46//! [`ResourceManager`] records allocation and restoration contracts; it does
47//! not prove the quantum semantics of a consuming algorithm. A compiler stage
48//! must call [`ResourceManager::verify_idle`] at an appropriate boundary to
49//! ensure that every temporary lease has been released and that the circuit and
50//! resource indexes still agree.
51//!
52//! # Example
53//!
54//! ```
55//! use cqlib_core::circuit::Circuit;
56//! use cqlib_core::compile::resource::{
57//! AncillaRequirement, ResourceLimits, ResourceManager, ResourcePolicy, ResourceRequest,
58//! };
59//! use std::collections::BTreeSet;
60//!
61//! let mut circuit = Circuit::new(2);
62//! let mut resources = ResourceManager::from_circuit(
63//! &circuit,
64//! ResourcePolicy {
65//! max_pre_layout_clean_ancillas: 1,
66//! allow_dirty_borrowing: false,
67//! },
68//! ResourceLimits::default(),
69//! )?;
70//! // Preview is side-effect free, so a planner may compare several candidates.
71//! let plan = resources.preview(&ResourceRequest {
72//! requirement: AncillaRequirement::CleanZero,
73//! count: 1,
74//! excluded: BTreeSet::new(),
75//! })?;
76//! let lease = resources.commit(&mut circuit, plan)?;
77//! // Pass lease.qubits() to an algorithm that restores the clean-zero contract.
78//! resources.release(&lease)?;
79//! resources.verify_idle(&circuit)?;
80//! # Ok::<(), Box<dyn std::error::Error>>(())
81//! ```
82
83mod error;
84mod manager;
85mod model;
86mod policy;
87
88pub use error::ResourceError;
89pub use manager::ResourceManager;
90pub use model::{AncillaRequirement, ResourceLease, ResourcePlan, ResourceRequest};
91pub use policy::{ResourceLimits, ResourcePolicy};
92
93#[cfg(test)]
94mod manager_test;