diff --git a/paddle_quantum/utils.py b/paddle_quantum/utils.py index 4f7f1fe572ce72e808aca83c88b023ac1878b6b8..11534dc3a3b0d0959b7b141b92b8d001a25711e9 100644 --- a/paddle_quantum/utils.py +++ b/paddle_quantum/utils.py @@ -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 diff --git a/test_documents/test.py b/test_documents/test.py new file mode 100644 index 0000000000000000000000000000000000000000..38b676cdf428019f64d75ac37cf0eb2f9d6a9a84 --- /dev/null +++ b/test_documents/test.py @@ -0,0 +1,16 @@ +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)