<commit_before>{
 "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": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyquil.api import SyncConnection\n",
    "from itertools import product\n",
    "from mock import patch, Mock\n",
    "from pyquil.job_results import JobResult\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": 10,
   "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=len(target_bitstring)):\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": 11,
   "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": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "with patch(\"pyquil.api.JobConnection\") as cxn:\n",
    "    jobresult = Mock(JobResult)\n",
    "    jobresult.is_done.return_value = True\n",
    "    jobresult.decode.return_value = [\"\".join([bit for bit in target_bitstring])]\n",
    "    cxn.run_and_measure.return_value = jobresult"
   ]
  },
  {
   "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": 13,
   "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": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DEFGATE GROVER_ORACLE:\n",
      "    1.0, 0, 0, 0, 0, 0, 0, 0\n",
      "    0, 1.0, 0, 0, 0, 0, 0, 0\n",
      "    0, 0, -1.0, 0, 0, 0, 0, 0\n",
      "    0, 0, 0, 1.0, 0, 0, 0, 0\n",
      "    0, 0, 0, 0, 1.0, 0, 0, 0\n",
      "    0, 0, 0, 0, 0, 1.0, 0, 0\n",
      "    0, 0, 0, 0, 0, 0, 1.0, 0\n",
      "    0, 0, 0, 0, 0, 0, 0, 1.0\n",
      "\n",
      "DEFGATE HADAMARD_DIFFUSION:\n",
      "    1.0, 0, 0, 0, 0, 0, 0, 0\n",
      "    0, -1.0, 0, 0, 0, 0, 0, 0\n",
      "    0, 0, -1.0, 0, 0, 0, 0, 0\n",
      "    0, 0, 0, -1.0, 0, 0, 0, 0\n",
      "    0, 0, 0, 0, -1.0, 0, 0, 0\n",
      "    0, 0, 0, 0, 0, -1.0, 0, 0\n",
      "    0, 0, 0, 0, 0, 0, -1.0, 0\n",
      "    0, 0, 0, 0, 0, 0, 0, -1.0\n",
      "\n",
      "H 0\n",
      "H 1\n",
      "H 2\n",
      "GROVER_ORACLE 0 1 2\n",
      "H 2\n",
      "H 1\n",
      "H 0\n",
      "HADAMARD_DIFFUSION 0 1 2\n",
      "H 0\n",
      "H 1\n",
      "H 2\n",
      "GROVER_ORACLE 0 1 2\n",
      "H 2\n",
      "H 1\n",
      "H 0\n",
      "HADAMARD_DIFFUSION 0 1 2\n",
      "H 0\n",
      "H 1\n",
      "H 2\n",
      "\n"
     ]
    }
   ],
   "source": [<commit_msg>removing spurrious comment in test file<commit_after>{
 "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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyquil.api import SyncConnection\n",
    "from itertools import product\n",
    "from mock import patch, Mock\n",
    "from pyquil.job_results import JobResult\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": null,
   "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=len(target_bitstring)):\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": null,
   "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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with patch(\"pyquil.api.JobConnection\") as cxn:\n",
    "    jobresult = Mock(JobResult)\n",
    "    jobresult.is_done.return_value = True\n",
    "    jobresult.decode.return_value = [\"\".join([bit for bit in target_bitstring])]\n",
    "    cxn.run_and_measure.return_value = jobresult"
   ]
  },
  {
   "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": null,
   "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": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
{'fraction_non_alphanumeric': 0.17499114417286574, 'fraction_numerical': 0.02361553902467824, 'mean_word_length': 2.459967320261438, 'pattern_counts': {'":': 97, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6062863', 'n_tokens_mistral': 3341, 'n_tokens_neox': 3121, 'n_words': 829}