2023年2月9日 星期四

Python 量子運算(一五):內積

 Python 量子運算(一五):內積

2023/01/11

-----


Fig. 15.1. Inner product.

-----

代碼 15.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
# Program 15.1:Inner product
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt


class Vec:
    def __init__(self, t1, t2, v_r, a_r):
        self.t1 = t1    # theta 1
        self.t2 = t2    # theta 2

        self.v_r = v_r  # vector radius
        self.v_x = v_r * np.cos(t2)
        self.v_y = v_r * np.sin(t2)

        self.a = np.linspace(t1, t2, 100)  # arc
        self.a_r = a_r                     # arc radius
        self.a_x = a_r * np.cos(self.a)
        self.a_y = a_r * np.sin(self.a)


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

    # coordinates
    ax.plot([-0.1, 1], [0, 0], 'k')
    ax.plot([0, 0], [-0.1, 1], 'k')

    # vector setting
    v1 = Vec(0, np.pi/3, 1, 0.6)
    v2 = Vec(0, np.pi/12, 1, 0.5)
    v3 = Vec(np.pi/12, np.pi/3, 1, 0.4)

    # vector x
    plt.quiver(0, 0, v1.v_x, v1.v_y, scale=1.25, color='r')
    ax.plot([0, v1.v_x], [v1.v_y, v1.v_y], 'r', linestyle=':')
    ax.plot([v1.v_x, v1.v_x], [0, v1.v_y], 'r', linestyle=':')
    ax.text(v1.v_x+0.05, v1.v_y+0.05, r"$\mathbf{x}$", color='r', fontsize=32)
    ax.text(v1.v_x-0.05, -0.1, r"$x1$", color='r', fontsize=32)
    ax.text(-0.15, v1.v_y, r"$x2$", color='r', fontsize=32)

    # vector y
    plt.quiver(0, 0, v2.v_x, v2.v_y, scale=1.2, color='b')
    ax.plot([0, v2.v_x], [v2.v_y, v2.v_y], 'b', linestyle=':')
    ax.plot([v2.v_x, v2.v_x], [0, v2.v_y], 'b', linestyle=':')
    ax.text(v2.v_x+0.05, v2.v_y+0.05, r"$\mathbf{y}$", color='b', fontsize=32)
    ax.text(v2.v_x-0.05, -0.1, r"$y1$", color='b', fontsize=32)
    ax.text(-0.15, v2.v_y, r"$y2$", color='b', fontsize=32)

    # arc
    ax.plot(v1.a_x, v1.a_y, 'r')
    ax.text(0.36, 0.36, r"$\alpha$", color='r', fontsize=32)
    ax.plot(v2.a_x, v2.a_y, 'b')
    ax.text(0.41, 0.03, r"$\beta$", color='b', fontsize=32)
    ax.plot(v3.a_x, v3.a_y, 'k')
    ax.text(0.11, 0.11, r"$\alpha - \beta$", color='k', fontsize=32)

    ax.text(0.5, -0.34, '(a)', fontsize=20)
    ax.set_axis_off()

    return


def Subplot_2():
    ax = plt.subplot(222)

    # string setting
    s1_1 = r'$\mathbf{x}=(x_1,\cdots,x_n),$'
    s1_2 = r'$\mathbf{y}=(y_1,\cdots,y_n).$'

    s2_1 = r'$\mathbf{x} \cdot \mathbf{y}$'
    s2_2 = r'$=x_1y_1+ \cdots +x_ny_n$'
    s2_3 = r'$=\sum_{i=1}^n x_iy_i.$'

    s3_1 = r'$\mathbf{x}^T\mathbf{y}$'
    s3_2 = r'$=\begin{bmatrix}x_1 \cdots x_n\end{bmatrix}$'\
           r'$\begin{bmatrix}y_1\\ \vdots \\y_n\end{bmatrix}$'
    s3_3 = r'$=\sum_{i=1}^n x_iy_i$.'

    # string output
    ax.text(0.1, 0.75, s1_1)
    ax.text(0.1, 0.63, s1_2)

    ax.text(0.1, 0.45, s2_1)
    ax.text(0.3, 0.45, s2_2)
    ax.text(0.3, 0.33, s2_3)

    ax.text(0.1, 0.15, s3_1)
    ax.text(0.3, 0.15, s3_2)
    ax.text(0.3, 0.03, s3_3)

    ax.text(0.5, -0.15, '(b)', fontsize=20)
    ax.set_axis_off()

    return


def Subplot_3():
    ax = plt.subplot(223)

    # string setting
    s1 = r'$\|\mathbf{x}\|=\sqrt{x_1^2+x_2^2},$'
    s2_1 = r'$\langle \mathbf{x}, \mathbf{y} \rangle$'
    s2_2 = r'$=\|\mathbf{x}\| \cos(\alpha - \beta) \|\mathbf{y}\|$'
    s2_3 = r'$=x_1y_1+x_2y_2.$'

    # string output
    ax.text(0.10, 0.75, s1)

    ax.text(0.20, 0.55, s2_1)
    ax.text(0.10, 0.35, s2_2)
    ax.text(0.10, 0.15, s2_3)

    ax.text(0.5, -0.15, '(c)', fontsize=20)
    ax.set_axis_off()

    return


def Subplot_4():
    ax = plt.subplot(224)

    # string setting
    s1_1 = r'$\cos (\alpha - \beta)$'
    s1_2 = r'$=\cos\alpha\cos\beta+\sin\alpha\sin\beta$'
    s1_3 = r'$=\frac{x_1}{\|\mathbf{x}\|}\frac{y_1}{\|\mathbf{y}\|}$'\
           r'$+\frac{x_2}{\|\mathbf{x}\|}\frac{y_2}{\|\mathbf{y}\|}$'
    s1_4 = r'$=\frac{x_1y_1+x_2y_2}{\|\mathbf{x}\|\|\mathbf{y}\|}.$'

    # string output
    ax.text(0.20, 0.75, s1_1)
    ax.text(0.10, 0.55, s1_2)
    ax.text(0.10, 0.35, s1_3)
    ax.text(0.10, 0.15, s1_4)

    ax.text(0.5, -0.15, '(d)', fontsize=20)
    ax.set_axis_off()

    return


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/0015_001.png')
plt.show()

解說:

-----

References


[1] Inner Product -- from Wolfram MathWorld

https://mathworld.wolfram.com/InnerProduct.html


[2] 內積的定義 | 線代啟示錄

https://ccjou.wordpress.com/2010/01/27/%E5%85%A7%E7%A9%8D%E7%9A%84%E5%AE%9A%E7%BE%A9/


[3] python 兩點連線matplotlib_yanni0616的博客-CSDN博客_python兩點之間連線

https://blog.csdn.net/yanni0616/article/details/99696020


[4] Python Classes

https://www.w3schools.com/python/python_classes.asp


# norm

# \|

[5] How to write norm symbol in LaTeX like ||a||?

https://www.physicsread.com/latex-norm-symbol/


# 向量

[6] 「LaTeX」LaTeX 中三種向量表示:粗體1,粗體2,箭頭向量- 嗶哩嗶哩

https://www.bilibili.com/read/cv3599113

-----

Python 量子運算(目錄)

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

-----

沒有留言:

張貼留言

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