2022年12月18日 星期日

Python 量子運算(七):球面上的量子位元

Python 量子運算(七):球面上的量子位元

2022/11/25

-----


Fig. 7.1. Qubit Psi on the Bloch sphere.

-----



-----

代碼 7.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
# Program 7.1:Quantum bit Psi
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

from qiskit.visualization import plot_bloch_vector

# figure setting
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = r'\usepackage{{amsmath}}'
mpl.rcParams['font.size'] = 80
fig = plt.figure(figsize=(16, 16))
ax1 = fig.add_subplot(111, projection='3d')  # 3d subplot

# vector Psi setting
radius = 1
theta_max = np.pi / 6  # angle between Psi and z axis
phi_max = np.pi / 3    # angle between Psi and x axis
Psi_z = radius * np.cos(theta_max)
Psi_xy = radius * np.sin(theta_max)
Psi_x = Psi_xy * np.cos(phi_max)
Psi_y = Psi_xy * np.sin(phi_max)

# draw the Bloch sphere and vector Psi
plot_bloch_vector([Psi_x, Psi_y, Psi_z], ax=ax1)
# ax1.scatter(Psi_y, -Psi_x, 0)  # projection(Psi) on xy plane

# lables(standard)
ax1.text(0, -1.8, 0, r"$x$")
ax1.text(1.4, 0, 0, r"$y$")
ax1.text(0, 0, 1.4, r"$z$")
ax1.text(-0.4, 0, 1.2, r"$\vert0\rangle$")
ax1.text(0, 0, -1.8, r"$\vert1\rangle$")

# 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$")

# projection lines(Psi): vertical and horozantal
ax1.plot([Psi_y, Psi_y], [-Psi_x, -Psi_x], zs=[0, Psi_z])  # vertical
ax1.plot([0, Psi_y], [0, -Psi_x], zs=[0, 0])               # horizontal

# curves: theta and phi
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

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

解說:


-----

References


# Bloch sphere

[1] Bloch sphere - Wikipedia

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


# 3d line

[2] python - How can I make a simple 3D line with Matplotlib? - Stack Overflow

https://stackoverflow.com/questions/11541123/how-can-i-make-a-simple-3d-line-with-matplotlib


# 3d curve

[3] Depicting a helix

https://scipython.com/book/chapter-7-matplotlib/examples/depicting-a-helix/

-----

Python 量子運算(目錄)

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

-----

沒有留言:

張貼留言

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