# Inconsitency in matrix multiplication

**URL:** <https://discourse.julialang.org/t/inconsitency-in-matrix-multiplication/66193>\
**Category:** Quantum\
**Tags:** question\
**Created:** [August 11, 2021, 10:22am UTC](https://discourse.julialang.org/t/inconsitency-in-matrix-multiplication/66193 "2021-08-11T10:22:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joseph\_S\_Rebeirro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joseph_s_rebeirro/32/28067_2.png) [@Joseph\_S\_Rebeirro](https://discourse.julialang.org/u/Joseph_S_Rebeirro)\
**Post date:** [August 11, 2021, 10:22am UTC](https://discourse.julialang.org/t/inconsitency-in-matrix-multiplication/66193/1 "2021-08-11T10:22:15Z")

</div>

Hi All,

I was playing with the Quantum optics package and I saw something weird  
So the resulting matrix is inconsistent in comparison with other CAS systems (Mathematica)  
**Mathematica:**

```julia
 # Spin operators 

Sx = 1/Sqrt[2] { { 0, 1, 0} , { 1, 0, 1}, {0, 1, 0 } };
Sy = I/Sqrt[2]{{0, -1, 0}, {1, 0, -1}, {0, 1, 0}};
Sz = { {1, 0, 0 }, {0, 0, 0}, {0, 0, -1}};
eye = IdentityMatrix[3];

(* Electron triplet operators *)

eSx = KroneckerProduct[Sx, eye];
eSy = KroneckerProduct[Sy, eye];
eSz = KroneckerProduct[Sz, eye];

 (* Nuclear operators *)
 
Ix = KroneckerProduct[eye, Sx];
Iy = KroneckerProduct[eye, Sy];
Iz = KroneckerProduct[eye, Sz];
 
(* Total angular momentum *)
 Jx = eSx + Ix;
Jy = eSy + Iy;
Jz = eSz + Iz;

(*J^2*)
Jx*Jx+Jy*Jy+Jz*Jz

(*gives me *)
(4	0	0	0	0	0	0	0	0
0	1	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0
0	0	0	1	0	0	0	0	0
0	0	0	0	0	0	0	0	0
0	0	0	0	0	1	0	0	0
0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	1	0
0	0	0	0	0	0	0	0	4
)

```

**Julia:**

```julia
using TensorOperations
using LinearAlgebra

Sx=1/√2 * [[0 1 0]
            [1 0 1] 
            [0 1 0]]

Sy=1/√2 * [[0 -1im 0]
            [1im 0 -1im] 
            [0 1im 0]]

Sz= [[1 0 0]
        [0 0 0] 
        [0 0 -1]]

S=[Sx,Sy,Sz]
eye=[Matrix(1*I,3,3) ,Matrix(1*I,3,3) ,Matrix(1*I,3,3)]
eS=kron.(S,eye)
IS=kron.(eye,S)
JS=eS+IS
real.(JS'*JS)

# This gives 

9×9 Matrix{Float64}:
 6.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 4.0 0.0 2.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 2.0 0.0 2.0 0.0 0.0 0.0 0.0
 0.0 2.0 0.0 4.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 2.0 0.0 4.0 0.0 2.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 4.0 0.0 2.0 0.0
 0.0 0.0 0.0 0.0 2.0 0.0 2.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 2.0 0.0 4.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 6.0

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 11, 2021, 10:39am UTC](https://discourse.julialang.org/t/inconsitency-in-matrix-multiplication/66193/2 "2021-08-11T10:39:24Z")

</div>

where is Julia code that results different stuff?

---

<div class="post-metadata">

**Author:** ![Joseph\_S\_Rebeirro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joseph_s_rebeirro/32/28067_2.png) [@Joseph\_S\_Rebeirro](https://discourse.julialang.org/u/Joseph_S_Rebeirro)\
**Post date:** [August 11, 2021, 10:51am UTC](https://discourse.julialang.org/t/inconsitency-in-matrix-multiplication/66193/3 "2021-08-11T10:51:14Z")

</div>

sorry was editing it noticed it later my bad

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [August 11, 2021, 11:47am UTC](https://discourse.julialang.org/t/inconsitency-in-matrix-multiplication/66193/4 "2021-08-11T11:47:46Z")

</div>

This:

> [@Joseph\_S\_Rebeirro](#):
>
> `JS'*JS`

is not equivalent to the Mathematica code. In the Julia code, `JS'*JS` is equal to

```julia
JS[1]'*JS[1] + JS[2]'*JS[2] + JS[3]'*JS[3]

```

where `'` denotes the adjoint and `*` is matrix multiplication. I don’t know much about Mathematica, but I assume that `Jx*Jx+Jy*Jy+Jz*Jz` in Mathematica uses elementwise multiplication and no adjoint.

If you use elementwise multiplication and no adjoint in Julia, you get the same result as in Mathematica:

```julia
julia> real(JS[1] .* JS[1] + JS[2].*JS[2] + JS[3].*JS[3])
9×9 Matrix{Float64}:
 4.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 4.0

```

---

<div class="post-metadata">

**Author:** ![Joseph\_S\_Rebeirro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joseph_s_rebeirro/32/28067_2.png) [@Joseph\_S\_Rebeirro](https://discourse.julialang.org/u/Joseph_S_Rebeirro)\
**Post date:** [August 11, 2021, 12:40pm UTC](https://discourse.julialang.org/t/inconsitency-in-matrix-multiplication/66193/5 "2021-08-11T12:40:27Z")

</div>

Thank you for the clarification.  
my bad …
