Skip to content

Commit

Permalink
Merge branch 'main' of github.com:glassnotes/CPEN-400Q
Browse files Browse the repository at this point in the history
  • Loading branch information
glassnotes committed Mar 30, 2023
2 parents e92ab2e + 0f78bf3 commit ea95ed8
Show file tree
Hide file tree
Showing 7 changed files with 613 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ wish.
* 17 - Mixed states, noise, and quantum channels
* 19 - Noisy quantum systems; introducing the oracle
* 20 - The oracle, query complexity, and Deutsch's algorithm
* 21 - Grover's algorithm

## Resources

Expand Down
204 changes: 204 additions & 0 deletions demos/Lecture20-Demos.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "2944e1a3-aae3-40ed-ad15-b9002c1aac81",
"metadata": {},
"source": [
"# Demos: Lecture 20"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "bf787988-6a84-436f-98ed-43e3c0d186b4",
"metadata": {},
"outputs": [],
"source": [
"import pennylane as qml\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"id": "6160520c-3db8-467a-a6bc-3fd8e1ce9d54",
"metadata": {},
"source": [
"## Demo 1: Deutsch's algorithm"
]
},
{
"cell_type": "markdown",
"id": "e27598eb-14fb-4988-b463-64f2e184540f",
"metadata": {},
"source": [
"\n",
"<img src=\"fig/deutsch_2.png\">"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "5aafd8d0-f2f1-40f3-8094-4d8e7f269d6e",
"metadata": {},
"outputs": [],
"source": [
"def oracle(func=1):\n",
" # 1: f(0) = f(1) = 0\n",
" # Do nothing!\n",
" \n",
" # 2: f(0) = f(1) = 1\n",
" if func == 2:\n",
" qml.CNOT(wires=[0, 1])\n",
" qml.PauliX(wires=0)\n",
" qml.CNOT(wires=[0, 1])\n",
" qml.PauliX(wires=0)\n",
" \n",
" # 3: f(0) = 0, f(1) = 1\n",
" if func == 3:\n",
" qml.CNOT(wires=[0, 1])\n",
" \n",
" # 4: f(0) = 1, f(1) = 0\n",
" if func == 4:\n",
" qml.PauliX(wires=0)\n",
" qml.CNOT(wires=[0, 1])\n",
" qml.PauliX(wires=0)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "682fe38b-3cee-4d5f-b34b-29fa9845fce4",
"metadata": {},
"outputs": [],
"source": [
"dev = qml.device('default.qubit', wires=2, shots=1)\n",
"\n",
"@qml.qnode(dev)\n",
"def deutsch_algorithm(func=None):\n",
" qml.PauliX(wires=1)\n",
" qml.Hadamard(wires=1)\n",
" qml.Hadamard(wires=0)\n",
" \n",
" oracle(func)\n",
" \n",
" qml.Hadamard(wires=0)\n",
" \n",
" return qml.sample(wires=0)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "3699835c-7ee7-4bad-84b4-dd5c05755b49",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor(0, requires_grad=True)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"deutsch_algorithm(1)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "a328497f-d8da-4480-b35c-2552e9c6e706",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor(0, requires_grad=True)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"deutsch_algorithm(2)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "a11f439d-0897-4333-ab02-0727550ae5d8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor(1, requires_grad=True)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"deutsch_algorithm(3)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "ce56d081-d4f9-4de4-8931-ea0c464e0f15",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor(1, requires_grad=True)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"deutsch_algorithm(4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa3f13c6-7ce2-48e4-b6c2-1781833d6130",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
122 changes: 122 additions & 0 deletions demos/Lecture21-Demos-Blank.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "2944e1a3-aae3-40ed-ad15-b9002c1aac81",
"metadata": {},
"source": [
"# Demos: Lecture 21"
]
},
{
"cell_type": "markdown",
"id": "791fb07d-2803-4c3d-8db4-204d64521865",
"metadata": {},
"source": [
"## Demo 1: Grover's algorithm"
]
},
{
"cell_type": "markdown",
"id": "9a4c5242-c8e1-4bed-956f-5030a941d932",
"metadata": {},
"source": [
"<img src=\"fig/grover_full.png\" width=\"400px\">"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa18b6e2-989a-454b-a48a-d4616bee7f0d",
"metadata": {},
"outputs": [],
"source": [
"import pennylane as qml\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fc4a1d76-863f-41a7-8c79-43fc0aba128b",
"metadata": {},
"outputs": [],
"source": [
"n_bits = 5\n",
"special_string = '' \n",
"\n",
"dev = qml.device('default.qubit', wires=n_bits+1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "424747fb-e9f3-4f19-99a2-24a92a0b40e5",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "b848aeb4-d47e-4b47-ba9c-ab828f5fb808",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "071d6fcd-5f66-4534-b339-f106595be629",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "cd66dcca-36b6-4d2f-8a66-70900c02a0e7",
"metadata": {},
"source": [
"For plotting after the implementation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8141da30-eb89-493b-979c-365d097c7c2e",
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"for n in range(10):\n",
" plt.bar(\n",
" list(range(2 ** n_bits)),\n",
" grover(num_steps=n)\n",
" )\n",
" plt.show()"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit ea95ed8

Please sign in to comment.