Created
October 31, 2018 05:55
-
-
Save piyo7/7a27392033c0f0d27145816e625693cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BinaryQuadraticModel({'aux0': -1.0, 'q0': -0.5, 'q1': 0.0, 'q3': 0.0, 'aux1': -1.0, 'q2': 0.5, 'q4': 1.0, 'aux2': -1.0, 'q5': 0.5}, {('q0', 'aux0'): 1.0, ('q1', 'aux0'): -1.0, ('q1', 'q0'): 0.5, ('q3', 'aux0'): -1.0, ('q3', 'q0'): 0.5, ('q3', 'q1'): 1.5, ('q1', 'aux1'): 1.0, ('q2', 'aux1'): -1.0, ('q2', 'q1'): 0.5, ('q4', 'aux1'): -1.0, ('q4', 'q1'): 0.5, ('q4', 'q2'): 1.5, ('q3', 'aux2'): 1.0, ('q4', 'aux2'): -1.0, ('q4', 'q3'): 0.5, ('q5', 'aux2'): -1.0, ('q5', 'q3'): 0.5, ('q5', 'q4'): 1.5, ('q2', 'q0'): 1.0, ('q3', 'q2'): 1.0, ('q4', 'q0'): 1.0, ('q5', 'q0'): 1.0, ('q5', 'q1'): 1.0, ('q5', 'q2'): 1.0}, 0.0, Vartype.SPIN)" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import dwavebinarycsp as dbc\n", | |
"\n", | |
"csp = dbc.ConstraintSatisfactionProblem(dbc.SPIN)\n", | |
"tri_constraint = lambda x, y, z: x*y*z == 1\n", | |
"csp.add_constraint(tri_constraint, ['q0', 'q1', 'q3'])\n", | |
"csp.add_constraint(tri_constraint, ['q1', 'q2', 'q4'])\n", | |
"csp.add_constraint(tri_constraint, ['q3', 'q4', 'q5'])\n", | |
"csp.add_constraint(lambda *xs: sum(xs) == 0, ['q0', 'q1', 'q2', 'q3', 'q4', 'q5'])\n", | |
"bqm = dbc.stitch(csp, min_classical_gap=.1)\n", | |
"\n", | |
"bqm" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$$1.0 aux_{0} q_{0} - 1.0 aux_{0} q_{1} - 1.0 aux_{0} q_{3} - 1.0 aux_{0} + 1.0 aux_{1} q_{1} - 1.0 aux_{1} q_{2} - 1.0 aux_{1} q_{4} - 1.0 aux_{1} + 1.0 aux_{2} q_{3} - 1.0 aux_{2} q_{4} - 1.0 aux_{2} q_{5} - 1.0 aux_{2} + 0.5 q_{0} q_{1} + 1.0 q_{0} q_{2} + 0.5 q_{0} q_{3} + 1.0 q_{0} q_{4} + 1.0 q_{0} q_{5} - 0.5 q_{0} + 0.5 q_{1} q_{2} + 1.5 q_{1} q_{3} + 0.5 q_{1} q_{4} + 1.0 q_{1} q_{5} + 1.0 q_{2} q_{3} + 1.5 q_{2} q_{4} + 1.0 q_{2} q_{5} + 0.5 q_{2} + 0.5 q_{3} q_{4} + 0.5 q_{3} q_{5} + 1.5 q_{4} q_{5} + 1.0 q_{4} + 0.5 q_{5}$$" | |
], | |
"text/plain": [ | |
"1.0⋅aux₀⋅q₀ - 1.0⋅aux₀⋅q₁ - 1.0⋅aux₀⋅q₃ - 1.0⋅aux₀ + 1.0⋅aux₁⋅q₁ - 1.0⋅aux₁⋅q₂\n", | |
" - 1.0⋅aux₁⋅q₄ - 1.0⋅aux₁ + 1.0⋅aux₂⋅q₃ - 1.0⋅aux₂⋅q₄ - 1.0⋅aux₂⋅q₅ - 1.0⋅aux₂\n", | |
" + 0.5⋅q₀⋅q₁ + 1.0⋅q₀⋅q₂ + 0.5⋅q₀⋅q₃ + 1.0⋅q₀⋅q₄ + 1.0⋅q₀⋅q₅ - 0.5⋅q₀ + 0.5⋅q₁\n", | |
"⋅q₂ + 1.5⋅q₁⋅q₃ + 0.5⋅q₁⋅q₄ + 1.0⋅q₁⋅q₅ + 1.0⋅q₂⋅q₃ + 1.5⋅q₂⋅q₄ + 1.0⋅q₂⋅q₅ + \n", | |
"0.5⋅q₂ + 0.5⋅q₃⋅q₄ + 0.5⋅q₃⋅q₅ + 1.5⋅q₄⋅q₅ + 1.0⋅q₄ + 0.5⋅q₅" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import sympy\n", | |
"expr = bqm.offset\n", | |
"for symb, coef in bqm.linear.items():\n", | |
" expr += sympy.symbols(symb) * coef\n", | |
"for (symb1, symb2), coef in bqm.quadratic.items():\n", | |
" expr += sympy.symbols(symb1) * sympy.symbols(symb2) * coef\n", | |
"\n", | |
"sympy.init_printing()\n", | |
"expr" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"True -9.0 {'aux0': 1, 'aux1': 1, 'aux2': 1, 'q0': -1, 'q1': 1, 'q2': 1, 'q3': -1, 'q4': 1, 'q5': -1}\n", | |
"True -9.0 {'aux0': 1, 'aux1': 1, 'aux2': 1, 'q0': -1, 'q1': -1, 'q2': -1, 'q3': 1, 'q4': 1, 'q5': 1}\n", | |
"True -9.0 {'aux0': -1, 'aux1': 1, 'aux2': 1, 'q0': 1, 'q1': -1, 'q2': 1, 'q3': -1, 'q4': -1, 'q5': 1}\n", | |
"True -9.0 {'aux0': 1, 'aux1': -1, 'aux2': -1, 'q0': 1, 'q1': 1, 'q2': -1, 'q3': 1, 'q4': -1, 'q5': -1}\n" | |
] | |
} | |
], | |
"source": [ | |
"import dimod\n", | |
"import itertools\n", | |
"\n", | |
"res = dimod.ExactSolver().sample(bqm)\n", | |
"min_energy = next(res.data()).energy\n", | |
"for sample in itertools.takewhile(lambda sample: sample.energy == min_energy, res.data()):\n", | |
" print(csp.check(sample.sample), sample.energy, sample.sample)" | |
] | |
} | |
], | |
"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.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment