diff --git a/docs/tutorials/pauli-correlation-encoding-for-qaoa.ipynb b/docs/tutorials/pauli-correlation-encoding-for-qaoa.ipynb index 70b02102501..a5b4fe3dab0 100644 --- a/docs/tutorials/pauli-correlation-encoding-for-qaoa.ipynb +++ b/docs/tutorials/pauli-correlation-encoding-for-qaoa.ipynb @@ -11,11 +11,27 @@ "---\n", "\n", "\n", - "{/* cspell:ignore lbrack setminus coloneqq rbrack // latex that isn't being ignored for some reason */}\n", + "{/* cspell:ignore lbrack setminus coloneqq rbrack binom rhobeg nfev PCEFQ */}\n", "\n", "# Pauli Correlation Encoding to reduce Maxcut requirements\n", "\n", - "*Usage estimate: 30 minutes on an Eagle r3 processor (NOTE: This is an estimate only. Your runtime might vary.)*" + "*Usage estimate: 35 minutes on an Eagle r3 processor (NOTE: This is an estimate only. Your runtime might vary.)*" + ] + }, + { + "cell_type": "markdown", + "id": "7239458e-d833-490e-8462-eaf2b5a115d4", + "metadata": {}, + "source": [ + "## Learning outcomes\n", + "After going through this tutorial, users should expect the following outcomes:\n", + "- Understand the theoretical principles behind Pauli Correlation Encoding (PCE), including how multi‑body Pauli strings enable polynomial compression of classical optimization problems.\n", + "- Implement PCE in practice to encode and solve large‑scale optimization tasks on near‑term quantum hardware.\n", + "\n", + "## Prerequisites\n", + "We recommend familiarity with the following topics before going through this tutorial:\n", + "- [Variational Quantum Algorithms](/learning/courses/variational-algorithm-design)\n", + "- [QAOA and Maxcut](/docs/tutorials/quantum-approximate-optimization-algorithm)" ] }, { @@ -54,11 +70,11 @@ "id": "1f28c0b3-b6dd-4627-90e6-db70b9114cd0", "metadata": {}, "source": [ - "in the Figure 1 from [\\[1\\]](#references) , Max-Cut problem is used as an example to illustrate the PCE approach. The Max-Cut problem with $m=9$ nodes is encoded into a Pauli correlation space, representing the optimization problem as a correlation matrix, specifically, 2-body Pauli-matrix correlations across $n=3$ qubits $(Q_1, Q_2, Q_3)$. Node colors indicate the Pauli string used for each encoded node.\n", + "in the Figure 1 from [\\[1\\]](#references) , [Max-Cut](/docs/tutorials/quantum-approximate-optimization-algorithm) problem is used as an example to illustrate the PCE approach. The Max-Cut problem with $m=9$ nodes is encoded into a Pauli correlation space, representing the optimization problem as a correlation matrix, specifically, 2-body Pauli-matrix correlations across $n=3$ qubits $(Q_1, Q_2, Q_3)$. Node colors indicate the Pauli string used for each encoded node.\n", "For example, that node 1, which corresponds to binary variable $x_1$, is encoded by the expectation value of $Z_1 \\otimes Z_2 \\otimes I_3$, while $x_8$ is encoded by $I_1 \\otimes Y_2 \\otimes Y_3$.\n", - "This corresponds to compressing the problem’s $m$ variables into $ n = O(m^{1/2})$ qubits. More broadly, $k $-body correlations enable polynomial compressions of order $k$. The chosen Pauli set comprises three subsets of mutually-commuting Pauli strings, allowing all $m$ correlations to be experimentally estimated with only three measurement settings.\n", + "This corresponds to compressing the problem’s $m$ variables into $ n = O(m^{1/2})$ qubits. More broadly, $k $-body correlations enable polynomial compressions of order $k$, with $k>1$. The chosen Pauli set comprises three subsets of mutually-commuting Pauli strings, allowing all $m$ correlations to be experimentally estimated with only three measurement settings.\n", "\n", - "A loss function $\\mathcal{L}$ of Pauli expectation values that imitates the original Max-Cut objective function is constructed. The loss function is then optimized using a quantum-classical optimization solver, such as the Variational Quantum Eigensolver (VQE).\n", + "A loss function $\\mathcal{L}$ of Pauli expectation values that imitates the original Max-Cut objective function is constructed. The loss function is then optimized using a quantum-classical optimization solver, such as the [Variational Quantum Eigensolver (VQE)](/learning/courses/quantum-diagonalization-algorithms/vqe).\n", "\n", "Once the optimization is complete, the solution is decoded back to the original optimization space, yielding the optimal Max-Cut solution.\n", "\n", @@ -79,7 +95,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "5abb33b8-080d-4375-ac16-7788f2f1516a", "metadata": {}, "outputs": [], @@ -88,7 +104,9 @@ "\n", "import numpy as np\n", "import rustworkx as rx\n", - "from scipy.optimize import minimize\n", + "import networkx as nx\n", + "\n", + "from scipy.optimize import minimize, OptimizeResult\n", "\n", "from qiskit.circuit.library import efficient_su2\n", "from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n", @@ -97,16 +115,12 @@ "from qiskit_ibm_runtime import QiskitRuntimeService\n", "from qiskit_ibm_runtime import Session\n", "from rustworkx.visualization import mpl_draw\n", - "\n", - "service = QiskitRuntimeService()\n", - "backend = service.least_busy(\n", - " operational=True, simulator=False, min_num_qubits=127\n", - ")" + "from qiskit_aer import AerSimulator" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 2, "id": "89c2999b-d309-4cf4-820e-9ef8a5cd5807", "metadata": {}, "outputs": [], @@ -123,6 +137,37 @@ " return cut_size" ] }, + { + "cell_type": "markdown", + "id": "19cc2e86-204f-4ebf-b1a3-942025cc7015", + "metadata": {}, + "source": [ + "# Small Scale Simulator Example" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "af0c1db0-3b69-459c-8013-5149ede620c2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "We are using the aer_simulator_from(ibm_pittsburgh)\n" + ] + } + ], + "source": [ + "service = QiskitRuntimeService()\n", + "real_backend = service.least_busy(\n", + " operational=True, simulator=False, min_num_qubits=156\n", + ")\n", + "backend = AerSimulator.from_backend(real_backend)\n", + "print(f\"We are using the {backend.name}\")" + ] + }, { "cell_type": "markdown", "id": "c8084430-5386-4788-97c3-c6e4fb7cb191", @@ -138,14 +183,12 @@ "\n", "\n", "### Graph -> Hamiltonian\n", - "This tutorial uses a random graph with 1000 nodes.\n", - "\n", - "The problem size might be hard to visualize, so below is a graph with 100 nodes. (Rendering a graph with 1,000 nodes directly would make it too dense to see anything!) The graph we are working with is ten times larger." + "Let us first consider a random graph with 100 nodes." ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 4, "id": "37edb718-2bab-49d7-ad66-5f2f67d2aeff", "metadata": {}, "outputs": [ @@ -160,36 +203,26 @@ } ], "source": [ - "mpl_draw(rx.undirected_gnp_random_graph(100, 0.1, seed=42))" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "ddbb83ea-848d-4ab7-8c69-52b1683b09eb", - "metadata": {}, - "outputs": [], - "source": [ - "num_nodes = 1000 # Number of nodes in graph\n", - "graph = rx.undirected_gnp_random_graph(num_nodes, 0.1, seed=42)" + "num_nodes = 100 # Number of nodes in graph\n", + "seed = 42\n", + "graph = rx.undirected_gnp_random_graph(num_nodes, 0.1, seed=seed)\n", + "mpl_draw(graph)" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "id": "eb7a80dc-74ea-472b-a13f-11cb8d4c0ca9", "metadata": {}, "outputs": [], "source": [ - "import networkx as nx\n", - "\n", "nx_graph = nx.Graph()\n", "nx_graph.add_nodes_from(range(num_nodes))" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 6, "id": "63451877-908a-4e80-9e11-23eb0d288bfc", "metadata": {}, "outputs": [], @@ -200,7 +233,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 7, "id": "515e7220-586f-4e2a-82b6-3885e3e38566", "metadata": {}, "outputs": [ @@ -208,7 +241,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Initial cut size: 28075\n" + "Initial cut size: 345\n" ] } ], @@ -222,9 +255,9 @@ "id": "57e3804e-61ae-45c5-9d94-3665cf01784b", "metadata": {}, "source": [ - "We encode the graph with 1000 nodes into 2-body Pauli-matrix correlations across 100 qubits. The graph is represented as a correlation matrix, where each node is encoded by a Pauli string. The sign of the expectation value of the Pauli string indicates the partition of the node. For example, node 0 is encoded by a Pauli string, $\\prod_0 = I_{19} \\otimes ... I_2 \\otimes X_1 \\otimes X_0$. The sign of the expectation value of this Pauli string indicates the partition of node 0. We define a *Pauli-correlation encoding* (PCE) relative to $\\prod$ as\n", + "We encode the graph with 100 nodes into 2-body Pauli-matrix correlations across nine qubits (see the explanation below). The graph is represented as a correlation matrix, where each node is encoded by a Pauli string. The sign of the expectation value of the Pauli string indicates the partition of the node. For example, node 0 is encoded by a Pauli string, $\\prod_0 = I_{8} \\otimes ... I_2 \\otimes X_1 \\otimes X_0$. The sign of the expectation value of this Pauli string indicates the partition of node 0. We define a *Pauli-correlation encoding* (PCE) relative to $\\prod$ as\n", "\n", - "$ x_i \\coloneqq \\textit{sgn}(\\langle\\prod_i \\rangle) $\n", + "$$ x_i \\coloneqq \\textit{sgn}(\\langle\\prod_i \\rangle) $$\n", "\n", "where $x_i$ is the partition of node $i$ and $\\langle \\prod_i \\rangle \\coloneqq \\langle \\psi |\\prod_i| \\psi \\rangle $ is the expectation value of the Pauli string encoding node $i$ over a quantum state $|\\psi \\rangle$." ] @@ -239,9 +272,35 @@ "Then, we encode the nodes in each set using the Pauli strings with $X$, $Y$, and $Z$, respectively." ] }, + { + "cell_type": "markdown", + "id": "4d5d48e5-ebe8-4b36-9496-288f40174a7b", + "metadata": {}, + "source": [ + "For that, we need to extract a relationship between the nuber of nodes and qubits that we will need to encode all the nodes. Using all possible permutations for the encoding yields to:\n", + "\n", + "$$\n", + "m=3\\binom{n}{k}.\n", + "$$\n", + "\n", + "In this notebook we consider $k=2$, hence:\n", + "\n", + "$$\n", + "m = \\frac{3}{2} n(n-1).\n", + "$$\n", + "\n", + "Therefore, the number of qubits $n$ needed to express a certain number of nodes $m$ read as:\n", + "\n", + "$$\n", + "n = \\left\\lceil \\frac{1 + \\sqrt{1 + \\tfrac{8}{3}m}}{2} \\right\\rceil.\n", + "$$\n", + "\n", + "*Note that the $\\lceil \\cdot \\rceil$ symbol represents the ceiling function, which rounds any real number up to the next integer. This ensures that the number of qubits is an integer.*" + ] + }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 13, "id": "8ea1f545-3e9f-4620-bde8-755178ad3ec9", "metadata": {}, "outputs": [ @@ -249,20 +308,22 @@ "name": "stdout", "output_type": "stream", "text": [ - "List 1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 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, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332]\n", - "List 2: [333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611, 612, 613, 614, 615, 616, 617, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665]\n", - "List 3: [666, 667, 668, 669, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701, 702, 703, 704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720, 721, 722, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735, 736, 737, 738, 739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765, 766, 767, 768, 769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 794, 795, 796, 797, 798, 799, 800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 810, 811, 812, 813, 814, 815, 816, 817, 818, 819, 820, 821, 822, 823, 824, 825, 826, 827, 828, 829, 830, 831, 832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843, 844, 845, 846, 847, 848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 858, 859, 860, 861, 862, 863, 864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, 880, 881, 882, 883, 884, 885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899, 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927, 928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999]\n" + "Number of qubits: 9\n", + "List 1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]\n", + "List 2: [33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65]\n", + "List 3: [66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]\n" ] } ], "source": [ - "num_qubits = 100\n", + "num_qubits = int(np.ceil((1 + np.sqrt(1 + (8 / 3) * num_nodes)) / 2))\n", "\n", "list_size = num_nodes // 3\n", "node_x = [i for i in range(list_size)]\n", "node_y = [i for i in range(list_size, 2 * list_size)]\n", "node_z = [i for i in range(2 * list_size, num_nodes)]\n", "\n", + "print(f\"Number of qubits: {num_qubits}\")\n", "print(\"List 1:\", node_x)\n", "print(\"List 2:\", node_y)\n", "print(\"List 3:\", node_z)" @@ -270,7 +331,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 14, "id": "d2649acb-7857-4cf4-88dc-4ef381a8552f", "metadata": {}, "outputs": [], @@ -318,18 +379,30 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 15, "id": "035f6b4a-4de0-452a-b60f-7260f9e3103a", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "\"Output" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Build the quantum circuit\n", - "qc = efficient_su2(num_qubits, [\"ry\", \"rz\"], reps=2)" + "qc = efficient_su2(num_qubits, su2_gates=[\"ry\", \"rz\"], reps=2)\n", + "qc.draw(\"mpl\")" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 16, "id": "162f1384-98f5-406e-b5ae-0e12d0ad4b59", "metadata": {}, "outputs": [], @@ -357,12 +430,12 @@ "\n", "$\\mathcal{L}^{(\\text{reg})}$ is defined as $\\mathcal{L}^{(\\text{reg})} \\coloneqq \\beta \\nu \\lbrack \\frac{1}{m} \\sum_{i \\in V} \\text{tanh} (\\alpha \\langle\\prod_i \\rangle)^2 \\rbrack ^2$\n", "\n", - "where $\\beta=1/2$, $\\nu = |E|/2 + (m -1) /4$, and $m$ is the number of nodes in the graph." + "where $\\beta=1/2$, $\\nu = |E|/2 + (m -1) /4$, $|E|$ is the number of edges, and $m$ is the number of nodes in the graph." ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 17, "id": "fe608e6a-08ce-493d-9b0a-eb9d6e0028ff", "metadata": {}, "outputs": [], @@ -430,12 +503,12 @@ "id": "583ba786-e2e9-4593-bf75-00723b589d78", "metadata": {}, "source": [ - "In this tutorial, we set `max_iter=50` for the optimization loop for demonstration purpose. If we increase the number of iterations, we can expect better results." + "In this tutorial, we set `max_iter=50` in the optimization loop for demonstration purposes. If we increase the number of iterations, we can expect better results." ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 18, "id": "8d203dd5-8b72-4b78-a36e-c10fdef3ebc3", "metadata": {}, "outputs": [], @@ -454,7 +527,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "id": "8d9d6313-9bcd-4ffb-b40c-361d18c68afe", "metadata": {}, "outputs": [ @@ -462,68 +535,70 @@ "name": "stdout", "output_type": "stream", "text": [ - "Iter 0: 16659.649201600296\n", - "Iter 1: 12104.242957555361\n", - "Iter 2: 6541.137221994661\n", - "Iter 3: 6650.6188244671985\n", - "Iter 4: 7033.193518185085\n", - "Iter 5: 6743.687931793412\n", - "Iter 6: 6223.574718684094\n", - "Iter 7: 6457.3302709535965\n", - "Iter 8: 6581.316449107595\n", - "Iter 9: 6365.761052029896\n", - "Iter 10: 6415.872673527322\n", - "Iter 11: 6421.996561600348\n", - "Iter 12: 6636.372822791712\n", - "Iter 13: 6965.174320702346\n", - "Iter 14: 6774.236562696287\n", - "Iter 15: 6393.837617108355\n", - "Iter 16: 6234.311401676519\n", - "Iter 17: 6518.192237615901\n", - "Iter 18: 6559.933925068997\n", - "Iter 19: 6646.157979243488\n", - "Iter 20: 6573.726111605048\n", - "Iter 21: 6190.642092901959\n", - "Iter 22: 6653.06500163594\n", - "Iter 23: 6545.713700369988\n", - "Iter 24: 6399.996441760465\n", - "Iter 25: 6115.959687941808\n", - "Iter 26: 6665.915093554849\n", - "Iter 27: 6832.882201259893\n", - "Iter 28: 6541.392749578919\n", - "Iter 29: 6813.3456910443165\n", - "Iter 30: 6460.800944368402\n", - "Iter 31: 6359.635437029245\n", - "Iter 32: 6040.891641882451\n", - "Iter 33: 6573.930674936448\n", - "Iter 34: 6668.031753293785\n", - "Iter 35: 6450.002712889748\n", - "Iter 36: 6519.8298811058075\n", - "Iter 37: 6467.134502398199\n", - "Iter 38: 6655.284651397334\n", - "Iter 39: 6371.168353987336\n", - "Iter 40: 6480.337259347923\n", - "Iter 41: 6339.256786764425\n", - "Iter 42: 6588.635046825541\n", - "Iter 43: 6617.677964971322\n", - "Iter 44: 6469.0441600679205\n", - "Iter 45: 6567.874244906106\n", - "Iter 46: 6217.899975264532\n", - "Iter 47: 6783.481394627947\n", - "Iter 48: 6813.371853626112\n", - "Iter 49: 6506.5871531488765\n", - " message: Maximum number of function evaluations has been exceeded.\n", + "Iter 0: 159.88755362682548\n", + "Iter 1: 113.46202580636677\n", + "Iter 2: 56.76494226400048\n", + "Iter 3: 32.63357946896002\n", + "Iter 4: 21.517837239610117\n", + "Iter 5: 30.96034960483569\n", + "Iter 6: 20.780475923938027\n", + "Iter 7: 24.54251816279811\n", + "Iter 8: 27.834486461763042\n", + "Iter 9: 16.705460776812693\n", + "Iter 10: 18.020587887236864\n", + "Iter 11: 12.252379762741352\n", + "Iter 12: 5.253885750886939\n", + "Iter 13: 6.985984759592262\n", + "Iter 14: 6.908717244584757\n", + "Iter 15: 12.915466016863858\n", + "Iter 16: 4.105776920457279\n", + "Iter 17: 11.707504530740305\n", + "Iter 18: 7.154360511076546\n", + "Iter 19: 10.3890865704735\n", + "Iter 20: 10.376147647857252\n", + "Iter 21: 2.533430195296697\n", + "Iter 22: 3.8612421907795462\n", + "Iter 23: 6.103735057461906\n", + "Iter 24: -1.1190368234312347\n", + "Iter 25: 6.125915279494738\n", + "Iter 26: 11.086280445482455\n", + "Iter 27: 10.102569882302827\n", + "Iter 28: -0.02664415648133822\n", + "Iter 29: 7.621887727398785\n", + "Iter 30: 5.967346615554497\n", + "Iter 31: 3.85345716014828\n", + "Iter 32: 4.5494846149011\n", + "Iter 33: 10.006668112637232\n", + "Iter 34: -3.1927138938527877\n", + "Iter 35: 2.8829882366285116\n", + "Iter 36: 3.3130087521654144\n", + "Iter 37: -4.907566569808272\n", + "Iter 38: -4.980134722109894\n", + "Iter 39: -2.990457463896541\n", + "Iter 40: -5.938401817344579\n", + "Iter 41: -2.1807712386469724\n", + "Iter 42: -1.0945774380342126\n", + "Iter 43: -4.7548102593556685\n", + "Iter 44: -3.8762362299208144\n", + "Iter 45: -4.9348321021624\n", + "Iter 46: -6.487722842864011\n", + "Iter 47: 0.7064210113389331\n", + "Iter 48: -2.3428323031772216\n", + "Iter 49: -2.626032270380895\n", + " message: Return from COBYLA because the objective function has been evaluated 50 times.\n", " success: False\n", - " status: 2\n", - " fun: 6040.891641882451\n", - " x: [ 1.375e+00 1.951e+00 ... 1.923e-01 4.087e-02]\n", - " nfev: 50\n", - " maxcv: 0.0\n" + " status: 3\n", + " fun: -2.626032270380895\n", + " x: [ 1.375e+00 1.951e+00 ... 9.395e-01 8.948e-01]\n", + " nfev: 50\n" ] } ], "source": [ - "# Run the optimization using Session\n", + "max_iter = 50\n", + "counter = {\"i\": 0}\n", + "last_x = {\"value\": None}\n", + "last_fun = {\"value\": None}\n", "\n", "with Session(backend=backend) as session:\n", " estimator = Estimator(mode=session)\n", @@ -531,15 +606,33 @@ " experiment_result = []\n", "\n", " def loss_func(x):\n", - " return loss_func_estimator(\n", + " last_x[\"value\"] = x.copy()\n", + " if counter[\"i\"] + 1 > max_iter:\n", + " return last_fun[\"value\"]\n", + " counter[\"i\"] += 1\n", + " val = loss_func_estimator(\n", " x, qc, [pce[0], pce[1], pce[2]], estimator, graph\n", " )\n", + " last_fun[\"value\"] = val\n", + " return val\n", "\n", - " np.random.seed(42)\n", + " np.random.seed(seed)\n", " initial_params = np.random.rand(qc.num_parameters)\n", + "\n", " result = minimize(\n", - " loss_func, initial_params, method=\"COBYLA\", options={\"maxiter\": 50}\n", + " loss_func, initial_params, method=\"COBYLA\", options={\"rhobeg\": 1.0}\n", " )\n", + "\n", + " if counter[\"i\"] >= max_iter:\n", + " result = OptimizeResult(\n", + " message=f\"Return from COBYLA because the objective function has been evaluated {max_iter} times.\",\n", + " success=False,\n", + " status=3,\n", + " fun=last_fun[\"value\"],\n", + " x=last_x[\"value\"],\n", + " nfev=counter[\"i\"],\n", + " )\n", + "\n", "print(result)" ] }, @@ -555,7 +648,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 20, "id": "fa2db108-754b-4036-af98-87f5390a9c11", "metadata": {}, "outputs": [ @@ -563,7 +656,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "{0, 1, 4, 8, 9, 10, 12, 13, 14, 15, 16, 18, 25, 27, 31, 32, 34, 36, 38, 39, 40, 41, 44, 46, 47, 48, 49, 50, 51, 52, 57, 60, 61, 62, 63, 64, 65, 66, 68, 71, 79, 81, 82, 86, 88, 91, 92, 93, 94, 95, 96, 99, 100, 105, 106, 107, 112, 114, 115, 121, 123, 129, 133, 134, 145, 147, 161, 165, 166, 168, 171, 173, 184, 185, 187, 188, 192, 193, 194, 196, 197, 198, 202, 205, 206, 207, 208, 209, 210, 211, 215, 217, 218, 219, 220, 221, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 238, 241, 242, 243, 244, 246, 247, 248, 249, 251, 252, 253, 255, 256, 257, 258, 259, 261, 262, 264, 265, 266, 268, 269, 270, 272, 273, 275, 276, 277, 278, 279, 281, 283, 284, 285, 286, 288, 292, 293, 294, 299, 300, 303, 305, 306, 307, 308, 310, 312, 313, 314, 316, 317, 319, 321, 326, 327, 328, 333, 336, 338, 340, 341, 342, 344, 345, 346, 349, 351, 352, 353, 356, 357, 360, 361, 362, 363, 364, 366, 368, 370, 374, 378, 379, 380, 381, 382, 383, 384, 386, 387, 388, 389, 390, 391, 393, 394, 395, 396, 397, 398, 404, 405, 406, 409, 411, 413, 415, 416, 418, 421, 425, 426, 427, 428, 429, 433, 434, 435, 437, 444, 450, 456, 457, 458, 459, 462, 463, 465, 467, 469, 470, 472, 476, 479, 484, 487, 489, 492, 493, 497, 498, 499, 502, 506, 508, 513, 516, 517, 518, 519, 521, 523, 526, 527, 528, 531, 532, 533, 535, 536, 537, 539, 540, 541, 542, 543, 544, 545, 547, 549, 550, 552, 557, 562, 563, 564, 565, 567, 568, 569, 570, 571, 572, 573, 576, 578, 579, 580, 583, 585, 587, 588, 589, 591, 595, 596, 597, 600, 602, 603, 604, 605, 606, 607, 608, 609, 610, 612, 618, 619, 623, 624, 625, 626, 627, 628, 630, 632, 636, 637, 640, 644, 646, 649, 652, 656, 657, 658, 659, 661, 662, 663, 664, 667, 669, 670, 671, 672, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 692, 693, 694, 695, 696, 698, 700, 701, 703, 706, 707, 708, 709, 712, 713, 714, 716, 717, 718, 719, 721, 722, 723, 724, 725, 726, 728, 730, 731, 733, 734, 735, 737, 739, 740, 741, 743, 744, 746, 748, 750, 751, 752, 753, 754, 758, 760, 761, 762, 763, 764, 765, 766, 774, 778, 780, 782, 787, 795, 800, 802, 803, 808, 809, 812, 818, 822, 825, 827, 834, 836, 840, 843, 845, 847, 850, 853, 854, 857, 858, 863, 864, 865, 866, 867, 868, 869, 870, 872, 873, 874, 875, 876, 878, 880, 881, 882, 883, 884, 885, 887, 888, 889, 890, 891, 893, 894, 895, 896, 898, 901, 902, 903, 904, 905, 907, 908, 910, 911, 912, 913, 914, 915, 916, 917, 918, 920, 921, 923, 925, 926, 928, 929, 930, 932, 934, 935, 936, 938, 939, 941, 943, 945, 946, 947, 948, 949, 953, 955, 956, 957, 958, 959, 961, 966, 975, 978, 980, 983, 988, 990, 996, 999} {2, 3, 5, 6, 7, 11, 17, 19, 20, 21, 22, 23, 24, 26, 28, 29, 30, 33, 35, 37, 42, 43, 45, 53, 54, 55, 56, 58, 59, 67, 69, 70, 72, 73, 74, 75, 76, 77, 78, 80, 83, 84, 85, 87, 89, 90, 97, 98, 101, 102, 103, 104, 108, 109, 110, 111, 113, 116, 117, 118, 119, 120, 122, 124, 125, 126, 127, 128, 130, 131, 132, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 146, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 162, 163, 164, 167, 169, 170, 172, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 186, 189, 190, 191, 195, 199, 200, 201, 203, 204, 212, 213, 214, 216, 222, 223, 224, 237, 239, 240, 245, 250, 254, 260, 263, 267, 271, 274, 280, 282, 287, 289, 290, 291, 295, 296, 297, 298, 301, 302, 304, 309, 311, 315, 318, 320, 322, 323, 324, 325, 329, 330, 331, 332, 334, 335, 337, 339, 343, 347, 348, 350, 354, 355, 358, 359, 365, 367, 369, 371, 372, 373, 375, 376, 377, 385, 392, 399, 400, 401, 402, 403, 407, 408, 410, 412, 414, 417, 419, 420, 422, 423, 424, 430, 431, 432, 436, 438, 439, 440, 441, 442, 443, 445, 446, 447, 448, 449, 451, 452, 453, 454, 455, 460, 461, 464, 466, 468, 471, 473, 474, 475, 477, 478, 480, 481, 482, 483, 485, 486, 488, 490, 491, 494, 495, 496, 500, 501, 503, 504, 505, 507, 509, 510, 511, 512, 514, 515, 520, 522, 524, 525, 529, 530, 534, 538, 546, 548, 551, 553, 554, 555, 556, 558, 559, 560, 561, 566, 574, 575, 577, 581, 582, 584, 586, 590, 592, 593, 594, 598, 599, 601, 611, 613, 614, 615, 616, 617, 620, 621, 622, 629, 631, 633, 634, 635, 638, 639, 641, 642, 643, 645, 647, 648, 650, 651, 653, 654, 655, 660, 665, 666, 668, 673, 691, 697, 699, 702, 704, 705, 710, 711, 715, 720, 727, 729, 732, 736, 738, 742, 745, 747, 749, 755, 756, 757, 759, 767, 768, 769, 770, 771, 772, 773, 775, 776, 777, 779, 781, 783, 784, 785, 786, 788, 789, 790, 791, 792, 793, 794, 796, 797, 798, 799, 801, 804, 805, 806, 807, 810, 811, 813, 814, 815, 816, 817, 819, 820, 821, 823, 824, 826, 828, 829, 830, 831, 832, 833, 835, 837, 838, 839, 841, 842, 844, 846, 848, 849, 851, 852, 855, 856, 859, 860, 861, 862, 871, 877, 879, 886, 892, 897, 899, 900, 906, 909, 919, 922, 924, 927, 931, 933, 937, 940, 942, 944, 950, 951, 952, 954, 960, 962, 963, 964, 965, 967, 968, 969, 970, 971, 972, 973, 974, 976, 977, 979, 981, 982, 984, 985, 986, 987, 989, 991, 992, 993, 994, 995, 997, 998}\n" + "{0, 2, 3, 8, 9, 11, 12, 13, 17, 18, 20, 22, 23, 24, 25, 26, 27, 30, 35, 37, 38, 40, 43, 46, 48, 49, 50, 51, 53, 57, 61, 62, 63, 66, 67, 68, 70, 71, 74, 77, 81, 82, 83, 84, 87, 88, 94, 96, 99} {1, 4, 5, 6, 7, 10, 14, 15, 16, 19, 21, 28, 29, 31, 32, 33, 34, 36, 39, 41, 42, 44, 45, 47, 52, 54, 55, 56, 58, 59, 60, 64, 65, 69, 72, 73, 75, 76, 78, 79, 80, 85, 86, 89, 90, 91, 92, 93, 95, 97, 98}\n" ] } ], @@ -571,14 +664,21 @@ "# Calculate the partitions based on the final expectation values\n", "# If the expectation value is positive, the node belongs to partition 0 (par0)\n", "# Otherwise, the node belongs to partition 1 (par1)\n", + "def get_partitions(experiment_result):\n", + " par0, par1 = set(), set()\n", + " best_index = min(\n", + " range(len(experiment_result)),\n", + " key=lambda i: experiment_result[i][\"loss\"],\n", + " )\n", + " for i in experiment_result[best_index][\"exp_map\"]:\n", + " if experiment_result[best_index][\"exp_map\"][i] >= 0:\n", + " par0.add(i)\n", + " else:\n", + " par1.add(i)\n", + " return par0, par1, best_index\n", "\n", - "par0, par1 = set(), set()\n", "\n", - "for i in experiment_result[-1][\"exp_map\"]:\n", - " if experiment_result[-1][\"exp_map\"][i] >= 0:\n", - " par0.add(i)\n", - " else:\n", - " par1.add(i)\n", + "par0, par1, best_index = get_partitions(experiment_result)\n", "print(par0, par1)" ] }, @@ -592,7 +692,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 21, "id": "bd85ceae-ef8b-4e21-b447-e92ca92e06eb", "metadata": {}, "outputs": [ @@ -600,7 +700,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Cut size: 24682\n" + "Cut size: 268\n" ] } ], @@ -620,7 +720,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 22, "id": "b0df38ef-98d0-4d8c-bbdf-75d14e4680f7", "metadata": {}, "outputs": [ @@ -628,16 +728,15 @@ "name": "stdout", "output_type": "stream", "text": [ - "[1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0,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, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1]\n" + "[1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1]\n" ] } ], "source": [ - "best_bits = []\n", "cur_bits = []\n", "\n", - "for i in experiment_result[-1][\"exp_map\"]:\n", - " if experiment_result[-1][\"exp_map\"][i] >= 0:\n", + "for i in experiment_result[best_index][\"exp_map\"]:\n", + " if experiment_result[best_index][\"exp_map\"][i] >= 0:\n", " cur_bits.append(1)\n", " else:\n", " cur_bits.append(0)\n", @@ -646,7 +745,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 23, "id": "232658b0-a86b-4cb5-aff8-c8f2423491b6", "metadata": {}, "outputs": [ @@ -654,34 +753,266 @@ "name":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, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1]\n" ] } ], "source": [ "# Swap the partitions and calculate the cut size\n", - "best_cut = 0\n", - "for edge0, edge1 in graph.edge_list():\n", - " swapped_bits = cur_bits.copy()\n", - " swapped_bits[edge0], swapped_bits[edge1] = (\n", - " swapped_bits[edge1],\n", - " swapped_bits[edge0],\n", - " )\n", "\n", - " cur_partition = [set(), set()]\n", - " for i, bit in enumerate(swapped_bits):\n", - " if bit > 0:\n", - " cur_partition[0].add(i)\n", - " else:\n", - " cur_partition[1].add(i)\n", - " cut_size = calc_cut_size(graph, cur_partition[0], cur_partition[1])\n", - " if best_cut < cut_size:\n", - " best_cut = cut_size\n", - " best_bits = swapped_bits\n", "\n", + "def swap_partitions(graph, cur_bits):\n", + " best_cut = 0\n", + " best_bits = []\n", + " for edge0, edge1 in graph.edge_list():\n", + " swapped_bits = cur_bits.copy()\n", + " swapped_bits[edge0], swapped_bits[edge1] = (\n", + " swapped_bits[edge1],\n", + " swapped_bits[edge0],\n", + " )\n", + "\n", + " cur_partition = [set(), set()]\n", + " for i, bit in enumerate(swapped_bits):\n", + " if bit > 0:\n", + " cur_partition[0].add(i)\n", + " else:\n", + " cur_partition[1].add(i)\n", + " cut_size = calc_cut_size(graph, cur_partition[0], cur_partition[1])\n", + " if best_cut < cut_size:\n", + " best_cut = cut_size\n", + " best_bits = swapped_bits\n", + " return best_cut, best_bits\n", + "\n", + "\n", + "best_cut, best_bits = swap_partitions(graph, cur_bits)\n", "print(best_cut, best_bits)" ] }, + { + "cell_type": "markdown", + "id": "5142e8d0-ef6f-4abb-974e-ed25e5bfe692", + "metadata": {}, + "source": [ + "# Large Scale Hardware Example" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d91c4ac2-3c5b-4afb-aa2c-42d1b50fa674", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "We are using 33 qubits\n", + "We are using the ibm_pittsburgh\n", + "Iter 0: 57399.57543902076\n", + "Iter 1: 56458.787143794\n", + "Iter 2: 40778.45608998947\n", + "Iter 3: 35571.58511146131\n", + "Iter 4: 33861.6835761173\n", + "Iter 5: 39697.22637736274\n", + "Iter 6: 34984.77893767163\n", + "Iter 7: 32051.882157096858\n", + "Iter 8: 26134.153216063707\n", + "Iter 9: 24914.322627065787\n", + "Iter 10: 24030.21227315425\n", + "Iter 11: 23047.463945514\n", + "Iter 12: 22629.42866110748\n", + "Iter 13: 17374.859132614685\n", + "Iter 14: 18020.11637762458\n", + "Iter 15: 17924.7066364044\n", + "Iter 16: 15825.1992250984\n", + "Iter 17: 16553.346711978447\n", + "Iter 18: 12393.565736512377\n", + "Iter 19: 11994.021456089155\n", + "Iter 20: 11199.994322735669\n", + "Iter 21: 9624.895532927634\n", + "Iter 22: 9073.811130188606\n", + "Iter 23: 9836.721241931278\n", + "Iter 24: 10555.925186133794\n", + "Iter 25: 9179.1179493286\n", + "Iter 26: 8495.394826965305\n", + "Iter 27: 8913.688189840399\n", + "Iter 28: 7830.448471810181\n", + "Iter 29: 7757.430542422075\n", + "Iter 30: 6796.187594518731\n", + "Iter 31: 7307.985913766867\n", + "Iter 32: 7340.225833330675\n", + "Iter 33: 7064.731899380469\n", + "Iter 34: 7632.270657372515\n", + "Iter 35: 7049.154710767935\n", + "Iter 36: 7486.118442084411\n", + "Iter 37: 6302.12602219333\n", + "Iter 38: 6244.934230209166\n", + "Iter 39: 7154.9748739261395\n", + "Iter 40: 6482.109600054041\n", + "Iter 41: 5718.475169152395\n", + "Iter 42: 5693.008457857462\n", + "Iter 43: 4869.782667921923\n", + "Iter 44: 4957.625304450959\n", + "Iter 45: 5582.240637063214\n", + "Iter 46: 4983.90082772116\n", + "Iter 47: 5416.268575648202\n", + "Iter 48: 4809.98398457807\n", + "Iter 49: 5092.527306646118\n", + " message: Return from COBYLA because the objective function has been evaluated 50 times.\n", + " success: False\n", + " status: 3\n", + " fun: 5092.527306646118\n", + " x: [ 1.375e+00 1.951e+00 ... 7.259e-01 8.971e-01]\n", + " nfev: 50\n", + "Cut size: 56152\n", + "The best Max-Cut value achieved for a graph with 1500 nodes on 33 qubits is 56219\n", + "and the specific partition we obtained is [1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1,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 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n" + ] + } + ], + "source": [ + "# -------------------------Step 1-------------------------\n", + "\n", + "num_nodes = 1500 # Number of nodes in graph\n", + "graph = rx.undirected_gnp_random_graph(num_nodes, 0.1, seed=seed)\n", + "nx_graph = nx.Graph()\n", + "nx_graph.add_nodes_from(range(num_nodes))\n", + "for edge in graph.edge_list():\n", + " nx_graph.add_edge(edge[0], edge[1])\n", + "\n", + "num_qubits = int(np.ceil((1 + np.sqrt(1 + (8 / 3) * num_nodes)) / 2))\n", + "\n", + "list_size = num_nodes // 3\n", + "node_x = [i for i in range(list_size)]\n", + "node_y = [i for i in range(list_size, 2 * list_size)]\n", + "node_z = [i for i in range(2 * list_size, num_nodes)]\n", + "\n", + "pauli_correlation_encoding_x = build_pauli_correlation_encoding(\n", + " \"X\", node_x, num_qubits\n", + ")\n", + "pauli_correlation_encoding_y = build_pauli_correlation_encoding(\n", + " \"Y\", node_y, num_qubits\n", + ")\n", + "pauli_correlation_encoding_z = build_pauli_correlation_encoding(\n", + " \"Z\", node_z, num_qubits\n", + ")\n", + "print(f\"We are using {num_qubits} qubits\")\n", + "\n", + "# -------------------------Step 2-------------------------\n", + "backend = real_backend\n", + "print(f\"We are using the {backend.name}\")\n", + "qc = efficient_su2(num_qubits, [\"ry\", \"rz\"], reps=2)\n", + "pm = generate_preset_pass_manager(optimization_level=3, backend=backend)\n", + "qc = pm.run(qc)\n", + "# -------------------------Step 3-------------------------\n", + "pce = []\n", + "pce.append(\n", + " [op.apply_layout(qc.layout) for op in pauli_correlation_encoding_x]\n", + ")\n", + "pce.append(\n", + " [op.apply_layout(qc.layout) for op in pauli_correlation_encoding_y]\n", + ")\n", + "pce.append(\n", + " [op.apply_layout(qc.layout) for op in pauli_correlation_encoding_z]\n", + ")\n", + "\n", + "# Run the optimization using a session.\n", + "max_iter = 50\n", + "counter = {\"i\": 0}\n", + "with Session(backend=backend) as session:\n", + " estimator = Estimator(mode=session)\n", + " estimator.options.environment.job_tags = [\"TUT-PCEFQ\"]\n", + " experiment_result = []\n", + "\n", + " def loss_func(x):\n", + " last_x[\"value\"] = x.copy()\n", + " if counter[\"i\"] + 1 > max_iter:\n", + " return last_fun[\"value\"]\n", + " counter[\"i\"] += 1\n", + " val = loss_func_estimator(\n", + " x, qc, [pce[0], pce[1], pce[2]], estimator, graph\n", + " )\n", + " last_fun[\"value\"] = val\n", + " return val\n", + "\n", + " np.random.seed(seed)\n", + " initial_params = np.random.rand(qc.num_parameters)\n", + " result = minimize(\n", + " loss_func, initial_params, method=\"COBYLA\", options={\"rhobeg\": 1.0}\n", + " )\n", + " if counter[\"i\"] >= max_iter:\n", + " result = OptimizeResult(\n", + " message=f\"Return from COBYLA because the objective function has been evaluated {max_iter} times.\",\n", + " success=False,\n", + " status=3,\n", + " fun=last_fun[\"value\"],\n", + " x=last_x[\"value\"],\n", + " nfev=counter[\"i\"],\n", + " )\n", + "print(result)\n", + "\n", + "# -------------------------Step 4-------------------------\n", + "\n", + "par0, par1, best_index = get_partitions(experiment_result)\n", + "cut_size = calc_cut_size(graph, par0, par1)\n", + "print(f\"Cut size: {cut_size}\")\n", + "\n", + "best_bits = []\n", + "cur_bits = []\n", + "for i in experiment_result[best_index][\"exp_map\"]:\n", + " if experiment_result[best_index][\"exp_map\"][i] >= 0:\n", + " cur_bits.append(1)\n", + " else:\n", + " cur_bits.append(0)\n", + "best_cut, best_bits = swap_partitions(graph, cur_bits)\n", + "# Print final solution\n", + "\n", + "print(\n", + " f\"The best Max-Cut value achieved for a graph with {num_nodes} nodes on {num_qubits} qubits is {best_cut}\"\n", + ")\n", + "print(f\"and the specific partition we obtained is {best_bits}\")" + ] + }, + { + "cell_type": "markdown", + "id": "aef4395d-d666-445d-9bdf-94fb19fba72e", + "metadata": {}, + "source": [ + "### Next cell to be deleted after peer-review!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2f768e35-5b03-4792-b18b-e1a704d930b4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Initial cut size: 62005\n" + ] + } + ], + "source": [ + "large_scale_cut_size, large_scale_partition = nx.approximation.one_exchange(\n", + " nx_graph, seed=1\n", + ")\n", + "print(f\"Initial cut size: {large_scale_cut_size}\")" + ] + }, + { + "cell_type": "markdown", + "id": "9d68b120-bf43-4070-b897-013652c824d7", + "metadata": {}, + "source": [ + "## What to look into next\n", + "If you found this work interesting you might be interested in the following material:\n", + "- [Advanced Techniques for QAOA](/docs/tutorials/advanced-techniques-for-qaoa)\n", + "- [Combine error mitigation options with the Estimator primitive](/docs/tutorials/combine-error-mitigation-techniques)" + ] + }, { "cell_type": "markdown", "id": "99f8259d-fb92-4fae-ab68-96b1e50929a0", @@ -703,6 +1034,14 @@ "\n", "[Link to survey](https://your.feedback.ibm.com/jfe/form/SV_8ANZAlsKSFf6DA2)" ] + }, + { + "cell_type": "markdown", + "id": "0c934e9b-2864-4292-94c9-7fa9b5bce007", + "metadata": {}, + "source": [ + "© IBM Corp. 2024-2026" + ] } ], "metadata": { diff --git a/public/docs/images/tutorials/pauli-correlation-encoding-for-qaoa/extracted-outputs/035f6b4a-4de0-452a-b60f-7260f9e3103a-0.avif b/public/docs/images/tutorials/pauli-correlation-encoding-for-qaoa/extracted-outputs/035f6b4a-4de0-452a-b60f-7260f9e3103a-0.avif new file mode 100644 index 00000000000..f9d1923907c Binary files /dev/null and b/public/docs/images/tutorials/pauli-correlation-encoding-for-qaoa/extracted-outputs/035f6b4a-4de0-452a-b60f-7260f9e3103a-0.avif differ diff --git a/public/docs/images/tutorials/pauli-correlation-encoding-for-qaoa/extracted-outputs/37edb718-2bab-49d7-ad66-5f2f67d2aeff-0.avif b/public/docs/images/tutorials/pauli-correlation-encoding-for-qaoa/extracted-outputs/37edb718-2bab-49d7-ad66-5f2f67d2aeff-0.avif index bf4abce0e04..32fc9865ba3 100644 Binary files a/public/docs/images/tutorials/pauli-correlation-encoding-for-qaoa/extracted-outputs/37edb718-2bab-49d7-ad66-5f2f67d2aeff-0.avif and b/public/docs/images/tutorials/pauli-correlation-encoding-for-qaoa/extracted-outputs/37edb718-2bab-49d7-ad66-5f2f67d2aeff-0.avif differ