Paddle_VQE.py 3.5 KB
Newer Older
Q
Quleaf 已提交
1 2
# !/usr/bin/env python3
# Copyright (c) 2020 Institute for Quantum Computing, Baidu Inc. All Rights Reserved.
Q
Quleaf 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Q
Quleaf 已提交
16
r"""
Q
Quleaf 已提交
17 18 19 20 21
VQE: To learn more about the functions and properties of this application,
you could check the corresponding Jupyter notebook under the Tutorial folder.
"""

import os
Q
Quleaf 已提交
22
import platform
Q
Quleaf 已提交
23

Q
Quleaf 已提交
24
import paddle
Q
Quleaf 已提交
25
from numpy import savez
Q
Quleaf 已提交
26 27 28 29

import paddle_quantum
from paddle_quantum.ansatz import Circuit
from paddle_quantum.loss import ExpecVal
Q
Quleaf 已提交
30 31 32
from paddle_quantum.VQE.benchmark import benchmark_result
from paddle_quantum.VQE.chemistrysub import H2_generator

Q
Quleaf 已提交
33 34 35 36 37 38

__all__ = [
    "Paddle_VQE",
]


Q
Quleaf 已提交
39
def Paddle_VQE(Hamiltonian, N, D=2, ITR=80, LR=0.2):
Q
Quleaf 已提交
40 41 42 43 44 45 46 47
    r"""Main Learning network using dynamic graph

    Args:
        Hamiltonian: Hamiltonian
        N: Width of QNN
        D: Depth of QNN. Defaults to 2.
        ITR: Number of iterations. Defaults to 80.
        LR: Learning rate. Defaults to 0.2.
Q
Quleaf 已提交
48 49
    """

Q
Quleaf 已提交
50
    # Determine the dimensions of network
Q
Quleaf 已提交
51 52 53 54 55
    net = Circuit(N)
    net.real_entangled_layer(depth=D)
    net.ry(qubits_idx='full')

    loss_func = ExpecVal(paddle_quantum.Hamiltonian(Hamiltonian))
Q
Quleaf 已提交
56 57 58 59 60 61 62 63 64 65 66

    # Usually, we recommend Adam optimizer for better result. If you wish, you could use SGD or RMS prop.
    opt = paddle.optimizer.Adam(learning_rate=LR, parameters=net.parameters())

    # Record optimization results
    summary_iter, summary_loss = [], []

    # Optimization iterations
    for itr in range(1, ITR + 1):

        # Run forward propagation to calculate loss function
Q
Quleaf 已提交
67 68
        state = net()
        loss = loss_func(state)
Q
Quleaf 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

        # In dynamic graph, run backward propagation to minimize loss function
        loss.backward()
        opt.minimize(loss)
        opt.clear_grad()

        # Update optimized results
        summary_loss.append(loss.numpy())
        summary_iter.append(itr)

        # Print results
        if itr % 20 == 0:
            print("iter:", itr, "loss:", "%.4f" % loss.numpy())
            print("iter:", itr, "Ground state energy:", "%.4f Ha" % loss.numpy())

    # Save results in the 'output' directory
    os.makedirs("output", exist_ok=True)
    savez("./output/summary_data", iter=summary_iter, energy=summary_loss)
Q
Quleaf 已提交
87 88


Q
Quleaf 已提交
89
if __name__ == '__main__':
Q
Quleaf 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    # Read data from built-in function or xyz file depending on OS
    sysStr = platform.system()

    if sysStr == 'Windows':
        #  Windows does not support SCF, using H2_generator instead
        print('Molecule data will be read from built-in function')
        hamiltonian, N = H2_generator()
        print('Read Process Finished')

    elif sysStr in ('Linux', 'Darwin'):
        # for linux only
        from paddle_quantum.VQE.chemistrygen import read_calc_H
        # Hamiltonian and cnot module preparing, must be executed under Linux
        # Read the H2 molecule data
        print('Molecule data will be read from h2.xyz')
        hamiltonian, N = read_calc_H(geo_fn='h2.xyz')
        print('Read Process Finished')

    else:
        print("Don't support this OS.")

    Paddle_VQE(hamiltonian, N)
    benchmark_result()