未验证 提交 201f7858 编写于 作者: Y yangguohao 提交者: GitHub

Update trotter.py

上级 ba36dc4d
......@@ -18,6 +18,7 @@ Trotter Hamiltonian time evolution circuit module
from paddle_quantum.utils import Hamiltonian
from paddle_quantum.circuit import UAnsatz
from collections import defaultdict
import warnings
import numpy as np
import re
......@@ -259,11 +260,14 @@ def __add_first_order_trotter_block(circuit, tau, grouped_hamiltonian, reverse=F
assert isinstance(hamiltonian, Hamiltonian)
# decompose the Hamiltonian into 3 lists
coeffs, pauli_words, sites = hamiltonian.decompose_with_sites()
# apply rotational gate of each term
for term_index in range(len(coeffs)):
# get the sorted pauli_word and site (an array of qubit indices) according to their qubit indices
pauli_word, site = __sort_pauli_word(pauli_words[term_index], sites[term_index])
add_n_pauli_gate(circuit, 2 * tau * coeffs[term_index], pauli_word, site)
if pauli_words==['XX','YY','ZZ']:
optimal_circuit(circuit,[tau*i for i in coeffs],sites[0])
else:
# apply rotational gate of each term
for term_index in range(len(coeffs)):
# get the sorted pauli_word and site (an array of qubit indices) according to their qubit indices
pauli_word, site = __sort_pauli_word(pauli_words[term_index], sites[term_index])
add_n_pauli_gate(circuit, 2 * tau * coeffs[term_index], pauli_word, site)
# in the reverse mode, if the Hamiltonian is a single element list, reverse the order its each term
else:
if len(grouped_hamiltonian) == 1:
......@@ -388,7 +392,65 @@ def add_n_pauli_gate(circuit, theta, pauli_word, which_qubits):
circuit.h(which_qubits[qubit_index])
elif re.match(r'Y', pauli_word[qubit_index], flags=re.I):
circuit.rx(- PI / 2, which_qubits[qubit_index])
def add_optimal_circuit(circuit,theta,which_qubits):
r""" 添加一个优化电路,哈密顿量为'XXYYZZ'`
Args:
circuit (UAnsatz): 需要添加门的电路
theta list(tensor or float): 旋转角度需要传入三个参数
which_qubits (list or np.ndarray): ``pauli_word`` 中的每个算符所作用的量子比特编号
"""
p = np.pi/2
x,y,z = theta
alpha = paddle.to_tensor(3*p-4*x*p+2*x,dtype='float64')
beta = paddle.to_tensor(-3*p+4*y*p-2*y,dtype='float64')
gamma = paddle.to_tensor(2*z-p,dtype='float64')
which_qubits.sort()
a,b = which_qubits
circuit.rz(paddle.to_tensor(p,dtype='float64'),b)
circuit.cnot([b,a])
circuit.rz(gamma,a)
circuit.ry(alpha,b)
circuit.cnot([a,b])
circuit.ry(beta,b)
circuit.cnot([b,a])
circuit.rz(paddle.to_tensor(-p,dtype='float64'),a)
def __group_hamiltonian_optimal(hamiltonian):
r""" 将哈密顿量拆分成 XXYYZZ 为组合,以及剩余项两个部分,并返回由他们组成的列表
Args:
hamiltonian (Hamiltonian): Paddle Quantum 中的 Hamiltonian 类
Notes:
X0,X1,Y0,Y1,Z0,Z1以及X1,X2,Y1,Y2,Z1,Z2会被分成两组XXYYZZ,且他们的site都要一样。以及其他剩余项
"""
grouped_hamiltonian = []
coeffs, pauli_words, sites = hamiltonian.decompose_with_sites()
grouped_terms_indices = []
left_over_terms_indices = []
d = defaultdict(list)
for term_index in range(len(coeffs)):
site = sites[term_index]
pauli_word = pauli_words[term_index]
#print(pauli_word)
for pauli in ['XX', 'YY', 'ZZ']:
assert isinstance(pauli_word, str), "Each pauli word should be a string type"
if (pauli_word==pauli or pauli_word==pauli.lower()):
d[tuple(site)].append((pauli,term_index))
if len(d[tuple(site)])==3:
terms_indices_to_be_grouped = [x[1] for x in d[tuple(site)]]
grouped_terms_indices.extend(terms_indices_to_be_grouped)
grouped_hamiltonian.append(hamiltonian[terms_indices_to_be_grouped])
for term_index in range(len(coeffs)):
if term_index not in grouped_terms_indices:
left_over_terms_indices.append(term_index)
if len(left_over_terms_indices):
for term_index in left_over_terms_indices:
grouped_hamiltonian.append(hamiltonian[term_index])
return grouped_hamiltonian
def __group_hamiltonian_xyz(hamiltonian):
r""" 将哈密顿量拆分成 X、Y、Z 以及剩余项四个部分,并返回由他们组成的列表
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册