2023年2月14日 星期二

Python 量子運算(二二):克羅內克積

Python 量子運算(二二):克羅內克積

2023/02/09

-----


Fig. 22.1. Kronecker.

-----

目標是張量積,但進行之前,有必要先瞭解克羅內克積以及外積。


先看一下參考文獻一的結論:

外積是克羅內克積的特例 [2]。

對於向量而言,外積和是張量積等價的 [2]。


「In linear algebra, an outer product is the tensor product of two coordinate vectors, a special case of the Kronecker product of matrices. 

在線性代數中,外積是兩個坐標向量的張量積,是矩陣的克羅內克積的特例。」[2]。

-----

代碼 22.1



  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
144
145
146
147
# Program 22.1:Kronecker product
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt


class Point:
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z


def Line(ax, A, B):
    ax.plot([A.x, B.x], [A.y, B.y], [A.z, B.z], 'b')
    return


def Cube(ax, P1, P2):

    # parallel to x axis
    for z in range(P1.z, P2.z+1):
        S = Point(P1.x, P1.y, z)
        E = Point(P2.x, P1.y, z)
        Line(ax, S, E)
    for y in range(P1.y, P2.y+1):
        S = Point(P1.x, y, P2.z)
        E = Point(P2.x, y, P2.z)
        Line(ax, S, E)

    # parallel to y axis
    for x in range(P1.x, P2.x+1):
        S = Point(x, P1.y, P2.z)
        E = Point(x, P2.y, P2.z)
        Line(ax, S, E)
    for z in range(P1.z, P2.z+1):
        S = Point(P2.x, P1.y, z)
        E = Point(P2.x, P2.y, z)
        Line(ax, S, E)

    # parallel to z axis
    for x in range(P1.x, P2.x+1):
        S = Point(x, P1.y, P1.z)
        E = Point(x, P1.y, P2.z)
        Line(ax, S, E)
    for y in range(P1.y, P2.y+1):
        S = Point(P2.x, y, P1.z)
        E = Point(P2.x, y, P2.z)
        Line(ax, S, E)

    return


def Set(ax, lim):
    ax.set_xlim([0, lim])
    ax.set_ylim([0, lim])
    ax.set_zlim([0, lim])
    ax.set_axis_off()
    return


def Subplot_1():
    ax = plt.subplot(221)

    # string setting
    s1 = r'$\mathbf{A} \otimes \mathbf{B} =$'
    s2 = (
        r'$\begin{bmatrix}$'
        r'$a_{11} \mathbf{B} & \cdots & a_{1n} \mathbf{B} \\$'
        r'$\vdots & \ddots & \vdots \\$'
        r'$a_{m1} \mathbf{B} & \cdots & a_{mn} \mathbf{B}$'
        r'$\end{bmatrix}$'
    )

    # string output
    ax.text(0.1, 0.65, s1)
    ax.text(0.1, 0.35, s2)

    plt.title('Kronecker Product')
    ax.text(0.5, 0.05, '(a)', fontsize=20)
    ax.set_axis_off()

    return


def Subplot_2():
    ax = fig.add_subplot(222, projection='3d')  # 3d subplot

    P1 = Point(0, 0, 0)
    P2 = Point(3, 1, 3)
    Cube(ax, P1, P2)
    Set(ax, 6)

    for i in range(P2.z):
        for j in range(P2.x):
            ax.text(j, 0, i, 'B')

    plt.title('Rank 2 Tensor', color='r')
    ax.text(P2.x, P2.y, P2.z, r'matrix $(\mathbf{A} \otimes \mathbf{B})$')
    ax.text(4, 0, -1, '(b)', fontsize=20)

    return


def Subplot_3():
    ax = fig.add_subplot(223, projection='3d')  # 3d subplot

    P1 = Point(0, 0, 0)
    P2 = Point(3, 1, 3)
    Cube(ax, P1, P2)
    Set(ax, 6)

    plt.title('Rank 2 Tensor', color='r')
    ax.text(P2.x, P2.y, P2.z, 'matrix A')
    ax.text(4, 0, -1, '(c)', fontsize=20)

    return


def Subplot_4():
    ax = fig.add_subplot(224, projection='3d')  # 3d subplot

    P1 = Point(0, 0, 0)
    P2 = Point(4, 1, 3)
    Cube(ax, P1, P2)
    Set(ax, 6)

    plt.title('Rank 2 Tensor', color='r')
    ax.text(P2.x, P2.y, P2.z, 'matrix B')
    ax.text(4, 0, -1, '(d)', fontsize=20)

    return


# figure setting
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = r'\usepackage{{amsmath}}'
mpl.rcParams['font.size'] = 40
fig = plt.figure(figsize=(16, 16))

Subplot_1()
Subplot_2()
Subplot_3()
Subplot_4()

# plt.savefig('/content/drive/My Drive/pqc/0022_001.png')
plt.show()

解說:

-----

References


[1] Kronecker product - Wikipedia

https://en.wikipedia.org/wiki/Kronecker_product


[2] 淺談張量分解(四):外積、Kronecker積和張量積- 知乎

https://zhuanlan.zhihu.com/p/26774182


[3] 8 ways to use the Kronecker product - The DO Loop

https://blogs.sas.com/content/iml/2020/07/27/8-ways-kronecker-product.html


[4] numpy.kron — NumPy v1.24 Manual

https://numpy.org/doc/stable/reference/generated/numpy.kron.html


# 矩陣

[5] 使用說明:數學公式 - 維基教科書,自由的教學讀本

https://zh.wikibooks.org/zh-tw/Help:%E6%95%B0%E5%AD%A6%E5%85%AC%E5%BC%8F


# 字串太長

[6] Python 慣用語 - 8 太長怎麼辦 « Python Life

http://seanlin.logdown.com/posts/210861-python-idioms-8-too-long

-----

Python 量子運算(目錄)

https://mandhistory.blogspot.com/2022/01/quantum-computing.html

-----

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。