{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# QAOA Tasks Tutorial" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem Generation\n", "\n", "First, we generate and save all of the random instances of the problem. This is not computationally intensive but very important to do first so we have a fixed set of random instances." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "### HardwareGridProblemGenerationTask\n", "Generate 'Hardware Grid' problems for a named device.\n", "\n", "This is a subgraph of the device's harware topology with random\n", "+-1 weights on edges.\n", "\n", "#### See Also\n", "`generate_hardware_grid_problem` \n", "\n", "#### Attributes\n", " - `dataset_id`: A unique identifier for this dataset.\n", " - `device_name`: The device to generate problems for.\n", " - `instance_i`: Generate random instances indexed by this number.\n", " - `n_qubits`: Generate an n-qubit instance.\n" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import recirq\n", "from recirq.qaoa.experiments.problem_generation_tasks import \\\n", " SKProblemGenerationTask, HardwareGridProblemGenerationTask, ThreeRegularProblemGenerationTask, \\\n", " generate_3_regular_problem, generate_sk_problem, generate_hardware_grid_problem\n", "\n", "pgen_dataset_id = '2020-03-tutorial'\n", "hardware_grid_problem_tasks = [\n", " HardwareGridProblemGenerationTask(\n", " dataset_id=pgen_dataset_id,\n", " device_name='Sycamore23',\n", " instance_i=i,\n", " n_qubits=n\n", " )\n", " for i in range(5)\n", " for n in range(2, 8 + 1, 2)\n", "]\n", "recirq.display_markdown_docstring(HardwareGridProblemGenerationTask)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "### SKProblemGenerationTask\n", "Generate a Sherrington-Kirkpatrick problem.\n", "\n", "This is a complete (fully-connected) graph with random +-1\n", "weights on edges.\n", "\n", "#### See Also\n", "`generate_sk_problem` \n", "\n", "#### Attributes\n", " - `dataset_id`: A unique identifier for this dataset.\n", " - `instance_i`: Generate random instances indexed by this number.\n", " - `n_qubits`: Generate an n-qubit instance.\n" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sk_problem_tasks = [\n", " SKProblemGenerationTask(\n", " dataset_id=pgen_dataset_id,\n", " instance_i=i,\n", " n_qubits=n\n", " )\n", " for i in range(5)\n", " for n in range(3, 7 + 1, 2)\n", "]\n", "recirq.display_markdown_docstring(SKProblemGenerationTask)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "three_regular_problem_tasks = [\n", " ThreeRegularProblemGenerationTask(\n", " dataset_id=pgen_dataset_id,\n", " instance_i=i,\n", " n_qubits=n\n", " )\n", " for i in range(5)\n", " for n in range(3, 8 + 1) if 3 * n % 2 == 0\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run the tasks" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for task in hardware_grid_problem_tasks:\n", " generate_hardware_grid_problem(task)\n", "for task in sk_problem_tasks:\n", " generate_sk_problem(task)\n", "for task in three_regular_problem_tasks:\n", " generate_3_regular_problem(task)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Angle Precomputation" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "### AnglePrecomputationTask\n", "Pre-compute optimized angles classically for a given problem.\n", "\n", "#### See Also\n", "`precompute_angles` \n", "\n", "#### Attributes\n", " - `dataset_id`: A unique identifier for this dataset.\n", " - `generation_task`: The input task which specifies the problem.\n", " - `p`: QAOA depth hyperparameter p. The number of parameters is 2*p.\n" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from recirq.qaoa.experiments.angle_precomputation_tasks import \\\n", " AnglePrecomputationTask, precompute_angles\n", "\n", "apre_dataset_id = '2020-03-tutorial'\n", "precompute_tasks = [\n", " AnglePrecomputationTask(\n", " dataset_id=apre_dataset_id,\n", " generation_task=gen_task,\n", " p=p)\n", " for gen_task in recirq.roundrobin(\n", " hardware_grid_problem_tasks,\n", " sk_problem_tasks,\n", " three_regular_problem_tasks,\n", " )\n", " for p in range(1, 3 + 1)\n", "]\n", "recirq.display_markdown_docstring(AnglePrecomputationTask)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for task in precompute_tasks:\n", " precompute_angles(task)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Precomputed Angle Data Collection" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "### PrecomputedDataCollectionTask\n", "PrecomputedDataCollectionTask(dataset_id: str, precomputation_task: recirq.qaoa.experiments.angle_precomputation_tasks.AnglePrecomputationTask, device_name: str, n_shots: int, structured: bool = False, echoed: bool = False)" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from recirq.qaoa.experiments.precomputed_execution_tasks import \\\n", " PrecomputedDataCollectionTask, collect_data\n", " \n", "\n", "dcol_dataset_id = '2020-03-tutorial'\n", "data_collection_tasks = [\n", " PrecomputedDataCollectionTask(\n", " dataset_id=dcol_dataset_id,\n", " precomputation_task=pre_task,\n", " device_name='Syc23-simulator',\n", " n_shots=50_000,\n", " structured=True,\n", " )\n", " for pre_task in precompute_tasks\n", "]\n", "recirq.display_markdown_docstring(PrecomputedDataCollectionTask)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "await recirq.execute_in_queue(collect_data, data_collection_tasks, num_workers=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Landscape Data Collection" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "### P1LandscapeDataCollectionTask\n", "Collect data for a p=1 landscape\n", "\n", "This task does exactly one (beta, gamma) point. You will have\n", "to run many of these tasks to get a full landscape\n", "\n", "#### See Also\n", "`collect_p1_landscape_data` \n", "\n", "#### Attributes\n", " - `dataset_id`: A unique identifier for this dataset.\n", " - `generation_Task`: The task specifying the problem to collect data for\n", " - `device_name`: The device to run on\n", " - `n_shots`: The number of shots to take\n", " - `gamma`: The problem unitary parameter gamma\n", " - `beta`: The driver unitary parameter beta\n", " - `line_placement_strategy`: Only used for SK model problems. Options include 'brute_force', 'random', 'greedy', 'anneal', 'mst', and 'mixed'.\n" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from recirq.qaoa.experiments.p1_landscape_tasks import \\\n", " P1LandscapeDataCollectionTask, \\\n", " get_data_collection_tasks_on_a_grid, \\\n", " collect_either_landscape_or_cal\n", "\n", "recirq.display_markdown_docstring(P1LandscapeDataCollectionTask)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hardware_grid_problem_task = HardwareGridProblemGenerationTask(\n", " dataset_id=pgen_dataset_id,\n", " device_name='Sycamore23',\n", " instance_i=0,\n", " n_qubits=4\n", ")\n", "data_collection_tasks = get_data_collection_tasks_on_a_grid(\n", " pgen_task=hardware_grid_problem_task,\n", " dataset_id=dcol_dataset_id,\n", " gamma_res=11,\n", " beta_res=11,\n", " device_name='Syc23-simulator',\n", " epoch=\"grid\")\n", "\n", "await recirq.execute_in_queue(collect_either_landscape_or_cal,\n", " data_collection_tasks,\n", " num_workers=2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sk_problem_task = SKProblemGenerationTask(\n", " dataset_id=pgen_dataset_id,\n", " instance_i=0,\n", " n_qubits=3,\n", ")\n", "data_collection_tasks = get_data_collection_tasks_on_a_grid(\n", " pgen_task=sk_problem_task,\n", " dataset_id=dcol_dataset_id,\n", " gamma_res=11,\n", " beta_res=11,\n", " device_name='Syc23-simulator',\n", " epoch=\"sk\")\n", "await recirq.execute_in_queue(collect_either_landscape_or_cal,\n", " data_collection_tasks,\n", " num_workers=2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "three_regular_problem_task = ThreeRegularProblemGenerationTask(\n", " dataset_id=pgen_dataset_id,\n", " instance_i=0,\n", " n_qubits=4\n", ")\n", "data_collection_tasks = get_data_collection_tasks_on_a_grid(\n", " pgen_task=three_regular_problem_task,\n", " dataset_id=dcol_dataset_id,\n", " device_name='Syc23-simulator',\n", " gamma_res=11,\n", " beta_res=11,\n", " epoch=\"tr\")\n", "\n", "await recirq.execute_in_queue(collect_either_landscape_or_cal,\n", " data_collection_tasks,\n", " num_workers=2)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }