deep_causality_physics/quantum/
gates.rs

1/*
2 * SPDX-License-Identifier: MIT
3 * Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
4 */
5
6use deep_causality_multivector::{CausalMultiVector, MultiVector};
7use deep_causality_num::Complex;
8
9/// Standard Quantum Gates interface.
10pub trait QuantumGates {
11    fn gate_identity() -> Self;
12    fn gate_x() -> Self;
13    fn gate_y() -> Self;
14    fn gate_z() -> Self;
15    fn gate_hadamard() -> Self;
16    /// Controlled-NOT gate.
17    /// Note: Implementation for single MultiVector implies 2-qubit representation support
18    /// or specific tensor structure.
19    fn gate_cnot() -> Self;
20}
21
22/// Core Quantum State Operations (Dirac Notation).
23pub trait QuantumOps {
24    /// Hermitian Conjugate (Adjoint) $A^\dagger$.
25    fn dag(&self) -> Self;
26
27    /// Inner Product (Dirac Bracket): $\langle \phi | \psi \rangle$.
28    fn bracket(&self, other: &Self) -> Complex<f64>;
29
30    /// Expectation Value: $\langle \psi | A | \psi \rangle$.
31    fn expectation_value(&self, operator: &Self) -> Complex<f64>;
32
33    /// Normalize the state vector: $|\psi\rangle / \sqrt{\langle\psi|\psi\rangle}$.
34    fn normalize(&self) -> Self;
35}
36
37impl QuantumOps for CausalMultiVector<Complex<f64>> {
38    fn dag(&self) -> Self {
39        // Hermitian conjugate: reverse basis (reversion) and conjugate coefficients
40        let reverted = self.reversion();
41        let conjugated_data = reverted
42            .data()
43            .iter()
44            .map(|c| Complex::new(c.re, -c.im))
45            .collect::<Vec<_>>();
46        // Construct with the same metric; do not silently drop conjugation
47        // If this ever fails, return a zero-initialized vector with the same metric to avoid wrong math.
48        CausalMultiVector::new(conjugated_data, reverted.metric()).unwrap_or_else(|_| {
49            CausalMultiVector::new(
50                vec![Complex::new(0.0, 0.0); reverted.data().len()],
51                reverted.metric(),
52            )
53            .expect("consistent metric")
54        })
55    }
56
57    fn bracket(&self, other: &Self) -> Complex<f64> {
58        let prod = self.dag().geometric_product(other);
59        prod.get(0).cloned().unwrap_or(Complex::new(0.0, 0.0))
60    }
61
62    fn expectation_value(&self, operator: &Self) -> Complex<f64> {
63        let bra_psi = self.dag();
64        let a_psi = operator.geometric_product(self);
65        let prod = bra_psi.geometric_product(&a_psi);
66        prod.get(0).cloned().unwrap_or(Complex::new(0.0, 0.0))
67    }
68
69    fn normalize(&self) -> Self {
70        use deep_causality_multivector::MultiVectorL2Norm;
71        self.normalize_l2()
72    }
73}