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
use std::ops::{Mul, Add};
use nalgebra::{DefaultAllocator, U1, RowVector2, RowVectorN, DimName, U2};
use nalgebra::allocator::Allocator;

use {SQRT_2_INVERSE, Ket, Outer, Complex};

/// Generic Bra. You can multiply it by ket to get an inner product (scalar).
#[derive(Clone, Debug, PartialEq)]
pub struct Bra<D: DimName>(pub(crate) RowVectorN<Complex, D>)
    where DefaultAllocator: Allocator<Complex, U1, D>;

impl<D: DimName> Bra<D>
    where DefaultAllocator: Allocator<Complex, U1, D>
{
    /// 2-dimension "up" bra
    pub fn up() -> Bra<U2> {
        Bra::<U2>(
            RowVector2::new(
                Complex::new(1.0, 0.0),
                Complex::new(0.0, 0.0),
            )
        )
    }

    /// 2-dimension "down" bra
    pub fn down() -> Bra<U2> {
        Bra::<U2>(
            RowVector2::new(
                Complex::new(0.0, 0.0),
                Complex::new(1.0, 0.0),
            )
        )
    }

    /// 2-dimension "right" bra
    pub fn right() -> Bra<U2> {
        Bra::<U2>(
            RowVector2::new(
                Complex::new(SQRT_2_INVERSE, 0.0),
                Complex::new(SQRT_2_INVERSE, 0.0),
            )
        )
    }

    /// 2-dimension "left" bra
    pub fn left() -> Bra<U2> {
        Bra::<U2>(
            RowVector2::new(
                Complex::new(SQRT_2_INVERSE, 0.0),
                Complex::new(-SQRT_2_INVERSE, 0.0),
            )
        )
    }

    /// 2-dimension "inward" bra
    pub fn inw() -> Bra<U2> {
        Bra::<U2>(
            RowVector2::new(
                Complex::new(SQRT_2_INVERSE, 0.0),
                Complex::new(0.0, SQRT_2_INVERSE),
            )
        )
    }

    /// 2-dimension "outward" bra
    pub fn out() -> Bra<U2> {
        Bra::<U2>(
            RowVector2::new(
                Complex::new(SQRT_2_INVERSE, 0.0),
                Complex::new(0.0, -SQRT_2_INVERSE),
            )
        )
    }
}

impl<D: DimName> Mul<Ket<D>> for Bra<D>
    where DefaultAllocator: Allocator<Complex, D> + Allocator<Complex, U1, D>
{
    type Output = Complex;

    fn mul(self, other: Ket<D>) -> Self::Output {
        let mut m = self.0;
        for f in m.iter_mut() { *f = f.conj(); }
        (m * other.0)[0]
    }
}

// Only bra can be multiplied my square matrix
impl<D: DimName> Mul<Outer<D>> for Bra<D>
    where DefaultAllocator: Allocator<Complex, D, D> + Allocator<Complex, U1, D>
{
    type Output = Self;

    fn mul(self, other: Outer<D>) -> Self::Output {
        Bra(self.0 * other.0)
    }
}

impl<D: DimName> Add for Bra<D>
    where DefaultAllocator: Allocator<Complex, U1, D>
{
    type Output = Self;

    fn add(self, other: Bra<D>) -> Self::Output {
        Bra(self.0 + other.0)
    }
}

impl<D: DimName> Mul<Complex> for Bra<D>
    where DefaultAllocator: Allocator<Complex, U1, D>
{
    type Output = Self;

    fn mul(self, other: Complex) -> Self::Output {
        Bra(self.0 * other)
    }
}

impl<D: DimName> From<Ket<D>> for Bra<D>
    where DefaultAllocator: Allocator<Complex, D> + Allocator<Complex, D, D> + Allocator<Complex, U1, D>
{
    fn from(v: Ket<D>) -> Self {
        Bra(v.0.transpose())
    }
}

impl<D: DimName> From<RowVectorN<Complex, D>> for Bra<D>
    where DefaultAllocator: Allocator<Complex, U1, D>
{
    fn from(v: RowVectorN<Complex, D>) -> Self {
        Bra(v)
    }
}

impl<D: DimName> ::std::fmt::Display for Bra<D>
    where DefaultAllocator: Allocator<Complex, U1, D>,
    DefaultAllocator: Allocator<usize, U1, D>
{
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "{}", self.0)
    }
}