Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Quantum
提交
201f7858
Q
Quantum
项目概览
PaddlePaddle
/
Quantum
大约 1 年 前同步成功
通知
20
Star
492
Fork
166
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
5
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Q
Quantum
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
5
Issue
5
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
201f7858
编写于
11月 05, 2021
作者:
Y
yangguohao
提交者:
GitHub
11月 05, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update trotter.py
上级
ba36dc4d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
67 addition
and
5 deletion
+67
-5
paddle_quantum/trotter.py
paddle_quantum/trotter.py
+67
-5
未找到文件。
paddle_quantum/trotter.py
浏览文件 @
201f7858
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录