未验证 提交 77853b49 编写于 作者: Q QuLeaf 提交者: GitHub

Merge branch 'master' into task80

......@@ -26,7 +26,7 @@ from paddle import imag, real, reshape, kron, matmul, trace
from paddle_quantum.utils import partial_trace, dagger, pauli_str_to_matrix
from paddle_quantum import shadow
from paddle_quantum.intrinsic import *
from paddle_quantum.state import density_op
from paddle_quantum.state import density_op,vec
__all__ = [
"UAnsatz",
......@@ -60,6 +60,29 @@ class UAnsatz:
# Record history of adding gates to the circuit
self.__history = []
def expand(self,new_n):
"""
为原来的量子电路进行比特数扩展
Args:
new_n(int):扩展后的量子比特数
"""
assert new_n>=self.n,'扩展后量子比特数要大于原量子比特数'
diff = new_n-self.n
dim = 2**diff
if self.__state is not None:
if self.__run_mode=='density_matrix':
shape = (dim,dim)
_state = paddle.to_tensor(density_op(diff))
elif self.__run_mode=='state_vector':
shape = (dim,)
_state = paddle.to_tensor(vec(0,diff))
_state= paddle.reshape(_state,shape)
_state = kron(self.__state,_state)
self.__state = _state
self.n = new_n
def __add__(self, cir):
r"""重载加法 ‘+’ 运算符,用于拼接两个维度相同的电路
......
......@@ -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",
......@@ -59,6 +60,7 @@ __all__ = [
"plot_n_qubit_state_in_bloch_sphere",
"plot_state_in_bloch_sphere",
"plot_rotation_in_bloch_sphere",
"img_to_density_matrix",
]
......@@ -915,17 +917,21 @@ class Hamiltonian:
pass
return self.coefficients, self.__pauli_words
def construct_h_matrix(self):
def construct_h_matrix(self, n_qubit=None):
r"""构建 Hamiltonian 在 Z 基底下的矩阵。
Returns:
np.ndarray: Z 基底下的哈密顿量矩阵形式
"""
coefs, pauli_words, sites = self.decompose_with_sites()
n_qubit = 1
for site in sites:
if type(site[0]) is int:
n_qubit = max(n_qubit, max(site) + 1)
if n_qubit is None:
n_qubit = 1
for site in sites:
if type(site[0]) is int:
print(n_qubit,(site))
n_qubit = max(n_qubit, max(site) + 1)
else:
assert n_qubit>=self.n_qubits,"输入的量子数不小于哈密顿量表达式中所对应的量子比特数"
h_matrix = np.zeros([2 ** n_qubit, 2 ** n_qubit], dtype='complex64')
spin_ops = SpinOps(n_qubit, use_sparse=True)
for idx in range(len(coefs)):
......@@ -1719,3 +1725,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
通过在UAnsatz类中添加新的成员函数expand来实现扩展
from paddle_quantum.circuit import UAnsatz
from paddle import kron
from paddle_quantum.state import vec,density_op
import paddle
#density_matrix
def test_density_matrix():
cir = UAnsatz(1)
cir.ry(paddle.to_tensor(1,dtype='float64'),0)
state = cir.run_density_matrix()
cir.expand(3)
print(cir.get_state())
cir2 = UAnsatz(3)
cir2.ry(paddle.to_tensor(1,dtype='float64'),0)
cir2.run_density_matrix()
print(cir2.get_state())
#state_vector
def test_state_vector():
cir = UAnsatz(1)
cir.ry(paddle.to_tensor(1,dtype='float64'),0)
state = cir.run_state_vector()
cir.expand(3)
print(cir.get_state())
cir2 = UAnsatz(3)
cir2.ry(paddle.to_tensor(1,dtype='float64'),0)
cir2.run_state_vector()
print(cir2.get_state())
test_density_matrix()
test_state_vector()
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)
from paddle_quantum.utils import Hamiltonian
h = Hamiltonian([(1, 'Z0, Z1')])
print(h.construct_h_matrix())
print(h.construct_h_matrix(4))
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册