|
13 | 13 | "id": "c8374048-2ead-45c4-ada1-724c73532cf7", |
14 | 14 | "metadata": {}, |
15 | 15 | "source": [ |
16 | | - "This notebook demonstrates the QAOA algorithm using Classiq. It covers the Max-Cut problem as an example of a combinatorial optimization problem, showcasing how QAOA can be applied to graph-based optimization tasks [[1](#QAOA)]." |
| 16 | + "This notebook demonstrates the QAOA algorithm using Classiq. It covers the Max-Cut problem as an example of a combinatorial optimization problem, showcasing how QAOA can be applied to unconstrained combinatorial optimization tasks [[1](#QAOA)]." |
17 | 17 | ] |
18 | 18 | }, |
19 | 19 | { |
|
106 | 106 | "\n", |
107 | 107 | "The QAOA algorithm comprises two key layers:\n", |
108 | 108 | "\n", |
109 | | - "1. **Objective Phase (Analog):** \n", |
| 109 | + "1. **Cost Layer:** \n", |
110 | 110 | " A phase rotation in the $Z$-direction is applied based on the cost function:\n", |
111 | 111 | " $$\n", |
112 | 112 | " |x\\rangle \\xrightarrow{U_{\\text{o}}(\\gamma)} e^{i\\gamma f_{\\text{obj}}(x)} |x\\rangle,\n", |
113 | 113 | " $$\n", |
114 | | - " where the objective function $f_{\\text{obj}}(x)$ is defined in [Cell 2](#cell-2) below.\n", |
| 114 | + " which encodes the cost associated with a given partition.\n", |
115 | 115 | "\n", |
116 | | - "2. **Mixing Operator (Digital):** \n", |
| 116 | + "2. **Mixer Layer:** \n", |
117 | 117 | " An $X$-rotation is applied to all qubits:\n", |
118 | 118 | " $$\n", |
119 | 119 | " U_B(\\beta) = e^{-i\\beta H_B}, \\quad \\text{with } H_B = \\sum_i X_i.\n", |
120 | 120 | " $$\n", |
121 | | - " This operator drives transitions between computational basis states, enabling exploration of different candidate partitions.\n", |
| 121 | + " This layer drives transitions between computational basis states based on the phases that were assigned by the previous layer.\n", |
122 | 122 | "\n", |
123 | 123 | "By alternating these layers, the QAOA ansatz iteratively refines the quantum state toward a partition that minimizes the Hamiltonian (and thus maximizes the number of cut edges).\n" |
124 | 124 | ] |
|
188 | 188 | "metadata": {}, |
189 | 189 | "outputs": [], |
190 | 190 | "source": [ |
191 | | - "graph_nodes = [0, 1, 2, 3, 4]\n", |
192 | 191 | "graph_edges = [(0, 1), (0, 2), (1, 2), (1, 3), (2, 4), (3, 4)]" |
193 | 192 | ] |
194 | 193 | }, |
|
223 | 222 | "\n", |
224 | 223 | "# Create a graph instance\n", |
225 | 224 | "G = nx.Graph()\n", |
226 | | - "G.add_nodes_from(graph_nodes)\n", |
227 | 225 | "G.add_edges_from(graph_edges)\n", |
228 | 226 | "\n", |
229 | 227 | "# Use a layout for better visualization\n", |
|
371 | 369 | }, |
372 | 370 | { |
373 | 371 | "cell_type": "code", |
374 | | - "execution_count": 6, |
| 372 | + "execution_count": 14, |
375 | 373 | "id": "4fc29cc0-46ce-4a05-ae3b-1e6d0ae8b886", |
376 | 374 | "metadata": {}, |
377 | 375 | "outputs": [], |
378 | 376 | "source": [ |
379 | 377 | "@qfunc\n", |
380 | 378 | "def qaoa_ansatz(\n", |
381 | 379 | " cost_layer: QCallable[CReal, QArray[QBit]],\n", |
382 | | - " num_layers: CInt,\n", |
383 | 380 | " gammas: CArray[CReal],\n", |
384 | 381 | " betas: CArray[CReal],\n", |
385 | 382 | " qba: QArray[QBit],\n", |
386 | 383 | "):\n", |
387 | 384 | " repeat(\n", |
388 | | - " num_layers,\n", |
| 385 | + " betas.len,\n", |
389 | 386 | " lambda i: [\n", |
390 | 387 | " cost_layer(gammas[i], qba),\n", |
391 | 388 | " mixer_layer(betas[i], qba),\n", |
|
406 | 403 | "id": "32903984-f50b-4580-909a-413823932c90", |
407 | 404 | "metadata": {}, |
408 | 405 | "source": [ |
409 | | - "As in vanilla QAOA, the quantum program first applies a `hadamard_transform()` to prepare the qubits in a uniform superposition. After this initial state preparation, the circuit sequentially applies the cost and mixer layers, each with their own parameters that are updated by the classical optimization loop as we will show in the next section." |
| 406 | + "The quantum program first applies a `hadamard_transform` to prepare the qubits in a uniform superposition. After this initial state preparation, the circuit sequentially applies the cost and mixer layers, each with their own parameters that are updated by the classical optimization loop as we will show in the next section." |
410 | 407 | ] |
411 | 408 | }, |
412 | 409 | { |
413 | 410 | "cell_type": "code", |
414 | | - "execution_count": 7, |
| 411 | + "execution_count": 15, |
415 | 412 | "id": "e6518b27-5e9e-4d8a-b880-60c76612d5db", |
416 | 413 | "metadata": {}, |
417 | 414 | "outputs": [], |
|
430 | 427 | " hadamard_transform(v) # Prepare a uniform superposition\n", |
431 | 428 | " qaoa_ansatz(\n", |
432 | 429 | " maxcut_cost_layer,\n", |
433 | | - " NUM_LAYERS,\n", |
434 | 430 | " params[0:NUM_LAYERS],\n", |
435 | 431 | " params[NUM_LAYERS : 2 * NUM_LAYERS],\n", |
436 | 432 | " v,\n", |
|
447 | 443 | }, |
448 | 444 | { |
449 | 445 | "cell_type": "code", |
450 | | - "execution_count": 8, |
| 446 | + "execution_count": 16, |
451 | 447 | "id": "fb2ad242-4ac2-4fc1-befd-65f8c8aca582", |
452 | 448 | "metadata": {}, |
453 | 449 | "outputs": [ |
454 | 450 | { |
455 | 451 | "name": "stdout", |
456 | 452 | "output_type": "stream", |
457 | 453 | "text": [ |
458 | | - "Opening: https://platform.classiq.io/circuit/2u7p64Ns4kjBako0sk6ONbGvXZD?version=0.70.0\n" |
| 454 | + "Opening: https://platform.classiq.io/circuit/2uGTpY3U86Dj3PPQN0Zzpxx2Oeh?version=0.70.0\n" |
459 | 455 | ] |
460 | 456 | } |
461 | 457 | ], |
|
0 commit comments