-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from ampolloreno/grover_example_and_docs
Grover example and docs
- Loading branch information
Showing
8 changed files
with
300 additions
and
18 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
MathJax.Hub.Config({ | ||
TeX: { | ||
Macros: { | ||
ket: ["\\left| #1 \\right\\rangle",1], | ||
bra: ["\\left\\langle #1 \\right|",1], | ||
braket: ["\\left\\langle #1 | #2 \\right\\rangle",2] | ||
} | ||
} | ||
}); | ||
MathJax.Ajax.loadComplete("[MathJax]/config/local/local.js"); |
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,5 @@ | ||
{% extends "!layout.html" %} | ||
{% set script_files = script_files + ["_static/mathjax_macros.js"] %} | ||
{% block extrahead %} | ||
{{ super() }} | ||
{% endblock %} |
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,8 @@ | ||
\newcommand{\sket}[1]{\left|\left. #1 \right\rangle\!\right\rangle} | ||
\newcommand{\sbra}[1]{\left\langle\!\left\langle #1 \right.\right|} | ||
\newcommand{\sbraket}[2]{\left\langle\!\left\langle #1 | #2 \right\rangle\!\right\rangle} | ||
\newcommand{\ket}[1]{\left| #1 \right\rangle} | ||
\newcommand{\bra}[1]{\left\langle #1 \right|} | ||
\newcommand{\braket}[2]{\left\langle #1 | #2 \right\rangle} | ||
\renewcommand{\vec}[1]{\text{vec}\left(#1\right)} | ||
\newcommand{\tr}[1]{\text{Tr}\left(#1\right)} |
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,143 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"This Notebook is a brief sketch of how to use Grover's algorithm.\n", | ||
"We start by declaring all necessary imports." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from pyquil.api import SyncConnection\n", | ||
"from itertools import product\n", | ||
"from mock import patch\n", | ||
"\n", | ||
"from grove.amplification.grover import Grover" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Grover's algorithm can be used to amplify the probability of an oracle-selected bitstring in an input superposition state." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"target_bitstring = '010'\n", | ||
"bit = (\"0\", \"1\")\n", | ||
"bitstring_map = {}\n", | ||
"target_bitstring_phase = -1\n", | ||
"nontarget_bitstring_phase = 1\n", | ||
"\n", | ||
"# We construct the bitmap for the oracle\n", | ||
"for bitstring in product(bit, repeat=3):\n", | ||
" bitstring = \"\".join(bitstring)\n", | ||
" if bitstring == target_bitstring:\n", | ||
" bitstring_map[bitstring] = target_bitstring_phase\n", | ||
" else:\n", | ||
" bitstring_map[bitstring] = nontarget_bitstring_phase" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We'll make sure the bitstring_map is as we expect." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"target_bitstring_phase = -1\n", | ||
"nontarget_bitstring_phase = 1\n", | ||
"for k,v in bitstring_map.items():\n", | ||
" if k == target_bitstring:\n", | ||
" assert v == target_bitstring_phase, \"The target bitstring has the wrong phase.\"\n", | ||
" else:\n", | ||
" assert v == nontarget_bitstring_phase, \"A nontarget bistring has the wrong phase.\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"To use Grover's algorithm on quantum hardware we need to define a connection to a QVM or QPU. However we don't have a real connection in this notebook, so we just mock out the response. If you intend to run this notebook on a QVM or QPU, ensure to replace `cxn` with a pyQuil connection object." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"with patch(\"pyquil.api.SyncConnection\") as cxn:\n", | ||
" cxn.run_and_measure.return_value = [[int(bit) for bit in target_bitstring]]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Now let's run Grover's algorithm. We instantiate the Grover object and then call its `find_bitstring` method.\n", | ||
"Finally we assert its correctness by checking with the bitstring we used to generate the object." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 12, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"grover = Grover()\n", | ||
"found_bistring = grover.find_bitstring(cxn, bitstring_map)\n", | ||
"assert found_bistring == target_bitstring, \"Found bitstring is not the expected bitstring\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"If the assertion succeeded we know we found the correct bitstring." | ||
] | ||
} | ||
], | ||
"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.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 1 | ||
} |
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