未验证 提交 45eda406 编写于 作者: Q QuLeaf 提交者: GitHub

Merge pull request #24 from yangguohao/task79

【PaddlePaddle Hackathon】79 图片编码为量子态 
......@@ -33,6 +33,7 @@ from scipy import sparse
import matplotlib as mpl
from paddle_quantum import simulator
import matplotlib.animation as animation
import matplotlib.image
__all__ = [
"partial_trace",
......@@ -58,6 +59,7 @@ __all__ = [
"Hamiltonian",
"plot_state_in_bloch_sphere",
"plot_rotation_in_bloch_sphere",
"img_to_density_matrix",
]
......@@ -1603,3 +1605,24 @@ def decompose(matrix):
pauli_form.append(pauli_site)
return pauli_form
def img_to_density_matrix(img_file):
r"""将图片编码为密度矩阵
Args:
img_file: 图片文件
Return:
rho:密度矩阵 ``
"""
img_matrix = matplotlib.image.imread(img_file)
#将图片转为灰度图
img_matrix = img_matrix.mean(axis=2)
#填充矩阵,使其变为[2**n,2**n]的矩阵
length = int(2**np.ceil(np.log2(np.max(img_matrix.shape))))
img_matrix = np.pad(img_matrix,((0,length-img_matrix.shape[0]),(0,length-img_matrix.shape[1])),'constant')
#trace为1的密度矩阵
rho = img_matrix@img_matrix.T
rho = rho/np.trace(rho)
return rho
from paddle_quantum.utils import img_to_density_matrix
import paddle
import matplotlib.image
import numpy as np
img_file = '/home/aistudio/f1.jpeg'
rho = (img_to_density_matrix(img_file))
#半正定
w,_=np.linalg.eig(rho)
print(all(w>=0))
#迹为1
print(np.trace(rho))
#shape为[2**n,2**n]
print(rho.shape)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册