Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Womanium Final Assignment - Karan Y Islur #399

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 223 additions & 0 deletions community/womanium/assignments/Karan_Islur_hw4.ipynb.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "wTqEQ9-Pvryc"
},
"source": [
"# Linear Graph with 16 nodes\n",
"\n",
"\n",
"\n",
"***Quantum Walk***"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"id": "nDsgok_g5uPq"
},
"outputs": [],
"source": [
"from classiq import *\n",
"\n",
"size = 4\n",
"\n",
"@qfunc\n",
"def prepare_minus(x: QBit):\n",
" X(x)\n",
" H(x)\n",
"\n",
"@qfunc\n",
"def diffuzer_oracle(aux: Output[QNum],x:QNum):\n",
" aux^=(x!=0)\n",
"\n",
"@qfunc\n",
"def zero_diffuzer(x: QNum):\n",
" aux = QNum('aux')\n",
" allocate(1,aux)\n",
" within_apply(compute=lambda: prepare_minus(aux),\n",
" action=lambda: diffuzer_oracle)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "hJvyxUGsxx6r"
},
"source": [
"# W Operator\n",
"\n",
"The superposition of all the adjacent vertices for all the vertices in the linear graph is created by this function. The first and the last vertex has only one adjacent verex compared to all other vertices, so the for loop is customized accordingly for these two vertices."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def W_iteration(i:int,vertices: QNum, adjacent_vertices:QNum):\n",
"\n",
" prob = [0]* 2**size\n",
" if i==0:\n",
" prob[i+1] = 1.0 #Probability\n",
" elif i==((2**size)-1):\n",
" prob[i-1] = 1.0 #Probability\n",
" else:\n",
" prob[i-1] = 0.5 #Probability\n",
" prob[i+1] = 0.5 #Probability\n",
" print(f'State={i}, prob vec ={prob}')\n",
"\n",
" control(ctrl=vertices==i,\n",
" operand=lambda: within_apply(\n",
" compute= lambda: inplace_prepare_state(probabilities=prob, bound=0.01, target=adjacent_vertices),\n",
" action= lambda: zero_diffuzer(adjacent_vertices)))\n",
"\n",
"@qfunc\n",
"def W_operator(vertices:QNum, adjacent_vertices: QNum):\n",
" for i in range(2**size):\n",
" W_iteration(i,vertices,adjacent_vertices)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Mz6umPX69jHl"
},
"source": [
"# S Operator\n",
"\n",
"Here, the qubit should result in a 1 state for the edges of two adjacent vertices, so the edge oracle is changed accordingly."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "YhfGlIf_MdsV"
},
"outputs": [],
"source": [
"@qfunc\n",
"def edge_oracle(res:Output[QBit], vertices: QNum, adjacent_vertices: QNum):\n",
" res |= (((vertices-adjacent_vertices) == 1) | ((vertices-adjacent_vertices) == -1))\n",
"\n",
"@qfunc\n",
"def bitwise_swap(x: QArray[QBit], y:QArray[QBit]):\n",
" repeat(count= x.len,\n",
" iteration= lambda i: SWAP(x[i],y[i]))\n",
"\n",
"@qfunc\n",
"def S_operator(vertices:QNum, adjacent_vertices: QNum):\n",
" res = QNum('res')\n",
" edge_oracle(res,vertices,adjacent_vertices)\n",
" control(ctrl= res==1,\n",
" operand= lambda: bitwise_swap(vertices,adjacent_vertices))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0P8D2W7X997a"
},
"source": [
"# Main Function"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "9uQiPdVjMhfk",
"outputId": "28b9560b-293f-4769-fd40-bdae222c0441"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"State=0, prob vec =[0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
"State=1, prob vec =[0.5, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
"State=2, prob vec =[0, 0.5, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
"State=3, prob vec =[0, 0, 0.5, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
"State=4, prob vec =[0, 0, 0, 0.5, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
"State=5, prob vec =[0, 0, 0, 0, 0.5, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
"State=6, prob vec =[0, 0, 0, 0, 0, 0.5, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0]\n",
"State=7, prob vec =[0, 0, 0, 0, 0, 0, 0.5, 0, 0.5, 0, 0, 0, 0, 0, 0, 0]\n",
"State=8, prob vec =[0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0.5, 0, 0, 0, 0, 0, 0]\n",
"State=9, prob vec =[0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0.5, 0, 0, 0, 0, 0]\n",
"State=10, prob vec =[0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0.5, 0, 0, 0, 0]\n",
"State=11, prob vec =[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0.5, 0, 0, 0]\n",
"State=12, prob vec =[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0.5, 0, 0]\n",
"State=13, prob vec =[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0.5, 0]\n",
"State=14, prob vec =[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0.5]\n",
"State=15, prob vec =[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.0, 0]\n"
]
}
],
"source": [
"@qfunc\n",
"def main(vertices:Output[QNum], adjacent_vertices:Output[QNum]):\n",
" '''function'''\n",
"\n",
" allocate(size,vertices)\n",
" hadamard_transform(vertices)\n",
" allocate(size,adjacent_vertices)\n",
"\n",
" W_operator(vertices,adjacent_vertices)\n",
" S_operator(vertices,adjacent_vertices)\n",
"\n",
"qmod = create_model(main)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"id": "-cENeHTsQzdA"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Opening: https://platform.classiq.io/circuit/9bfc927c-ffe6-431c-8011-c71f4d35b8bd?version=0.43.3\n"
]
}
],
"source": [
"qprog = synthesize(qmod)\n",
"show(qprog)"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"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.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading