From 70a3e5aec1ef0ad03895c45909a7e7a5761de578 Mon Sep 17 00:00:00 2001 From: yangguohao <70266361+yangguohao@users.noreply.github.com> Date: Sun, 10 Oct 2021 21:16:40 +0800 Subject: [PATCH 1/6] Update utils.py --- paddle_quantum/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paddle_quantum/utils.py b/paddle_quantum/utils.py index a314ca4..74e6b10 100644 --- a/paddle_quantum/utils.py +++ b/paddle_quantum/utils.py @@ -914,14 +914,14 @@ def decompose_pauli_words(self): pass return self.coefficients, self.__pauli_words - def construct_h_matrix(self): + def construct_h_matrix(self,n_qubit): r"""构建 Hamiltonian 在 Z 基底下的矩阵。 Returns: np.ndarray: Z 基底下的哈密顿量矩阵形式 """ coefs, pauli_words, sites = self.decompose_with_sites() - n_qubit = 1 + assert n_qubit>=self.num_qubit,"输入的量子数不小于哈密顿量表达式中所对应的量子比特数" for site in sites: if type(site[0]) is int: n_qubit = max(n_qubit, max(site) + 1) From 71a8add17b5c4b494e6211622040ff460c17183e Mon Sep 17 00:00:00 2001 From: yangguohao <70266361+yangguohao@users.noreply.github.com> Date: Sun, 10 Oct 2021 22:55:02 +0800 Subject: [PATCH 2/6] Update circuit.py --- paddle_quantum/circuit.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/paddle_quantum/circuit.py b/paddle_quantum/circuit.py index dc4e8b9..5fb1c99 100644 --- a/paddle_quantum/circuit.py +++ b/paddle_quantum/circuit.py @@ -59,16 +59,7 @@ def __init__(self, n): paddle.to_tensor(np.array([math.pi / 4])), paddle.to_tensor(np.array([-math.pi / 4]))] # Record history of adding gates to the circuit self.__history = [] - - def expand(self,new_n): - """ - 为原来的量子电路进行比特数扩展 - - Args: - new_n(int):扩展后的量子比特数 - """ - self.n = new_n - + def __add__(self, cir): r"""重载加法 ‘+’ 运算符,用于拼接两个维度相同的电路 From ddf695c80300378abe8df579d5ba96af490bc20b Mon Sep 17 00:00:00 2001 From: yangguohao <70266361+yangguohao@users.noreply.github.com> Date: Fri, 15 Oct 2021 09:19:39 +0800 Subject: [PATCH 3/6] Update circuit.py --- paddle_quantum/circuit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle_quantum/circuit.py b/paddle_quantum/circuit.py index 5fb1c99..2eba39f 100644 --- a/paddle_quantum/circuit.py +++ b/paddle_quantum/circuit.py @@ -59,7 +59,7 @@ def __init__(self, n): paddle.to_tensor(np.array([math.pi / 4])), paddle.to_tensor(np.array([-math.pi / 4]))] # Record history of adding gates to the circuit self.__history = [] - + def __add__(self, cir): r"""重载加法 ‘+’ 运算符,用于拼接两个维度相同的电路 From 10f4dbb2a09f81dacf41a91e2681fa15e5957dd3 Mon Sep 17 00:00:00 2001 From: yangguohao <70266361+yangguohao@users.noreply.github.com> Date: Tue, 19 Oct 2021 01:52:53 +0800 Subject: [PATCH 4/6] Update utils.py --- paddle_quantum/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle_quantum/utils.py b/paddle_quantum/utils.py index 74e6b10..a037ecc 100644 --- a/paddle_quantum/utils.py +++ b/paddle_quantum/utils.py @@ -914,7 +914,7 @@ def decompose_pauli_words(self): pass return self.coefficients, self.__pauli_words - def construct_h_matrix(self,n_qubit): + def construct_h_matrix(self, n_qubit): r"""构建 Hamiltonian 在 Z 基底下的矩阵。 Returns: From 8a3f9634a315daedb76154025f59915ee184c00c Mon Sep 17 00:00:00 2001 From: yangguohao <70266361+yangguohao@users.noreply.github.com> Date: Wed, 27 Oct 2021 16:25:23 +0800 Subject: [PATCH 5/6] Update utils.py --- paddle_quantum/utils.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/paddle_quantum/utils.py b/paddle_quantum/utils.py index a037ecc..4f7f1fe 100644 --- a/paddle_quantum/utils.py +++ b/paddle_quantum/utils.py @@ -914,17 +914,21 @@ def decompose_pauli_words(self): pass return self.coefficients, self.__pauli_words - def construct_h_matrix(self, n_qubit): + def construct_h_matrix(self, n_qubit=None): r"""构建 Hamiltonian 在 Z 基底下的矩阵。 Returns: np.ndarray: Z 基底下的哈密顿量矩阵形式 """ coefs, pauli_words, sites = self.decompose_with_sites() - assert n_qubit>=self.num_qubit,"输入的量子数不小于哈密顿量表达式中所对应的量子比特数" - 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)): From 429f5283f60a441fcb9e543bca985595d0f861e5 Mon Sep 17 00:00:00 2001 From: yangguohao <70266361+yangguohao@users.noreply.github.com> Date: Wed, 27 Oct 2021 16:26:48 +0800 Subject: [PATCH 6/6] Create test_construct_h_matrix.py --- test_documents/test_construct_h_matrix.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 test_documents/test_construct_h_matrix.py diff --git a/test_documents/test_construct_h_matrix.py b/test_documents/test_construct_h_matrix.py new file mode 100644 index 0000000..399de22 --- /dev/null +++ b/test_documents/test_construct_h_matrix.py @@ -0,0 +1,6 @@ +from paddle_quantum.utils import Hamiltonian + +h = Hamiltonian([(1, 'Z0, Z1')]) + +print(h.construct_h_matrix()) +print(h.construct_h_matrix(4))