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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
pub mod density;
pub mod vector;
pub use density::StateDensityMatrix;
pub use vector::StateVector;
use crate::core::errors::StateError;
use crate::{Gate, Measurement, MeasurementResult, QuantumChannel};
pub trait Validatable {
fn is_valid(&self) -> Result<(), StateError>;
}
pub trait GateApplicable {
fn apply(&mut self, gate: &Gate, target_qubits: &[usize]) -> Result<(), StateError>;
fn apply_controlled(
&mut self,
gate: &Gate,
target_qubits: &[usize],
control_qubits: &[usize],
) -> Result<(), StateError>;
}
pub trait Measurable {
fn set_measurement(
&self,
measurement: &Measurement,
target_qubits: &[usize],
) -> Result<Vec<f64>, StateError>;
fn measure(
&mut self,
measurement: &Measurement,
target_qubits: &[usize],
) -> Result<MeasurementResult, StateError>;
}
pub trait PurityComputable {
fn purity(&self) -> f64;
}
pub trait StateClone {
fn clone_box(&self) -> Box<dyn QuantumStateImpl>;
}
impl<T> StateClone for T
where
T: 'static + QuantumStateImpl + Clone,
{
fn clone_box(&self) -> Box<dyn QuantumStateImpl> {
Box::new(self.clone())
}
}
pub trait QuantumStateImpl:
Validatable
+ GateApplicable
+ Measurable
+ PurityComputable
+ StateClone
+ std::fmt::Debug
+ Send
+ Sync
{
fn as_any(&self) -> &dyn std::any::Any;
fn as_density_matrix(&self) -> Result<StateDensityMatrix, StateError>;
fn try_apply_channel(
&mut self,
channel: &QuantumChannel,
target_qubits: &[usize],
) -> Result<bool, StateError>;
}
/// Represents a general quantum state that can be pure (vector) or mixed (density matrix).
#[derive(Debug)]
pub struct QuantumState {
pub state: Box<dyn QuantumStateImpl>,
}
impl Clone for QuantumState {
fn clone(&self) -> Self {
Self {
state: self.state.clone_box(),
}
}
}
impl QuantumState {
/// Creates a new quantum state initialized to the ground state |0...0> as a `StateVector`.
///
/// # Arguments
///
/// * `num_qubits` - The number of qubits in the quantum system.
///
/// # Returns
///
/// A `QuantumState` instance wrapping a `StateVector`.
///
/// # Example
/// ```rust
/// use qcrypto::state::QuantumState;
///
/// let state = QuantumState::new(2); // |00>
/// ```
pub fn new(num_qubits: usize) -> Self {
QuantumState {
state: Box::new(StateVector::new(num_qubits)),
}
}
/// Verifies that the internal state representation is mathematically valid.
///
/// # Returns
///
/// A `Result` indicating success if valid, or a `StateError`.
///
/// # Errors
///
/// Returns a `StateError` if dimensions are mismatched or if the probabilities do not normalize properly.
///
/// # Example
/// ```rust
/// use qcrypto::state::QuantumState;
///
/// let state = QuantumState::new(1);
/// assert!(state.is_valid().is_ok());
/// ```
pub fn is_valid(&self) -> Result<(), StateError> {
self.state.is_valid()
}
/// Applies a local quantum gate to the specified target qubits.
///
/// # Arguments
///
/// * `gate` - The quantum gate to apply.
/// * `target_qubits` - The indices of the qubits the gate acts upon.
///
/// # Returns
///
/// A `Result` containing a mutable reference to the state.
///
/// # Errors
///
/// Returns a `StateError` if target indices are invalid or dimensions mismatch.
///
/// # Example
/// ```rust
/// use qcrypto::{state::QuantumState, Gate};
///
/// let mut state = QuantumState::new(1);
/// state.apply(&Gate::x(), &[0]).unwrap();
/// ```
pub fn apply(&mut self, gate: &Gate, target_qubits: &[usize]) -> Result<&mut Self, StateError> {
self.state.apply(gate, target_qubits)?;
Ok(self)
}
/// Applies a controlled quantum gate to the specified target qubits.
///
/// # Arguments
///
/// * `gate` - The quantum gate to apply.
/// * `target_qubits` - The indices of the target qubits.
/// * `control_qubits` - Optional slice containing the indices of the control qubits.
///
/// # Returns
///
/// A `Result` containing a mutable reference to the state.
///
/// # Errors
///
/// Returns a `StateError` if target/control indices are invalid or dimensions mismatch.
///
/// # Example
/// ```rust
/// use qcrypto::{state::QuantumState, Gate};
///
/// let mut state = QuantumState::new(2);
/// state.apply(&Gate::h(), &[0]).unwrap();
/// state.apply_controlled(&Gate::x(), &[1], &[0]).unwrap();
/// ```
pub fn apply_controlled(
&mut self,
gate: &Gate,
target_qubits: &[usize],
control_qubits: &[usize],
) -> Result<&mut Self, StateError> {
self.state
.apply_controlled(gate, target_qubits, control_qubits)?;
Ok(self)
}
/// Calculates measurement outcome probabilities without collapsing the state.
///
/// # Arguments
///
/// * `measurement` - The `Measurement` operator to simulate.
/// * `target_qubits` - The indices of the qubits to apply the measurement on.
///
/// # Returns
///
/// A `Result` containing a vector of probabilities corresponding to each possible measurement outcome.
///
/// # Errors
///
/// Returns a `StateError` if the indices are invalid or if there are duplicate targets.
///
/// # Example
/// ```rust
/// use qcrypto::{state::QuantumState, Measurement};
///
/// let state = QuantumState::new(1);
/// let probs = state.set_measurement(&Measurement::z_basis(), &[0]).unwrap();
/// assert_eq!(probs, vec![1.0, 0.0]);
/// ```
pub fn set_measurement(
&self,
measurement: &Measurement,
target_qubits: &[usize],
) -> Result<Vec<f64>, StateError> {
self.state.set_measurement(measurement, target_qubits)
}
/// Performs a physical measurement, collapsing the quantum state according to the outcome.
///
/// # Arguments
///
/// * `measurement` - The `Measurement` to perform.
/// * `target_qubits` - The indices of the qubits being measured.
///
/// # Returns
///
/// A `Result` containing a `MeasurementResult` with the index of the triggered operator and its associated value.
///
/// # Errors
///
/// Returns a `StateError` if dimensions or indices are invalid, or if the resulting trace is zero.
///
/// # Example
/// ```rust
/// use qcrypto::{state::QuantumState, Measurement};
///
/// let mut state = QuantumState::new(1);
/// let result = state.measure(&Measurement::z_basis(), &[0]).unwrap();
/// assert_eq!(result.value, 0.0);
/// ```
pub fn measure(
&mut self,
measurement: &Measurement,
target_qubits: &[usize],
) -> Result<MeasurementResult, StateError> {
self.state.measure(measurement, target_qubits)
}
/// Applies a quantum channel (noise model) to the specified qubits.
///
/// Note: If the state is currently a `StateVector`, this operation will automatically
/// convert it into a `StateDensityMatrix` to support mixed states.
///
/// # Arguments
///
/// * `channel` - The quantum channel to apply.
/// * `target_qubits` - The indices of the qubits affected by the channel.
///
/// # Returns
///
/// A `Result` indicating success or failure.
///
/// # Errors
///
/// Returns a `StateError` if dimensions or indices are invalid.
///
/// # Example
/// ```rust
/// use qcrypto::{QuantumState, QuantumChannel};
///
/// let mut state = QuantumState::new(1);
/// let channel = QuantumChannel::bit_flip(1.0);
/// state.apply_channel(&channel, &[0]).unwrap();
/// ```
pub fn apply_channel(
&mut self,
channel: &QuantumChannel,
target_qubits: &[usize],
) -> Result<(), StateError> {
if !self.state.try_apply_channel(channel, target_qubits)? {
let mut dm = self.state.as_density_matrix()?;
dm.apply_channel(channel, target_qubits)?;
self.state = Box::new(dm);
}
Ok(())
}
/// Composes the current quantum state with an ancilla state via the tensor product.
///
/// If both states are `StateVector`, the resulting state is also `StateVector`.
/// Otherwise, the resulting state is returned as a `StateDensityMatrix`.
///
/// # Arguments
///
/// * `ancilla_state` - The quantum state to append to the system.
///
/// # Returns
///
/// A `Result` containing the newly combined `QuantumState`.
///
/// # Errors
///
/// Returns a `StateError` if tensor operations fail.
///
/// # Example
/// ```rust
/// use qcrypto::{QuantumState, Gate};
/// # use num_complex::Complex64;
///
/// let state1 = QuantumState::new(1); // |0>
/// let mut state2 = QuantumState::new(1); // |0>
/// state2.apply(&Gate::x(), &[0]); // |1>
/// let combined = state1.compose(&state2).unwrap(); // |01>
///
/// // We must downcast the internal Box<dyn QuantumStateImpl> to access the raw StateVector
/// let vector_state = combined.state
/// .as_any()
/// .downcast_ref::<qcrypto::state::StateVector>()
/// .unwrap();
///
/// for (i, &val) in vector_state.amplitudes.indexed_iter() {
/// let expected = if i == 1 { 1.0 } else { 0.0 };
/// assert_eq!(val, Complex64::new(expected, 0.0));
/// }
/// ```
pub fn compose(&self, ancilla_state: &QuantumState) -> Result<QuantumState, StateError> {
if let (Some(v1), Some(v2)) = (
self.state.as_any().downcast_ref::<StateVector>(),
ancilla_state.state.as_any().downcast_ref::<StateVector>(),
) {
Ok(QuantumState {
state: Box::new(v1.compose(v2)?),
})
} else {
let dm1 = self.state.as_density_matrix()?;
let dm2 = ancilla_state.state.as_density_matrix()?;
Ok(QuantumState {
state: Box::new(dm1.compose(&dm2)?),
})
}
}
/// Calculates the purity of the quantum state $Tr(\rho^2)$.
///
/// Returns 1.0 for completely pure states, and $< 1.0$ for mixed states.
///
/// # Returns
///
/// The computed purity as an `f64`.
///
/// # Example
/// ```rust
/// use qcrypto::{QuantumState, QuantumChannel};
///
/// let mut state = QuantumState::new(1);
/// assert_eq!(state.purity(), 1.0);
/// state.apply_channel(&QuantumChannel::depolarizing(0.5), &[0]).unwrap();
/// assert!(state.purity() < 1.0);
/// ```
pub fn purity(&self) -> f64 {
self.state.purity()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::errors::MeasurementError;
use num_complex::Complex64;
#[test]
fn test_apply_out_of_bounds() {
let mut state = QuantumState::new(1);
let result = state.apply(&Gate::x(), &[1]);
assert!(matches!(result, Err(StateError::IndexOutOfBounds { .. })));
}
#[test]
fn test_apply_dimension_mismatch() {
let mut state = QuantumState::new(2);
let result = state.apply(&Gate::cnot(), &[0]);
assert!(matches!(result, Err(StateError::DimensionMismatch { .. })));
}
#[test]
fn test_measurement_duplicate_qubits() {
let state = QuantumState::new(2);
let result = state.set_measurement(&Measurement::bell_basis(), &[0, 0]);
assert!(matches!(
result,
Err(StateError::MeasurementError(
MeasurementError::DuplicateQubit(0)
))
));
}
#[test]
fn test_apply_identity_sequence() {
let mut state = QuantumState::new(1);
state
.apply(&Gate::x(), &[0])
.unwrap()
.apply(&Gate::h(), &[0])
.unwrap()
.apply(&Gate::z(), &[0])
.unwrap()
.apply(&Gate::h(), &[0])
.unwrap();
let vector = state.state.as_any().downcast_ref::<StateVector>().unwrap();
assert!((vector.amplitudes[0].re - 1.0).abs() < 1e-12);
assert!((vector.amplitudes[1].re).abs() < 1e-12);
}
#[test]
fn test_apply_controlled_bell_state() {
let mut state = QuantumState::new(2); // |00>
state
.apply(&Gate::h(), &[0])
.unwrap()
.apply_controlled(&Gate::x(), &[1], &[0])
.unwrap();
let vector = state.state.as_any().downcast_ref::<StateVector>().unwrap();
let expected_val = 1.0 / std::f64::consts::SQRT_2;
for (i, &val) in vector.amplitudes.indexed_iter() {
let expected = if i == 0 || i == 3 { expected_val } else { 0.0 };
assert!((val - Complex64::new(expected, 0.0)).norm() < 1e-12);
}
}
#[test]
fn test_apply_cnot_bell_state() {
// Same Bell state but using Gate::cnot() through apply()
let mut state = QuantumState::new(2);
state
.apply(&Gate::h(), &[0])
.unwrap()
.apply(&Gate::cnot(), &[0, 1])
.unwrap();
let vector = state.state.as_any().downcast_ref::<StateVector>().unwrap();
let s = 1.0 / std::f64::consts::SQRT_2;
// Φ+ = (|00⟩+|11⟩)/√2
assert!((vector.amplitudes[0] - Complex64::new(s, 0.0)).norm() < 1e-12);
assert!(vector.amplitudes[1].norm() < 1e-12);
assert!(vector.amplitudes[2].norm() < 1e-12);
assert!((vector.amplitudes[3] - Complex64::new(s, 0.0)).norm() < 1e-12);
}
#[test]
fn test_apply_cnot_equivalence() {
// apply(&cnot, &[0,1]) must produce the same result as apply_controlled(&x, &[1], &[0])
let mut state_a = QuantumState::new(2);
state_a
.apply(&Gate::h(), &[0])
.unwrap()
.apply(&Gate::cnot(), &[0, 1])
.unwrap();
let mut state_b = QuantumState::new(2);
state_b
.apply(&Gate::h(), &[0])
.unwrap()
.apply_controlled(&Gate::x(), &[1], &[0])
.unwrap();
let va = state_a
.state
.as_any()
.downcast_ref::<StateVector>()
.unwrap();
let vb = state_b
.state
.as_any()
.downcast_ref::<StateVector>()
.unwrap();
for (a, b) in va.amplitudes.iter().zip(vb.amplitudes.iter()) {
assert!((a - b).norm() < 1e-12, "cnot via apply != apply_controlled");
}
}
#[test]
fn test_apply_cnot_control_does_not_fire() {
// |01⟩: qubit 0 = 0, so CNOT(control=0) should NOT flip qubit 1.
let mut state = QuantumState::new(2);
state.apply(&Gate::x(), &[1]).unwrap(); // |01⟩
state.apply(&Gate::cnot(), &[0, 1]).unwrap();
let vector = state.state.as_any().downcast_ref::<StateVector>().unwrap();
assert!(
(vector.amplitudes[1] - Complex64::new(1.0, 0.0)).norm() < 1e-12,
"Must stay |01⟩"
);
}
#[test]
fn test_apply_cnot_reversed_targets() {
// targets=[1,0] → gate qubit 0 (control)=state qubit 1, gate qubit 1 (target)=state qubit 0
// |01⟩: qubit 1 = 1 → control fires → flip qubit 0 → |11⟩
let mut state = QuantumState::new(2);
state.apply(&Gate::x(), &[1]).unwrap(); // |01⟩
state.apply(&Gate::cnot(), &[1, 0]).unwrap();
let vector = state.state.as_any().downcast_ref::<StateVector>().unwrap();
assert!(
(vector.amplitudes[3] - Complex64::new(1.0, 0.0)).norm() < 1e-12,
"Expected |11⟩"
);
}
#[test]
fn test_apply_swap() {
// |10⟩ with SWAP → |01⟩
let mut state = QuantumState::new(2);
state.apply(&Gate::x(), &[0]).unwrap(); // |10⟩
state.apply(&Gate::swap(), &[0, 1]).unwrap();
let vector = state.state.as_any().downcast_ref::<StateVector>().unwrap();
assert!(
(vector.amplitudes[1] - Complex64::new(1.0, 0.0)).norm() < 1e-12,
"Expected |01⟩"
);
assert!(vector.amplitudes[2].norm() < 1e-12, "|10⟩ should be empty");
}
#[test]
fn test_measure_collapse() {
let mut state = QuantumState::new(1);
let _result = state
.apply(&Gate::h(), &[0])
.unwrap()
.measure(&Measurement::z_basis(), &[0])
.unwrap();
assert!((state.purity() - 1.0).abs() < 1e-12);
let vector = state.state.as_any().downcast_ref::<StateVector>().unwrap();
// Norm should be 1.0
let norm_sqr: f64 = vector.amplitudes.iter().map(|c| c.norm_sqr()).sum();
assert!((norm_sqr - 1.0).abs() < 1e-12);
let mut hit_0 = false;
let mut hit_1 = false;
for _ in 0..20 {
let mut state = QuantumState::new(1);
state.apply(&Gate::h(), &[0]).unwrap();
let result = state.measure(&Measurement::z_basis(), &[0]).unwrap();
let vector = state.state.as_any().downcast_ref::<StateVector>().unwrap();
if result.value == 0.0 {
assert!((vector.amplitudes[0].re - 1.0).abs() < 1e-12);
assert!((vector.amplitudes[1].re).abs() < 1e-12);
hit_0 = true;
} else {
assert!((vector.amplitudes[1].re - 1.0).abs() < 1e-12);
assert!((vector.amplitudes[0].re).abs() < 1e-12);
hit_1 = true;
}
if hit_0 && hit_1 {
break;
}
}
assert!(hit_0 && hit_1, "Both branches should be hit eventually");
}
#[test]
fn test_apply_channel_conversion() {
let mut state = QuantumState::new(1); // Starts as StateVector |0>
// Assert it is currently a vector
assert!(state.state.as_any().downcast_ref::<StateVector>().is_some());
state.apply(&Gate::h(), &[0]).unwrap(); // |+>
let channel = QuantumChannel::bit_flip(1.0);
state.apply_channel(&channel, &[0]).unwrap(); // Applies noise, should convert to density matrix
// Assert it is NOW a density matrix
assert!(
state
.state
.as_any()
.downcast_ref::<StateDensityMatrix>()
.is_some()
);
let dm = state
.state
.as_any()
.downcast_ref::<StateDensityMatrix>()
.unwrap();
// Since X|+> = |+>, density matrix should be exactly |+><+|
for &val in dm.density_matrix.iter() {
assert!((val - Complex64::new(0.5, 0.0)).norm() < 1e-12);
}
}
#[test]
fn test_apply_channel_on_density_matrix() {
let mut state = QuantumState::new(1);
let channel = QuantumChannel::bit_flip(1.0);
// First conversion
state.apply_channel(&channel, &[0]).unwrap();
// Second apply directly on density matrix
state.apply_channel(&channel, &[0]).unwrap();
let dm = state
.state
.as_any()
.downcast_ref::<StateDensityMatrix>()
.unwrap();
assert!((dm.density_matrix[[0, 0]].re - 1.0).abs() < 1e-12);
}
#[test]
fn test_quantum_state_clone() {
let state = QuantumState::new(1);
let cloned = state.clone();
assert!(cloned.is_valid().is_ok());
}
#[test]
fn test_quantum_state_is_valid() {
let state = QuantumState::new(2);
assert!(state.is_valid().is_ok());
}
#[test]
fn test_quantum_state_compose_vector() {
let state1 = QuantumState::new(1);
let mut state2 = QuantumState::new(1);
state2.apply(&Gate::x(), &[0]).unwrap();
let combined = state1.compose(&state2).unwrap();
let v = combined
.state
.as_any()
.downcast_ref::<StateVector>()
.unwrap();
assert!((v.amplitudes[1].re - 1.0).abs() < 1e-12);
}
#[test]
fn test_quantum_state_compose_density_matrix() {
let mut state1 = QuantumState::new(1);
state1
.apply_channel(&QuantumChannel::bit_flip(1.0), &[0])
.unwrap(); // Converts to DM
let mut state2 = QuantumState::new(1);
state2
.apply_channel(&QuantumChannel::bit_flip(1.0), &[0])
.unwrap(); // Converts to DM
let combined = state1.compose(&state2).unwrap();
let dm = combined
.state
.as_any()
.downcast_ref::<StateDensityMatrix>()
.unwrap();
assert!((dm.density_matrix[[3, 3]].re - 1.0).abs() < 1e-12);
}
#[test]
fn test_entanglement_correlation() {
// Create Bell state |\Phi+> = (|00> + |11>) / √2
let mut state = QuantumState::new(2);
state
.apply(&Gate::h(), &[0])
.unwrap()
.apply_controlled(&Gate::x(), &[1], &[0])
.unwrap();
// Measure qubit 0 in Z basis
let result_q0 = state.measure(&Measurement::z_basis(), &[0]).unwrap();
// Now measure qubit 1 — it MUST give the same outcome (entanglement correlation)
let result_q1 = state.measure(&Measurement::z_basis(), &[1]).unwrap();
assert_eq!(
result_q0.value, result_q1.value,
"Entangled qubits must collapse to the same value in Bell state Φ+"
);
}
}