2023年2月9日 星期四

Python 量子運算(一四):點積

Python 量子運算(一四):點積

2023/01/03

-----


Fig. 14.1. Dot product.

-----

代碼 14.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
# Program 14.1:Dot product
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt


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

    # basic setting
    theta = np.linspace(0, np.pi/2, 100)
    axis = np.linspace(0, 1, 100)
    zero = axis * 0
    radius = 0.7
    x1 = radius * np.cos(theta)
    y1 = radius * np.sin(theta)

    # basic plotting
    ax.plot(axis, zero, 'k')  # y = 0(x_axis)
    ax.plot(zero, axis, 'k')  # x = 0(y_axis)
    ax.plot(x1, y1, 'k')      # cicle

    # vector setting
    coordinates = [0, 0]      # original point
    splt_radian = np.pi / 4   # subplot

    # vector plotting
    v_p1 = splt_radian
    v_p2 = 0
    vector_1 = [radius*np.cos(v_p1), radius*np.sin(v_p1)]  # direction
    vector_2 = [radius*np.cos(v_p2), radius*np.sin(v_p2)]  # direction
    plt.quiver(coordinates[0], coordinates[1], vector_1[0], vector_1[1],
               scale=1.15, color='r')
    plt.quiver(coordinates[0], coordinates[1], vector_2[0], vector_2[1],
               scale=1.1, color='b')
    plt.quiver(coordinates[0], coordinates[1]-0.05, vector_1[0], vector_2[1],
               scale=1.15, color='k')

    # red line
    r_line_y = np.linspace(0, radius*np.sin(1.05*splt_radian), 100)
    r_line_x = r_line_y * 0 + radius * np.cos(1.05*splt_radian)
    ax.plot(r_line_x, r_line_y, 'r', linestyle=':')

    # red arc
    phi = np.linspace(0, splt_radian, 100)
    x2 = 0.2 * np.cos(phi)
    y2 = 0.2 * np.sin(phi)
    ax.plot(x2, y2, 'r')

    ax.text(0.25, 0.1, r"$\theta=45^{\circ}$", fontsize='20')
    ax.text(radius+0.1, 0.05, r"$\vec u=(1,0)$", color='b')
    ax.text(radius*np.cos(v_p1)+0.1, radius*np.sin(v_p1),
            r"$\vec v=(\frac{\sqrt 2}{2},\frac{\sqrt 2}{2})$", color='r')
    ax.text(radius*np.cos(v_p1)+0.1, -0.15,
            r"$\vec w=(\frac{\sqrt 2}{2},0)$", color='k')
    ax.text(0.5, -0.28, '(a)', fontsize=20)
    ax.set_axis_off()

    return


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

    # string setting
    s1 = r'${\rm cos}\ \theta=\frac{\vert \vec w\vert}{\vert \vec v\vert}$'
    s2 = r'${\vert \vec w\vert}={\vert \vec v\vert}{\rm cos}\ \theta$'
    s3 = r'$\vec u \cdot \vec v={\vert \vec u\vert}{\vert \vec w\vert}=$'\
         r'${\vert \vec u\vert}{\vert \vec v\vert}{\rm cos}\ \theta$'

    # string output
    ax.text(0.10, 0.75, s1)
    ax.text(0.10, 0.45, s2)
    ax.text(0.10, 0.15, s3)
    ax.text(0.5, -0.15, '(b)', fontsize=20)
    ax.set_axis_off()

    return


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

    # string setting
    s1_1 = r'$\vec v\cdot \vec u$'
    s1_2 = r'$=\begin{bmatrix}\sqrt2/2\quad \sqrt2/2\end{bmatrix}$'\
           r'$\begin{bmatrix}1\\0\end{bmatrix}$'
    s2 = r'$=\frac{\sqrt 2}{2}*1+\frac{\sqrt 2}{2}*0$'
    s3 = r'$=\frac{\sqrt 2}{2}$'

    # string output
    ax.text(0.10, 0.75, s1_1)
    ax.text(0.30, 0.75, s1_2)
    ax.text(0.30, 0.45, s2)
    ax.text(0.30, 0.15, s3)
    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'$\vec u\cdot \vec v$'
    s1_2 = r'$=\begin{bmatrix}1\quad 0\end{bmatrix}$'\
           r'$\begin{bmatrix}\sqrt2/2\\\sqrt2/2\end{bmatrix}$'
    s2 = r'$=1*\frac{\sqrt 2}{2}+0*\frac{\sqrt 2}{2}$'
    s3 = r'$=\frac{\sqrt 2}{2}$'

    # string output
    ax.text(0.10, 0.75, s1_1)
    ax.text(0.30, 0.75, s1_2)
    ax.text(0.30, 0.45, s2)
    ax.text(0.30, 0.15, s3)
    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/0014_001.png')
plt.show()

解說:

-----

References


[1] Dot Product -- from Wolfram MathWorld

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


[2] Engineering Math | ShareTechnote

https://www.sharetechnote.com/html/Handbook_EngMath_Matrix_InnerProduct.html


[3] Dot Product of Two Vectors – GeoGebra

https://www.geogebra.org/m/e95pnpwk


[4] Dot Product vs Cross Product - Difference & Similarities in Tabular Form - Physics In My View

https://physicsinmyview.com/2020/10/dot-product-vs-cross-product.html

-----

Python 量子運算(目錄)

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

-----

沒有留言:

張貼留言

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