Skip to content

Commit

Permalink
working bqm tsp example
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Jul 8, 2024
1 parent af1d013 commit c04f9fd
Showing 1 changed file with 50 additions and 57 deletions.
107 changes: 50 additions & 57 deletions docs/source/example_bqm_tsp.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,9 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 11,
"metadata": {},
"outputs": [
{
"ename": "ExpressionNotHandledException",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mExpressionNotHandledException\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[1], line 18\u001b[0m\n\u001b[1;32m 14\u001b[0m dst_sum \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m dst_matrix[oim][oi]\n\u001b[1;32m 15\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m dst_sum\n\u001b[0;32m---> 18\u001b[0m tsp_f \u001b[38;5;241m=\u001b[39m \u001b[43mtsp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbind\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdst_matrix\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 19\u001b[0m tsp_f\u001b[38;5;241m.\u001b[39mexpressions\n\u001b[1;32m 20\u001b[0m \u001b[38;5;66;03m# bqm = tsp_f.to_bqm()\u001b[39;00m\n",
"File \u001b[0;32m~/.pyenv/versions/3.10.13/envs/qlasskit_310-env/lib/python3.10/site-packages/qlasskit/qlassfun.py:110\u001b[0m, in \u001b[0;36mUnboundQlassf.bind\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 106\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124moriginal_f is not available in python notebooks!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 108\u001b[0m original_f \u001b[38;5;241m=\u001b[39m orig\n\u001b[0;32m--> 110\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_do_translate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfun_ast\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moriginal_f\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/.pyenv/versions/3.10.13/envs/qlasskit_310-env/lib/python3.10/site-packages/qlasskit/qlassfun.py:297\u001b[0m, in \u001b[0;36mQlassF.from_function.<locals>._do_translate\u001b[0;34m(fun_ast, original_f)\u001b[0m\n\u001b[1;32m 295\u001b[0m fun \u001b[38;5;241m=\u001b[39m ast2ast(fun_ast\u001b[38;5;241m.\u001b[39mbody[\u001b[38;5;241m0\u001b[39m])\n\u001b[1;32m 296\u001b[0m \u001b[38;5;66;03m# print(ast.dump(fun, indent=4))\u001b[39;00m\n\u001b[0;32m--> 297\u001b[0m fun_name, args, fun_ret, exps \u001b[38;5;241m=\u001b[39m \u001b[43mtranslate_ast\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfun\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtypes\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdefs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 299\u001b[0m exps \u001b[38;5;241m=\u001b[39m bool_optimizer\u001b[38;5;241m.\u001b[39mapply(exps)\n\u001b[1;32m 301\u001b[0m \u001b[38;5;66;03m# Return the qlassf object\u001b[39;00m\n",
"File \u001b[0;32m~/.pyenv/versions/3.10.13/envs/qlasskit_310-env/lib/python3.10/site-packages/qlasskit/ast2logic/t_ast.py:49\u001b[0m, in \u001b[0;36mtranslate_ast\u001b[0;34m(fun, types, defs)\u001b[0m\n\u001b[1;32m 47\u001b[0m exps \u001b[38;5;241m=\u001b[39m []\n\u001b[1;32m 48\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m stmt \u001b[38;5;129;01min\u001b[39;00m fun\u001b[38;5;241m.\u001b[39mbody:\n\u001b[0;32m---> 49\u001b[0m s_exps, env \u001b[38;5;241m=\u001b[39m \u001b[43mtranslate_statement\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstmt\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43menv\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mret_\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mttype\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 50\u001b[0m exps\u001b[38;5;241m.\u001b[39mappend(s_exps)\n\u001b[1;32m 52\u001b[0m exps_flat \u001b[38;5;241m=\u001b[39m flatten(exps)\n",
"File \u001b[0;32m~/.pyenv/versions/3.10.13/envs/qlasskit_310-env/lib/python3.10/site-packages/qlasskit/ast2logic/t_statement.py:58\u001b[0m, in \u001b[0;36mtranslate_statement\u001b[0;34m(stmt, env, ret_type)\u001b[0m\n\u001b[1;32m 52\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exceptions\u001b[38;5;241m.\u001b[39mStatementNotHandledException(\n\u001b[1;32m 53\u001b[0m stmt, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124monly name target supported\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 54\u001b[0m )\n\u001b[1;32m 56\u001b[0m target \u001b[38;5;241m=\u001b[39m stmt\u001b[38;5;241m.\u001b[39mtargets[\u001b[38;5;241m0\u001b[39m]\u001b[38;5;241m.\u001b[39mid\n\u001b[0;32m---> 58\u001b[0m tval, val \u001b[38;5;241m=\u001b[39m \u001b[43mtranslate_expression\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstmt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalue\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43menv\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# TODO: typecheck\u001b[39;00m\n\u001b[1;32m 59\u001b[0m res \u001b[38;5;241m=\u001b[39m decompose_to_symbols(val, \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtarget\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 61\u001b[0m env\u001b[38;5;241m.\u001b[39mbind(Binding(target, tval, [x[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;28;01mfor\u001b[39;00m x \u001b[38;5;129;01min\u001b[39;00m res]), rebind\u001b[38;5;241m=\u001b[39mtarget \u001b[38;5;129;01min\u001b[39;00m env)\n",
"File \u001b[0;32m~/.pyenv/versions/3.10.13/envs/qlasskit_310-env/lib/python3.10/site-packages/qlasskit/ast2logic/t_expression.py:74\u001b[0m, in \u001b[0;36mtranslate_expression\u001b[0;34m(expr, env)\u001b[0m\n\u001b[1;32m 71\u001b[0m _sval \u001b[38;5;241m=\u001b[39m expr\u001b[38;5;241m.\u001b[39mslice \u001b[38;5;66;03m# type: ignore\u001b[39;00m\n\u001b[1;32m 73\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(_sval, ast\u001b[38;5;241m.\u001b[39mConstant):\n\u001b[0;32m---> 74\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exceptions\u001b[38;5;241m.\u001b[39mExpressionNotHandledException(expr)\n\u001b[1;32m 76\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(expr\u001b[38;5;241m.\u001b[39mvalue, ast\u001b[38;5;241m.\u001b[39mName):\n\u001b[1;32m 77\u001b[0m sn \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mexpr\u001b[38;5;241m.\u001b[39mvalue\u001b[38;5;241m.\u001b[39mid\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m_sval\u001b[38;5;241m.\u001b[39mvalue\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n",
"\u001b[0;31mExpressionNotHandledException\u001b[0m: "
]
}
],
"outputs": [],
"source": [
"# Encode the TSP using qlasskit and parameters\n",
"\n",
Expand All @@ -37,63 +20,73 @@
"\n",
"@qlassf\n",
"def tsp(\n",
" dst_matrix: Parameter[Qmatrix[Qint[3], 3, 3]], order: Qlist[Qint[2], 3]\n",
") -> Qint[4]:\n",
" dst_matrix: Parameter[Qmatrix[Qint[4], 3, 3]], order: Qlist[Qint[2], 3]\n",
") -> Qint[6]:\n",
" dst_sum = Qint4(0)\n",
" assertion = False\n",
" \n",
" if sum(order) != 3:\n",
" assertion = True\n",
"\n",
" for i in range(2):\n",
" oim = order[i]\n",
" oi = order[i + 1]\n",
" dst_sum += dst_matrix[oim][oi]\n",
" dst_sum += dst_matrix[oim][oi] if not assertion else 0xF\n",
" return dst_sum\n",
"\n",
"\n",
"tsp_f = tsp.bind(dst_matrix=[[0, 1, 1], [1, 0, 1], [1, 1, 0]])\n",
"tsp_f.expressions\n",
"# bqm = tsp_f.to_bqm()"
"dst_matrix=[\n",
" [0xF, 1, 4], \n",
" [1, 0xF, 1], \n",
" [1, 1, 0xF]\n",
"]\n",
"tsp_f = tsp.bind(dst_matrix=dst_matrix)\n",
"# tsp_f.expressions"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 12,
"metadata": {},
"outputs": [
{
"ename": "ExpressionNotHandledException",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mExpressionNotHandledException\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[2], line 17\u001b[0m\n\u001b[1;32m 13\u001b[0m dst_sum \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m dst_matrix[v]\n\u001b[1;32m 14\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m dst_sum\n\u001b[0;32m---> 17\u001b[0m tsp_f \u001b[38;5;241m=\u001b[39m \u001b[43mtsp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbind\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdst_matrix\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 18\u001b[0m tsp_f\u001b[38;5;241m.\u001b[39mexpressions\n\u001b[1;32m 19\u001b[0m \u001b[38;5;66;03m# bqm = tsp_f.to_bqm()\u001b[39;00m\n",
"File \u001b[0;32m~/.pyenv/versions/3.10.13/envs/qlasskit_310-env/lib/python3.10/site-packages/qlasskit/qlassfun.py:110\u001b[0m, in \u001b[0;36mUnboundQlassf.bind\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 106\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124moriginal_f is not available in python notebooks!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 108\u001b[0m original_f \u001b[38;5;241m=\u001b[39m orig\n\u001b[0;32m--> 110\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_do_translate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfun_ast\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moriginal_f\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/.pyenv/versions/3.10.13/envs/qlasskit_310-env/lib/python3.10/site-packages/qlasskit/qlassfun.py:297\u001b[0m, in \u001b[0;36mQlassF.from_function.<locals>._do_translate\u001b[0;34m(fun_ast, original_f)\u001b[0m\n\u001b[1;32m 295\u001b[0m fun \u001b[38;5;241m=\u001b[39m ast2ast(fun_ast\u001b[38;5;241m.\u001b[39mbody[\u001b[38;5;241m0\u001b[39m])\n\u001b[1;32m 296\u001b[0m \u001b[38;5;66;03m# print(ast.dump(fun, indent=4))\u001b[39;00m\n\u001b[0;32m--> 297\u001b[0m fun_name, args, fun_ret, exps \u001b[38;5;241m=\u001b[39m \u001b[43mtranslate_ast\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfun\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtypes\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdefs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 299\u001b[0m exps \u001b[38;5;241m=\u001b[39m bool_optimizer\u001b[38;5;241m.\u001b[39mapply(exps)\n\u001b[1;32m 301\u001b[0m \u001b[38;5;66;03m# Return the qlassf object\u001b[39;00m\n",
"File \u001b[0;32m~/.pyenv/versions/3.10.13/envs/qlasskit_310-env/lib/python3.10/site-packages/qlasskit/ast2logic/t_ast.py:49\u001b[0m, in \u001b[0;36mtranslate_ast\u001b[0;34m(fun, types, defs)\u001b[0m\n\u001b[1;32m 47\u001b[0m exps \u001b[38;5;241m=\u001b[39m []\n\u001b[1;32m 48\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m stmt \u001b[38;5;129;01min\u001b[39;00m fun\u001b[38;5;241m.\u001b[39mbody:\n\u001b[0;32m---> 49\u001b[0m s_exps, env \u001b[38;5;241m=\u001b[39m \u001b[43mtranslate_statement\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstmt\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43menv\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mret_\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mttype\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 50\u001b[0m exps\u001b[38;5;241m.\u001b[39mappend(s_exps)\n\u001b[1;32m 52\u001b[0m exps_flat \u001b[38;5;241m=\u001b[39m flatten(exps)\n",
"File \u001b[0;32m~/.pyenv/versions/3.10.13/envs/qlasskit_310-env/lib/python3.10/site-packages/qlasskit/ast2logic/t_statement.py:58\u001b[0m, in \u001b[0;36mtranslate_statement\u001b[0;34m(stmt, env, ret_type)\u001b[0m\n\u001b[1;32m 52\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exceptions\u001b[38;5;241m.\u001b[39mStatementNotHandledException(\n\u001b[1;32m 53\u001b[0m stmt, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124monly name target supported\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 54\u001b[0m )\n\u001b[1;32m 56\u001b[0m target \u001b[38;5;241m=\u001b[39m stmt\u001b[38;5;241m.\u001b[39mtargets[\u001b[38;5;241m0\u001b[39m]\u001b[38;5;241m.\u001b[39mid\n\u001b[0;32m---> 58\u001b[0m tval, val \u001b[38;5;241m=\u001b[39m \u001b[43mtranslate_expression\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstmt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalue\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43menv\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# TODO: typecheck\u001b[39;00m\n\u001b[1;32m 59\u001b[0m res \u001b[38;5;241m=\u001b[39m decompose_to_symbols(val, \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtarget\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 61\u001b[0m env\u001b[38;5;241m.\u001b[39mbind(Binding(target, tval, [x[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;28;01mfor\u001b[39;00m x \u001b[38;5;129;01min\u001b[39;00m res]), rebind\u001b[38;5;241m=\u001b[39mtarget \u001b[38;5;129;01min\u001b[39;00m env)\n",
"File \u001b[0;32m~/.pyenv/versions/3.10.13/envs/qlasskit_310-env/lib/python3.10/site-packages/qlasskit/ast2logic/t_expression.py:74\u001b[0m, in \u001b[0;36mtranslate_expression\u001b[0;34m(expr, env)\u001b[0m\n\u001b[1;32m 71\u001b[0m _sval \u001b[38;5;241m=\u001b[39m expr\u001b[38;5;241m.\u001b[39mslice \u001b[38;5;66;03m# type: ignore\u001b[39;00m\n\u001b[1;32m 73\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(_sval, ast\u001b[38;5;241m.\u001b[39mConstant):\n\u001b[0;32m---> 74\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exceptions\u001b[38;5;241m.\u001b[39mExpressionNotHandledException(expr)\n\u001b[1;32m 76\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(expr\u001b[38;5;241m.\u001b[39mvalue, ast\u001b[38;5;241m.\u001b[39mName):\n\u001b[1;32m 77\u001b[0m sn \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mexpr\u001b[38;5;241m.\u001b[39mvalue\u001b[38;5;241m.\u001b[39mid\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m_sval\u001b[38;5;241m.\u001b[39mvalue\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n",
"\u001b[0;31mExpressionNotHandledException\u001b[0m: "
"name": "stdout",
"output_type": "stream",
"text": [
"Vars: 18 \n",
"Interactions: 81\n"
]
}
],
"source": [
"# Encode the TSP using qlasskit and parameters\n",
"\n",
"from qlasskit import qlassf, Parameter, Qlist, Qmatrix, Qint\n",
"\n",
"\n",
"@qlassf\n",
"def tsp(dst_matrix: Parameter[Qlist[Qint[3], 9]], order: Qlist[Qint[2], 3]) -> Qint[4]:\n",
" dst_sum = Qint4(0)\n",
" for i in range(2):\n",
" oim = order[i]\n",
" oi = order[i + 1]\n",
" v = oim * 3 + oi\n",
" dst_sum += dst_matrix[v]\n",
" return dst_sum\n",
"\n",
"bqm = tsp_f.to_bqm()\n",
"print(\"Vars:\", bqm.num_variables, \"\\nInteractions:\", bqm.num_interactions)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[DecodedSample(1.0, {'order': (2, 0, 1)}), DecodedSample(1.0, {'order': (1, 2, 0)}), DecodedSample(1.0, {'order': (1, 2, 0)}), DecodedSample(3.0, {'order': (0, 3, 2)}), DecodedSample(3.0, {'order': (2, 2, 2)}), DecodedSample(3.0, {'order': (2, 3, 2)}), DecodedSample(3.0, {'order': (1, 3, 0)}), DecodedSample(3.0, {'order': (3, 0, 3)}), DecodedSample(3.0, {'order': (3, 2, 0)}), DecodedSample(3.0, {'order': (3, 2, 3)})]\n",
"{'order': (2, 0, 1)}\n"
]
}
],
"source": [
"import neal\n",
"from qlasskit.bqm import decode_samples\n",
"\n",
"tsp_f = tsp.bind(dst_matrix=[0, 1, 1, 1, 0, 1, 1, 1, 0])\n",
"tsp_f.expressions\n",
"# bqm = tsp_f.to_bqm()"
"sa = neal.SimulatedAnnealingSampler()\n",
"sampleset = sa.sample(bqm, num_reads=10)\n",
"decoded_samples = decode_samples(tsp_f, sampleset)\n",
"print(decoded_samples)\n",
"best_sample = min(decoded_samples, key=lambda x: x.energy)\n",
"print(best_sample.sample)\n"
]
}
],
Expand Down

0 comments on commit c04f9fd

Please sign in to comment.