{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Y4hVgt6yQyOa" }, "source": [ "# 1-An Introduction to OpenJij" ] }, { "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/001-Introduction.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "OpenJij is a heuristic optimization library of the Ising model and QUBO.\n", "It has a Python interface, therefore it can be easily written in Python, although the core part of the optimization is implemented with C++.\n", "\n", "Let's install OpenJij by pip. \n", "(If you're using Colab, you'll need to install cmake first ``$ !pip install cmake``.)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "colab": {}, "colab_type": "code", "id": "JZW9IAE9QyOd" }, "outputs": [], "source": [ "# !pip install openjij" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "x9a_Imk1QyOn" }, "source": [ "### The Ising model\n", "\n", "The Ising model is a model treated in physics and is written as follows.\n", "\n", "$$H(\\{\\sigma_i\\}) = \\sum_{i > j} J_{ij}\\sigma_i \\sigma_j + \\sum_{i=1}^N h_i \\sigma_i$$\n", "$$\\sigma_i \\in \\{-1, 1\\}, i=1,\\cdots N$$\n", "\n", "$H(\\{\\sigma_i\\})$ is called Hamiltonian, it is like an energy or a cost function. \n", "$\\sigma_i$ is a binary variable it takes only two values $\\{1, -1\\}$.\n", "\n", "> $\\sigma_i$ corresponds to the physical quantity called spin in physics, it is sometimes called a spin variable or spin simply.\n", "> Spin seems a micro magnet. The value of this variable corresponds to the physical state (the orientation of the magnet).\n", "> $\\sigma_i = -1$ means the magnet is up and 1 means magnet is down.\n", "\n", "\n", "$H$ depends on the combination of variables $\\{\\sigma_i\\} = \\{\\sigma_1, \\sigma_2, \\cdots, \\sigma_N\\}$. \n", "$J_{ij}, h_i$ represents the problem, and they are called interaction coefficients, longitudinal magnetic fields respectively.\n", "\n", "OpenJij is a library which searches a combination of spin variables $\\{\\sigma_i\\}$ that minimizes $H(\\{\\sigma_i\\})$ when given $J_{ij}$ and $h_i$.\n", "\n", "Let me show you a example." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "j0iZqoWtQyOn" }, "source": [ "## Solve a problem with OpenJij\n", "\n", "We assume that the number of variables is $N=5$, \n", "and the longitudinal field and interaction are\n", "\n", "$$h_i = -1~\\text{for} ~\\forall i, ~ J_{ij} = -1~\\text{for} ~\\forall i, j$$\n", "\n", "respectively.\n", "\n", "In this case, since all interaction coefficients are negative, the energy is lower when each spin variable takes the same value.\n", "Also, since all longitudinal magnetic fields are negative, the energy is lower when each spin takes 1.\n", "Therefore, we can predict the answer of this problem is $\\{\\sigma_i\\} = \\{1, 1, 1, 1, 1\\}$.\n", "\n", "Now let's solve the Ising model using OpenJij." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "h_i: {0: -1, 1: -1, 2: -1, 3: -1, 4: -1}\n", "Jij: {(0, 1): -1, (0, 2): -1, (0, 3): -1, (0, 4): -1, (1, 2): -1, (1, 3): -1, (1, 4): -1, (2, 3): -1, (2, 4): -1, (3, 4): -1}\n" ] } ], "source": [ "import openjij as oj\n", "\n", "# Create interaction coefficients and longitudinal magnetic fields that represent the problem.\n", "# OpenJij accepts problems in dictionary type.\n", "\n", "N = 5\n", "h = {i: -1 for i in range(N)}\n", "J = {(i, j): -1 for i in range(N) for j in range(i+1, N)}\n", "\n", "print('h_i: ', h)\n", "print('Jij: ', J)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 1 1 1 1]]\n", "[{0: 1, 1: 1, 2: 1, 3: 1, 4: 1}]\n" ] } ], "source": [ "# First, create an instance of Sampler to solve the problem.\n", "sampler = oj.SASampler(num_reads=1)\n", "# Solve the problem(h, J) by using the method of sampler.\n", "response = sampler.sample_ising(h, J)\n", "\n", "# The results (states) of calculation is in result.states.\n", "print(response.states)\n", "\n", "# If you want to see the result with subscript, use response.samples.\n", "print([s for s in response.samples()])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When dealing with large problem, you can also use numpy-based interface. The following form of matrix is assumed:\n", "\n", "$$ \\begin{pmatrix}\n", "h_{0} & J_{0,1} & \\cdots & J_{0,N-1}\\\\\n", "J_{1,0} & h_{1} & \\cdots & J_{1,N-1}\\\\\n", "\\vdots & \\vdots & \\vdots & \\vdots\\\\\n", "J_{N-1,0} & J_{N-1,1} & \\cdots & h_{N-1}\\\\\n", "\\end{pmatrix} $$" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "#!pip install numpy -U" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-1. -0.5 -0.5 -0.5]\n", " [-0.5 -1. -0.5 -0.5]\n", " [-0.5 -0.5 -1. -0.5]\n", " [-0.5 -0.5 -0.5 -1. ]]\n", "BinaryQuadraticModel({3: -1.0, 2: -1.0, 1: -1.0, 0: -1.0}, {(1, 2): -1.0, (2, 3): -1.0, (1, 3): -1.0, (0, 3): -1.0, (0, 2): -1.0, (0, 1): -1.0}, 0.0, Vartype.SPIN, sparse=False)\n", "[[1 1 1 1]]\n" ] } ], "source": [ "import numpy as np\n", "mat = np.array([[-1,-0.5,-0.5,-0.5],[-0.5,-1,-0.5,-0.5],[-0.5,-0.5,-1,-0.5],[-0.5,-0.5,-0.5,-1]])\n", "print(mat)\n", "\n", "bqm = oj.BinaryQuadraticModel.from_numpy_matrix(mat, vartype='SPIN')\n", "print(bqm)\n", "\n", "sampler = oj.SASampler(num_reads=1)\n", "response = sampler.sample(bqm)\n", "\n", "print(response.states)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Explanation of OpenJij\n", "\n", "Then, I will explain the code showed above.\n", "OpenJij has two interfaces currently. One of them used above is the same interface as D-Wave Ocean.\n", "Therefore, if you get used to using OpenJij, easy changing to Ocean.\n", "\n", "> Another interface will not be explained here, but it can be easily extended by using the structure of OpenJij (graph, updater, and algorithm) directly. \n", "> In the present situation, however, it would be sufficient to be able to use the interface which treated in the cell above." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sampler\n", "\n", "In the above, we created an instance of Sampler after creating the problem in the dictionary type.\n", "\n", "```python\n", "sampler = oj.SASampler(num_reads=1)\n", "```\n", "\n", "Here, this Sampler is the object of selecting what algorithm and hardware you use.\n", "When you want to try the other algorithm, change this Sampler.\n", "\n", "> The algorithm treated in OpenJij is a heuristic and stochastic algorithm, it may return the different solutions each time the problem is solved, so it may not always obtain the optimal solution. \n", "> Therefore, we solve the problem many times and search for a good solution. So we call Sampler which means that we sample the solutions.\n", "\n", "``SASampler`` treated in the above cell is a sampler that samples the solution by using an algorithm called ``Simulated annealing``.\n", "For example, we can choose other samples below.\n", "\n", "- SQASampler: An algorithm for simulating quantum annealing called Simulated Quantum Annealing (SQA) with a classical computer.\n", "\n", "- GPUSQASampler: It runs SQA on GPU. It can treat only a special structure called Chimera graph." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### sample_ising(h, J)\n", "\n", "When you want to solve the Ising model, you can use ``.sample_ising(h, J)``.\n", "As I will explain later, when you treat QUBO which equivalent to the Ising model optimization, use ``.sample_qubo(Q)``.\n", "\n", "\n", "### Response\n", "\n", "``.sample_ising(h, J)`` returns the Response class.\n", "This class contains results of solutions and energies that Sampler solved.\n", "\n", "- .states:\n", " - type: list (list (int))\n", " - This list contains solutions to the number of iteration. > In physics, an array of spins called a state. Since .states stores the solutions to the number of iteration, we defined as .states from the meaning that multiple states are stored.\n", "\n", "- .energies:\n", " - type: list (float)\n", " - This list contains the energy of each solution to the number of iteration.\n", "\n", "The Response class inherits the SampleSet class of D-Wave's dimod. More detailed information can be found at the following links\n", "[SampleSet: dimod documantation](https://docs.ocean.dwavesys.com/projects/dimod/en/latest/reference/sampleset.html).\n", "\n", "Let's look at the acutual code." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'a': 1, 'b': 1, 'c': -1}\n", "-4.0\n" ] } ], "source": [ "# keys of h, J dictionaries can treat not only numbers.\n", "h = {'a': -1, 'b': -1}\n", "J = {('a', 'b'): -1, ('b', 'c'): 1}\n", "# # Try solving 10 times by SA at a time. With the argument called num_reads.\n", "sampler = oj.SASampler(num_reads=10) \n", "response = sampler.sample_ising(h, J)\n", "print(response.first.sample)\n", "print(response.first.energy)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 1 -1]\n", " [ 1 1 -1]\n", " [ 1 1 -1]\n", " [ 1 1 -1]\n", " [ 1 1 -1]\n", " [ 1 1 -1]\n", " [ 1 1 -1]\n", " [ 1 1 -1]\n", " [ 1 1 -1]\n", " [ 1 1 -1]]\n" ] } ], "source": [ "print(response.states)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can see that the response.states contains 10 solutions when you open it.\n", "Since this problem is easy, returned the same answer is ``[1, -1, 1]`` every iterations." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "4mekWlvTQyO4" }, "source": [ "> The parameters passed in the constructor such as ``num_reads`` can be overwritten by the method that executes sampling such as ``.sample_ising``.\n", ">\n", "> ```\n", "> response = sampler.sample_ising(h, J, num_reads=2)\n", "> response.states\n", "> > [[1, 1, -1],[1, 1, -1]]\n", "> ```" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-4., -4., -4., -4., -4., -4., -4., -4., -4., -4.])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Next, let's look the energies.\n", "response.energies" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "NYAnma_WQyO7" }, "source": [ "Of course, the energies also take the same value every iterations.\n", "The solutions in response.states are list type, so you confuse the corresponding to the strings ``\"a\", \"b\", \"c\"`` when you set the problem.\"\n", "That is in response.variables." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Variables(['a', 'b', 'c'])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response.variables" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "5rf7wB7LQyPJ" }, "source": [ "``.first`` is useful if you only want to know the state with the lowest energy value." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "colab": {}, "colab_type": "code", "id": "-J1utQATQyPK", "outputId": "839e1260-3b1c-432d-a4b6-7b318ba1048c" }, "outputs": [ { "data": { "text/plain": [ "Sample(sample={'a': 1, 'b': 1, 'c': -1}, energy=-4.0, num_occurrences=1)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response.first" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Let's try to solve QUBO\n", "\n", "When you want to solve social problems,\n", "it is often easier to formulate as QUBO(Quadratic Unconstraited Binary Optimization, ) than the Ising model.\n", "Basically, it returns the same solution when using the Ising model.\n", "\n", "> Also, QUBO is often called as UBQP (Unconstraited binary quadratic optimization problems).\n", "\n", "QUBO is written as follows.\n", "\n", "$$H(\\{q_i\\}) = \\sum_{i\\geq j} Q_{ij}q_i q_j$$\n", "$$q_i \\in \\{0, 1\\}$$\n", "\n", "Binary variables take 0 or 1, that is difference between QUBO and the Ising model.\n", "\n", "There are other ways to define $\\sum$ and $Q_{ij}$ (for example, make $Q_{ij}$ a symmetric matrix), but in this time we have formulated it as above.\n", "\n", "> The Ising model and QUBO are equivalent for the reason why they be converted to each other. You can convert these equation: $q_i=(\\sigma_i + 1)/2$\n", "\n", "In the case of QUBO, you search a combination of $\\{q_i\\}$ that minimizes $H(\\{q_i\\})$ under the $Q_{ij}$ which is given by a problem.\n", "\n", "Also, since $q_i^2 = q_i$ (because $q_i$ takes only 0 or 1),\n", "you can write as fllows\n", "\n", "$$H(\\{q_i\\}) = \\sum_{i > j} Q_{ij}q_i q_j + \\sum_i Q_{ii} q_i$$\n", "\n", "Namely, the diagonal components of $Q_{ij}$ is corresponds to the coefficients of the first-order term of $q_i$.\n", "\n", "Let's solve QUBO with OpenJij" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 1 0]]\n" ] } ], "source": [ "# Q_ij (dict)\n", "Q = {(0, 0): -1, (0, 1): -1, (1, 2): 1, (2, 2): 1}\n", "sampler = oj.SASampler()\n", "# use .sample_qubo to solve QUBO\n", "response = sampler.sample_qubo(Q)\n", "print(response.states)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "CxbDNkSuQyPU" }, "source": [ "In QUBO, the solutions are made by 0 or 1 because the variables take 0 or 1." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "8IRw4pHOQyPV" }, "source": [ "## Let's solve a little difficult problem\n", "\n", "You want to solve a problem of QUBO which has $Q_{ij}$ given by 50 random variables." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "N = 100\n", "# Create Q_ij randomly\n", "import random\n", "Q = {(i, j): random.uniform(-1, 1) for i in range(N) for j in range(i+1, N)}\n", "\n", "# Solve by OpenJij\n", "sampler = oj.SASampler()\n", "response = sampler.sample_qubo(Q, num_reads=100)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-192.88732848, -192.88732848, -192.88732848, -192.88732848,\n", " -192.88732848])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Look at some energies.\n", "response.energies[:5]" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "syclwqyyQyPg" }, "source": [ "You will realize that the energies takes a different values than before.\n", "If you make $Q_{ij}$ randomly, the problem becomes difficult generally.\n", "Therefore, SASampler does not necessarily give the same solution every time.\n", "Now let's look at the histogram of energies." ] }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ey0jTZhhIU7OC5oTxquq/H/irgXcm2WPcOYP9gQU9lr0Q+ATNg3SexRb2QB3NHoaBRtG8JK/uMf37fWzjBOAq4OIkZ9LsDewBHAKcXVVXbGTds4D/CnwpyYk0VxWdSHOY6MnxC1bVY0k+BxwDnFNVD/RRo7TZPIGsUbQ9cGWP13s2dwNVdQvNo0AfpXkG7j/S/IP+OJu4k7mqHqW5A/onwHk0wfIBmvMUvR6S/vft+5mbW5/UL590Js0C7WWttwBLquqsCfNOpXnm8t5V9WSv9aXp8jCRNATtCeEfAKtpbjb7IM1hoi+MW2ZfYD/gvcCJBoEGyTCQhqNoblz7RZpDS18H/ryqxh8m+jTwKmA58PHOK9RI8TCRJMkTyJIkw0CShGEgScIwkCRhGEiSgP8PdZj49OXKj/0AAAAASUVORK5CYII=\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "import matplotlib.pyplot as plt\n", "plt.hist(response.energies, bins=15)\n", "plt.xlabel('Energy', fontsize=15)\n", "plt.ylabel('Frequency', fontsize=15)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "K61nEm6fQyPl" }, "source": [ "The lower the energy, the better, but you can find that the high energy solutions appear sometimes.\n", "However, almost half of them seem to have the lowest energy.\n", "\n", "Openjij solved the problem 100 times.\n", "Since the optimal solution is the lowest energy,\n", "the lowest energy solution among the solved (sampled) solutions should be close to the optimal solution.\n", "Therefore, we search for the solution from ``.states``.\n", "\n", "> Caution: SA can not always give an optimal solution.\n", "Therefore, there is no guarantee that the solution with the lowest energy in this time is an optimal solution.\n", "It is an approximate solution.\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Sample(sample={0: 1, 1: 0, 2: 1, 3: 1, 4: 1, 5: 0, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 0, 12: 0, 13: 1, 14: 0, 15: 0, 16: 0, 17: 1, 18: 1, 19: 0, 20: 1, 21: 1, 22: 1, 23: 1, 24: 0, 25: 1, 26: 1, 27: 0, 28: 1, 29: 1, 30: 1, 31: 1, 32: 0, 33: 1, 34: 0, 35: 1, 36: 0, 37: 0, 38: 1, 39: 1, 40: 0, 41: 1, 42: 0, 43: 1, 44: 0, 45: 0, 46: 0, 47: 1, 48: 0, 49: 1, 50: 0, 51: 0, 52: 0, 53: 1, 54: 1, 55: 0, 56: 1, 57: 1, 58: 1, 59: 1, 60: 1, 61: 1, 62: 1, 63: 1, 64: 0, 65: 1, 66: 1, 67: 1, 68: 1, 69: 1, 70: 1, 71: 1, 72: 0, 73: 1, 74: 1, 75: 1, 76: 0, 77: 1, 78: 1, 79: 1, 80: 1, 81: 1, 82: 1, 83: 0, 84: 1, 85: 0, 86: 1, 87: 1, 88: 1, 89: 0, 90: 0, 91: 1, 92: 1, 93: 0, 94: 0, 95: 0, 96: 1, 97: 1, 98: 0, 99: 1}, energy=-192.88732847679702, num_occurrences=1)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "min_samples = response.first\n", "min_samples" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "XBp8gWZEQyPr" }, "source": [ "Now you get the lowest energy solution.\n", "The solution which is contained in this min_states is the approximate solution in this time.\n", "Contained solutions have the same energy,\n", "but the arrangement of spins may differ in some cases (In the case of \"degenerated state\").\n", "\n", "So, after choosing the lowest energy state as above, choose the spin orientation from min_state as you like.\n", "Then, we solved the problem approximately.\n", "\n", "Next section, “2-Evaluation”, we will explain the index(\"time to solution\" and \"the residual energy\" etc.) for evaluate the solution." ] } ], "metadata": { "colab": { "include_colab_link": true, "name": "1-Introduction.ipynb", "provenance": [], "version": "0.3.2" }, "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.9.3" } }, "nbformat": 4, "nbformat_minor": 4 }