forked from rigetti/grove
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d9f0bd
commit e2c2a0e
Showing
8 changed files
with
193 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,3 +90,6 @@ ENV/ | |
|
||
# Pycharm | ||
.idea/ | ||
|
||
# converted Ipynb files | ||
examples/*.py |
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,14 @@ | ||
#!/bin/bash -e | ||
|
||
|
||
pushd ./examples | ||
echo "cleaning old compiled notebooks" | ||
rm -f *.py | ||
|
||
echo "Converting notebooks" | ||
jupyter nbconvert --to script *.ipynb | ||
|
||
echo "Running notebooks" | ||
python *.py | ||
|
||
popd |
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,164 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"This Notebook is a brief sketch of how to use Simon's algorithm.\n", | ||
"We start by declaring all necessary imports." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from collections import defaultdict\n", | ||
"from pyquil.api import SyncConnection\n", | ||
"import numpy as np\n", | ||
"from mock import patch\n", | ||
"\n", | ||
"from grove.simon.simon import Simon, create_valid_2to1_bitmap" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Simon's algorithm can be used to find the mask $m$ of a 2-to-1 periodic Boolean function defined by\n", | ||
"$$f(x) = f(x \\oplus m)$$\n", | ||
"where $\\oplus$ is the bit-wise XOR operator. To create one we can define a mask as a string and call a utility to generate a map. To assert the correct result we check it against an expected map" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"mask = '110'\n", | ||
"bm = create_valid_2to1_bitmap(mask, random_seed=42)\n", | ||
"expected_map = {\n", | ||
" '000': '001',\n", | ||
" '001': '101',\n", | ||
" '010': '000',\n", | ||
" '011': '111',\n", | ||
" '100': '000',\n", | ||
" '101': '111',\n", | ||
" '110': '001',\n", | ||
" '111': '101'\n", | ||
"}\n", | ||
"for k, v in bm.items():\n", | ||
" assert v == expected_map[k]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"To understand what a 2-to-1 function is let us revert the map and collect all keys tha point to the same value. As the assertion shows all values have 2 distinct origins" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"reverse_bitmap = defaultdict(list)\n", | ||
"for k, v in bm.items():\n", | ||
" reverse_bitmap[v].append(k)\n", | ||
"\n", | ||
"expected_reverse_bitmap = {\n", | ||
" '001': ['000', '110'],\n", | ||
" '101': ['001', '111'],\n", | ||
" '000': ['010', '100'],\n", | ||
" '111': ['011', '101']\n", | ||
"}\n", | ||
"\n", | ||
"for k, v in reverse_bitmap.items():\n", | ||
" assert sorted(v) == sorted(expected_reverse_bitmap[k])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"To use Simon's algorithm on a Quantum Hardware we need to define the connection to the QVM or QPU. However we don't have a real connection in this notebook, so we just mock out the response. If you run this notebook, ensure to replace `cxn` with a pyQuil connection object." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"with patch(\"pyquil.api.SyncConnection\") as cxn:\n", | ||
" # Need to mock multiple returns as an iterable\n", | ||
" cxn.run_and_measure.side_effect = [\n", | ||
" (np.asarray([0, 1, 1], dtype=int), ),\n", | ||
" (np.asarray([1, 1, 1], dtype=int), ),\n", | ||
" (np.asarray([1, 1, 1], dtype=int), ),\n", | ||
" (np.asarray([1, 0, 0], dtype=int), ),\n", | ||
" ]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Now let's run Simon's algorithm. We instantiate the Simon object and then call its `find_mask` mehtod with the connection object and the 2-to-1 function whose mask we wish to find.\n", | ||
"Finally we assert its correctness by checking with the mask we used to generate the function" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"sa = Simon()\n", | ||
"found_mask = sa.find_mask(cxn, bm)\n", | ||
"assert ''.join([str(b) for b in found_mask]) == mask, \"Found mask is not expected mask\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"If the assertion succeeded we know we found the mask and we got our result." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 2", | ||
"language": "python", | ||
"name": "python2" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 2 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython2", | ||
"version": "2.7.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
This file was deleted.
Oops, something went wrong.
Empty file.
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