{ "cells": [ { "cell_type": "markdown", "id": "dab9797a-d695-430d-9345-801697b584db", "metadata": {}, "source": [ "# Dynex SDK - Hello World " ] }, { "cell_type": "markdown", "id": "8d5c69b7", "metadata": {}, "source": [ "Please copy \"dynex.ini\" (your configuration file) in the working directory before using this example! The file can be downloaded in your market place, section \"INSTALLATON\" - \"STEP 2\" - button \"DOWNLOAD\"." ] }, { "cell_type": "code", "execution_count": null, "id": "ce9c9138-5168-49a6-953b-69dd0c2cd297", "metadata": {}, "outputs": [], "source": [ "import dynex\n", "import dimod\n", "from pyqubo import Array" ] }, { "cell_type": "markdown", "id": "e3d50bf1-174a-49a2-8e4a-4b38d755e3b9", "metadata": {}, "source": [ "## Verify Version of Dynex SDK" ] }, { "cell_type": "code", "execution_count": null, "id": "cf95e0fb-fd5e-42ef-a802-eaf70fb5eee7", "metadata": {}, "outputs": [], "source": [ "dynex.__version__" ] }, { "cell_type": "markdown", "id": "bc8795b4-bfd8-486e-b2e0-d98785b2aa49", "metadata": {}, "source": [ "## Test your configuration" ] }, { "cell_type": "markdown", "id": "2e763f4f-d24f-4ca7-93a3-cdab70950a0a", "metadata": {}, "source": [ "Successful run is required to start sampling with the Dynex SDK" ] }, { "cell_type": "code", "execution_count": null, "id": "442e7b70-7213-430c-82ef-5b165d1f382a", "metadata": {}, "outputs": [], "source": [ "dynex.test()" ] }, { "cell_type": "markdown", "id": "fced7cc0-66eb-4269-b7b4-b284516dcb3e", "metadata": {}, "source": [ "## View your API acccount" ] }, { "cell_type": "code", "execution_count": null, "id": "31e09dcd-a9bb-4d7a-80e0-f343faa3f3d1", "metadata": {}, "outputs": [], "source": [ "dynex.account_status()" ] }, { "cell_type": "markdown", "id": "1e49bfb6-7181-46e1-85a2-af8c051d9139", "metadata": {}, "source": [ "## Create a simple QUBO formulation" ] }, { "cell_type": "code", "execution_count": null, "id": "aebae30f-3fd8-4f3a-9cd2-bbc9281be89a", "metadata": {}, "outputs": [], "source": [ "N = 15\n", "K = 3\n", "numbers = [4.8097315016016315, 4.325157567810298, 2.9877429101815127,\n", " 3.199880179616316, 0.5787939511978596, 1.2520928214246918,\n", " 2.262867466401502, 1.2300003067401255, 2.1601079352817925,\n", " 3.63753899583021, 4.598232793833491, 2.6215815162575646,\n", " 3.4227134835783364, 0.28254151584552023, 4.2548151473817075]\n", "\n", "q = Array.create('q', N, 'BINARY')\n", "H = sum(numbers[i] * q[i] for i in range(N)) + 5.0 * (sum(q) - K)**2\n", "model = H.compile()\n", "Q, offset = model.to_qubo(index_label=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "d3f7fb3c-69f0-4078-92d7-1910533744b0", "metadata": {}, "outputs": [], "source": [ "# BQM from QUBO:\n", "bqm = dimod.BinaryQuadraticModel.from_qubo(Q, offset)" ] }, { "cell_type": "markdown", "id": "d82490d8-17f9-47c5-a0a2-84bcc6611939", "metadata": {}, "source": [ "## Sample on Dynex" ] }, { "cell_type": "code", "execution_count": null, "id": "9c390424-e2ed-4c12-8d3c-12ce9a86d4af", "metadata": {}, "outputs": [], "source": [ "model = dynex.BQM(bqm);\n", "sampler = dynex.DynexSampler(model, mainnet=False, description='Dynex SDK test');\n", "sampleset = sampler.sample(num_reads=5000, annealing_time = 200, debugging=False);\n", "print('Result:')\n", "print(sampleset)" ] }, { "cell_type": "markdown", "id": "171a368e-bf05-46f7-aaaf-122034f23dcd", "metadata": {}, "source": [ "## Draw Sampleset" ] }, { "cell_type": "code", "execution_count": null, "id": "eb8bd278-3162-4b72-9315-a1c64313e7bb", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "id": "d64d678e-62a4-42a5-90fd-d50d2e493905", "metadata": {}, "outputs": [], "source": [ "# generate data:\n", "data = np.zeros(len(sampleset.variables)) \n", "for s in sampleset.record: #s[0] samples [1] energy [2] num_oc\n", " for i in range(0,len(s[0])):\n", " data[i] += ( s[0][i] * s[2] ); # weighted by num_oc\n", "\n", "plt.bar(sampleset.variables, data)\n", "plt.bar(sampleset.variables, sampleset.first.sample.values())\n", "plt.xlabel(\"Variables\")\n", "plt.ylabel(\"Occurences (positive)\")\n", "plt.title(\"Sampleset (orange: lowest ground state)\")\n", "plt.show() " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }