未验证 提交 00b1396a 编写于 作者: Q QuLeaf 提交者: GitHub

Merge pull request #25 from yangguohao/task77

[PaddlePaddle Hackathon]77 为哈密顿量矩阵实现指定量子比特数
...@@ -914,17 +914,21 @@ class Hamiltonian: ...@@ -914,17 +914,21 @@ class Hamiltonian:
pass pass
return self.coefficients, self.__pauli_words return self.coefficients, self.__pauli_words
def construct_h_matrix(self): def construct_h_matrix(self, n_qubit=None):
r"""构建 Hamiltonian 在 Z 基底下的矩阵。 r"""构建 Hamiltonian 在 Z 基底下的矩阵。
Returns: Returns:
np.ndarray: Z 基底下的哈密顿量矩阵形式 np.ndarray: Z 基底下的哈密顿量矩阵形式
""" """
coefs, pauli_words, sites = self.decompose_with_sites() coefs, pauli_words, sites = self.decompose_with_sites()
if n_qubit is None:
n_qubit = 1 n_qubit = 1
for site in sites: for site in sites:
if type(site[0]) is int: if type(site[0]) is int:
print(n_qubit,(site))
n_qubit = max(n_qubit, max(site) + 1) 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') h_matrix = np.zeros([2 ** n_qubit, 2 ** n_qubit], dtype='complex64')
spin_ops = SpinOps(n_qubit, use_sparse=True) spin_ops = SpinOps(n_qubit, use_sparse=True)
for idx in range(len(coefs)): for idx in range(len(coefs)):
......
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.
先完成此消息的编辑!
想要评论请 注册