3
$\begingroup$

I would like to write code to realize the Kronecker Product of $ n $ matrices, for instance when $ n=4 $ and the matrices are Pauli matrices

KroneckerProduct[PauliMatrix[1], PauliMatrix[1], PauliMatrix[1], PauliMatrix[1]]

Is there a convenient way to first incorporate the general $ n $ in the code, so that I don't have to write the code each time I change $ n $?

$\endgroup$
0

3 Answers 3

7
$\begingroup$

Sequence[ ] would be the go-to, but it flattens out the list.

Plan B: You can create a new operator that will 'fold' up the list.

Create the operator

kronk = Fold[KroneckerProduct]; 

Get your list of arbitrary length:

n = 4; res = Table[PauliMatrix[1], n];

Use your operator on the list

kronk[res] == KroneckerProduct[PauliMatrix[1], PauliMatrix[1], PauliMatrix[1], PauliMatrix[1]]
 (* True *)
$\endgroup$
1
  • $\begingroup$ @MilkY thank you! $\endgroup$
    – user34104
    Dec 14, 2018 at 0:00
3
$\begingroup$

Use Apply (@@):

kronkPauli[ind_List] := KroneckerProduct @@ PauliMatrix[ind]

kronkPauli[{1, 1, 1, 1}]

Update

Above kronkPauli accepts any combinations of PauliMatrixes (for $ i=0,1,2,3 $). But if you want only a couple of ones with $ i=1 $, the resultant matrix is just an antidiagonal one, with all $ 1 $s located at the antidiagonal positions.

For that special kind of matrixes, it can, at least, be realized by below two methods:

matrix[n_] := SparseArray[{i_, j_} /; i + j == 2^n + 1 -> 1, 2^{n, n}]
matrix2[n_] := IdentityMatrix[2^n] // Reverse
$\endgroup$
3
  • $\begingroup$ Doh...must be a pathological thing with me and Apply[ ], just never use it much. $\endgroup$
    – MikeY
    Dec 14, 2018 at 19:47
  • 2
    $\begingroup$ @MikeY Why it is so, may I ask? $\endgroup$ Dec 16, 2018 at 7:24
  • 1
    $\begingroup$ Just a mental block, I guess. I think of a List as a fundamental data type that gets passed as an argument rather than the head of an expression that can be replaced to magically co-opt it. My loss, but I am working on it! $\endgroup$
    – MikeY
    Dec 16, 2018 at 16:37
1
$\begingroup$

I have been searching for the same problem and I solved it in an easier way to perform the same task.

n = 4;
NUf = PauliMatrix[1];
For[i = 1, i <= n - 1, i++, {
  NUf = KroneckerProduct[NUf, PauliMatrix[1]];
  }]

the matrix form of the result enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.