2023年2月14日 星期二

Python 量子運算(二三):外積

Python 量子運算(二三):外積

2023/02/10

-----


Fig. 23.1. Outer product.

-----

「Connection with the Kronecker product

The outer product and Kronecker product are closely related; in fact the same symbol is commonly used to denote both operations.

與克羅內克積的關係

外積與克羅內克積很接近; 事實上,同一個符號通常用於表示這兩種操作。

」[1]。


-----

代碼 23.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
148
149
150
151
152
153
154
155
156
157
158
# Program 23.1:Outer 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{u} \otimes \mathbf{v} = \ $'
        r'$\mathbf{u} \mathbf{v}^{\operatorname{T}} =$'
    )
    s2 = (
        r'$\begin{bmatrix}$'
        r'$u_{1} \mathbf{v}^{\operatorname{T}} \\$'
        r'$\vdots \\$'
        r'$u_{m} \mathbf{v}^{\operatorname{T}}$'
        r'$\end{bmatrix}$'
    )

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

    plt.title('Outer 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(1, 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, r'$\mathbf{v}^{\operatorname{T}}$')

    plt.title('Rank 2 Tensor', color='r')
    ax.text(P2.x, P2.y, P2.z, r'matrix $(\mathbf{u} \otimes \mathbf{v})$')
    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(1, 1, 3)
    Cube(ax, P1, P2)
    Set(ax, 6)

    plt.title('Rank 1 Tensor', color='r')
    ax.text(P2.x, P2.y, P2.z, r'vector $\mathbf{u}$')
    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, 1)
    Cube(ax, P1, P2)

    P3 = Point(5, 0, 0)
    P4 = Point(6, 1, 4)
    Cube(ax, P3, P4)

    Set(ax, 6)

    plt.title("Rank 1 Tensor", color='r')
    ax.text(0, 0, 3, 'Row Vecor(shape 1x4)', fontsize=16)
    ax.text(3, 0, 5, 'Column Vector(shape 4x1)', fontsize=16)
    ax.text(-1, 1, 1, r'vector $\mathbf{v}^{\operatorname{T}}$')
    ax.text(P4.x, P4.y, P4.z, r'vector $\mathbf{v}$')
    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/0023_001.png')
# plt.show()


解說:

-----

References


[1] Outer product - Wikipedia

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


[2] NumPy Illustrated: The Visual Guide to NumPy | by Lev Maximov | Better Programming

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


[3] matplotlib - Python - Plotting colored grid based on values - Stack Overflow

https://stackoverflow.com/questions/43971138/python-plotting-colored-grid-based-on-values

-----

Python 量子運算(目錄)

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

-----

沒有留言:

張貼留言

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