{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Deutsch–Jozsaアルゴリズム"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from blueqat import Circuit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "n = 10\n",
    "\n",
    "\n",
    "def constant0(c):\n",
    "    return c\n",
    "\n",
    "\n",
    "def constant1(c):\n",
    "    return c.x[n]\n",
    "\n",
    "\n",
    "def balance(c):\n",
    "    r = random.randint(0, n - 1)\n",
    "    return c.cx[r, n]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def distinguish(oracle):\n",
    "    c = Circuit(n + 1).x[n].h[:]\n",
    "    res = oracle(c).h[:].m[:n].run(shots=100)\n",
    "    if res['0' * (n+1)]:\n",
    "        print('constant')\n",
    "    else:\n",
    "        print('balance')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "constant\n",
      "constant\n",
      "balance\n"
     ]
    }
   ],
   "source": [
    "distinguish(constant0)\n",
    "distinguish(constant1)\n",
    "distinguish(balance)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}