Skip to content

Instantly share code, notes, and snippets.

@nisuikiba
Created February 24, 2019 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nisuikiba/529edd0a5dd67ac0d4ea734924aae55b to your computer and use it in GitHub Desktop.
Save nisuikiba/529edd0a5dd67ac0d4ea734924aae55b to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from blueqat import opt\n",
"import numpy as np\n",
"from sympy import *"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [],
"source": [
"N = 6 #料理の数\n",
"\n",
"P = 80 #たんぱく質の推奨値\n",
"\n",
"L = 240 #脂質の推奨値\n",
"\n",
"C = 450 #炭水化物の推奨値\n",
"\n",
"E = 780 #エネルギーの推奨値"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [],
"source": [
"protain = [46, 25.6, 28.4, 47.2, 9.6, 14.8]\n",
"lipid = [246.6, 144, 120.6, 169.2, 73.8, 148.5]\n",
"carbon = [29.6, 149.2, 55.2, 59.6, 107.6, 74.8]\n",
"\n",
"energy = []\n",
"for i in range (N):\n",
" energy.append(round(protain[i]+lipid[i]+carbon[i], 1))"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [],
"source": [
"q0, q1, q2, q3, q4, q5 = symbols(\"q0 q1 q2 q3 q4 q5\")\n",
"q = [q0, q1, q2, q3, q4, q5]\n",
"\n",
"#各コスト関数を計算\n",
"cost_p = 0\n",
"cost_l = 0\n",
"cost_c = 0\n",
"cost_e = 0\n",
"\n",
"sigma = 0\n",
"\n",
"for i in range (N):\n",
" sigma += protain[i]*q[i]\n",
"cost_p = (P - sigma)**2\n",
"\n",
"for i in range (N):\n",
" sigma += lipid[i]*q[i]\n",
"cost_l = (L - sigma)**2\n",
"\n",
"for i in range (N):\n",
" sigma += carbon[i]*q[i]\n",
"cost_c = (C - sigma)**2\n",
"\n",
"for i in range (N):\n",
" cost_e += energy[i]*q[i]\n",
"cost_e = (E - sigma)**2"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [],
"source": [
"#展開\n",
"cost_p = expand(cost_p)\n",
"cost_c = expand(cost_c)\n",
"cost_l = expand(cost_l)\n",
"cost_e = expand(cost_e)\n",
"\n",
"#qi**2 = qi\n",
"cost_p = cost_p.subs([(q0**2, q0), (q1**2, q1), (q2**2, q2), (q3**2, q3), (q4**2, q4), (q5**2, q5)])\n",
"cost_c = cost_c.subs([(q0**2, q0), (q1**2, q1), (q2**2, q2), (q3**2, q3), (q4**2, q4), (q5**2, q5)])\n",
"cost_l = cost_l.subs([(q0**2, q0), (q1**2, q1), (q2**2, q2), (q3**2, q3), (q4**2, q4), (q5**2, q5)])\n",
"cost_e = cost_e.subs([(q0**2, q0), (q1**2, q1), (q2**2, q2), (q3**2, q3), (q4**2, q4), (q5**2, q5)])"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [],
"source": [
"#6*6の行列を作成\n",
"qubo_p = np.zeros((6,6))\n",
"qubo_l = np.zeros((6,6))\n",
"qubo_c = np.zeros((6,6))\n",
"qubo_e = np.zeros((6,6))\n",
"\n",
"flag = 0\n",
"\n",
"for i in range (N):\n",
" for j in range(N):\n",
" if i < j:\n",
" qubo_p[i][j] = cost_p.coeff(q[i]*q[j], 1)\n",
" elif j == i:\n",
" flag = cost_p.coeff(q[i], 1) \n",
" qubo_p[i][i] = flag.subs([(q0, 0), (q1, 0), (q2, 0), (q3, 0), (q4, 0), (q5, 0)])\n",
" \n",
"for i in range (N):\n",
" for j in range(N):\n",
" if i < j:\n",
" qubo_l[i][j] = cost_l.coeff(q[i]*q[j], 1)\n",
" elif j == i:\n",
" flag = cost_l.coeff(q[i], 1) \n",
" qubo_l[i][i] = flag.subs([(q0, 0), (q1, 0), (q2, 0), (q3, 0), (q4, 0), (q5, 0)])\n",
" \n",
"for i in range (N):\n",
" for j in range(N):\n",
" if i < j:\n",
" qubo_c[i][j] = cost_c.coeff(q[i]*q[j], 1)\n",
" elif j == i:\n",
" flag = cost_c.coeff(q[i], 1) \n",
" qubo_c[i][i] = flag.subs([(q0, 0), (q1, 0), (q2, 0), (q3, 0), (q4, 0), (q5, 0)])\n",
" \n",
"for i in range (N):\n",
" for j in range(N):\n",
" if i < j:\n",
" qubo_e[i][j] = cost_e.coeff(q[i]*q[j], 1)\n",
" elif j == i:\n",
" flag = cost_e.coeff(q[i], 1) \n",
" qubo_e[i][i] = flag.subs([(q0, 0), (q1, 0), (q2, 0), (q3, 0), (q4, 0), (q5, 0)])"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[-645063.56 512474.56 352980.56 486688.48 295849.68 403788.04]\n",
" [ 0. -637065.6 312390.72 427774.72 272344. 359774.24]\n",
" [ 0. 0. -471993.16 292604.96 181407.28 243984.12]\n",
" [ 0. 0. 0. -588975.2 247865.76 334935.76]\n",
" [ 0. 0. 0. 0. -431418.28 209431. ]\n",
" [ 0. 0. 0. 0. 0. -526208.85]]\n"
]
}
],
"source": [
"qubo = qubo_p + qubo_l + qubo_c + qubo_e\n",
"print(qubo)"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Counter({'000101': 9,\n",
" '010100': 4,\n",
" '101000': 9,\n",
" '010001': 8,\n",
" '010010': 7,\n",
" '100001': 8,\n",
" '000110': 7,\n",
" '100100': 7,\n",
" '011000': 10,\n",
" '001011': 14,\n",
" '110000': 9,\n",
" '100010': 8})"
]
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = opt.opt() \n",
"a.qubo = qubo\n",
"result = a.sa(shots = 100, sampler = \"fast\")\n",
"opt.counter(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment