Variational Quantum Eigensolver(VQE) example by MindQuantum

举报
霹雳火 发表于 2022/01/17 20:29:49 2022/01/17
【摘要】 The variational quantum eigensolver is a hybridclassical-quantum algorithm that variationally determines the ground state energy of a Hamiltonian

The variational quantum eigensolver is a hybridclassical-quantum algorithm that variationally determines the ground state energy of a Hamiltonian. In this tutorial, I am going to implement VQE for finding the ground state energy of H 2 H_2 by mindquantum.

MindQuantum is general quantum computing framework designed by Mindspore and Hiq. MindQuantum can efficiently solve problem such as quantum machine learning, quantum chemistry simulation and so on.
MindQuantum Architecture

The problem we want to tackle is based on Ref. [1]. In this paper, O’Malley reported that their first experiment demonstrated the VQE proposed in 2014 [2] on a real quantum computer. The schematic I am going to reproduce is the following, and I will implement the “software”.
vqe-diagram
The molecular hydrogen Hamiltonian is

H = g 0 I + g 1 Z 0 + g 2 Z 1 + g 3 Z 0 Z 1 + g 4 Y 0 Y 1 + g 5 X 0 X 1 H = g_0 I + g_1 Z_0 + g_2 Z_1 + g_3 Z_0 Z_1 + g_4 Y_0 Y_1 + g_5 X_0 X_1

where { X i , Z i , Y i } \{X_i, Z_i, Y_i\} denote Pauli matrices acting on the ith qubit and the real scalars {g_i} are efficiently computable functions of hydrogen-hydrogen bond length R. Let’s build a Hamiltonian for the H2 molecule with different bond length. Firstly, we import mindquantum package.

import mindquantum as mq

Defind the function for Hamiltonian:

def hamiltonian(coeff):
    a, b, c, d, e, f = coeff
    h = mq.QubitOperator('', a) + mq.QubitOperator("Z0", b) + mq.QubitOperator("Z1", c) + mq.QubitOperator("Z0 Z1", d) + mq.QubitOperator("Y0 Y1", e) + mq.QubitOperator("X0 X1", f)
    return mq.Hamiltonian(h)

Then we should prepare the ansatz. The ansatz for our problem is

ψ ( θ ) = e i θ X 0 Y 1 01 |\psi(\theta)\rangle = e^{-i\theta X_0 Y_1}|01\rangle

from mindquantum.core.operators import TimeEvolution
# Initial Hartree Fock state
hf = mq.Circuit()
hf += mq.X.on(0)
# Create ansatz circuit
u = mq.QubitOperator("X0 Y1", 't')
ansatz_circuit = TimeEvolution(u, 1.0).circuit

The quantum circuit for ansatz is shown:
ansatz
We compute the expectation for Hamiltonian as

E ( θ ) = ψ ( θ ) H ( θ ) ψ ( θ ) E(\theta) = \langle \psi(\theta)|H(\theta)|\psi(\theta)\rangle

By varying the parameters, we can find a minimum value , which represents the VQE approximation to ground state energy of hydrogen molecule.

from mindquantum.algorithm.nisq.chem import Transform
from mindquantum.framework import MQAnsatzOnlyLayer
from mindspore.common.parameter import Parameter
import numpy as np
import matplotlib.pyplot as plt

def plot_scatter(x, y):
    plt.figure(facecolor='w')
    plt.minorticks_on()
    plt.scatter(x, y, marker='o', c='r', label="VQE")
    plt.xlabel("Bond Length "r"$R$")
    plt.ylabel("Total Energy(hartree)")
    plt.legend()
    #plt.title("Energy surface of molecular hydrogen as determined by VQE")
    plt.savefig("H2 energy.pdf")
    return None
    
def vqe(total_circuit, all_coeff, dist, init_amp=[0.0]):
    #all_iter_list = []; 
    Ei_min_list = []
    for _, coeff in enumerate(all_coeff):
        hh = hamiltonian(coeff)
        grad_ops = mq.Simulator("projectq", total_circuit.n_qubits).get_expectation_with_grad(hh, total_circuit)
        pqcent = MQAnsatzOnlyLayer(grad_ops)
        pqcent.weight = Parameter(ms.Tensor(init_amp, pqcent.weight.dtype) )
        
        init_E = pqcent()
        
        optimzer = ms.nn.Adagrad(pqcent.trainable_params(), learning_rate=4e-2)
        train_pqcent = ms.nn.TrainOneStepCell(pqcent, optimzer)
        
        eps = 1.e-8
        E_diff = eps * 1e3
        E_last = init_E.asnumpy() + E_diff
        
        iter_idx = 0
        #iter_list = []
        Ei_list = []
        
        
        while abs(E_diff) > eps:
            Ei = train_pqcent().asnumpy()
        
            if iter_idx % 5 == 0:
                #iter_list.append(iter_idx)
                Ei_list.append(Ei)
            E_diff = E_last - Ei
            E_last = Ei
            iter_idx += 1
        #all_iter_list.append(iter_list)
        Ei_min_list.append(min(Ei_list) )
    plot_scatter(dist, Ei_min_list)
    
    return None

Runing the code we design above, we get the result.
ansatz

The source files for this tutorial can be find here: VQE with MindQuantum

Reference

[1] O’Malley, Peter JJ, et al. “Scalable quantum simulation of molecular energies.” Physical Review X 6.3 (2016): 031007.

[2]Peruzzo, Alberto, et al. “A variational eigenvalue solver on a photonic quantum processor.” Nature communications 5.1 (2014): 1-7.

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。