benchmark.py 2.8 KB
Newer Older
Q
Quleaf 已提交
1
# Copyright (c) 2020 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.

"""
Benchmark
"""
from matplotlib import pyplot
Q
Quleaf 已提交
19 20
from numpy import diag, max, min, load, ones
from paddle_quantum.utils import pauli_str_to_matrix
Q
Quleaf 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34
from paddle_quantum.QAOA.QAOA_Prefunc import generate_graph, H_generator


def benchmark_QAOA(classical_graph_adjacency=None, N=None):
    """
     This function benchmarks the performance of QAOA. Indeed, it compares its approximate solution obtained
     from QAOA with predetermined parameters, such as iteration step = 120 and learning rate = 0.1, to the exact solution
     to the classical problem.
    """
    # Generate the graph and its adjacency matrix from the classical problem, such as the Max-Cut problem
    if all(var is None for var in (classical_graph_adjacency, N)):
        N = 4
        _, classical_graph_adjacency = generate_graph(N, 1)

Q
Quleaf 已提交
35 36 37
    # Convert the Hamiltonian's list form to matrix form
    H_matrix = pauli_str_to_matrix(H_generator(N, classical_graph_adjacency), N)
    H_diag = diag(H_matrix).real
Q
Quleaf 已提交
38
    # Compute the exact solution of the original problem to benchmark the performance of QAOA
Q
Quleaf 已提交
39 40
    H_max = max(H_diag)
    H_min = min(H_diag)
Q
Quleaf 已提交
41

Q
Quleaf 已提交
42
    print('H_max:', H_max, '  H_min:', H_min)
Q
Quleaf 已提交
43 44 45 46

    # Load the data of QAOA
    x1 = load('./output/summary_data.npz')

Q
Quleaf 已提交
47
    H_min = ones([len(x1['iter'])]) * H_min
Q
Quleaf 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

    # Plot it
    pyplot.figure(1)
    loss_QAOA, = pyplot.plot(x1['iter'], x1['energy'], \
                                        alpha=0.7, marker='', linestyle="--", linewidth=2, color='m')
    benchmark, = pyplot.plot(
        x1['iter'],
        H_min,
        alpha=0.7,
        marker='',
        linestyle=":",
        linewidth=2,
        color='b')
    pyplot.xlabel('Number of iteration')
    pyplot.ylabel('Performance of the loss function for QAOA')

    pyplot.legend(
        handles=[loss_QAOA, benchmark],
        labels=[
            r'Loss function $\left\langle {\psi \left( {\bf{\theta }} \right)} '
            r'\right|H\left| {\psi \left( {\bf{\theta }} \right)} \right\rangle $',
            'The benchmark result',
        ],
        loc='best')

    # Show the picture
    pyplot.show()


def main():
    """
    main
    """

    benchmark_QAOA()


if __name__ == '__main__':
    main()