Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Quantum
提交
f9f04245
Q
Quantum
项目概览
PaddlePaddle
/
Quantum
8 个月 前同步成功
通知
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看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
f9f04245
编写于
10月 27, 2021
作者:
Y
yangguohao
提交者:
GitHub
10月 27, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update test.py
上级
7f4ee1c8
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
26 addition
and
72 deletion
+26
-72
test_and_documents/test.py
test_and_documents/test.py
+26
-72
未找到文件。
test_and_documents/test.py
浏览文件 @
f9f04245
import
math
import
numpy
as
np
import
paddle
from
paddle
import
matmul
,
transpose
,
trace
from
paddle_quantum.circuit
import
UAnsatz
from
paddle_quantum.utils
import
dagger
,
random_pauli_str_generator
,
pauli_str_to_matrix
from
paddle_quantum.state
import
vec
,
vec_random
,
density_op
,
density_op_random
theta_size
=
4
num_qubits
=
2
ITR
=
80
LR
=
0.5
SEED
=
1
paddle
.
seed
(
SEED
)
path
=
'/home/aistudio/飞桨常规赛:量子电路合成/Question_2_Unitary.txt'
from
paddle
import
kron
from
paddle_quantum.state
import
vec
,
density_op
import
paddle
#普通构建方法
def
U_theta
(
theta
,
num_qubits
):
cir
=
UAnsatz
(
num_qubits
)
cir
.
ry
(
theta
[
0
],
0
)
cir
.
ry
(
theta
[
1
],
1
)
cir
.
cnot
([
0
,
1
])
cir
.
ry
(
theta
[
2
],
0
)
cir
.
ry
(
theta
[
3
],
1
)
return
cir
.
U
#
通过量子比特扩展
def
U_theta2
(
theta
,
num_qubits
):
#
density_matrix
def
test_density_matrix
(
):
cir
=
UAnsatz
(
1
)
cir
.
ry
(
theta
[
0
],
0
)
cir
.
expand
(
num_qubits
)
cir
.
ry
(
theta
[
1
],
1
)
cir
.
cnot
([
0
,
1
])
cir
.
ry
(
theta
[
2
],
0
)
cir
.
ry
(
theta
[
3
],
1
)
return
cir
.
U
cir
.
ry
(
paddle
.
to_tensor
(
1
,
dtype
=
'float64'
),
0
)
state
=
cir
.
run_density_matrix
()
cir
.
expand
(
3
)
print
(
cir
.
get_state
())
class
Optimization_exl
(
paddle
.
nn
.
Layer
):
def
__init__
(
self
,
shape
,
dtype
=
'float64'
):
super
(
Optimization_exl
,
self
).
__init__
()
f
=
np
.
loadtxt
(
path
)
self
.
u
=
paddle
.
to_tensor
(
f
)
self
.
theta
=
self
.
create_parameter
(
shape
=
shape
,
default_initializer
=
paddle
.
nn
.
initializer
.
Uniform
(
low
=
0
,
high
=
2
*
np
.
pi
),
dtype
=
dtype
,
is_bias
=
False
)
def
forward
(
self
):
#U = U_theta(self.theta,num_qubits)
U
=
U_theta2
(
self
.
theta
,
num_qubits
)
U_dagger
=
dagger
(
U
)
loss
=
1
-
(
0.25
*
paddle
.
real
(
trace
(
matmul
(
self
.
u
,
U_dagger
)))[
0
])
return
loss
def
result
(
self
):
return
self
.
theta
cir2
=
UAnsatz
(
3
)
cir2
.
ry
(
paddle
.
to_tensor
(
1
,
dtype
=
'float64'
),
0
)
cir2
.
run_density_matrix
()
print
(
cir2
.
get_state
())
loss_list
=
[]
parameter_list
=
[]
myLayer
=
Optimization_exl
([
theta_size
])
opt
=
paddle
.
optimizer
.
Adam
(
learning_rate
=
LR
,
parameters
=
myLayer
.
parameters
())
#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
())
for
itr
in
range
(
ITR
):
loss
=
myLayer
()[
0
]
loss
.
backward
()
opt
.
minimize
(
loss
)
opt
.
clear_grad
()
cir2
=
UAnsatz
(
3
)
cir2
.
ry
(
paddle
.
to_tensor
(
1
,
dtype
=
'float64'
),
0
)
cir2
.
run_state_vector
()
print
(
cir2
.
get_state
())
loss_list
.
append
(
loss
.
numpy
()[
0
])
parameter_list
.
append
(
myLayer
.
parameters
()[
0
].
numpy
())
#if itr % 5 == 0:
#print('iter:', itr, ' loss: %.4f' % loss.numpy())
print
(
myLayer
.
result
())
"""
U_theta 结果
Tensor(shape=[4], dtype=float64, place=CPUPlace, stop_gradient=False,
[-0.43797367, 7.05269038, 3.48305136, 0.42135362])
U_theta2 结果
Tensor(shape=[4], dtype=float64, place=CPUPlace, stop_gradient=False,
[-0.43797367, 7.05269038, 3.48305136, 0.42135362])
"""
test_density_matrix
()
test_state_vector
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录