{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "tcLyUtUqhkTr" }, "source": [ "# 5-OpenJij GPU" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "view-in-github" }, "source": [ "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/OpenJij/OpenJijTutorial/blob/master/source/en/005-OpenJijGPU.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> This document is deprecated. Please go to the next chapter." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Vrd0CwLJhvVQ" }, "source": [ "In this section, we introduce a method of Simulated Quantum Annealing (SQA) with GPU.\n", "\n", "## Setup and installation in Google Colab\n", "\n", "The average PC may not have a CUDA environment, or may not have an NVIDIA GPU. However, with **Google colaboratory**, anyone can use GPGPUs.\n", "\n", "### GPU Settings\n", "\n", "We open Jupyter Notebook file we want to run on GPU with Google Colab.\n", "\n", "```\n", "Runtime > Change runtime type > choose Hardware accelerator GPU\n", "```\n", "\n", "This setting allows you to run Jupyter Notebook on GPU.\n", "\n", "### Installation of OpenJij \n", "\n", "To use GPU modules from OpenJij, one has to build from source codes when installing OpenJij.\n", "We first update CMake, which is needed for the installation, to the latest version." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 173 }, "colab_type": "code", "id": "o8UaiBs1hkwh", "outputId": "3a0075b7-00ea-4cad-9ff1-47b0965e833f" }, "outputs": [], "source": [ "!pip install -U cmake" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "-DuFQsPK90LI" }, "source": [ "After that, we uninstall & reinstall OpenJij with option `--no-binary :all:`, which forces to build from source codes during installation." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 377 }, "colab_type": "code", "id": "s1pjFhAa95Zs", "outputId": "e398ccc6-abca-420b-8e9b-0626ab3283bc" }, "outputs": [], "source": [ "!pip uninstall openjij -y\n", "!pip install openjij --no-binary :all: --no-cache-dir" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "JMgqfmZ-h8X3" }, "source": [ "## Calculation with GPUSQA\n", "\n", "OpenJij GPUs support Monte Carlo calculations on Chimeragraphs. \n", "We make GPUSQASampler instance at first.\n", "If the notebook gives an error \"No modules found. ...\", please try to restart the session so that the reinstalled version of OpenJij is loaded correctly." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": {}, "colab_type": "code", "id": "TYhWp0eqh3Yb" }, "outputs": [], "source": [ "import openjij as oj\n", "\n", "sampler = oj.GPUSQASampler(num_reads=100, unit_num_L=2)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "oOmEWWBxh_3O" }, "source": [ "`unit_num_L` specifies the length of one side of the two-dimensional lattice of chimera-unit cells. \n", "In the example above, we have created a system with 2x2=4 chimera-unit cells.\n", "\n", "Since a chimera-unit cell consists of one 8-bit, the total number of bits is calculated as follows.\n", " \n", "${\\rm total\\_num} = {\\rm unit\\_num\\_L}^2 \\times 8$\n", "\n", "The GPUSQASampler perform SQA calculations for the size of the system of chimeragraphs created here, even if you specify fewer variables, such as sampler_qubo, later. We note that `unit_num_L` is not too large.\n", "Also, `unit_num_L` is not specified in the constructor but is used as an argument to run `sample_qubo`.\n", "\n", "## Execution\n", "\n", "Next we execute SQA. As with Sampler before we introduce, we can use `sample_qubo` (or `sample_ising`).\n", "However, if you specify an interaction for a subscript that does not satisfy the chimeragraph, error occurs." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 1000 }, "colab_type": "code", "id": "_XIsZ9z2h_Rv", "outputId": "be24e99d-b0b9-4766-c686-815ca027694c" }, "outputs": [ { "data": { "text/plain": [ "Response(rec.array([([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1),\n", " ([1, 1, 1, 0], -2., 1), ([1, 1, 1, 0], -2., 1)],\n", " dtype=[('sample', 'i1', (4,)), ('energy', '