示例#1
0
def main(N=4):
    """
    This is the main function which maps the classical problem to the quantum version solved by QAOA and outputs
    the quantum solution and its corresponding classical ones. Here, N=4 is a 4-qubit example to show how QAOA works.

    """
    # Generate the adjacency matrix from the description of the problem-based graph
    _, classical_graph_adjacency = generate_graph(N, 1)
    Paddle_QAOA(classical_graph_adjacency)
示例#2
0
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)

    # 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
    # Compute the exact solution of the original problem to benchmark the performance of QAOA
    H_max = max(H_diag)
    H_min = min(H_diag)

    print('H_max:', H_max, '  H_min:', H_min)

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

    H_min = ones([len(x1['iter'])]) * H_min

    # 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()
示例#3
0
def main(N=4):
    """
    QAOA Main
    """

    classical_graph, classical_graph_adjacency = generate_graph(N, 1)
    print(classical_graph_adjacency)
    prob_measure = Paddle_QAOA(classical_graph_adjacency)

    # Flatten array[[]] to []
    prob_measure = prob_measure.flatten()
    # Plot it!
    plot_graph(prob_measure, classical_graph, N)
示例#4
0
def main(N=4):
    # number of qubits or number of nodes in the graph
    N = 4
    classical_graph, classical_graph_adjacency = generate_graph(N,
                                                                GRAPHMETHOD=1)
    print(classical_graph_adjacency)

    # Convert the Hamiltonian's list form to matrix form
    H_matrix = pauli_str_to_matrix(H_generator(N, classical_graph_adjacency),
                                   N)

    H_diag = np.diag(H_matrix).real
    H_max = np.max(H_diag)
    H_min = np.min(H_diag)

    print(H_diag)
    print('H_max:', H_max, '  H_min:', H_min)

    pos = nx.circular_layout(classical_graph)
    nx.draw(classical_graph,
            pos,
            width=4,
            with_labels=True,
            font_weight='bold')
    plt.show()

    classical_graph, classical_graph_adjacency = generate_graph(N, 1)

    opt_cir = Paddle_QAOA(classical_graph_adjacency,
                          N=4,
                          P=4,
                          METHOD=1,
                          ITR=120,
                          LR=0.1)

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

    H_min = np.ones([len(x1['iter'])]) * H_min

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

    plt.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 plot
    plt.show()

    with fluid.dygraph.guard():
        # Measure the output state of the QAOA circuit for 1024 shots by default
        prob_measure = opt_cir.measure(plot=True)

    # Find the max value in measured probability of bitstrings
    max_prob = max(prob_measure.values())
    # Find the bitstring with max probability
    solution_list = [
        result[0] for result in prob_measure.items() if result[1] == max_prob
    ]
    print("The output bitstring:", solution_list)

    # Draw the graph representing the first bitstring in the solution_list to the MaxCut-like problem
    head_bitstring = solution_list[0]

    node_cut = [
        "blue" if head_bitstring[node] == "1" else "red"
        for node in classical_graph
    ]

    edge_cut = [
        "solid"
        if head_bitstring[node_row] == head_bitstring[node_col] else "dashed"
        for node_row, node_col in classical_graph.edges()
    ]
    nx.draw(
        classical_graph,
        pos,
        node_color=node_cut,
        style=edge_cut,
        width=4,
        with_labels=True,
        font_weight="bold",
    )
    plt.show()