{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// import quantum-circuit npm module\n",
    "const QuantumCircuit = require(\"quantum-circuit\");\n",
    "\n",
    "// create instance of QuantumCircuit object\n",
    "let circ = new QuantumCircuit();\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<svg class=\"qc-circuit\" width=\"140\" height=\"140\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\"><line class=\"qc-wire\" x1=\"0\" x2=\"140\" y1=\"40\" y2=\"40\" stroke=\"black\" stroke-width=\"1\" /><line class=\"qc-wire\" x1=\"0\" x2=\"140\" y1=\"100\" y2=\"100\" stroke=\"black\" stroke-width=\"1\" /><g class=\"qc-gate-group\"><rect class=\"qc-gate-box\" x=\"20\" y=\"20\" width=\"40\" height=\"40\" stroke=\"black\" fill=\"white\" stroke-width=\"1\" /><text class=\"qc-gate-label\" x=\"40\" y=\"40\" alignment-baseline=\"middle\" text-anchor=\"middle\">H</text></g><g class=\"qc-gate-group\"><line class=\"qc-gate-link\" x1=\"100\" x2=\"100\" y1=\"40\" y2=\"100\" stroke=\"black\" stroke-width=\"1\" /><circle class=\"qc-gate-dot\" cx=\"100\" cy=\"40\" r=\"5\" stroke=\"black\" fill=\"black\" stroke-width=\"1\" /><ellipse class=\"qc-gate-not\" cx=\"100\" cy=\"100\" rx=\"20\" ry=\"20\" stroke=\"black\" fill=\"white\" stroke-width=\"1\" /><line class=\"qc-gate-not-line\" x1=\"100\" x2=\"100\" y1=\"80\" y2=\"120\" stroke=\"black\" stroke-width=\"1\" /><line class=\"qc-gate-not-line\" x1=\"80\" x2=\"120\" y1=\"100\" y2=\"100\" stroke=\"black\" stroke-width=\"1\" /></g></svg>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "State:\n",
      " 0.70710678+0.00000000i|00>\t50.00000%\n",
      " 0.70710678+0.00000000i|11>\t50.00000%\n",
      "\n",
      "Probabilities:\n",
      "[ 0.5, 0.5 ]\n",
      "\n",
      "Measured:\n",
      "[ 0, 0 ]\n",
      "[ 0, 0 ]\n",
      "[ 1, 1 ]\n",
      "[ 0, 0 ]\n",
      "[ 0, 0 ]\n",
      "[ 0, 0 ]\n",
      "[ 1, 1 ]\n",
      "[ 0, 0 ]\n",
      "[ 0, 0 ]\n",
      "[ 0, 0 ]\n"
     ]
    }
   ],
   "source": [
    "// clear circuit\n",
    "circ.clear();\n",
    "\n",
    "// add gates to circuit\n",
    "circ.addGate(\"h\", -1, 0);\n",
    "circ.addGate(\"cx\", -1, [0, 1]);\n",
    "\n",
    "// draw circuit\n",
    "$$html$$ = circ.exportSVG(true);\n",
    "\n",
    "\n",
    "// execute circuit\n",
    "circ.run();\n",
    "\n",
    "// print state\n",
    "console.log(\"State:\");\n",
    "console.log(circ.stateAsString(true));\n",
    "\n",
    "// print probabilities\n",
    "console.log(\"Probabilities:\");\n",
    "console.log(circ.probabilities());\n",
    "\n",
    "// measure and print (ten times)\n",
    "console.log(\"\");\n",
    "console.log(\"Measured:\");\n",
    "for(var i = 0; i < 10; i++ ) {\n",
    "    console.log(circ.measureAll(true));\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Javascript (Node.js)",
   "language": "javascript",
   "name": "javascript"
  },
  "language_info": {
   "file_extension": ".js",
   "mimetype": "application/javascript",
   "name": "javascript",
   "version": "9.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
