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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// This code is part of Cqlib.
//
// (C) Copyright China Telecom Quantum Group 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.
//! Quantum state representations for simulation.
//!
//! This module provides different representations of quantum states:
//!
//! - [`Statevector`]: Represents pure quantum states as a vector of complex amplitudes.
//! Efficient for simulating ideal quantum circuits without noise.
//!
//! - [`DensityMatrix`]: Represents mixed quantum states as a density matrix.
//! Capable of simulating both pure and mixed states, including quantum channels
//! via Kraus operators.
//!
//! - [`DensityMatrixNoise`]: Extends the density matrix simulator with a configurable
//! noise model for realistic quantum simulations including gate errors and readout noise.
//!
//! - [`StabilizerState`]: Simulates Clifford circuits exponentially faster than
//! state-vector methods using the Aaronson-Gottesman symplectic tableau algorithm.
//! Supports thousands of qubits; rejects non-Clifford gates with a clear error.
//!
//! Circuit simulation assigns storage position `i` to `circuit.qubits()[i]`.
//! States retain this mapping for `sample` and `probs`; states constructed from
//! dimensions or arrays use IDs `0..num_qubits` until the first successful
//! `apply_circuit`. Once bound, a state rejects circuits with a different qubit
//! ID-to-position mapping before executing gates. Failed applications preserve
//! the existing mapping; gates completed before an error are not rolled back.
//! Low-level gate indices and full probability arrays refer to storage positions.
//!
//! Use the compilation result's `final_layout` to translate logical output IDs
//! to final physical IDs, then select those IDs in a measurement of the physical
//! circuit. The state maps each physical ID to its position in that circuit's
//! `qubits()` order. Measurement input order defines result bits from least to
//! most significant, with bit 0 displayed at the right of a bitstring.
//!
//! # Choosing a Simulator
//!
//! | Simulator | Use Case | Max Qubits | Noise Support |
//! |-----------|----------|------------|---------------|
//! | [`Statevector`] | Ideal universal circuits | ~30 | No |
//! | [`DensityMatrix`] | Mixed states, quantum channels | ~15 | Kraus operators |
//! | [`DensityMatrixNoise`] | Realistic device simulation | ~15 | Full noise model |
//! | [`StabilizerState`] | Clifford-only circuits | 10 000+ | No |
//!
//! # Examples
//!
//! ```rust
//! use cqlib_core::qis::{Statevector, DensityMatrix};
//!
//! // Pure state simulation
//! let mut sv = Statevector::new(3);
//! sv.apply_h(0);
//! sv.apply_cx(0, 1);
//! let probs_sv = sv.probabilities();
//!
//! // Mixed state simulation
//! let mut dm = DensityMatrix::new(3);
//! dm.apply_h(0);
//! dm.apply_cx(0, 1);
//! let probs_dm = dm.probabilities();
//!
//! // Results should be identical for pure states
//! assert!((probs_sv[0] - probs_dm[0]).abs() < 1e-10);
//! ```
//!
//! Stabilizer sampling with aggregated measurement counts:
//!
//! ```rust
//! use cqlib_core::qis::StabilizerState;
//! use std::collections::HashMap;
//!
//! let mut state = StabilizerState::new(2);
//! state.apply_h(0).unwrap();
//! state.apply_cx(0, 1).unwrap();
//!
//! let mut counts = HashMap::new();
//! for outcome in state.sample_shots(1000) {
//! *counts.entry(outcome.to_bitstring(2)).or_insert(0usize) += 1;
//! }
//!
//! assert!(counts.keys().all(|bits| bits == "00" || bits == "11"));
//! ```
use crate;
use crateQisError;
pub use validate_terminal_measurements;
use HashMap;
use Arc;
// None denotes the default identity order. Sharing the map keeps state copies
// used for sampling from duplicating circuit metadata.
type QubitMap = ;
/// Prepares a circuit mapping without changing an already bound state's labels.
/// Resolves a measurement's IDs to storage positions for the existing projection helpers.
pub use ;
pub use DensityMatrix;
pub use DensityMatrixNoise;
pub use StabilizerState;
pub use Statevector;