2023年2月23日 星期四

Python 量子運算(二八):投影算子

Python 量子運算(二八):投影算子

2023/02/23

-----


Fig. 28.1. Projection operator.

-----

代碼 28.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
# Program 28.1:Projection operator
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

from qiskit.visualization import plot_bloch_vector


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 Subplot_1():
    ax = plt.subplot(221)

    s1 = (
        r'$\vert 0 \rangle \equiv $'
        r'$\begin{bmatrix} 1 \\ 0 \end{bmatrix} \mapsto (0,0,1)$'
    )

    s2 = (
        r'$\vert 1 \rangle \equiv $'
        r'$\begin{bmatrix} 0 \\ 1 \end{bmatrix} \mapsto (0,0,-1)$'
    )

    s3 = r'$\psi_x=\cos \phi \sin \theta$'
    s4 = r'$\psi_y=\sin \phi \sin \theta$'
    s5 = r'$\psi_z=\cos \theta$'

    ax.text(0.15, 0.90, s1)
    ax.text(0.15, 0.60, s2)
    ax.text(0.15, 0.30, s3)
    ax.text(0.15, 0.20, s4)
    ax.text(0.15, 0.10, s5)

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

    return


def Subplot_2():
    ax1 = fig.add_subplot(222, projection='3d')

    # P1 = Point(0, 0, 1)
    # plot_bloch_vector([P1.x, P1.y, P1.z], ax=ax1)
    # ax1.scatter(P1.x, P1.y, P1.z)

    # P2 = Point(0, 0, 0)
    # Line(ax1, P1, P2)

    theta = np.pi / 6
    rotation = (3/2) * np.pi  # transfer 3d to qiskit 3d
    phi_1 = np.pi / 3         # 3d
    phi_2 = rotation + phi_1  # qiskit 3d

    PO = Point(0, 0, 0)
    PB_1 = Point(np.sin(theta)*np.cos(phi_1), np.sin(theta)*np.sin(phi_1), 0)
    PB_2 = Point(np.sin(theta)*np.cos(phi_2), np.sin(theta)*np.sin(phi_2), 0)
    PA_1 = Point(PB_1.x, PB_1.y, np.cos(theta))
    PA_2 = Point(PB_2.x, PB_2.y, np.cos(theta))

    plot_bloch_vector([PA_1.x, PA_1.y, PA_1.z], ax=ax1)  # qiskit 3d

    Line(ax1, PO, PB_2)
    # Line(ax1, PO, PA_2)
    Line(ax1, PA_2, PB_2)

    # lables(psi)
    ax1.text(0.35, 0, 0.8, r"$\vert\psi\rangle$")
    ax1.text(0.05, 0, 0.4, r"$\theta$")
    ax1.text(-0.1, 0, -0.45, r"$\phi$")

    # curves: theta and phi
    theta_max = np.pi / 6  # angle between psi and z axis
    phi_max = np.pi / 3    # angle between psi and x axis
    phi_offset = -np.pi / 2  # xy coordinate rotation from matplotlib to qiskit
    curve_radius = 0.3
    n = 20

    c1 = np.linspace(0, theta_max, n)
    x1 = curve_radius * np.sin(c1) * np.cos(phi_max+phi_offset)
    y1 = curve_radius * np.sin(c1) * np.sin(phi_max+phi_offset)
    z1 = curve_radius * np.cos(c1)
    ax1.plot(x1, y1, z1, 'g', lw=2)  # curve theta

    c2 = np.linspace(phi_offset, phi_max+phi_offset, n)
    x2 = curve_radius * np.cos(c2)
    y2 = curve_radius * np.sin(c2)
    z2 = c2 * 0
    ax1.plot(x2, y2, z2, 'r', lw=2)  # curve phi

    ax1.text(0, 0, -1.8, '(b)', fontsize=20)
    ax.set_axis_off()

    return


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

    # string setting
    s1_1 = r'$\vert\psi\rangle$'

    s1_2 = (
        r'$=\cos\frac{\theta}{2}\ \vert0\rangle'
        r'+e^{i\phi}\sin\frac{\theta}{2}\ \vert1\rangle$'
    )

    s2 = (
        r'$=\begin{bmatrix}\cos\frac{\theta}{2}\\$'
        r'$\ e^{i\phi}\sin\frac{\theta}{2}\ \end{bmatrix}$'
    )

    s3 = r'$(0\leq\theta\leq\pi,\ 0\leq\phi<2\pi)$'

    # string output
    ax.text(0.10, 0.75, s1_1)
    ax.text(0.25, 0.75, s1_2)
    ax.text(0.25, 0.45, s2)
    ax.text(0.10, 0.15, s3)

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

    return


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

    # string setting
    s1 = r'$P = \vert \psi \rangle \langle \psi \vert $'

    s2 = (
        r'$=\ $'
        r'$\begin{bmatrix}$'
        r'$\cos^2 \frac{\theta}{2} & $'
        r'$e^{-i\phi} \sin{\frac{\theta}{2}} \cos{\frac{\theta}{2}}\\$'
        r'$e^{i\phi} \sin{\frac{\theta}{2}} \cos{\frac{\theta}{2}} &$'
        r'$\sin^2 \frac{\theta}{2}$'
        r'$\end{bmatrix}$'
    )

    s3 = (
        r'$=\ \frac{1}{2}$'
        r'$\begin{bmatrix}$'
        r'$1+\psi_z & \psi_x-i\psi_y \\$'
        r'$\psi_x+i\psi_y & 1-\psi_z$'
        r'$\end{bmatrix},$'
    )

    s4 = (
        r'$where\ P_{ij}(i,j=0,1) = \langle i \vert P \vert j \rangle .$'
    )

    # string output
    ax.text(0.10, 0.90, s1, color='r')
    ax.text(0.10, 0.65, s2, color='r', fontsize=32)
    ax.text(0.10, 0.35, s3, color='r')
    ax.text(0.10, 0.10, s4, color='r', fontsize=32)

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

    return


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

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

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

解說:

-----

References


[1] Projection Operators and Completeness

https://quantummechanics.ucsd.edu/ph130a/130_notes/node185.html


[2] linear algebra - What is the idea behind a projection operator? What does it do? - Mathematics Stack Exchange

https://math.stackexchange.com/questions/1303977/what-is-the-idea-behind-a-projection-operator-what-does-it-do


[3] 黃子嘉 - 線代離散研究室: [線性代數] 請教4個的觀念

http://zjhwang.blogspot.com/2009/07/4.html


[4] 特殊矩陣 (5):冪等矩陣 | 線代啟示錄

https://ccjou.wordpress.com/2009/09/29/%E7%89%B9%E6%AE%8A%E7%9F%A9%E9%99%A3-%E4%BA%94%EF%BC%9A%E5%86%AA%E7%AD%89%E7%9F%A9%E9%99%A3/

-----

Python 量子運算(目錄)

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

-----

沒有留言:

張貼留言

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