利用qiskit搭建p层QAOA电路

目录

 1.A paper about QAOA

 2.The core thoughts of QAOA

 3.How to realize the Hamiltonian named HD

 4.How to realize the Hamiltonian named Hp

 5.How to realize   QAOA  ansatz with p levels


1.A paper about QAOA

This paper summarizes the core  thoughts of QAOA and introduces the other applications of QAOA in  combinational problems,it is very popular and easy to understand for me.

I recommend the paper for you and I wish it can help you to understand QAOA better. A part of messages of the paper are as follows.

 2.The core thoughts of QAOA

 3.How to realize the Hamiltonian named HD

I won't introduce QAOA ,I think   the paper is enough for you to know the algorithm and here are many excellent blogs and materials you can find.

def create_HD(parameter,G_info):
    nqubits = len(G_info.nodes())
    beta = parameter

    # 创建量子电路
    qc_mix = QuantumCircuit(nqubits)
    # 在每个qubit上添加单比特旋转门x
    for i in range(0, nqubits):
        qc_mix.rx(beta, i)
    return qc_mix

    4.How to realize the Hamiltonian named Hp

def create_Hp(parameter,G_info):
    nqubits = len(G_info.nodes())
    gamma = parameter

    # 创建量子电路
    qc_mix = QuantumCircuit(nqubits)
    # CNOT,Rz,CNOT
    edge = G_info.edges()
    for pair_nodes in edge:
        i = pair_nodes[0]
        j = pair_nodes[1]
        qc_mix.cx(i,j)
        qc_mix.rz(-1*gamma,j)
        qc_mix.cx(i,j)
        qc_mix.barrier()
    return qc_mix
            
            

5.How to realize   QAOA  ansatz with p levels

# 创建QAOA电路
def create_qaoa(parameters_info,G):
    # 电路层数
    p = int(len(parameters_info)/2)
    # 创建初始态
    nqubits = len(G.nodes())
    circ = QuantumCircuit(nqubits)
    for i in range(0,nqubits):
        circ.h(i)
    # p层电路
    G_info = G
   
    parameter_HD = parameters_info[0:p]
    parameter_Hp = parameters_info[p:]
   
    for i in range(0,p):
        # 创建HD
        parameter1 = parameter_HD[i]
        circ1 = create_HD(parameter1,G_info)
        
        # 创建Hp
        parameter2 = parameter_Hp[i]
        circ2 = create_Hp(parameter2,G_info)
        
        # 用于存储p层的HD+Hp
        new_circ = circ2.compose(circ1)
        circ = circ.compose(new_circ) 
        
    # 返回电路
    qaoa_circ = circ
    qaoa_circ.measure_all()
    return qaoa_circ
    
# 本段代码目的:先创建一个空图G,然后为图G加结点、边
G = nx.Graph()
global G

# 添加结点,结点的对象可以是整数、字符串
G.add_nodes_from([0,1,2,3])
G.add_edges_from([ (1, 2), (2, 3), (3, 0)])

# 图的显示
nx.draw(G, with_labels=True, alpha=0.8, node_size=500)

parameters = []
p = int(input('请输入电路层数:'))
for i in range(0,2*p):
    parameter= random.random()
    parameter = parameter*2*pi
    parameters.append(parameter)
print('初始化参数:')
print(parameters)
# 显示电路   
circ = create_qaoa(parameters,G)
circ.draw(output='mpl')

补充:最近跑代码总是得不到想要的数值结果,发现是电路搭错了,目标哈密顿量电路应该是在初始哈密顿量的前面,可是我搭建的顺序反了。

正确的搭建:

 new_circ = circ2.compose(circ1)

而不是:

 new_circ = circ1.compose(circ2)

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值