[ SKILL_DOCUMENTATION ]
# QuTiP 可视化
## 布洛赫球 (Bloch Sphere)
在布洛赫球上可视化量子比特态。
### 基本用法
python
from qutip import *
import matplotlib.pyplot as plt
# 创建布洛赫球
b = Bloch()
# 添加态
psi = (basis(2, 0) + basis(2, 1)).unit()
b.add_states(psi)
# 添加向量
b.add_vectors([1, 0, 0]) # X轴
# 显示
b.show()
### 多态显示
python
# 添加多个态
states = [(basis(2, 0) + basis(2, 1)).unit(),
(basis(2, 0) + 1j*basis(2, 1)).unit()]
b.add_states(states)
# 添加点
b.add_points([[0, 1, 0], [0, -1, 0]])
# 自定义颜色
b.point_color = ['r', 'g']
b.point_marker = ['o', 's']
b.point_size = [20, 20]
b.show()
### 动画
python
# 动画演示态演化
states = result.states # 来自 sesolve/mesolve
b = Bloch()
b.vector_color = ['r']
b.view = [-40, 30] # 视角
# 创建动画
from matplotlib.animation import FuncAnimation
def animate(i):
b.clear()
b.add_states(states[i])
b.make_sphere()
return b.axes
anim = FuncAnimation(b.fig, animate, frames=len(states),
interval=50, blit=False, repeat=True)
plt.show()
### 自定义
python
b = Bloch()
# 球体外观
b.sphere_color = '#FFDDDD'
b.sphere_alpha = 0.1
b.frame_alpha = 0.1
# 坐标轴
b.xlabel = ['$|+\\rangle$', '$|-\\rangle$']
b.ylabel = ['$|+i\\rangle$', '$|-i\\rangle$']
b.zlabel = ['$|0\\rangle$', '$|1\\rangle$']
# 字体大小
b.font_size = 20
b.font_color = 'black'
# 视角
b.view = [-60, 30]
# 保存图片
b.save('bloch.png')
## 维格纳函数 (Wigner Function)
相空间准概率分布。
### 基本计算
python
# 创建态
psi = coherent(N, alpha)
# 计算维格纳函数
xvec = np.linspace(-5, 5, 200)
W = wigner(psi, xvec, xvec)
# 绘图
fig, ax = plt.subplots(1, 1, figsize=(6, 6))
cont = ax.contourf(xvec, xvec, W, 100, cmap='RdBu')
ax.set_xlabel('Re(α)')
ax.set_ylabel('Im(α)')
plt.colorbar(cont, ax=ax)
plt.show()
### 特殊颜色映射
python
# 维格纳颜色映射强调负值
from qutip import wigner_cmap
W = wigner(psi, xvec, xvec)
fig, ax = plt.subplots()
cont = ax.contourf(xvec, xvec, W, 100, cmap=wigner_cmap(W))
ax.set_title('Wigner Function')
plt.colorbar(cont)
plt.show()
### 3D 曲面图
python
from mpl_toolkits.mplot3d import Axes3D
X, Y = np.meshgrid(xvec, xvec)
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, W, cmap='RdBu', alpha=0.8)
ax