maxcut.py 3.2 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 22
To learn more about the functions and properties of this application,
you could check the corresponding Jupyter notebook under the Tutorial folder.
"""

import numpy as np
import networkx as nx
Q
Quleaf 已提交
23 24 25 26 27
import paddle
import paddle_quantum
from paddle_quantum.ansatz import Circuit
from paddle_quantum.loss import ExpecVal
from paddle_quantum import Hamiltonian
Q
Quleaf 已提交
28 29 30 31 32 33 34 35

__all__ = [
    "maxcut_hamiltonian",
    "find_cut",
]


def maxcut_hamiltonian(E):
Q
Quleaf 已提交
36
    r"""Generate the Hamiltonian for Max-Cut problem
Q
Quleaf 已提交
37 38

    Args:
Q
Quleaf 已提交
39
        E (list): Edges of the graph
Q
Quleaf 已提交
40 41

    Returns:
Q
Quleaf 已提交
42
        list: list form of the generated Hamiltonian
Q
Quleaf 已提交
43 44 45 46 47 48 49 50 51
    """
    H_D_list = []
    for (u, v) in E:
        H_D_list.append([-1.0, 'z' + str(u) + ',z' + str(v)])

    return H_D_list


def find_cut(G, p, ITR, LR, print_loss=False, shots=0, plot=False):
Q
Quleaf 已提交
52
    r"""Find the approximated solution of given Max-Cut problem via QAOA
Q
Quleaf 已提交
53 54

    Args:
Q
Quleaf 已提交
55 56 57 58 59 60 61
        G (NetworkX graph): Graph
        p (int): depth of the QAOA circuit
        ITR (int): maximum iteration times for optimization
        LR (float): learning rate of the Adam optimizer
        print_loss (bool, optional): whether print the loss value during optimization. Defaults to ``False``, not print
        shots (int, optional): measurement times at the final output of QAOA circuit, Defaults to ``0``, exact probability distribution
        plot (bool, optional): whether plot the result of measurement, Defaults to ``False``, not plot
Q
Quleaf 已提交
62 63 64 65

    Returns:
        tuple: tuple containing:

Q
Quleaf 已提交
66 67
            string: approximated solution
            dict: measurement results and their frequencies
Q
Quleaf 已提交
68 69 70 71 72 73 74 75 76 77
    """
    V = list(G.nodes())
    # Map nodes' labels to integers from 0 to |V|-1
    # node_mapping = {V[i]:i for i in range(len(V))}
    # G_mapped = nx.relabel_nodes(G, node_mapping)
    G_mapped = nx.convert_node_labels_to_integers(G)
    V = list(G_mapped.nodes())
    E = list(G_mapped.edges())
    n = len(V)
    H_D_list = maxcut_hamiltonian(E)
Q
Quleaf 已提交
78 79 80 81 82
    net = Circuit(n)
    net.superposition_layer()
    net.qaoa_layer(E, V, p)
    hamiltonian = Hamiltonian(H_D_list)
    loss_func = ExpecVal(hamiltonian)
Q
Quleaf 已提交
83 84 85
    opt = paddle.optimizer.Adam(learning_rate=LR, parameters=net.parameters())

    for itr in range(1, ITR + 1):
Q
Quleaf 已提交
86 87
        state = net()
        loss = -loss_func(state)
Q
Quleaf 已提交
88 89 90 91 92 93
        loss.backward()
        opt.minimize(loss)
        opt.clear_grad()
        if print_loss and itr % 10 == 0:
            print("iter:", itr, "  loss:", "%.4f" % loss.numpy())

Q
Quleaf 已提交
94 95
    state = net()
    prob_measure = state.measure(shots=shots, plot=plot)
Q
Quleaf 已提交
96 97 98
    cut_bitstring = max(prob_measure, key=prob_measure.get)

    return cut_bitstring, prob_measure