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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// 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 entropy and entanglement measures.
//!
//! This module provides a comprehensive collection of entropy measures and
//! entanglement metrics for quantum information analysis.
//!
//! # Entropy Measures
//!
//! - [`linear_entropy`]: Fast O(4^N) approximation, no eigendecomposition required
//! - [`renyi_entropy`]: Generalized entropy family, including collision entropy (α=2)
//! - [`entanglement_entropy_pure`]: Bipartite entanglement for pure states
//!
//! # Entanglement Metrics
//!
//! - [`negativity`]: Based on partial transpose, works for mixed states
//! - [`concurrence`]: Exact 2-qubit entanglement measure
//! - [`entanglement_of_formation`]: Derived from concurrence
//!
//! # Performance Notes
//!
//! | Function | Complexity | Requires EVD |
//! |----------|-----------|--------------|
//! | `linear_entropy` | O(4^N) | No |
//! | `renyi_entropy` | O(8^N) | Yes |
//! | `entanglement_entropy_pure` | O(8^N) | Yes |
//! | `negativity` | O(8^N) | Yes |
//! | `concurrence` | O(8^N) | Yes |
use QisError;
use ;
use ;
use Side;
use *;
/// Calculates the linear entropy of a quantum state.
///
/// The linear entropy is defined as:
/// $$S_L(\rho) = 1 - \text{Tr}(\rho^2) = 1 - \text{Purity}(\rho)$$
///
/// This is a computationally efficient approximation of the Von Neumann entropy,
/// requiring only O(4^N) time without eigendecomposition. It serves as a measure
/// of mixedness: 0 for pure states, approaching $1 - 1/2^N$ for maximally mixed states.
///
/// # Arguments
///
/// * `dm` - The density matrix representing the quantum state.
///
/// # Returns
///
/// The linear entropy as a value in [0, 1), or an error if calculation fails.
///
/// # Examples
///
/// ```
/// use cqlib_core::qis::entropy::linear_entropy;
/// use cqlib_core::qis::state::DensityMatrix;
///
/// // Pure state: linear entropy = 0
/// let dm_pure = DensityMatrix::new(2);
/// let s_l = linear_entropy(&dm_pure).unwrap();
/// assert!(s_l < 1e-10);
/// ```
/// Calculates the Rényi entropy of order α.
///
/// The Rényi entropy is defined as:
/// $$S_\alpha(\rho) = \frac{1}{1-\alpha} \log_2(\text{Tr}(\rho^\alpha)) = \frac{1}{1-\alpha} \log_2\left(\sum_i \lambda_i^\alpha\right)$$
///
/// where $\lambda_i$ are the eigenvalues of the density matrix.
///
/// # Special Cases
///
/// - When α → 1: Approaches Von Neumann entropy (gracefully handled internally)
/// - When α = 2: Collision entropy, important for quantum cryptography
/// - When α → ∞: Min-entropy
///
/// # Arguments
///
/// * `dm` - The density matrix representing the quantum state.
/// * `alpha` - The order parameter. Must be positive and not equal to 1.0
/// (use Von Neumann entropy directly for α = 1).
///
/// # Returns
///
/// The Rényi entropy in bits (base-2 logarithm), or an error if α ≤ 0.
///
/// # Examples
///
/// ```
/// use cqlib_core::qis::entropy::renyi_entropy;
/// use cqlib_core::qis::state::DensityMatrix;
///
/// let dm = DensityMatrix::new(2);
/// // For pure state, all Rényi entropies equal 0
/// let s2 = renyi_entropy(&dm, 2.0).unwrap();
/// assert!(s2 < 1e-10);
/// ```
///
/// # Notes
///
/// For α very close to 1.0 (within `f64::EPSILON`), this function automatically
/// falls back to computing the Von Neumann entropy for numerical stability.
/// Calculates the entanglement entropy for a bipartite pure state.
///
/// For a pure state |ψ⟩ of a composite system AB, the entanglement entropy is
/// defined as the Von Neumann entropy of the reduced density matrix of subsystem A:
/// $$E(|\psi\rangle) = S(\rho_A) = -\text{Tr}(\rho_A \log_2 \rho_A)$$
///
/// where ρ_A = Tr_B(|ψ⟩⟨ψ|) is the reduced density matrix obtained by tracing
/// out subsystem B.
///
/// # Arguments
///
/// * `sv` - The statevector representing the pure quantum state.
/// * `subsys_a` - Indices of qubits belonging to subsystem A. All other qubits
/// are considered part of subsystem B.
///
/// # Returns
///
/// The entanglement entropy in bits, or an error if validation fails.
///
/// # Errors
///
/// Returns `QisError::InvalidSubsystem` if:
/// - `subsys_a` is empty or contains all qubits
/// - There are duplicate indices in `subsys_a`
///
/// Returns `QisError::IndexOutOfBounds` if any index in `subsys_a` is out of bounds.
///
/// # Examples
///
/// ```
/// use cqlib_core::qis::entropy::entanglement_entropy_pure;
/// use cqlib_core::qis::state::Statevector;
///
/// // Create Bell state |Φ+⟩ = (|00⟩ + |11⟩)/√2
/// let mut sv = Statevector::new(2);
/// sv.apply_h(0);
/// sv.apply_cx(0, 1);
///
/// // Entanglement entropy should be 1.0 for maximally entangled Bell state
/// let ee = entanglement_entropy_pure(&sv, &[0]).unwrap();
/// assert!((ee - 1.0).abs() < 1e-10);
/// ```
/// Calculates the negativity entanglement measure.
///
/// The negativity is based on the Peres-Horodecki criterion (PPT criterion). For a
/// bipartite state ρ, perform partial transpose on subsystem A. If the resulting
/// matrix has negative eigenvalues, the state is entangled.
///
/// Formula: $\mathcal{N}(\rho) = \sum_{\lambda_i < 0} |\lambda_i| = \frac{||\rho^{T_A}||_1 - 1}{2}$
///
/// where $||\cdot||_1$ denotes the trace norm (sum of singular values).
///
/// # Arguments
///
/// * `dm` - The density matrix of the bipartite quantum state.
/// * `subsys_a` - The qubit indices comprising subsystem A to be transposed.
///
/// # Returns
///
/// The negativity value (≥ 0), or an error if calculation fails.
/// A value of 0 indicates the state is separable (for 2⊗2 and 2⊗3 systems,
/// this is a necessary and sufficient condition).
///
/// # Examples
///
/// ```
/// use cqlib_core::qis::entropy::negativity;
/// use cqlib_core::qis::state::DensityMatrix;
///
/// // Bell state |Φ+⟩
/// let mut dm = DensityMatrix::new(2);
/// dm.apply_h(0);
/// dm.apply_cx(0, 1);
///
/// // Negativity should be 0.5 for maximally entangled Bell state
/// let neg = negativity(&dm, &[0]).unwrap();
/// assert!((neg - 0.5).abs() < 1e-10);
/// ```
///
/// # Notes
///
/// Unlike logarithmic negativity, this measure is not additive under tensor products.
/// For an additive measure, use `logarithmic_negativity` from the metrics module.
/// Calculates the concurrence for a 2-qubit quantum state.
///
/// The concurrence is an exact entanglement measure specifically for two-qubit systems.
/// It ranges from 0 (separable state) to 1 (maximally entangled state).
///
/// Formula: $C(\rho) = \max(0, \lambda_1 - \lambda_2 - \lambda_3 - \lambda_4)$
///
/// where $\lambda_i$ are the eigenvalues (in descending order) of the matrix
/// $R = \sqrt{\sqrt{\rho} \tilde{\rho} \sqrt{\rho}}$, and $\tilde{\rho}$ is the
/// "spin-flipped" density matrix:
/// $$\tilde{\rho} = (\sigma_y \otimes \sigma_y) \rho^* (\sigma_y \otimes \sigma_y)$$
///
/// # Arguments
///
/// * `dm` - The density matrix of a 2-qubit quantum state.
///
/// # Returns
///
/// The concurrence value in [0, 1], or an error if the state is not 2-qubit.
///
/// # Errors
///
/// Returns `QisError::UnsupportedDimension` if `dm.num_qubits != 2`.
///
/// # Examples
///
/// ```
/// use cqlib_core::qis::entropy::concurrence;
/// use cqlib_core::qis::state::DensityMatrix;
///
/// // Bell state |Φ+⟩: concurrence = 1.0
/// let mut dm = DensityMatrix::new(2);
/// dm.apply_h(0);
/// dm.apply_cx(0, 1);
///
/// let c = concurrence(&dm).unwrap();
/// assert!((c - 1.0).abs() < 1e-10);
///
/// // Separable state: concurrence = 0
/// let dm_sep = DensityMatrix::new(2);
/// let c_sep = concurrence(&dm_sep).unwrap();
/// assert!(c_sep < 1e-10);
/// ```
/// Calculates the entanglement of formation for a 2-qubit state.
///
/// The entanglement of formation quantifies the minimum amount of entanglement
/// required to prepare a given mixed state. For 2-qubit systems, it has a closed-form
/// expression in terms of the concurrence.
///
/// Formula: $E_F(\rho) = H\left(\frac{1 + \sqrt{1 - C^2}}{2}\right)$
///
/// where $H(x) = -x \log_2(x) - (1-x) \log_2(1-x)$ is the binary entropy function,
/// and $C$ is the concurrence.
///
/// # Arguments
///
/// * `dm` - The density matrix of a 2-qubit quantum state.
///
/// # Returns
///
/// The entanglement of formation in bits, or an error if the state is not 2-qubit.
///
/// # Examples
///
/// ```
/// use cqlib_core::qis::entropy::entanglement_of_formation;
/// use cqlib_core::qis::state::DensityMatrix;
///
/// // Bell state |Φ+⟩: EOF = 1.0
/// let mut dm = DensityMatrix::new(2);
/// dm.apply_h(0);
/// dm.apply_cx(0, 1);
///
/// let eof = entanglement_of_formation(&dm).unwrap();
/// assert!((eof - 1.0).abs() < 1e-10);
/// ```
///
/// # Notes
///
/// This measure represents the minimum number of Bell pairs required to asymptotically
/// create the state ρ using LOCC (Local Operations and Classical Communication).
/// Helper function to compute the matrix square root of a positive semi-definite matrix.
///
/// Computes √M = U √D U† where M = U D U† is the eigendecomposition.