Message Boards Message Boards

0
|
1507 Views
|
7 Replies
|
4 Total Likes
View groups...
Share
Share this post:

The error of dot product between two matrices

Posted 7 months ago

See my following example:

In[10]:= Array[a, 4, 0] . Array[PauliMatrix, 4, 0]

Out[10]= {{a[0] + a[3], a[1] - I a[2]}, {a[1] + I a[2], a[0] - a[3]}}

In[13]:= Array[PauliMatrix, 4, 0] . Array[a, 4, 0]

During evaluation of In[13]:= Dot::dotsh: Tensors {{{1,0},{0,1}},{{0,1},{1,0}},{{0,-I},{I,0}},{{1,0},{0,-1}}} and {a[0],a[1],a[2],a[3]} have incompatible shapes.

Out[13]= {{{1, 0}, {0, 1}}, {{0, 1}, {1, 0}}, {{0, -I}, {I, 0}}, {{1, 
    0}, {0, -1}}} . {a[0], a[1], a[2], a[3]}

How can I fix the second method above?

Regards, Zhao

POSTED BY: Hongyi Zhao
7 Replies

Part of my problem with Subscript is that I am very old guard, and I first learned the language the ascii way. People that came to it later often prefer to have something that looks more like familiar mathematical notation. The real drawback that comes to mind right now is that it is more more complicated to write patterns with Subscript. Oh, another: compare the simplicity:

Array[Subscript[a, #1, #2] &, {3, 3}]
Array[a, {3, 3}]
POSTED BY: Gianluca Gorni
Posted 7 months ago

I tend to avoid Subscript in calculations too.

What are the disadvantages of using Subscript for calculations? Is it not suitable to appear as an expression in calculations? Furthermore, even if this is true, why do many people still use it this way?

POSTED BY: Hongyi Zhao

I have thought of another solution, using Dot and Inactivate:

Array[Inactive[PauliMatrix], 4, 0] . 
  Array[Subscript[a, #] &, 4, 0] // Activate

I tend to avoid Subscript in calculations too. If needed for better display, it is easy to restore subscripts with a replacement rule:

prodForCalculation = 
 Inner[Times, Array[PauliMatrix, 4, 0], Array[a, 4, 0], Plus, 1]
forDisplay = a[i_] :> Subscript[a, i];
prodForDisplay = prodForCalculation /. forDisplay

I am not very familiar with Indexed.

POSTED BY: Gianluca Gorni
Posted 7 months ago

Thank you. It works.

Now I changed to use Subscript as follows for nicer forms in the ultimate results:

In[20]:= Inner[Times, Array[PauliMatrix, 4, 0], 
 Array[Subscript[a, #] &, 4, 0], Plus, 1]

Out[20]= {{Subscript[a, 0] + Subscript[a, 3], 
  Subscript[a, 1] - I Subscript[a, 2]}, {Subscript[a, 1] + 
   I Subscript[a, 2], Subscript[a, 0] - Subscript[a, 3]}}

On the other hand, I also noticed the following comment here:

If you want to do anything with the variables, indeed do not use Subscript, it will generally be very confusing and frustrating...

The new function

Indexed

might be useful as well...

So, I would like to know whether I should preferentially choose to use Indexed instead of Subscript?

Regards, Zhao

POSTED BY: Hongyi Zhao

The array Array[PauliMatrix, 4, 0] has three levels, the first of length 4 and the last with length 2. The array Array[a, 4, 0] has length 4. You can multiply them only in one direction, not in the other. To do what you have in mind, you need a generalization of Dot, with the instruction to use only the first level of the Pauli array:

Inner[Times, Array[PauliMatrix, 4, 0], Array[a, 4, 0], Plus, 1]
POSTED BY: Gianluca Gorni
Posted 7 months ago

But, as you can see, the following two methods give the same result:

In[2]:= a[0] PauliMatrix[0] + a[1] PauliMatrix[1] + 
 a[2] PauliMatrix[2] + a[3] PauliMatrix[3]

Out[2]= {{a[0] + a[3], a[1] - I a[2]}, {a[1] + I a[2], a[0] - a[3]}}

In[3]:= PauliMatrix[0] a[0] + PauliMatrix[1] a[1] + 
 PauliMatrix[2]  a[2] + PauliMatrix[3] a[3]

Out[3]= {{a[0] + a[3], a[1] - I a[2]}, {a[1] + I a[2], a[0] - a[3]}}

Therefore, I want to rewrite them into corresponding more concise forms.

Regards, Zhao

POSTED BY: Hongyi Zhao

The Array[PauliMatrix, 4, 0] is not a matrix, it is a list of 4 matrices.

POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract