-
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 #95 from ampolloreno/DJ
Deutsch-Jozsa
- Loading branch information
Showing
10 changed files
with
430 additions
and
217 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"This Notebook is a brief sketch of how to use the Deutsch-Jozsa algorithm.\n", | ||
"We start by declaring all necessary imports." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"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", | ||
"from itertools import product\n", | ||
"\n", | ||
"from grove.deutsch_jozsa.deutsch_jozsa import DeutschJosza" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The Deutsch-Jozsa Algorithm can be used to determine if a binary-valued function is constant or balanced (returns 0 on exactly half of its inputs)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"bit_value = '0'\n", | ||
"bit = (\"0\", \"1\")\n", | ||
"constant_bitmap = {}\n", | ||
"\n", | ||
"# We construct the bitmap for the algorithm\n", | ||
"for bitstring in product(bit, repeat=2):\n", | ||
" bitstring = \"\".join(bitstring)\n", | ||
" constant_bitmap[bitstring] = bit_value" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We verify that we have a constant bitmap below." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"for value in constant_bitmap.values():\n", | ||
" assert value == bit_value, \"The constant_bitmap is not constant with value bit_value.\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"To use the Deutsch-Jozsa 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": 5, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"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, 0]))]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Now let's run the Deutsch Jozsa algorithm. We instantiate the Deutsch Jozsa object and then call its `is_constant` method with the connection object and the bitmap we defined above. Finally we assert its correctness by checking the output. (The method returns a boolean, so here we just check the returned boolean.)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"dj = DeutschJosza()\n", | ||
"is_constant = dj.is_constant(cxn, constant_bitmap)\n", | ||
"assert is_constant, \"The algorithm said the function was balanced.\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"If the assertion succeeded we know the algorithm returned the zero bitstring and successfully recognized the function as constant." | ||
] | ||
} | ||
], | ||
"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 was deleted.
Oops, something went wrong.
Oops, something went wrong.