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
175
176
177
178
179
180
181
// SPDX-FileCopyrightText: 2020 Evandro Chagas Ribeiro da Rosa <evandro@quantuloop.com>
// SPDX-FileCopyrightText: 2020 Rafael de Santiago <r.santiago@ufsc.br>
//
// SPDX-License-Identifier: Apache-2.0
//! # KBW: Ket Bitwise Quantum Simulator
//!
//! KBW is a high-performance quantum circuit simulator designed to work with the
//! [Ket](https://quantumket.org) quantum programming framework. It provides multiple
//! simulation backends that trade off memory usage against speed for different
//! circuit sizes and hardware targets.
//!
//! ## Simulator Backends
//!
//! | Type alias | Description |
//! |---|---|
//! | [`DenseSimulator`] | Full state-vector (double-buffer), single process |
//! | [`DenseV2Simulator`] | Full state-vector (in-place), single process |
//! | [`DenseV2BlockSimulator`] | Blocked state-vector for >local-memory qubit counts |
//! | [`DenseGPUSimulator`] | GPU-accelerated full state-vector via [CubeCL](https://github.com/tracel-ai/cubecl) |
//! | [`DenseGPUBlockSimulator`] | Blocked GPU state-vector for large circuits |
//! | [`SparseSimulator`] | Sparse (HashMap-based) state-vector, single process |
//! | [`SparseV2Simulator`] | Sparse (Vec-based, cache-friendly) state-vector |
//!
//! ## Execution Modes
//!
//! Each simulator can be used in two execution modes, configured via
//! [`quantum_execution::QubitManager`]:
//!
//! - **Live execution** ([`QubitManager::make_live_config`](quantum_execution::QubitManager::make_live_config)):
//! Executes each gate immediately as it is received. Best suited for interactive
//! or streaming workloads.
//! - **Batch execution** ([`QubitManager::make_batch_config`](quantum_execution::QubitManager::make_batch_config)):
//! Accumulates a full circuit description (including coupling-graph constraints
//! and gradient strategies) and executes it in one shot. Recommended for
//! variational algorithms and optimisation loops.
//!
//! ## C API
//!
//! A C-compatible ABI is exposed by [`c_api`] for embedding KBW in Python
//! extensions or other FFI consumers. The entry point is
//! [`c_api::kbw_make_configuration`].
//!
//! ## Environment Variables
//!
//! | Variable | Default | Description |
//! |---|---|---|
//! | `KBW_SEED` | random | Unsigned 64-bit integer seed for the RNG used by measurement and sampling. Set for deterministic results. |
//! | `KBW_BLOCK_SIZE` | `20` | Number of *local* qubits per block in the blocked simulators. The remaining qubits become *global* and trigger inter-block SWAP operations. |
//!
//! ## Quick Start
//!
//! ```rust,no_run
//! use kbw::DenseSimulator;
//! use ket::error::KetError;
//!
//! fn main() -> Result<(), KetError> {
//! let num_qubits = 10;
//! // Build a live-mode configuration for 10 qubits using the dense backend.
//! let config = DenseSimulator::make_live_config(num_qubits, false)?;
//! // Pass `config` to `ket::process::Process::new` to run a quantum program.
//! Ok(())
//! }
//! ```
use ;
/// Extension trait that adds simulator-specific helpers on top of the standard
/// [`num_traits::Float`] and arithmetic-assignment bounds.
///
/// All generic simulator implementations (`Dense`, `Sparse`, `DenseV2`, …) are
/// parameterised over `F: FloatOps` so that the same code can run in either
/// single-precision (`f32`) or double-precision (`f64`) mode.
/// Double-buffered, dense state-vector simulator using `f32` amplitudes.
/// Supports up to 32 qubits.
pub type Dense = Dense;
/// [`QubitManager`](quantum_execution::QubitManager) wrapping the [`Dense`] backend.
/// The recommended entry point for single-process dense simulation.
pub type DenseSimulator = QubitManager;
/// In-place dense state-vector simulator using `f32` amplitudes.
/// Slightly lower memory overhead than [`Dense`] due to the single-buffer design.
pub type DenseV2 = DenseV2;
/// [`QubitManager`](quantum_execution::QubitManager) wrapping the [`DenseV2`] backend.
pub type DenseV2Simulator = QubitManager;
/// Blocked [`DenseV2`] simulator that partitions the qubit register into
/// *local* (simulated) and *global* (index) portions, enabling circuits
/// with more qubits than fit in a single state-vector.
pub type DenseV2Block = Block;
/// [`QubitManager`](quantum_execution::QubitManager) wrapping the [`DenseV2Block`] backend.
pub type DenseV2BlockSimulator = QubitManager;
/// GPU-accelerated dense state-vector simulator using the WGPU runtime and `f32` amplitudes.
/// Requires a compatible GPU and the `wgpu` feature of `CubeCL`.
pub type DenseGPU = DenseGPU;
/// [`QubitManager`](quantum_execution::QubitManager) wrapping the [`DenseGPU`] backend.
pub type DenseGPUSimulator = QubitManager;
/// Blocked GPU dense state-vector; combines the WGPU backend with the block
/// partitioning scheme for circuits exceeding on-device memory.
pub type DenseGPUBlock = Block;
/// [`QubitManager`](quantum_execution::QubitManager) wrapping the [`DenseGPUBlock`] backend.
/// Note: the typo `DenseGPUS` is preserved for backwards compatibility.
pub type DenseGPUBlockSimulator = QubitManager;
/// Sparse state-vector simulator backed by a [`HashMap`](std::collections::HashMap),
/// using `f32` amplitudes. Efficient for circuits that remain in a low-entanglement
/// regime (e.g. few non-zero amplitudes).
pub type Sparse = Sparse;
/// [`QubitManager`](quantum_execution::QubitManager) wrapping the [`Sparse`] backend.
pub type SparseSimulator = QubitManager;
/// Sparse state-vector simulator backed by a sorted `Vec`, providing better
/// cache locality than [`Sparse`] at the cost of periodic sort-and-reduce passes.
pub type SparseV2 = SparseV2;
/// [`QubitManager`](quantum_execution::QubitManager) wrapping the [`SparseV2`] backend.
pub type SparseV2Simulator = QubitManager;