2023年2月9日 星期四

Python 量子運算(一八):和差角公式

Python 量子運算(一八):和差角公式

 2023/02/04

-----


Fig. 18.1. Angle sum and difference formulas.

-----

代碼 18.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# Program 18.1:Angle sum and difference formulas
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt


# defined by angle
class PointT1:
    def __init__(self, t, r):
        self.t = t  # theta

        self.r = r  # radius
        self.x = r * np.cos(t)
        self.y = r * np.sin(t)


# defined by points
class PointT2:
    def __init__(self, x, y):
        self.x = x
        self.y = y


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

    # circle
    c = np.linspace(0, np.pi*2, 100)
    r = 1
    c_x = r * np.cos(c)
    c_y = r * np.sin(c)
    ax.plot(c_x, c_y, 'g', linestyle='--')

    # points
    t_A = (31/24) * np.pi                # theta A setting
    PA = PointT1(t_A, r)                 # A
    PC = PointT1(t_A-np.pi, r)           # C, determined by A
    ax.plot([PA.x, PC.x], [PA.y, PC.y], 'purple', linewidth=5.0)  # AC
    ax.text(PA.x-0.1, PA.y-0.1, 'A')
    ax.text(PC.x+0.05, PC.y+0.05, 'C')

    PB = PointT2(PC.x, PA.y)             # B, determined by A, C
    ax.plot([PA.x, PB.x], [PA.y, PA.y], 'r', linestyle='--')  # AB
    ax.plot([PB.x, PB.x], [PB.y, PC.y], 'purple', linestyle='--')  # BC
    ax.text(PB.x+0.05, PB.y-0.1, 'B')

    PG = PointT2(PA.x, PC.y)             # G, determined by A, C
    ax.plot([PA.x, PG.x], [PA.y, PG.y], 'purple', linestyle='--')  # AG
    ax.plot([PC.x, PG.x], [PC.y, PG.y], 'purple', linestyle='--')  # CG
    ax.text(PG.x-0.1, PG.y+0.05, 'G')

    t_E = (47/24) * np.pi                # theta E setting
    PE = PointT1(t_E, r)                 # E
    ax.plot([PA.x, PE.x], [PA.y, PE.y], 'r', linewidth=5.0)  # AE
    ax.plot([PC.x, PE.x], [PC.y, PE.y], 'b', linewidth=5.0)  # CE
    ax.text(PE.x+0.05, PE.y, 'E')

    PD = PointT2(PE.x, PC.y)             # D, determined by C, E
    ax.plot([PC.x, PD.x], [PC.y, PD.y], 'b', linestyle='--')  # CD
    ax.plot([PD.x, PE.x], [PD.y, PE.y], 'b', linestyle='--')  # DE
    ax.text(PD.x+0.05, PD.y+0.05, 'D')

    PF = PointT2(PE.x, PB.y)             # F, determined by B, E
    ax.plot([PB.x, PF.x], [PB.y, PF.y], 'r', linestyle='--')  # BF
    ax.plot([PE.x, PF.x], [PE.y, PF.y], 'r', linestyle='--')  # EF
    ax.text(PF.x+0.05, PF.y-0.1, 'F')

    # angles
    ax.text(PA.x+0.25, PA.y+0.20, r'$\alpha$')
    ax.text(PA.x+0.35, PA.y+0.05, r'$\beta$')
    ax.text(PE.x-0.08, PE.y+0.25, r'$\beta$')
    ax.text(PC.x-0.35, PC.y-0.10, r'$\alpha+\beta$')

    # edges
    ax.text((PA.x+PE.x)/2-0.2, (PA.y+PE.y)/2,
            r'$\cos\alpha$', color='r')
    ax.text((PA.x+PF.x)/2-0.1, (PA.y+PF.y)/2,
            r'$\cos\alpha\cos\beta$', color='r')
    ax.text((PE.x+PF.x)/2, (PE.y+PF.y)/2,
            r'$\cos\alpha\sin\beta$', color='r')

    ax.text((PC.x+PE.x)/2-0.2, (PC.y+PE.y)/2,
            r'$\sin\alpha$', color='b')
    ax.text((PD.x+PE.x)/2, (PD.y+PE.y)/2,
            r'$\sin\alpha\cos\beta$', color='b')
    ax.text((PC.x+PD.x)/2-0.15, (PC.y+PD.y)/2,
            r'$\sin\alpha\sin\beta$', color='b')

    ax.text((PC.x+PG.x)/2-0.2, (PC.y+PG.y)/2,
            r'$\cos(\alpha+\beta)$', color='purple')
    ax.text((PA.x+PG.x)/2-0.4, (PA.y+PG.y)/2,
            r'$\sin(\alpha+\beta)$', color='purple')

    ax.text(0, -1.3, '(a)')
    ax.set_aspect(1)  # height : width
    ax.set_axis_off()

    return


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

    # string setting
    s1 = r'$\overline{AC}=1$'
    s2_1 = r'$\sin(\alpha+\beta)$'
    s2_2 = r'$=\overline{AG}=\overline{DE}+\overline{EF}$'
    s2_3 = r'$=\sin\alpha\cos\beta+\cos\alpha\sin\beta$'
    s3_1 = r'$\cos(\alpha+\beta)$'
    s3_2 = r'$=\overline{CG}=\overline{AF}-\overline{BF}$'
    s3_3 = r'$=\cos\alpha\cos\beta-\sin\alpha\sin\beta$'

    # string output
    ax.text(0.10, 0.95, s1, fontsize=24)

    ax.text(0.20, 0.80, s2_1, color='purple', fontsize=40)
    ax.text(0.10, 0.65, s2_2, fontsize=24)
    ax.text(0.10, 0.50, s2_3, fontsize=40)

    ax.text(0.20, 0.30, s3_1, color='purple', fontsize=40)
    ax.text(0.10, 0.15, s3_2, fontsize=24)
    ax.text(0.10, 0.00, s3_3, fontsize=40)

    ax.text(0.5, -0.09, '(b)')
    ax.set_axis_off()

    return


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

    # circle
    c = np.linspace(0, np.pi*2, 100)
    r = 1
    c_x = r * np.cos(c)
    c_y = r * np.sin(c)
    ax.plot(c_x, c_y, 'g', linestyle='--')

    t_A = (31/24) * np.pi                # theta A setting
    PA = PointT1(t_A, r)                 # A
    PC = PointT1(t_A-np.pi, r)           # C, determined by A
    ax.plot([PA.x, PC.x], [PA.y, PC.y])  # AC
    ax.plot([PA.x, PC.x], [PA.y, PC.y], 'purple', linewidth=5.0)  # AC
    ax.text(PA.x-0.1, PA.y-0.1, 'A')
    ax.text(PC.x+0.05, PC.y+0.05, 'C')

    PB = PointT2(PC.x, PA.y)             # B, determined by A, C
    ax.plot([PA.x, PB.x], [PA.y, PA.y])  # AB
    ax.plot([PB.x, PB.x], [PB.y, PC.y])  # BC
    ax.plot([PA.x, PB.x], [PA.y, PA.y], 'r', linewidth=5.0)  # AB
    ax.plot([PB.x, PB.x], [PB.y, PC.y], 'b', linewidth=5.0)  # BC
    ax.text(PB.x+0.05, PB.y-0.1, 'B')

    t_E = (47/24) * np.pi                # theta E setting
    PE = PointT1(t_E, r)                 # E
    ax.plot([PA.x, PE.x], [PA.y, PE.y], 'purple', linestyle='--')  # AE
    ax.plot([PC.x, PE.x], [PC.y, PE.y], 'purple', linestyle='--')  # CE
    ax.text(PE.x+0.05, PE.y, 'E')

    # finding Point G for learning
    slope_AE = (PE.y - PA.y) / (PE.x - PA.x)

    # finding Point D
    slope_CE = (PE.y - PC.y) / (PE.x - PC.x)  # -(1/slope_AE)
    slope_BD = slope_CE
    x1 = (slope_AE * PA.x - slope_BD * PB.x + PB.y - PA.y)
    x2 = (slope_AE - slope_BD)
    PD_x = x1 / x2
    PD_y = slope_AE * (PD_x - PA.x) + PA.y
    PD = PointT2(PD_x, PD_y)
    ax.plot([PB.x, PD.x], [PB.y, PD.y], 'r', linestyle='--')  # BD
    ax.text(PD.x, PD.y+0.05, 'D')

    # finding Point F
    PF_x = PE.x - PD.x + PB.x
    PF_y = PE.y - PD.y + PB.y
    PF = PointT2(PF_x, PF_y)
    ax.plot([PB.x, PF.x], [PB.y, PF.y], 'b', linestyle='--')  # BF
    ax.plot([PE.x, PF.x], [PE.y, PF.y], 'r', linestyle='--')  # EF
    ax.text(PF.x+0.05, PF.y-0.1, 'F')

    # angles
    ax.text(PA.x+0.20, PA.y+0.20, r'$\alpha-\beta$')
    ax.text(PA.x+0.35, PA.y+0.05, r'$\beta$')
    ax.text(PC.x+0.02, PC.y-0.30, r'$\beta$')

    # edges
    ax.text((PA.x+PB.x)/2, (PA.y+PB.y)/2+0.01,
            r'$\cos\alpha$', color='r')
    ax.text((PA.x+PD.x)/2, (PA.y+PD.y)/2+0.10,
            r'$\cos\alpha\cos\beta$', color='r')
    ax.text((PB.x+PD.x)/2-0.20, (PB.y+PD.y)/2,
            r'$\cos\alpha\sin\beta$', color='r')

    ax.text((PB.x+PC.x)/2-0.21, (PB.y+PC.y)/2,
            r'$\sin\alpha$', color='b')
    ax.text((PB.x+PF.x)/2, (PB.y+PF.y)/2-0.05,
            r'$\sin\alpha\sin\beta$', color='b')

    ax.text((PC.x+PE.x)/2, (PC.y+PE.y)/2,
            r'$\sin(\alpha-\beta)$', color='purple')

    ax.text(0, -1.38, '(c)')
    ax.set_aspect(1)  # height : width
    ax.set_axis_off()

    return


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

    # string setting
    s1 = r'$\overline{AC}=1$'
    s2_1 = r'$\sin(\alpha-\beta)$'
    s2_2 = r'$=\overline{CE}=\overline{CF}-\overline{EF}$'
    s2_3 = r'$=\sin\alpha\cos\beta-\cos\alpha\sin\beta$'
    s3_1 = r'$\cos(\alpha-\beta)$'
    s3_2 = r'$=\overline{AE}=\overline{AD}+\overline{DE}$'
    s3_3 = r'$=\cos\alpha\cos\beta+\sin\alpha\sin\beta$'

    # string output
    ax.text(0.10, 0.95, s1, fontsize=24)

    ax.text(0.20, 0.80, s2_1, color='purple', fontsize=40)
    ax.text(0.10, 0.65, s2_2, fontsize=24)
    ax.text(0.10, 0.50, s2_3, fontsize=40)

    ax.text(0.20, 0.30, s3_1, color='purple', fontsize=40)
    ax.text(0.10, 0.15, s3_2, fontsize=24)
    ax.text(0.10, 0.00, s3_3, fontsize=40)

    ax.text(0.5, -0.09, '(d)')
    ax.set_axis_off()

    return


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

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

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

解說:

-----

Python 量子運算(目錄)

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

-----

沒有留言:

張貼留言

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