-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add pyqubo optional dep * add pyqubo deps and doc placeholder * mocking to_bqm * bqm feature implementation and testing * add sampling to bqm testing * add int mul test, fix qlasskit export * fix to_bqm exporter and bqm testing * fix bqm exporting * fix bqm export bit order * upgrade test suite for bqm * fix error logging messages, update todo and readme, bump version, bqm example * minor fixtures * linter fix
- Loading branch information
Showing
21 changed files
with
591 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Binary Quadratic Model: Qubo & Ising " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Any qlassf function can be transformed to a binary quadratic model using the `to_bqm` function. In this example we want factorize the number 15." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Vars: 14 \n", | ||
"Interactions: 41\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from qlasskit import qlassf, Qint4, Qint3\n", | ||
"\n", | ||
"\n", | ||
"@qlassf\n", | ||
"def test_factor(a: Qint3, b: Qint3) -> Qint4:\n", | ||
" return Qint4(15) - (a * b)\n", | ||
"\n", | ||
"\n", | ||
"bqm = test_factor.to_bqm()\n", | ||
"\n", | ||
"print(\"Vars:\", bqm.num_variables, \"\\nInteractions:\", bqm.num_interactions)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"A qlassf function can also be exported as a QUBO model using `to_bqm('qubo')` or as an Ising model using `to_bqm('ising')`." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"\n", | ||
"### Running on simulated sampler annealer" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Now we can run a simulated sampler annealer to minimize this function; qlasskit offer a `decode_samples` helper function that translates sample result to the high level types of qlasskit." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"{'a': 5, 'b': 3}\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import neal\n", | ||
"from qlasskit.bqm import decode_samples\n", | ||
"\n", | ||
"sa = neal.SimulatedAnnealingSampler()\n", | ||
"sampleset = sa.sample(bqm, num_reads=10)\n", | ||
"decoded_samples = decode_samples(test_qubo, sampleset)\n", | ||
"best_sample = min(decoded_samples, key=lambda x: x.energy)\n", | ||
"print(best_sample.sample)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The result is 5 and 3 as expected (since 5 times 3 is equal to 15)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Running on DWave annealer" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "qlasskit_310-env", | ||
"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": 2 | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.