作业一:量子态的Bloch表示
原创2023年2月24日大约 3 分钟...约 852 字
作业一:量子态的 Bloch 表示
###### -- QUANTA -- #######
# Author: Y. Liu, W. Shi #
# Data: 2022-09-17 #
###########################
from qutip import *
from scipy.linalg import *
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
第一步:量子态的 Bloch 球表示
单比特量子态,在 Bloch 球上通过参数 以及 来描述,如下图所示。
例如,的坐标为。
给定参数以及,所描述的量子态用Dirac符号表示为:。
请在下面的代码框中,完善函数 getCoordFromPsi()
,以及 getPsiFromCoord()
,实现量子态矢量与 Bloch 球坐标的相互转换,并通过测试。
def getCoordFromPsi(psi):
coord = np.array([0.0000, 0.0000, 1.0000], dtype = float)
#######################################
#todo: complete this function.
#######################################
return coord
def getPsiFromCoord(coord):
psi = np.array([0.0000 + 0.0000j, 1.0000 + 0.0000j], dtype = complex)
#######################################
#todo: complete this function.
#######################################
return psi
测试部分
请直接运行以下代码块检查所实现函数的正确性
注:同学们可参考测试数据,但请勿修改测试代码!
#don't modify the code in this block
n_ck = 10
psi_ck = np.array( [[ -0.4557 + 0.8558j, 0.1531 + 0.1913j],
[ 0.0459 + 0.0052j, 0.8862 - 0.4611j],
[ -0.0598 + 0.0307j, -0.9459 - 0.3176j],
[ -0.7077 + 0.1489j, -0.5046 + 0.4716j],
[ -0.1643 + 0.2612j, 0.9362 - 0.1681j],
[ -0.7062 + 0.6726j, -0.2186 + 0.0326j],
[ -0.5454 + 0.5694j, -0.3613 - 0.4978j],
[ 0.0438 + 0.1245j, -0.0436 - 0.9903j],
[ 0.0000 + 0.0000j, 1.0000 + 0.0000j],
[ 0.0546 + 0.0326j, 0.9482 + 0.3113j]], dtype = complex)
coord_ck = np.array([[ 0.1880, -0.4364, 0.8799],
[ 0.0766, -0.0515, -0.9957],
[ 0.0937, 0.0960, -0.9910],
[ 0.8546, -0.5173, 0.0459],
[-0.3955, -0.4339, -0.8095],
[ 0.3527, 0.2480, 0.9023],
[-0.1728, 0.9544, 0.2433],
[-0.2504, -0.0760, -0.9652],
[ 0.0000, 0.0000, -1.0000],
[ 0.1238, -0.0278, -0.9919]], dtype = float)
def checkCoord2Psi():
print('Checking converting coordinate to state vector...')
err = [abs(np.matrix(getPsiFromCoord(coord_ck[c])) * np.matrix(psi_ck[c]).H)[0,0] - 1 for c in range(n_ck)]
if np.sum(err) < 0.01:
print('Pass!')
else:
print('Wrong Answer err = %.3f! Please Correct the code.' % np.sum(err))
def checkPsi2Coord():
print('Checking converting state vector to coordinate...')
err = [np.sum(abs(getCoordFromPsi(psi_ck[c]) - coord_ck[c])) for c in range(n_ck)]
if np.sum(err) < 0.05:
print('Pass!')
else:
print('Wrong Answer err = %.3f! Please Correct the code.' % np.sum(err))
checkCoord2Psi()
checkPsi2Coord()
第二步:Bloch 球的绘制
基于 Qutip 库,参考示例代码(可直接运行以查看效果),添加代码,将 psi_to_add
中包含的量子态全部绘制到 Block 球上。
psi_to_add = np.array([[ 1.0000 + 0.0000j, 0.0000 + 0.0000j],
[ 0.7071 + 0.0000j, 0.5000 + 0.5000j],
[ 0.0000 + 0.0000j, 0.0000 + 1.0000j],
[-0.7071 + 0.0000j, -0.5000 + 0.5000j],
[-1.0000 + 0.0000j, -0.0000 + 0.0000j]])
fig = plt.figure(figsize=(6,6))
axes = Axes3D(fig, auto_add_to_figure=False)
fig.add_axes(axes)
sphere = Bloch(axes = axes)
#example:
psi = np.array([1.0000, 0.0000])
coord = getCoordFromPsi(psi)
sphere.add_vectors(coord)
######################
#todo:
#converting the 5 states in psi_to_add into coordinates, and ploting them onto the Bloch sphere
######################
sphere.make_sphere()
#when using CMD to run this script, the Bloch sphere can be shown in an independent view by the following code.
#fig.show()
公众号:AI悦创【二维码】

AI悦创·编程一对一
AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然,还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线,随时响应!微信:Jiabcdefh
C++ 信息奥赛题解,长期更新!长期招收一对一中小学信息奥赛集训,莆田、厦门地区有机会线下上门,其他地区线上。微信:Jiabcdefh
方法一:QQ
方法二:微信:Jiabcdefh
