alice4space/qutree

View on GitHub
docs/examples/demo.ipynb

Summary

Maintainability
Test Coverage
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "257c99e6",
   "metadata": {},
   "source": [
    "# Usage examples "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "69100b47",
   "metadata": {},
   "source": [
    "[![github](https://img.shields.io/badge/-see%20sources-white?logo=github&labelColor=555)](https://github.com/alice4space/qutree/blob/main/docs/source/examples/demo.ipynb)\n",
    "[![colab](https://img.shields.io/badge/-open%20in%20colab-blue?logo=googlecolab&labelColor=555)](https://colab.research.google.com/github/alice4space/qutree/blob/main/docs/source/examples/demo.ipynb)\n",
    "[![colab](https://img.shields.io/badge/-open%20in%20mybinder-orange?logo=jupyter&labelColor=555)](https://mybinder.org/v2/gh/alice4space/qutree/main?labpath=docs%2Fsource%2Fexamples%2Fdemo.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af605d99",
   "metadata": {
    "nbsphinx": "hidden"
   },
   "outputs": [],
   "source": [
    "# this cell is hidden in the docs\n",
    "# it installs the requirements to run this demo\n",
    "#!python -m pip install qutree[demo]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ef860f7-7278-4df0-92cf-f04f38dbf892",
   "metadata": {},
   "outputs": [],
   "source": [
    "from urllib.request import urlretrieve\n",
    "import tempfile\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import pennylane as qml\n",
    "from scipy.stats import unitary_group"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9da037b-14f3-4f5f-a36d-546ef2599614",
   "metadata": {},
   "outputs": [],
   "source": [
    "from qutree import BBT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31dac37d-1a3e-4598-bd02-266fe7d3e2a2",
   "metadata": {},
   "source": [
    "## Bell state preparation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ce97def-a621-422d-828d-f9b20ade0813",
   "metadata": {},
   "outputs": [],
   "source": [
    "@qml.qnode(qml.device(\"default.qubit\", wires=2))\n",
    "def bell(t):\n",
    "    qml.Hadamard(wires=[0])\n",
    "    qml.CRY(np.pi*t,wires=[0,1])\n",
    "    return qml.state()\n",
    "\n",
    "ts = np.linspace(0,1,21)\n",
    "Ss = np.array([bell(t) for t in ts]).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1df4fe1b-6b55-4465-b79f-2632023b33f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "bbt = BBT(2)\n",
    "bbt.add_data(Ss)\n",
    "bbt.plot_tree()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b5a698e-26cb-4c47-9213-5fe6220473f6",
   "metadata": {},
   "source": [
    "The top sphere shows the effect of the hadamard gate : an even superposition between the two basis state without phase offset. \n",
    "Then on the bottom sphere we see the effect of the parametrised Control Y rotattion :\n",
    "The left sphere represent the subspace where the first qubit is 0. it is of course untouched by the CY gate.\n",
    "The right sphere represent the subspace where the first qubit is 1. The state is gradually rotated about the Y axis from 0 (blue) to 1 (red).\n",
    "The bell state corresponds to the red point."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1db6830b-2a75-4909-98cc-ca721dac2e22",
   "metadata": {},
   "source": [
    "## Study how an encoding spans the hilbert space"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec56257b-6a13-4100-bfd0-beb041fb376c",
   "metadata": {},
   "outputs": [],
   "source": [
    "dev = qml.device('default.qubit', wires=3)\n",
    "\n",
    "@qml.qnode(dev)\n",
    "def circuit(t):\n",
    "    state = np.array([1, 2j, 3, t*1j, 5, 6j, 7, 8j])\n",
    "    state = state / np.linalg.norm(state)\n",
    "    qml.MottonenStatePreparation(state_vector=state, wires=range(3))\n",
    "    return qml.state()\n",
    "\n",
    "print(qml.draw(circuit, expansion_strategy=\"device\", max_length=80)(4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a91657db-be56-4177-b8de-3b87932ee6fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "ts = np.linspace(0,8,21)\n",
    "Ss = np.array([circuit(t) for t in ts]).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4ffc513-106c-427e-a1f6-555c8ca4e501",
   "metadata": {},
   "outputs": [],
   "source": [
    "bbt = BBT(3)\n",
    "bbt.add_data(Ss)\n",
    "bbt.plot_tree()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d67120f0-1ffa-4b7e-a6a3-786975a844fe",
   "metadata": {},
   "source": [
    "## quantum states dataset from Machine Learning"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c343e361-8839-4cc3-9a23-656edd2b7210",
   "metadata": {},
   "source": [
    "The data is generated using the demo from pennylane about classification that can be found here : https://pennylane.ai/qml/demos/tutorial_variational_classifier.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc9e4256-3b79-4695-b7d3-fcc0118c30c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# read the file from data folder in github\n",
    "with tempfile.NamedTemporaryFile() as tmp:\n",
    "    url = 'https://raw.githubusercontent.com/alice4space/qutree/main/docs/source/examples/data/iris_quantum_kernel.npy'\n",
    "    urlretrieve(url, tmp.name)\n",
    "    states_ml = np.load(tmp.name,allow_pickle=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2175097-0591-4a73-9f03-edc43ad1c6b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "bbt = BBT(4)\n",
    "lam = 0.3\n",
    "cs = np.concatenate([np.random.random(50)*lam,np.random.random(50)*lam+1-lam])\n",
    "bbt.add_data(states_ml.T,colors=cs)\n",
    "bbt.plot_tree()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "518b20c9-29a4-40b0-be95-ed8bd55f7c9c",
   "metadata": {},
   "source": [
    "## hamiltonian simulation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d8989d9-981c-4db0-87c3-d4ebc0ca79d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "num_qubits = 4\n",
    "U = unitary_group.rvs(2**num_qubits,random_state = 0)\n",
    "D,S = np.linalg.eigh(U)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3da76d1a-68a4-4922-ad0d-9e87e686239d",
   "metadata": {},
   "source": [
    "### excite only two eigenstates : a single frequency"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee23a947-def0-47a3-977c-9a7c122e7080",
   "metadata": {},
   "outputs": [],
   "source": [
    "ka = 12\n",
    "kb = 2\n",
    "\n",
    "S0 = (S[:,ka]+S[:,kb])/np.sqrt(2)\n",
    "Sts = []\n",
    "ts = np.linspace(0,2*np.pi/np.abs(D[ka]-D[kb]),101)\n",
    "for t in ts:\n",
    "    Sts.append(S @ np.diag(np.exp(1j*D*t)) @ S.T.conj() @ S0)\n",
    "Sts = np.array(Sts).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9844a93f-39a9-4f19-83c8-e249d629ce92",
   "metadata": {},
   "outputs": [],
   "source": [
    "bbt = BBT(4)\n",
    "bbt.add_data(Sts,cmap='hsv')\n",
    "bbt.plot_tree()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac6324b5-3dcc-430b-9400-379c39538d16",
   "metadata": {},
   "source": [
    "### excite three eigenstates : three frequencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43b52496-890d-4e06-8fe4-45dc5412c68a",
   "metadata": {},
   "outputs": [],
   "source": [
    "ka = 12\n",
    "kb = 2\n",
    "kc = 4\n",
    "\n",
    "S0 = (S[:,ka]+S[:,kb]+S[:,kc])/np.sqrt(3)\n",
    "Sts = []\n",
    "ts = np.linspace(0,5,101)\n",
    "for t in ts:\n",
    "    Sts.append(S @ np.diag(np.exp(1j*D*t)) @ S.T.conj() @ S0)\n",
    "Sts = np.array(Sts).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c3fd893-e8cd-4456-b35f-8fdaf1dd93f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "bbt = BBT(4)\n",
    "bbt.add_data(Sts,cmap='jet')\n",
    "bbt.plot_tree()"
   ]
  }
 ],
 "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.8.3"
  },
  "vscode": {
   "interpreter": {
    "hash": "3a8723e76c97419226acc73879efcf4e97fe0647ebdba2efabf443849b0a40f0"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}