栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

用Google的TensorNetwork实现3个qubit的量子Fourier变换

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用Google的TensorNetwork实现3个qubit的量子Fourier变换

用Google的TensorNetwork实现了三个Qubit的Fourier变换

量子线路参考本人手绘

交换的部分可以看成4阶的张量

 

 

废话少说,直接上代码

"""
用张量网络实现
三个量子比特的傅里叶变换
"""
import numpy as np
import tensornetwork as tn

# 虚数单位
j = np.complex(0, 1)

# 0和1
a = np.array([1, 0], dtype=complex)
b = np.array([0, 1], dtype=complex)

# 三个Qubit
qubit1 = tn.Node(a)
qubit2 = tn.Node(a)
qubit3 = tn.Node(b)

# 点张量
dot = np.zeros((2, 2, 2), dtype=complex)
dot[0][0][0] = 1
dot[1][1][1] = 1
dot = tn.Node(dot)

# Hadmard 门张量
Hadmard = np.zeros((2, 2), dtype=complex)
Hadmard[0][0] = 1 / np.sqrt(2)
Hadmard[0][1] = 1 / np.sqrt(2)
Hadmard[1][0] = 1 / np.sqrt(2)
Hadmard[1][1] = -1 / np.sqrt(2)
Hadmard = tn.Node(Hadmard)

# R_k张量,这里只用到了R2和R3
R2 = np.zeros((2, 2, 2), dtype=complex)
R2[0][0][0] = 1
R2[1][1][0] = 1
R2[0][0][1] = 1
R2[1][1][1] = np.exp(2 * np.pi * j / (2 ** 2))

R3 = R2.copy()
R3[1][1][1] = np.exp(2 * np.pi * j / (2 ** 3))

R2 = tn.Node(R2)
R3 = tn.Node(R3)

# 量子傅里叶变换最后要交换Qubit的位置,这里定义一个交换张量
# 交换张量
exchange = np.zeros((2, 2, 2, 2), dtype=complex)
exchange[0][0][0][0] = 1
exchange[0][1][1][0] = 1
exchange[1][0][0][1] = 1
exchange[1][1][1][1] = 1
exchange = tn.Node(exchange)

# 量子线路中用到的量子门
# H_1_1 代表量子线路中的第一行的第一个Hadmard门
# 其余的以此类推
H_1_1 = Hadmard.copy()
R2_1_1 = R2.copy()
R3_1_1 = R3.copy()
dot_2_1 = dot.copy()
H_2_1 = Hadmard.copy()
R2_2_1 = R2.copy()
dot_3_1 = dot.copy()
dot_3_2 = dot.copy()
H_3_1 = Hadmard.copy()

# 将量子线路中的边连接起来
# 量子线路的第一行
edge_1_1 = qubit1[0] ^ H_1_1[0]
edge_1_2 = H_1_1[1] ^ R2_1_1[0]
edge_1_3 = R2_1_1[1] ^ R3_1_1[0]
edge_1_4 = exchange[0] ^ R3_1_1[1]

# 量子线路的第二行
edge_2_1 = qubit2[0] ^ dot_2_1[0]
edge_2_2 = dot_2_1[2] ^ R2_1_1[2]
edge_2_3 = dot_2_1[1] ^ H_2_1[0]
edge_2_4 = H_2_1[1] ^ R2_2_1[0]

# 量子线路的第三行
edge_3_1 = qubit3[0] ^ dot_3_1[0]
edge_3_2 = dot_3_1[2] ^ R3_1_1[2]
edge_3_3 = dot_3_1[1] ^ dot_3_2[0]
edge_3_4 = dot_3_2[2] ^ R2_2_1[2]
edge_3_5 = dot_3_2[1] ^ H_3_1[0]
edge_3_6 = exchange[1] ^ H_3_1[1]





# 将所有的边连起来
ans = qubit1 @ H_1_1 @ R2_1_1 @ R3_1_1
ans = ans @ dot_2_1 @ H_2_1 @ R2_2_1
ans = ans @ dot_3_1 @ dot_3_2 @ H_3_1
ans = ans @ qubit2 @ qubit3
ans = ans @ exchange

# 交换下标的位置,这个以后专门出一片博文解释
ans.reorder_axes([1, 0, 2])

# 打印张量的阶数
print(ans.shape)
# 打印张量
print(ans.tensor)



'''
# 用依次连接的方式实现
tn.contract(edge_1_1)
tn.contract(edge_1_2)
tn.contract(edge_1_3)
tn.contract(edge_1_4)
tn.contract(edge_2_1)
tn.contract(edge_2_2)
tn.contract(edge_2_3)
tn.contract(edge_2_4)
tn.contract(edge_3_1)
tn.contract(edge_3_2)
tn.contract(edge_3_3)
tn.contract(edge_3_4)
tn.contract(edge_3_5)
result = tn.contract(edge_3_6)

print(result.shape)
print(result.tensor)

'''



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/275783.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号