Skip to content

Instantly share code, notes, and snippets.

@mohzeki222
Created June 1, 2021 05:57
Show Gist options
  • Save mohzeki222/9693bf93bf10c9966a123d89554baa1a to your computer and use it in GitHub Desktop.
Save mohzeki222/9693bf93bf10c9966a123d89554baa1a to your computer and use it in GitHub Desktop.
chapter4.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "chapter4.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyOvGq9tt4DkQU7sT7wp/1NN",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/mohzeki222/9693bf93bf10c9966a123d89554baa1a/chapter4.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_1heBa6UIaVs"
},
"source": [
"### 続きをやれる環境作りをしましょう\n",
"\n",
"いつも通りD-Wave Ocean SDKの準備から外部データの読み込み準備までしましょう。"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"id": "JjKjmzlNxNOY",
"outputId": "32fe6d49-ac71-425a-a5ec-0362e7cfe422"
},
"source": [
"pip install dwave-ocean-sdk"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"Collecting dwave-ocean-sdk\n",
" Downloading https://files.pythonhosted.org/packages/8f/d8/aafd6cbd30c3e2a49e13621d021d5a964c21ee48f61b539c21d7b20c4e39/dwave_ocean_sdk-3.3.0-py3-none-any.whl\n",
"Collecting dwave-inspector==0.2.5\n",
" Downloading https://files.pythonhosted.org/packages/25/0b/b6600648c8f721bb957f2d337191e018b437df1fbb33f7bc1e5d8b041a51/dwave_inspector-0.2.5-py3-none-any.whl\n",
"Collecting dwave-system==1.4.0\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/1b/3c/1c61e054f9753239fc9a592fbe81646503d7331c3df158db0889df5473db/dwave_system-1.4.0-py3-none-any.whl (82kB)\n",
"\u001b[K |████████████████████████████████| 92kB 7.0MB/s \n",
"\u001b[?25hCollecting pyqubo==1.0.10\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/b7/8b/1a8d7ef9ec02fca382d4108779d376058a2a5bc8789891ee71f433b4efe7/pyqubo-1.0.10-cp37-cp37m-manylinux1_x86_64.whl (788kB)\n",
"\u001b[K |████████████████████████████████| 788kB 22.0MB/s \n",
"\u001b[?25hCollecting penaltymodel-mip==0.2.4; platform_machine == \"x86_64\" or platform_machine == \"amd64\" or platform_machine == \"AMD64\"\n",
" Downloading https://files.pythonhosted.org/packages/f5/d1/e73cb7f9fdcbe0023c49f2916f57f345654dec6f44567ee91949ac53a4d3/penaltymodel_mip-0.2.4-py3-none-any.whl\n",
"Collecting dwave-hybrid==0.6.1\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/01/91/52bd173354c6c7ab60702238e661c8d9701752194f5cdf75b6ce6ba9e5e0/dwave_hybrid-0.6.1-py3-none-any.whl (63kB)\n",
"\u001b[K |████████████████████████████████| 71kB 8.9MB/s \n",
"\u001b[?25hCollecting dwave-greedy==0.1.2\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/1b/d8/33a03b1fe60483e0ec987767af54d2336076ff9e3b66c7cfe703639e7573/dwave_greedy-0.1.2-cp37-cp37m-manylinux1_x86_64.whl (405kB)\n",
"\u001b[K |████████████████████████████████| 409kB 42.1MB/s \n",
"\u001b[?25hCollecting dimod==0.9.13\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/6c/fa/435d50ad5914861217c51944a7245373c2018aa71d3a66a3b8ea1bd32a00/dimod-0.9.13-cp37-cp37m-manylinux1_x86_64.whl (7.0MB)\n",
"\u001b[K |████████████████████████████████| 7.0MB 40.9MB/s \n",
"\u001b[?25hCollecting dwave-tabu==0.3.1\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/a3/65/e77a0e70ae12bfdb76517e2b5f79fe2bf4bafb70460d405a64a4f210c6d7/dwave_tabu-0.3.1-cp37-cp37m-manylinux1_x86_64.whl (790kB)\n",
"\u001b[K |████████████████████████████████| 798kB 41.6MB/s \n",
"\u001b[?25hCollecting penaltymodel-cache==0.4.3\n",
" Downloading https://files.pythonhosted.org/packages/12/24/d76f4ef9c162c0d2dd3404ba1dfff3be7af1d6ac689d67749b49349b8f80/penaltymodel_cache-0.4.3-py3-none-any.whl\n",
"Collecting dwave-qbsolv==0.3.2\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/4f/3a/5bcfd681ed141866dc7a0e5b58de790236b6e7896cade92ecad16c1a3dca/dwave_qbsolv-0.3.2-cp37-cp37m-manylinux1_x86_64.whl (204kB)\n",
"\u001b[K |████████████████████████████████| 204kB 55.8MB/s \n",
"\u001b[?25hCollecting penaltymodel==0.16.4\n",
" Downloading https://files.pythonhosted.org/packages/ae/78/86e98f2d154a3420b72c436b49a2b7369ab551cf27a8ced50ca6a8334e64/penaltymodel-0.16.4-py3-none-any.whl\n",
"Collecting dwave-neal==0.5.7\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/77/9e/033d58e47d4581ce7b92c00927d22c410b574c3c3b399d4c621e2a768d0c/dwave_neal-0.5.7-cp37-cp37m-manylinux1_x86_64.whl (397kB)\n",
"\u001b[K |████████████████████████████████| 399kB 43.6MB/s \n",
"\u001b[?25hCollecting dwave-cloud-client==0.8.4\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/59/fb/252345f02bc39d8d87861434429e8a440cb1bb71af30a6859be2350ef94a/dwave_cloud_client-0.8.4-py3-none-any.whl (87kB)\n",
"\u001b[K |████████████████████████████████| 92kB 11.5MB/s \n",
"\u001b[?25hCollecting penaltymodel-lp==0.1.4\n",
" Downloading https://files.pythonhosted.org/packages/70/53/ca76392b6dcd136eace77ab940704138443d30501d22a982646c89c559d8/penaltymodel_lp-0.1.4-py3-none-any.whl\n",
"Collecting dwave-networkx==0.8.8\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/34/51/feeaa00ae0ccfb6a56731d917f998bc9606f0db2a9719b9b4947a206defc/dwave_networkx-0.8.8-py2.py3-none-any.whl (81kB)\n",
"\u001b[K |████████████████████████████████| 81kB 10.3MB/s \n",
"\u001b[?25hCollecting dwavebinarycsp==0.1.2\n",
" Downloading https://files.pythonhosted.org/packages/e6/f2/8422100bfa1a9cce5c0f3ca8b088a3d1f46582c0085adb2db09837dbc0f1/dwavebinarycsp-0.1.2-py3-none-any.whl\n",
"Collecting minorminer==0.2.5\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/62/ca/158c0ceb98a4633360e810964fbacd87e328a598d80296a88ebd93b252f9/minorminer-0.2.5-cp37-cp37m-manylinux1_x86_64.whl (3.3MB)\n",
"\u001b[K |████████████████████████████████| 3.3MB 31.9MB/s \n",
"\u001b[?25hRequirement already satisfied: importlib-resources>=3.2.0; python_version < \"3.9\" in /usr/local/lib/python3.7/dist-packages (from dwave-inspector==0.2.5->dwave-ocean-sdk) (5.1.3)\n",
"Requirement already satisfied: Flask>=1.1.1 in /usr/local/lib/python3.7/dist-packages (from dwave-inspector==0.2.5->dwave-ocean-sdk) (1.1.4)\n",
"Collecting homebase<2.0.0,>=1.0.0\n",
" Downloading https://files.pythonhosted.org/packages/50/ad/e0080c35bd177682d5118a95bc2e7c1ac0541394b4ffce5e9554b6a077f9/homebase-1.0.1-py2.py3-none-any.whl\n",
"Requirement already satisfied: networkx<3.0,>=2.0 in /usr/local/lib/python3.7/dist-packages (from dwave-system==1.4.0->dwave-ocean-sdk) (2.5.1)\n",
"Requirement already satisfied: numpy<2.0.0,>=1.14.0 in /usr/local/lib/python3.7/dist-packages (from dwave-system==1.4.0->dwave-ocean-sdk) (1.19.5)\n",
"Requirement already satisfied: six>=1.11.0 in /usr/local/lib/python3.7/dist-packages (from pyqubo==1.0.10->dwave-ocean-sdk) (1.15.0)\n",
"Collecting Deprecated>=1.2.10\n",
" Downloading https://files.pythonhosted.org/packages/fb/73/994edfcba74443146c84b91921fcc269374354118d4f452fb0c54c1cbb12/Deprecated-1.2.12-py2.py3-none-any.whl\n",
"Collecting ortools<9.0.0,>=6.6.4659\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/8f/df/aff5a745ab55d95fcf50ec3c0aa8cbb370f807692eb818cfcda7062d607a/ortools-8.2.8710-cp37-cp37m-manylinux1_x86_64.whl (14.2MB)\n",
"\u001b[K |████████████████████████████████| 14.2MB 239kB/s \n",
"\u001b[?25hCollecting plucky>=0.4.3\n",
" Downloading https://files.pythonhosted.org/packages/d8/70/7b43e7280284bafecb345f4edb3eea7042cf0d089c5d112920eda650fda5/plucky-0.4.3-py2.py3-none-any.whl\n",
"Requirement already satisfied: click>5 in /usr/local/lib/python3.7/dist-packages (from dwave-hybrid==0.6.1->dwave-ocean-sdk) (7.1.2)\n",
"Requirement already satisfied: requests[socks]>=2.18 in /usr/local/lib/python3.7/dist-packages (from dwave-cloud-client==0.8.4->dwave-ocean-sdk) (2.23.0)\n",
"Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.7/dist-packages (from dwave-cloud-client==0.8.4->dwave-ocean-sdk) (2.8.1)\n",
"Requirement already satisfied: scipy<2.0.0,>=0.15.0 in /usr/local/lib/python3.7/dist-packages (from penaltymodel-lp==0.1.4->dwave-ocean-sdk) (1.4.1)\n",
"Requirement already satisfied: decorator<5.0.0,>=4.1.0 in /usr/local/lib/python3.7/dist-packages (from dwave-networkx==0.8.8->dwave-ocean-sdk) (4.4.2)\n",
"Collecting fasteners\n",
" Downloading https://files.pythonhosted.org/packages/78/20/c862d765287e9e8b29f826749ebae8775bdca50b2cb2ca079346d5fbfd76/fasteners-0.16-py2.py3-none-any.whl\n",
"Requirement already satisfied: zipp>=0.4; python_version < \"3.8\" in /usr/local/lib/python3.7/dist-packages (from importlib-resources>=3.2.0; python_version < \"3.9\"->dwave-inspector==0.2.5->dwave-ocean-sdk) (3.4.1)\n",
"Requirement already satisfied: itsdangerous<2.0,>=0.24 in /usr/local/lib/python3.7/dist-packages (from Flask>=1.1.1->dwave-inspector==0.2.5->dwave-ocean-sdk) (1.1.0)\n",
"Requirement already satisfied: Werkzeug<2.0,>=0.15 in /usr/local/lib/python3.7/dist-packages (from Flask>=1.1.1->dwave-inspector==0.2.5->dwave-ocean-sdk) (1.0.1)\n",
"Requirement already satisfied: Jinja2<3.0,>=2.10.1 in /usr/local/lib/python3.7/dist-packages (from Flask>=1.1.1->dwave-inspector==0.2.5->dwave-ocean-sdk) (2.11.3)\n",
"Requirement already satisfied: wrapt<2,>=1.10 in /usr/local/lib/python3.7/dist-packages (from Deprecated>=1.2.10->pyqubo==1.0.10->dwave-ocean-sdk) (1.12.1)\n",
"Collecting protobuf>=3.14.0\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/bb/f8/07b65b1c3a2334208208b8161857fd9c19f69ab5d2c5314ef5f0b5f07aa5/protobuf-3.17.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.0MB)\n",
"\u001b[K |████████████████████████████████| 1.0MB 28.4MB/s \n",
"\u001b[?25hRequirement already satisfied: absl-py>=0.11 in /usr/local/lib/python3.7/dist-packages (from ortools<9.0.0,>=6.6.4659->penaltymodel-mip==0.2.4; platform_machine == \"x86_64\" or platform_machine == \"amd64\" or platform_machine == \"AMD64\"->dwave-ocean-sdk) (0.12.0)\n",
"Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests[socks]>=2.18->dwave-cloud-client==0.8.4->dwave-ocean-sdk) (3.0.4)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests[socks]>=2.18->dwave-cloud-client==0.8.4->dwave-ocean-sdk) (2020.12.5)\n",
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests[socks]>=2.18->dwave-cloud-client==0.8.4->dwave-ocean-sdk) (1.24.3)\n",
"Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests[socks]>=2.18->dwave-cloud-client==0.8.4->dwave-ocean-sdk) (2.10)\n",
"Requirement already satisfied: PySocks!=1.5.7,>=1.5.6; extra == \"socks\" in /usr/local/lib/python3.7/dist-packages (from requests[socks]>=2.18->dwave-cloud-client==0.8.4->dwave-ocean-sdk) (1.7.1)\n",
"Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.7/dist-packages (from Jinja2<3.0,>=2.10.1->Flask>=1.1.1->dwave-inspector==0.2.5->dwave-ocean-sdk) (2.0.1)\n",
"Installing collected packages: dimod, homebase, plucky, dwave-cloud-client, dwave-tabu, dwave-networkx, fasteners, minorminer, dwave-system, dwave-inspector, dwave-neal, Deprecated, pyqubo, penaltymodel, protobuf, ortools, penaltymodel-mip, dwave-greedy, dwave-hybrid, penaltymodel-cache, dwave-qbsolv, penaltymodel-lp, dwavebinarycsp, dwave-ocean-sdk\n",
" Found existing installation: protobuf 3.12.4\n",
" Uninstalling protobuf-3.12.4:\n",
" Successfully uninstalled protobuf-3.12.4\n",
"Successfully installed Deprecated-1.2.12 dimod-0.9.13 dwave-cloud-client-0.8.4 dwave-greedy-0.1.2 dwave-hybrid-0.6.1 dwave-inspector-0.2.5 dwave-neal-0.5.7 dwave-networkx-0.8.8 dwave-ocean-sdk-3.3.0 dwave-qbsolv-0.3.2 dwave-system-1.4.0 dwave-tabu-0.3.1 dwavebinarycsp-0.1.2 fasteners-0.16 homebase-1.0.1 minorminer-0.2.5 ortools-8.2.8710 penaltymodel-0.16.4 penaltymodel-cache-0.4.3 penaltymodel-lp-0.1.4 penaltymodel-mip-0.2.4 plucky-0.4.3 protobuf-3.17.1 pyqubo-1.0.10\n"
],
"name": "stdout"
},
{
"output_type": "display_data",
"data": {
"application/vnd.colab-display-data+json": {
"pip_warning": {
"packages": [
"google"
]
}
}
},
"metadata": {
"tags": []
}
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "QjMK5z-W6qJV"
},
"source": [
"復習になりますが、ローカルファイルをGoogle colab上にアップロードして使う方法です。\n",
"まず次のようにライブラリを読み込みます。"
]
},
{
"cell_type": "code",
"metadata": {
"id": "zv4xnuBa_Agp"
},
"source": [
"from google.colab import files"
],
"execution_count": 2,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "7nG5EMhK_EiY"
},
"source": [
"この後以下のコードを実行するとファイルをアップロードすることができます。\n",
"講義時に配布したリンクからダウンロードしたファイルをGoogle colabにアップロードしてください。"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"resources": {
"http://localhost:8080/nbextensions/google.colab/files.js": {
"data": "Ly8gQ29weXJpZ2h0IDIwMTcgR29vZ2xlIExMQwovLwovLyBMaWNlbnNlZCB1bmRlciB0aGUgQXBhY2hlIExpY2Vuc2UsIFZlcnNpb24gMi4wICh0aGUgIkxpY2Vuc2UiKTsKLy8geW91IG1heSBub3QgdXNlIHRoaXMgZmlsZSBleGNlcHQgaW4gY29tcGxpYW5jZSB3aXRoIHRoZSBMaWNlbnNlLgovLyBZb3UgbWF5IG9idGFpbiBhIGNvcHkgb2YgdGhlIExpY2Vuc2UgYXQKLy8KLy8gICAgICBodHRwOi8vd3d3LmFwYWNoZS5vcmcvbGljZW5zZXMvTElDRU5TRS0yLjAKLy8KLy8gVW5sZXNzIHJlcXVpcmVkIGJ5IGFwcGxpY2FibGUgbGF3IG9yIGFncmVlZCB0byBpbiB3cml0aW5nLCBzb2Z0d2FyZQovLyBkaXN0cmlidXRlZCB1bmRlciB0aGUgTGljZW5zZSBpcyBkaXN0cmlidXRlZCBvbiBhbiAiQVMgSVMiIEJBU0lTLAovLyBXSVRIT1VUIFdBUlJBTlRJRVMgT1IgQ09ORElUSU9O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JWkUpOwogICAgICBjb25zdCBjaHVuayA9IG5ldyBVaW50OEFycmF5KGZpbGVEYXRhLCBwb3NpdGlvbiwgbGVuZ3RoKTsKICAgICAgcG9zaXRpb24gKz0gbGVuZ3RoOwoKICAgICAgY29uc3QgYmFzZTY0ID0gYnRvYShTdHJpbmcuZnJvbUNoYXJDb2RlLmFwcGx5KG51bGwsIGNodW5rKSk7CiAgICAgIHlpZWxkIHsKICAgICAgICByZXNwb25zZTogewogICAgICAgICAgYWN0aW9uOiAnYXBwZW5kJywKICAgICAgICAgIGZpbGU6IGZpbGUubmFtZSwKICAgICAgICAgIGRhdGE6IGJhc2U2NCwKICAgICAgICB9LAogICAgICB9OwogICAgICBwZXJjZW50LnRleHRDb250ZW50ID0KICAgICAgICAgIGAke01hdGgucm91bmQoKHBvc2l0aW9uIC8gZmlsZURhdGEuYnl0ZUxlbmd0aCkgKiAxMDApfSUgZG9uZWA7CiAgICB9CiAgfQoKICAvLyBBbGwgZG9uZS4KICB5aWVsZCB7CiAgICByZXNwb25zZTogewogICAgICBhY3Rpb246ICdjb21wbGV0ZScsCiAgICB9CiAgfTsKfQoKc2NvcGUuZ29vZ2xlID0gc2NvcGUuZ29vZ2xlIHx8IHt9OwpzY29wZS5nb29nbGUuY29sYWIgPSBzY29wZS5nb29nbGUuY29sYWIgfHwge307CnNjb3BlLmdvb2dsZS5jb2xhYi5fZmlsZXMgPSB7CiAgX3VwbG9hZEZpbGVzLAogIF91cGxvYWRGaWxlc0NvbnRpbnVlLAp9Owp9KShzZWxmKTsK",
"ok": true,
"headers": [
[
"content-type",
"application/javascript"
]
],
"status": 200,
"status_text": ""
}
},
"base_uri": "https://localhost:8080/",
"height": 115
},
"id": "85p42jhaBSUp",
"outputId": "ab8a003a-d337-4a80-b00a-a7222ac4be76"
},
"source": [
"files.upload()"
],
"execution_count": 3,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/html": [
"\n",
" <input type=\"file\" id=\"files-230e5bfe-6e2f-49de-abe2-54db71172328\" name=\"files[]\" multiple disabled\n",
" style=\"border:none\" />\n",
" <output id=\"result-230e5bfe-6e2f-49de-abe2-54db71172328\">\n",
" Upload widget is only available when the cell has been executed in the\n",
" current browser session. Please rerun this cell to enable.\n",
" </output>\n",
" <script src=\"/nbextensions/google.colab/files.js\"></script> "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
}
},
{
"output_type": "stream",
"text": [
"Saving QA4Uafter.csv to QA4Uafter.csv\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'QA4Uafter.csv': b'3.0,3.0,3.0,3.0\\n2.0,2.0,3.0,3.0\\n4.0,3.0,4.0,5.0\\n4.0,3.0,3.0,4.0\\n3.0,2.0,3.0,6.0\\n3.0,3.0,3.0,5.0\\n4.0,1.0,3.0,3.0\\n3.0,2.0,2.0,2.0\\n4.0,3.0,3.0,4.0\\n2.0,3.0,3.0,3.0\\n2.0,3.0,4.0,4.0\\n4.0,2.0,2.0,4.0\\n4.0,3.0,5.0,5.0\\n2.0,1.0,2.0,4.0\\n3.0,3.0,3.0,4.0\\n2.0,2.0,3.0,2.0\\n2.0,2.0,3.0,4.0\\n2.0,2.0,3.0,3.0\\n2.0,1.0,1.0,3.0\\n2.0,1.0,3.0,1.0\\n4.0,2.0,4.0,4.0\\n2.0,1.0,3.0,5.0\\n4.0,1.0,5.0,2.0\\n2.0,2.0,3.0,1.0\\n4.0,1.0,2.0,7.0\\n4.0,2.0,3.0,4.0\\n4.0,3.0,3.0,5.0\\n4.0,1.0,3.0,3.0\\n3.0,2.0,3.0,2.0\\n4.0,3.0,3.0,5.0\\n3.0,2.0,1.0,2.0\\n2.0,1.0,3.0,4.0\\n2.0,2.0,3.0,5.0\\n4.0,2.0,2.0,4.0\\n3.0,2.0,3.0,3.0\\n4.0,2.0,2.0,1.0\\n3.0,3.0,2.0,1.0\\n2.0,2.0,3.0,5.0\\n4.0,1.0,2.0,1.0\\n4.0,3.0,3.0,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0,3.0,5.0\\n3.0,2.0,3.0,1.0\\n4.0,3.0,3.0,3.0\\n1.0,1.0,4.0,6.0\\n2.0,2.0,3.0,3.0\\n2.0,1.0,3.0,4.0\\n4.0,3.0,5.0,5.0\\n2.0,1.0,4.0,4.0\\n1.0,2.0,1.0,4.0\\n4.0,3.0,3.0,4.0\\n4.0,2.0,5.0,1.0\\n4.0,3.0,3.0,5.0\\n1.0,3.0,3.0,4.0\\n4.0,2.0,4.0,3.0\\n4.0,1.0,5.0,3.0\\n4.0,1.0,3.0,5.0\\n4.0,1.0,3.0,5.0\\n1.0,1.0,3.0,3.0\\n1.0,1.0,4.0,3.0\\n3.0,3.0,3.0,2.0\\n1.0,1.0,4.0,3.0\\n4.0,2.0,3.0,4.0\\n4.0,3.0,3.0,6.0\\n1.0,1.0,4.0,1.0\\n1.0,2.0,3.0,5.0\\n3.0,1.0,2.0,2.0\\n2.0,2.0,3.0,5.0\\n4.0,2.0,2.0,4.0\\n1.0,1.0,3.0,5.0\\n4.0,1.0,3.0,5.0\\n3.0,3.0,3.0,5.0\\n2.0,3.0,3.0,4.0\\n4.0,2.0,3.0,4.0\\n1.0,1.0,1.0,1.0\\n4.0,2.0,3.0,3.0\\n4.0,2.0,5.0,2.0\\n'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 3
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aCrmx53uBOln"
},
"source": [
"前回同様に本イベントに参加した皆さんから演習パートに参加したいガチ勢の方々のアンケート結果を利用して、それらの方々の傾向を分析することで得られる**生成モデル**というものをボルツマン機械学習を実行して作りましょう。\n",
"先ほどアップロードしたファイルをpandasというライブラリで利用します。"
]
},
{
"cell_type": "code",
"metadata": {
"id": "osxmiDjsC4u5"
},
"source": [
"import pandas as pd\n",
"df = pd.read_csv('QA4Uafter.csv',header=None)"
],
"execution_count": 5,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "YUhgPw32GACF"
},
"source": [
"ここでdfはデータフレームと呼ばれる形式になっています。\n",
"前回同様に私たちが必要なのはcsvに入った数値ですので、次のように操作します。"
]
},
{
"cell_type": "code",
"metadata": {
"id": "BQC_e4lgGORp",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "e75033a3-b328-4cdf-d5a2-7d3c08902d4b"
},
"source": [
"import numpy as np\n",
"V = df.values.astype(np.int8)\n",
"print(V)"
],
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": [
"[[3 3 3 3]\n",
" [2 2 3 3]\n",
" [4 3 4 5]\n",
" [4 3 3 4]\n",
" [3 2 3 6]\n",
" [3 3 3 5]\n",
" [4 1 3 3]\n",
" [3 2 2 2]\n",
" [4 3 3 4]\n",
" [2 3 3 3]\n",
" [2 3 4 4]\n",
" [4 2 2 4]\n",
" [4 3 5 5]\n",
" [2 1 2 4]\n",
" [3 3 3 4]\n",
" [2 2 3 2]\n",
" [2 2 3 4]\n",
" [2 2 3 3]\n",
" [2 1 1 3]\n",
" [2 1 3 1]\n",
" [4 2 4 4]\n",
" [2 1 3 5]\n",
" [4 1 5 2]\n",
" [2 2 3 1]\n",
" [4 1 2 7]\n",
" [4 2 3 4]\n",
" [4 3 3 5]\n",
" [4 1 3 3]\n",
" [3 2 3 2]\n",
" [4 3 3 5]\n",
" [3 2 1 2]\n",
" [2 1 3 4]\n",
" [2 2 3 5]\n",
" [4 2 2 4]\n",
" [3 2 3 3]\n",
" [4 2 2 1]\n",
" [3 3 2 1]\n",
" [2 2 3 5]\n",
" [4 1 2 1]\n",
" [4 3 3 3]\n",
" [3 3 3 5]\n",
" [3 1 3 5]\n",
" [4 3 3 1]\n",
" [1 2 1 4]\n",
" [2 1 1 3]\n",
" [4 1 3 1]\n",
" [4 3 3 3]\n",
" [4 3 3 4]\n",
" [1 1 3 2]\n",
" [2 1 2 1]\n",
" [2 3 2 4]\n",
" [4 2 2 1]\n",
" [4 2 3 3]\n",
" [2 1 2 5]\n",
" [4 3 3 1]\n",
" [4 1 2 4]\n",
" [2 2 2 2]\n",
" [4 3 3 1]\n",
" [2 2 3 1]\n",
" [2 2 3 4]\n",
" [4 3 3 1]\n",
" [3 2 3 5]\n",
" [2 1 4 1]\n",
" [4 2 5 2]\n",
" [2 2 1 3]\n",
" [4 3 3 1]\n",
" [4 2 2 2]\n",
" [2 1 2 5]\n",
" [4 2 3 3]\n",
" [2 2 3 5]\n",
" [2 2 3 1]\n",
" [4 3 5 3]\n",
" [4 2 3 2]\n",
" [2 2 3 3]\n",
" [4 3 1 1]\n",
" [4 3 3 3]\n",
" [4 3 5 4]\n",
" [3 3 2 2]\n",
" [3 3 3 5]\n",
" [4 3 2 1]\n",
" [4 3 3 5]\n",
" [2 2 1 2]\n",
" [3 3 3 3]\n",
" [2 1 2 3]\n",
" [2 1 2 5]\n",
" [3 3 2 1]\n",
" [1 1 3 5]\n",
" [4 2 2 2]\n",
" [4 3 3 3]\n",
" [4 2 2 4]\n",
" [1 1 3 4]\n",
" [4 3 3 4]\n",
" [4 3 5 1]\n",
" [1 1 3 5]\n",
" [1 2 2 4]\n",
" [1 1 3 5]\n",
" [4 1 3 3]\n",
" [2 3 3 5]\n",
" [4 2 2 4]\n",
" [4 2 2 4]\n",
" [4 1 4 1]\n",
" [4 3 3 5]\n",
" [2 1 3 5]\n",
" [1 1 4 4]\n",
" [2 1 3 5]\n",
" [4 3 2 1]\n",
" [4 3 3 4]\n",
" [4 3 5 1]\n",
" [1 1 2 3]\n",
" [1 1 2 3]\n",
" [4 2 4 1]\n",
" [2 2 3 2]\n",
" [4 3 3 4]\n",
" [4 1 1 3]\n",
" [2 2 3 3]\n",
" [3 3 3 4]\n",
" [4 3 5 5]\n",
" [4 3 3 7]\n",
" [4 3 2 2]\n",
" [4 3 3 3]\n",
" [4 2 2 3]\n",
" [4 3 3 3]\n",
" [4 1 2 1]\n",
" [1 1 1 5]\n",
" [2 3 3 3]\n",
" [1 1 4 1]\n",
" [1 1 1 1]\n",
" [3 2 3 1]\n",
" [3 3 3 3]\n",
" [3 3 3 2]\n",
" [1 3 3 4]\n",
" [2 2 3 5]\n",
" [2 1 2 4]\n",
" [4 3 5 1]\n",
" [3 1 3 1]\n",
" [4 1 3 5]\n",
" [4 3 5 1]\n",
" [2 2 2 5]\n",
" [4 3 5 3]\n",
" [4 3 2 3]\n",
" [3 3 2 1]\n",
" [1 1 3 5]\n",
" [4 3 2 2]\n",
" [1 1 3 5]\n",
" [4 3 3 4]\n",
" [4 3 3 2]\n",
" [4 2 3 1]\n",
" [1 1 4 5]\n",
" [4 2 5 3]\n",
" [4 3 3 5]\n",
" [2 2 3 3]\n",
" [3 1 5 5]\n",
" [4 3 2 1]\n",
" [4 3 5 4]\n",
" [1 1 2 4]\n",
" [4 3 3 4]\n",
" [2 2 4 5]\n",
" [4 3 3 4]\n",
" [4 3 3 2]\n",
" [4 3 5 4]\n",
" [4 1 2 1]\n",
" [4 3 2 3]\n",
" [4 1 2 1]\n",
" [4 3 3 5]\n",
" [4 3 3 1]\n",
" [4 3 3 5]\n",
" [3 3 3 1]\n",
" [3 2 2 2]\n",
" [4 1 2 3]\n",
" [4 3 5 1]\n",
" [4 1 2 3]\n",
" [2 2 3 1]\n",
" [2 2 2 3]\n",
" [4 1 5 3]\n",
" [2 1 2 2]\n",
" [4 3 5 5]\n",
" [4 1 1 3]\n",
" [4 3 3 4]\n",
" [4 3 3 3]\n",
" [2 2 3 5]\n",
" [4 2 5 4]\n",
" [4 2 2 3]\n",
" [3 1 3 4]\n",
" [4 3 3 2]\n",
" [4 3 2 1]\n",
" [4 3 3 4]\n",
" [2 2 2 3]\n",
" [2 1 4 3]\n",
" [4 1 2 1]\n",
" [4 3 3 3]\n",
" [4 3 4 3]\n",
" [2 2 3 3]\n",
" [3 3 5 1]\n",
" [1 1 3 3]\n",
" [3 3 3 3]\n",
" [2 1 3 3]\n",
" [4 2 3 5]\n",
" [4 2 1 3]\n",
" [1 1 2 5]\n",
" [4 3 4 1]\n",
" [2 2 3 3]\n",
" [1 1 3 6]\n",
" [4 3 3 6]\n",
" [4 3 3 5]\n",
" [2 1 2 2]\n",
" [4 3 3 5]\n",
" [4 3 3 5]\n",
" [4 3 3 2]\n",
" [4 3 3 3]\n",
" [1 1 3 2]\n",
" [2 1 3 5]\n",
" [3 2 3 1]\n",
" [4 3 3 3]\n",
" [1 1 4 6]\n",
" [2 2 3 3]\n",
" [2 1 3 4]\n",
" [4 3 5 5]\n",
" [2 1 4 4]\n",
" [1 2 1 4]\n",
" [4 3 3 4]\n",
" [4 2 5 1]\n",
" [4 3 3 5]\n",
" [1 3 3 4]\n",
" [4 2 4 3]\n",
" [4 1 5 3]\n",
" [4 1 3 5]\n",
" [4 1 3 5]\n",
" [1 1 3 3]\n",
" [1 1 4 3]\n",
" [3 3 3 2]\n",
" [1 1 4 3]\n",
" [4 2 3 4]\n",
" [4 3 3 6]\n",
" [1 1 4 1]\n",
" [1 2 3 5]\n",
" [3 1 2 2]\n",
" [2 2 3 5]\n",
" [4 2 2 4]\n",
" [1 1 3 5]\n",
" [4 1 3 5]\n",
" [3 3 3 5]\n",
" [2 3 3 4]\n",
" [4 2 3 4]\n",
" [1 1 1 1]\n",
" [4 2 3 3]\n",
" [4 2 5 2]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "xnNan3K-GWlG"
},
"source": [
"Vに読み込んだデータを行列として格納しました。\n",
"どのようなサイズのデータかを調べるためには.shapeを利用しましょう。"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TtA9s6rUGfhB",
"outputId": "ac3bdc2c-e3fb-4063-94e8-b7e11cdb4f80"
},
"source": [
"N,M = V.shape\n",
"print(N,M)"
],
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": [
"246 4\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "PcCe6vkaGkbf"
},
"source": [
"これによると241人分のデータで、4項目のデータが格納されているということがわかります。\n",
"実際それぞれ4項目、つまり4列の数値が並んでいると思います。\n",
"これは事前に取ったアンケートの結果の数値となっています。"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "07nbgn3Kq-ox"
},
"source": [
"### one-hot表示\n",
"\n",
"ここで復習となりますが、one-hot表示を利用しましょう。\n",
"複数の要素があり、どれが利用されているかを0と1で示すものです。\n",
"例えば3は$(0,0,1,0,\\ldots)$と3番目に1が立ち、2は$(0,1,0,0,\\ldots)$と2番目に1が立ちます。\n",
"そのようなベクトルを用意すると、内積という計算で**同じかどうか**がわかります。\n",
"内積という計算は、各成分をかけて足し上げるということになっています。\n",
"3と2の場合は、\n",
"\\begin{equation}\n",
"0 \\times 0 + 0 \\times 1 + 1 \\times 0 + 0 \\times 0 +\\ldots = 0\n",
"\\end{equation}\n",
"となります。3と3であれば\n",
"\\begin{equation}\n",
"0 \\times 0 + 0 \\times 0 + 1 \\times 1 + 0 \\times 0 +\\ldots = 1\n",
"\\end{equation}\n",
"となります。これで同じかどうかを示すことができそうです。\n",
"\n",
"今回のアンケートでは、4つの項目があり、それぞれ4,3,5,7個の選択肢から答えを選んでもらいました。\n",
"それぞれ4つのうち1個が、3つのうち1個が、5つのうち1個が、7つのうち1個が1を取り、それ以外全て0となっているようなデータに加工すると都合がよさそうです。\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3E__SO9NtsIG"
},
"source": [
"その加工のために次のような関数を作ります。\n",
"実は関数というのは自分で作り出すことができます。def 関数名(引数名):として、その中でどんな計算を実行するのか、自由に書くことができます。\n",
"最後に結果を返すreturnで計算結果を出すようにもできます。"
]
},
{
"cell_type": "code",
"metadata": {
"id": "FI4184qJtVo4"
},
"source": [
"def onehot(data):\n",
" z = np.zeros(4+3+5+7)\n",
" z[data[0]-1] = 1\n",
" z[4+data[1]-1] = 1\n",
" z[4+3+data[2]-1] = 1\n",
" z[4+3+5+data[3]-1] = 1\n",
" return z"
],
"execution_count": 8,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "Z7N24HPxt7Ha"
},
"source": [
"この関数を利用すると、加工されたデータのベクトルが出力されますので、一個ずつデータを加工していきましょう。"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Jr2U9kkFvLVZ"
},
"source": [
"M = 4+3+5+7\n",
"Z = np.zeros(N*M).reshape(N,M)\n",
"\n",
"for i in range(N):\n",
" Z[i,:] = onehot(V[i,:])"
],
"execution_count": 9,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "4m2M9TnqwkpM"
},
"source": [
"これで加工終了です。"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2_ghszQrd7eV"
},
"source": [
"### ボルツマンマシン学習\n",
"\n",
"こうして用意されたアンケートデータを利用して新しいことを学んでいきましょう。\n",
"ボルツマン機械学習(ボルツマンマシン学習)です。\n",
"大胆な仮説ですが、このアンケートデータを、**量子アニーリングマシンからの出力結果**であると考えてみましょう。\n",
"アンケートの項目が$4$つあり、それぞれ$4,3,5,7$個の選択肢のうち、どれかが$1$となるという条件があります。\n",
"$0$と$1$をとる変数の数は$M=4+3+5+7$で、それぞれ次のような制約条件があります。\n",
"\n",
"第一項目について\n",
"\\begin{equation}\n",
"x_1+x_2+x_3+x_4 = 1\n",
"\\end{equation}\n",
"第二項目について\n",
"\\begin{equation}\n",
"x_5+x_6+x_7 = 1\n",
"\\end{equation}\n",
"第三項目については\n",
"\\begin{equation}\n",
"x_8+x_9+x_{10}+x_{11}+x_{12} = 1\n",
"\\end{equation}\n",
"第四項については\n",
"\\begin{equation}\n",
"x_{13}+x_{14}+x_{15}+x_{16}+x_{17}+x_{18}+x_{19} = 1\n",
"\\end{equation}\n",
"といった格好です。\n",
"\n",
"この条件を満たしつつ、$x_i$にはどのような関係があるのでしょうか。\n",
"例えば1つ目の項目は学生か社会人かなどの所属についての項目です。\n",
"それと他の項目にはどのような相関があるのでしょうか。\n",
"学生に多い傾向と、社会人に多い傾向などは見つかったりするのでしょうか。\n",
"\n",
"QUBO行列は、何度か登場してきたように最適化問題を解くために問題を指定するものでした。\n",
"その際には次のようなコスト関数を最小化するということを量子アニーリングマシンにお願いをする格好でした。\n",
"\\begin{equation}\n",
"E({\\bf x}) = \\sum_{i}\\sum_{j} Q_{ij} x_i x_j\n",
"\\end{equation}\n",
"これでわかることは$Q_{ij}$を大きくすると、$x_ix_j$は$0$になりがちです。\n",
"逆に$Q_{ij}$を小さくすると、$x_ix_j$は$1$になる傾向が強まります。\n",
"この$Q_{ij}$を微調整することで、僕らが持っているデータを再現するように$Q_{ij}$を作り出すことはできないでしょうか。\n",
"それができてしまえば、**参加者の傾向を真似した**量子アニーリングマシンが出来上がるということになります。\n",
"こうやって実際の傾向を表すデータと、人工的に用意したモデルの傾向から出てくる結果を、調整して合わせることにより、真似をすることを**機械学習**と言います。\n",
"今回はそのなかでもボルツマンマシン学習を取り扱っています。\n",
"\n",
"特に量子アニーリングマシンは、その内部的な事情からボルツマン分布という特殊な確率分布に従う傾向にあるということが、いくつかの研究・調査により明らかとなりました。\n",
"その性質を逆手に利用して、ボルツマンマシン学習に利用しようという研究が実施されています。\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2l4q1Ou__K3q"
},
"source": [
"\n",
"量子アニーリングマシンからはnum_readsを調整することにより、$x_ix_j$を大量に出力することができます。\n",
"それに対して私たちは実際のデータを、$Z_{im}$という形で持っています。\n",
"このとき$i$は$i$番目の人の結果でしたから、何度かデータを出力してもらったうちの1つであると考えます。そして$m$は、ここでいう、どの項目に$1$が立っているかということを示しています。\n",
"それに合わせて添字を書きます。\n",
"さらに先ほど考えたように、各項目においては制約条件がありますから、罰金法を利用して、\n",
"\\begin{equation}\n",
"E({\\bf x}) = \\sum_{m}\\sum_{n} Q_{mn} x_{m} x_{n} + \\lambda_1\\left( x_1+x_2+x_3+x_4 -1 \\right)^2 + \\lambda_2 \\left( x_5+x_6+x_7 -1 \\right)^2 + \\lambda_3 \\left( x_8+x_9+x_{10}+x_{11}+x_{12} - 1\\right)^2 + \\lambda_4 \\left( x_{13}+x_{14}+x_{15}+x_{16}+x_{17}+x_{18}+x_{19} - 1\\right)^2\n",
"\\end{equation}\n",
"というQUBO行列、ないしはコスト関数が対応します。\n",
"これを量子アニーリングマシンに実装して、同じように出力しましょう。\n",
"そうすると$x_m$に対してはいくつかの結果が得られます。\n",
"何番目の結果であるのかという添字を追加しましょう。\n",
"$x_{im}$とします。\n",
"そうすると実際のデータは$Z_{im}$で、仮に量子アニーリングマシンから出力した結果は$x_{im}$ということで\n",
"比較しやすいものとなりました。\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "hCjem1dc_M32"
},
"source": [
"\n",
"この結果を利用して、$Q_{mn}$をどのように調整したら良いのかを考えてみましょう。\n",
"$Q_{mn}$を上下させることによって$x_mx_n$を大きくしたり、小さくしたりさせることができます。\n",
"$Z_{im}$と比較して$x_{im}$が小さいときには、$x_{im}$を大きくさせましょう。そのためには$Q_{mn}$を小さくします。\n",
"逆に$Z_{im}$と比較して$x_{im}$が大きいときには、$x_{im}$を小さくさせましょう。そのためには$Q_{mn}$を大きくしましょう。\n",
"比較をするためには引き算をすれば良いですね。たくさんのデータ、たくさんの出力結果が集まっていますから、それらの平均を利用することにしましょう。\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dOFOUeY1_PDG"
},
"source": [
"\n",
"\\begin{equation}\n",
"Q_{mn} = Q_{mn} - \\eta\\left( \\frac{1}{N}\\sum_{i=1}^N Z_{im}Z_{in} - \\frac{1}{N'} \\sum_{i=1}^{N'} x_{im}x_{in}\\right)\n",
"\\end{equation}\n",
"ここで$N'$は量子アニーリングマシンから出力してもらう数です。$\\eta$はイータ(エータ)などと読むギリシャ文字ですが、学習率または勾配率などと呼ばれており、微調整をする程度を示します。\n",
"$Q_{mn}$を$Z_{im}$と$x_{im}$の平均値の比較をして、その結果に応じて微調整をするということになります。\n",
"\n",
"これを実際に実装してみましょう。\n",
"まずは量子アニーリングマシンに投入するQUBO行列のうち制約条件に関連する部分を作りましょう。"
]
},
{
"cell_type": "code",
"metadata": {
"id": "8mliN2c2Oyeq"
},
"source": [
"Qc = np.zeros(M**2).reshape(M,M)\n",
"\n",
"lam = 10.0\n",
"\n",
"for m in range(4):\n",
" for n in range(4):\n",
" Qc[m,n] = Qc[m,n] + lam\n",
" if m == n:\n",
" Qc[m,n] = Qc[m,n] -2*lam\n",
"\n",
"for m in range(4,4+3):\n",
" for n in range(4,4+3):\n",
" Qc[m,n] = Qc[m,n] + lam\n",
" if m == n:\n",
" Qc[m,n] = Qc[m,n] -2*lam\n",
"\n",
"for m in range(4+3,4+3+5):\n",
" for n in range(4+3,4+3+5):\n",
" Qc[m,n] = Qc[m,n] + lam\n",
" if m == n:\n",
" Qc[m,n] = Qc[m,n] -2*lam\n",
"\n",
"for m in range(4+3+5,4+3+5+7):\n",
" for n in range(4+3+5,4+3+5+7):\n",
" Qc[m,n] = Qc[m,n] + lam\n",
" if m == n:\n",
" Qc[m,n] = Qc[m,n] -2*lam\n"
],
"execution_count": 10,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "OclYxYU66SK_"
},
"source": [
"それぞれの制約条件についての項目を足し上げました。\n",
"うまくできたかな?とみたい場合にはprint()やmatplotlib.pyplotからimshow()などを利用しましょう。"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 265
},
"id": "E6fg36Fe6QwB",
"outputId": "51ea432b-c69b-4bd7-ec3e-15e87dcffe24"
},
"source": [
"import matplotlib.pyplot as plt\n",
"plt.imshow(Qc)\n",
"plt.show()"
],
"execution_count": 11,
"outputs": [
{
"output_type": "display_data",
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAQUAAAD4CAYAAADl7fPiAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAOoklEQVR4nO3df4xlZX3H8fen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+F5TVacN2jGRoTBIkp3LYT6G5fA+fY+TzdsHSQ1DQVJjKYXCxsXuwAJZDu/T9zjBlsxnCpIWxlK6UpC0AAwFSY0lEQpJLk/ySJLdSW5Y7P6MQ5LHkzyQZFeSnYvdn1FJsinJ/iQPTtv2yiQ7kjzavZ68mH3sa5b3+JEke7v/564kVy5mH+dj4kMhyQrgE8AVwAXANUkuWNxejc0lVbVmqX6/PYtbgctnbLsBuKuqzgPu6taXsls58j0C3Nz9P9dU1bYB+yfSxIcCU/M67q6qx6rqp8AXgfWL3CfNUTdR74EZm9cDt3XLtwHvXNBOjdgs73HJWgqhcCbwxLT1Pd22Y82cp8M/Bqyqqn3d8g+YmrrvWHR9kvu724slc4u0FEJhubioqt7E1G3SB5L8+mJ3aCHU1Hfix+L34p8EzgHWAPuAmxa3O3O3FEJhL7B62vpZ3bZjyjKbDv+pJKcDdK/7F7k/I1dVT1XVoar6GfBpltD/cymEwj3AeUlel+QEpmaR3rrIfRqpZTgd/lbg2m75WuDLi9iXsTgcep13sYT+n30rRI1dVR1Mcj2wHVgBbKqqhxa5W6O2CtgyVZqT44AvHCvT4XclAi4GTk2yB7gR+BjwT125gO8zVY90yZrlPV6cZA1Tt0aPA+9btA7Ok8OcJTWWwu2DpAVkKEhqGAqSGoaCpIahIKlhKEhqGAqSGv8Pz54/td1mrssAAAAASUVORK5CYII=\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"tags": [],
"needs_background": "light"
}
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "p2Iuf09T658c"
},
"source": [
"対角項(左上から右下に斜め)には$-2\\lambda$が、非対角項のうちブロック上に$\\lambda$が各項目の選択肢ごとに埋まっていればOKです。\n",
"\n",
"さて最初$Q_{m,n}$は幾つなのか分かりませんので適当な数値を入れておきましょう。\n",
"制約条件を満たした答えが出るのを妨げないために最初は$0$にしておきます。"
]
},
{
"cell_type": "code",
"metadata": {
"id": "ewWGNF8r6ejm"
},
"source": [
"Q = np.zeros(M**2).reshape(M,M)\n",
"Qmat = Q+Qc"
],
"execution_count": 12,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "0b3TdLhO7ajp"
},
"source": [
"これでQUBO行列の準備は終えました。\n",
"量子アニーリングマシンを使う準備を整えましょう。\n",
"今回は全結合(全てのビットが関係しうる)の埋め込みを利用します。\n",
"まずはいつもの通り量子アニーリングマシンの利用の準備を整えます。"
]
},
{
"cell_type": "code",
"metadata": {
"id": "MvP04Cn57ZoM"
},
"source": [
"token = '**'\n",
"endpoint = 'https://cloud.dwavesys.com/sapi/'\n",
"\n",
"from dwave.system import DWaveCliqueSampler\n",
"sampler = DWaveCliqueSampler(solver='DW_2000Q_6',token=token)"
],
"execution_count": 14,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Yt3Gp5_T8ATL"
},
"source": [
"Nsample = 100\n",
"sampleset = sampler.sample_qubo(Qmat,num_reads=Nsample, answer_mode = \"raw\")"
],
"execution_count": 15,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "pRVIPEykYXzr"
},
"source": [
"ここまでで結果の取得ができました。\n",
"answer_modeで\"raw\"というオプションを利用しましたが、これは生データを渡すということです。\n",
"デフォルトではanswer_mode=\"histogram\"となっており、同じ結果が出てきた場合は、その頻度を示すデータに整形してくれています。\n",
"\n",
"この結果を計算に利用しやすいように整形します。"
]
},
{
"cell_type": "code",
"metadata": {
"id": "pS_tjr-G8HNY"
},
"source": [
"x = np.zeros(Nsample*M).reshape(Nsample,M)\n",
"for k in range(Nsample):\n",
" x[k,:] = sampleset.record[k][0]"
],
"execution_count": 16,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "dBwXhPgWZBxJ"
},
"source": [
"この形は先ほど利用したZと同じ構造をしています。つまり$Z_{i,m}$ですね。\n",
"\n",
"誰が、どんな結果を出したかという格好になっています。\n",
"$x_{i,m}$についても$Z_{i,m}$と同じように平均の計算をしましょう。"
]
},
{
"cell_type": "code",
"metadata": {
"id": "l5DugdzhErHR"
},
"source": [
"Zmat = np.dot(Z.T,Z)/N\n",
"Xmat = np.dot(x.T,x)/Nsample"
],
"execution_count": 17,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "lO0r3WSNZeLJ"
},
"source": [
"この結果を比較して、先ほど用意した計算式通りにコードを組みます。"
]
},
{
"cell_type": "code",
"metadata": {
"id": "doBN3GJh8Mg-"
},
"source": [
"eta = 0.1\n",
"Q = Q - eta*(Zmat-Xmat)\n",
"Qmat = Q + Qc"
],
"execution_count": 18,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "wj5fjbLEZ1NZ"
},
"source": [
"この結果を調べるには次のように行列を画像で表示すると良いでしょう。"
]
},
{
"cell_type": "code",
"metadata": {
"id": "XJESSdTsEkDd",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 265
},
"outputId": "67e9f5df-0bb1-4441-d6e6-60ca97d976aa"
},
"source": [
"plt.imshow(Q)\n",
"plt.colorbar()\n",
"plt.show()"
],
"execution_count": 19,
"outputs": [
{
"output_type": "display_data",
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAUYAAAD4CAYAAACQYE9BAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAfkklEQVR4nO3de5Cc1Xnn8e9vRiN0QzdkCQlhILaCjSlDEgXWZZLC4U5sA7HjwF4s79olO2uq1pu4ErLOAutLFZtdx5tdHBPFVkFcDr6tiZUyMZYJLscuYyNrwYAxliDYkhBShIQuDJLm8uwf7zu41eqZOeftnp7u1u+jemu63z593vN2zzx6L+ecRxGBmZn9Qt90N8DMrNM4MJqZ1XFgNDOr48BoZlbHgdHMrM6M6W5AI0sW98eZpw8klX30hSWZtSu/QalG04uuWLAvq+p9w3OSy/Ypr6dBv9Ibfu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zq9erN18UHXi7XdK/AD+rW70EOBFm6TkR9tP7OP3OiIhXNFOBpK9T7GeKPRFxZTPba6eODIyNSNp0IszneCLsp/fROp1TG5iZ1XFgNDOr002BMTU/RLc7EfbT+2gdrWuuMZqZtUs3HTGambWFA6OZWZ2uCIySrpT0pKStkm6a7vZMBUnPSHpU0sN1+Xe7mqT1knZLeqxm3WJJGyVtKX8ums42NmucfbxV0o7y+3xY0tXT2UbL0/GBUVI/8EngKuAc4AZJ50xvq6bMmyLi/B7r/3YnUN+x9ybg/ohYBdxfPu9md3L8PgJ8ovw+z4+Iexu8bh2q4wMjRZ6YrRHxdEQcBT4PXDPNbbJE46TfvQa4q3x8F3BtWxvVYhOlGLbu1A2B8TRgW83z7eW6XpOcarYHLIuIneXj5yjSYPSiGyX9qDzV7urLBSeabgiMJ4qLIuJXKS4ZvF/Sb053g9ohiv5ivdhn7FPAq4DzgZ3Ax6e3OZajGwLjDuD0mucry3U95QRLNbtL0nKA8ufuaW5Py0XErogYiSLx8l/T299nz+mGwPgQsErSWZJmUmQn3DDNbWqpEzDV7AZgTfl4DfDVaWzLlBgL/KXr6O3vs+d0/HyMETEs6UbgPqAfWB8Rj09zs1ptGXCPJCi+k7/tlVSzZfrdi4ElkrYDtwC3AV8sU/H+DHjH9LWweePs48WSzqe4TPAM8N5pa6Bl85BAM7M63XAqbWbWVg6MZmZ1HBjNzOo4MJqZ1XFgNDOr48BoZlbHgdHMrM7/B+/aQBZoBF76AAAAAElFTkSuQmCC\n",
"text/plain": [
"<Figure size 432x288 with 2 Axes>"
]
},
"metadata": {
"tags": [],
"needs_background": "light"
}
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "a_Aj3gnNaavL"
},
"source": [
"一度だけの結果では、真似をするには限界がありますので、\n",
"これを繰り返します。\n",
"量子アニーリングマシンにQUBO行列を渡して、\n",
"Qの更新を繰り返すようにfor文を利用してみましょう。\n",
"\n",
"毎回毎回更新された様子を見るためにclear_outputを利用して、出力結果のアニメーションを実行します。"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 265
},
"id": "pN3ee3ZIE9lP",
"outputId": "47d686a5-ce36-4019-98de-63db8d42a2da"
},
"source": [
"from IPython.display import clear_output\n",
"\n",
"Tall = 50\n",
"for t in range(Tall):\n",
" sampleset = sampler.sample_qubo(Qmat,num_reads=Nsample, answer_mode = \"raw\")\n",
" x = np.zeros(Nsample*M).reshape(Nsample,M)\n",
" for k in range(Nsample):\n",
" x[k,:] = sampleset.record[k][0]\n",
" Xmat = np.dot(x.T,x)/Nsample\n",
" Q = Q - eta*(Zmat-Xmat)\n",
" Qmat = Q + Qc\n",
"\n",
" clear_output(True)\n",
" plt.imshow(Q)\n",
" plt.colorbar()\n",
" plt.show()\n"
],
"execution_count": 20,
"outputs": [
{
"output_type": "display_data",
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAUAAAAD4CAYAAACdfj8GAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAbhUlEQVR4nO3dfZAd1Xnn8e9vRu9CrxavEm/GWrJKdlGICpcrbAqwjQVFoTixE5StXZwlJZKyajdb+4bXVZBia6uc2vW6kkCZKEYFpBJg17vEyloxJnZSmLIdMyYy7wSh4mWEkNALEpKQNDP32T9uD3s93NGc0/fOTN/p34fqmnv7njl9uu/oobvPefooIjAzq6O+6W6Amdl0cQA0s9pyADSz2nIANLPacgA0s9qaNd0NaOeMZXNi+cp5SWX3Hl08ya3JMKLkonMXnMqqelZfI7ns0Eh/Vt2NSG/38FBe3fSljzJQRlnIOyYjjbz/10fGMZHy2h0n0o9h9E/OKI3hA4cYOXosfSfb+NTVC+PAwZGksj9++uSjEbG+k+1NhkoGwOUr5/Efvr4uqex/f2ISj2nmn0f/O+mH85LL38iqe9m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5hJtos6eniErmnL/Nnql4IgHVxZURcTvNS//OSfmm6GzQVojkOayaOxfoqcAmwFtgDfHl6m2Pt9EIA3A2c3/J+VbFuRqnZFKJ7JZ0LUPzcN83t6bqI2BsRI9GcOPdPmNnfZ8/qhQD4JLBa0sWS5tCcjW7bNLepq2o4heg24Obi9c3AN6axLZNiNMAXPs3M/j57VuWfBxgRw5I2A48C/cDWiHhumpvVbWcDj0iC5nfy5zNlCtFiWtWrgBWSBoE7gC8B/7OYYvU14Nemr4WdG2cfr5K0lubl/avArdPWQBuXU+HMrLZ64RLYzGxSOACaWW05AJpZbTkAmlltOQCaWW05AJpZbTkAmllt/T8ImPFoRuT1HgAAAABJRU5ErkJggg==\n",
"text/plain": [
"<Figure size 432x288 with 2 Axes>"
]
},
"metadata": {
"tags": [],
"needs_background": "light"
}
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "mPoTcXjobr8N"
},
"source": [
"例えば最初の項目で選択肢1番を答えた人は\n",
"次の項目でどんな選択肢を選ぶ傾向にあるのかを調べてみましょう。\n",
"Q[0,k]を見ることで調べることができます。"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Nx7JhFi-KAsg",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "f4af1729-5733-4a01-f850-d0592016b328"
},
"source": [
"print(Q[0,4],Q[0,5],Q[0,6],Q[0,7])"
],
"execution_count": 22,
"outputs": [
{
"output_type": "stream",
"text": [
"-0.1490243902439025 0.44807317073170677 0.5425365853658537 0.4043414634146347\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_qbLpiIFb_Yz"
},
"source": [
"最初の選択肢に負の数で大きさの大きなものが登場します。\n",
"$Q_{mn}$が負の数ですから$x_mx_n=1$になる傾向があるということで、\n",
"最初の選択肢を答える傾向にあるということがわかります。"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WRu-PtipcVx0"
},
"source": [
"次に最初の項目で選択肢2番を答えた人は、どうでしょうか。"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "mRdO5mBjbY2Z",
"outputId": "278ae0d8-7690-4530-b4b6-a51ee5bc9258"
},
"source": [
"print(Q[1,4],Q[1,5],Q[1,6],Q[1,7])"
],
"execution_count": 23,
"outputs": [
{
"output_type": "stream",
"text": [
"-0.08809756097560975 -0.10221951219512196 0.3956097560975612 0.33907317073170706\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1kB6jyjocZ3V"
},
"source": [
"次の項目では最初だけでなく2番目の選択肢も選ぶ傾向にあるようです。\n",
"\n",
"最初の選択肢で選択肢3番を選んだ人は次のようになります。"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "qU5BqTmscUXz",
"outputId": "f8645149-00fd-4a1c-f595-4598f5f27b21"
},
"source": [
"print(Q[2,4],Q[2,5],Q[2,6],Q[2,7])"
],
"execution_count": 24,
"outputs": [
{
"output_type": "stream",
"text": [
"0.018341463414634145 -0.012585365853658527 -0.16717073170731714 0.14426829268292685\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cld_rvgscsIp"
},
"source": [
"こちらは$3$番目の選択肢を選ぶ傾向にあります。"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "o5RVZvjpcv9b"
},
"source": [
"最後に選択肢4番を選んだ人は次の通り。"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ir0QFzmKck34",
"outputId": "74d69c6a-2cc5-4e56-d8fb-efe945869eb6"
},
"source": [
"print(Q[3,4],Q[3,5],Q[3,6],Q[3,7])"
],
"execution_count": 25,
"outputs": [
{
"output_type": "stream",
"text": [
"-0.03882926829268289 -0.0109512195121951 -0.7186829268292686 0.385073170731707\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "sw0pV2k_c7Vc"
},
"source": [
"おお、3番目の選択肢を選ぶ傾向にありますね。\n",
"このようにアンケート結果を反映した$Q_{ij}$を知ることができ、データの分析に利用することもできます。\n",
"また量子アニーリングマシンからは、元にしたデータに似たような傾向を示すものを出力させることができます。\n",
"そうした人工の生成モデルを作ることがボルツマンマシンでできるようになります。\n",
"そのボルツマンマシンから人工データを生成するためには通常マルコフ連鎖モンテカルロ法などの手法を適用して計算をする必要があるのですが、その代わりを量子アニーリングマシンが担うことも可能です。"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment