Paddle_SSVQE.py 4.7 KB
Newer Older
Q
Quleaf 已提交
1
# Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved.
Q
Quleaf 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#
# 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.

"""
Paddle_SSVQE: To learn more about the functions and properties of this application,
you could check the corresponding Jupyter notebook under the Tutorial folder.
"""
Q
Quleaf 已提交
19

Q
Quleaf 已提交
20
import numpy
Q
Quleaf 已提交
21 22

from paddle.complex import matmul
Q
Quleaf 已提交
23 24
from paddle import fluid
from paddle_quantum.circuit import UAnsatz
Q
Quleaf 已提交
25
from paddle_quantum.utils import dagger
Q
Quleaf 已提交
26
from paddle_quantum.SSVQE.HGenerator import H_generator
Q
Quleaf 已提交
27

Q
Quleaf 已提交
28
SEED = 14  # Choose the seed for random generator
Q
Quleaf 已提交
29 30 31 32 33 34 35 36 37 38

__all__ = [
    "U_theta",
    "Net",
    "Paddle_SSVQE",
]


def U_theta(theta, N):
    """
Q
Quleaf 已提交
39
    Quantum Neural Network
Q
Quleaf 已提交
40 41
    """

Q
Quleaf 已提交
42
    # Initialize the quantum neural network by the number of qubits (width of the network)
Q
Quleaf 已提交
43
    cir = UAnsatz(N)
Q
Quleaf 已提交
44

Q
Quleaf 已提交
45
    # Use a built-in QNN template
Q
Quleaf 已提交
46 47
    cir.universal_2_qubit_gate(theta)

Q
Quleaf 已提交
48
    # Return the Unitary matrix simulated by QNN
Q
Quleaf 已提交
49
    return cir.U
Q
Quleaf 已提交
50 51 52 53 54 55 56


class Net(fluid.dygraph.Layer):
    """
    Construct the model net
    """

Q
Quleaf 已提交
57 58
    def __init__(self, shape, param_attr=fluid.initializer.Uniform(low=0.0, high=2 * numpy.pi, seed=SEED),
                 dtype='float64'):
Q
Quleaf 已提交
59 60
        super(Net, self).__init__()

Q
Quleaf 已提交
61
        # Initialize theta by sampling from a uniform distribution [0, 2*pi]
Q
Quleaf 已提交
62
        self.theta = self.create_parameter(shape=shape, attr=param_attr, dtype=dtype, is_bias=False)
Q
Quleaf 已提交
63 64
        
    # Define the loss function and forward propagation mechanism
Q
Quleaf 已提交
65
    def forward(self, H, N):
Q
Quleaf 已提交
66
        # Apply QNN onto the initial state
Q
Quleaf 已提交
67
        U = U_theta(self.theta, N)
Q
Quleaf 已提交
68

Q
Quleaf 已提交
69
        # Calculate loss function
Q
Quleaf 已提交
70
        loss_struct = matmul(matmul(dagger(U), H), U).real
Q
Quleaf 已提交
71

Q
Quleaf 已提交
72 73
        # Use computational basis to calculate each expectation value, which is the same
        # as a diagonal element in U^dagger*H*U
Q
Quleaf 已提交
74
        loss_components = [
Q
Quleaf 已提交
75 76 77
            loss_struct[0][0],
            loss_struct[1][1],
            loss_struct[2][2],
Q
Quleaf 已提交
78 79 80
            loss_struct[3][3]
        ]

Q
Quleaf 已提交
81
        # Calculate the weighted loss function
Q
Quleaf 已提交
82 83
        loss = 4 * loss_components[0] + 3 * loss_components[1] + 2 * loss_components[2] + 1 * loss_components[3]

Q
Quleaf 已提交
84 85 86
        return loss, loss_components


Q
Quleaf 已提交
87 88 89
def Paddle_SSVQE(H, N=2, THETA_SIZE=15, ITR=50, LR=0.3):
    r"""
    Paddle_SSVQE
Q
Quleaf 已提交
90 91 92 93 94 95
    :param H: Hamiltonian
    :param N: Number of qubits/Width of QNN
    :param THETA_SIZE: Number of paramaters in QNN
    :param ITR: Number of iterations
    :param LR: Learning rate
    :return: First several smallest eigenvalues of the Hamiltonian
Q
Quleaf 已提交
96
    """
Q
Quleaf 已提交
97 98
    
    # Initialize PaddlePaddle dynamic graph machanism
Q
Quleaf 已提交
99
    with fluid.dygraph.guard():
Q
Quleaf 已提交
100
        # We need to convert Numpy array to variable supported in PaddlePaddle
Q
Quleaf 已提交
101
        hamiltonian = fluid.dygraph.to_variable(H)
Q
Quleaf 已提交
102

Q
Quleaf 已提交
103
        # Fix the dimensions of network
Q
Quleaf 已提交
104
        net = Net(shape=[THETA_SIZE])
Q
Quleaf 已提交
105 106
        
        # Use Adagrad optimizer
Q
Quleaf 已提交
107
        opt = fluid.optimizer.AdagradOptimizer(learning_rate=LR, parameter_list=net.parameters())
Q
Quleaf 已提交
108

Q
Quleaf 已提交
109
        # Optimization iterations
Q
Quleaf 已提交
110 111
        for itr in range(1, ITR + 1):

Q
Quleaf 已提交
112
            # Run forward propagation to calculate loss function and obtain energy spectrum
Q
Quleaf 已提交
113 114
            loss, loss_components = net(hamiltonian, N)

Q
Quleaf 已提交
115
            # In dynamic graph, run backward propogation to minimize loss function
Q
Quleaf 已提交
116 117 118
            loss.backward()
            opt.minimize(loss)
            net.clear_gradients()
Q
Quleaf 已提交
119 120
            
            # Print results
Q
Quleaf 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
            if itr % 10 == 0:
                print('iter:', itr, 'loss:', '%.4f' % loss.numpy()[0])
    return loss_components


def main():
    N = 2
    H = H_generator(N)

    loss_components = Paddle_SSVQE(H)

    print('The estimated ground state energy is: ', loss_components[0].numpy())
    print('The theoretical ground state energy: ', numpy.linalg.eigh(H)[0][0])

    print('The estimated 1st excited state energy is: ', loss_components[1].numpy())
    print('The theoretical 1st excited state energy: ', numpy.linalg.eigh(H)[0][1])

    print('The estimated 2nd excited state energy is: ', loss_components[2].numpy())
    print('The theoretical 2nd excited state energy: ', numpy.linalg.eigh(H)[0][2])

    print('The estimated 3rd excited state energy is: ', loss_components[3].numpy())
    print('The theoretical 3rd excited state energy: ', numpy.linalg.eigh(H)[0][3])

Q
Quleaf 已提交
144

Q
Quleaf 已提交
145 146
if __name__ == '__main__':
    main()