Commit db5a885c authored by Abhishek's avatar Abhishek

.

parent 5995e619
......@@ -471,26 +471,6 @@
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false,
"nbgrader": {
"grade": true,
"grade_id": "exercise1",
"locked": true,
"points": "2",
"solution": false
}
},
"outputs": [],
"source": [
"###\n",
"### AUTOGRADER TEST - DO NOT REMOVE\n",
"###\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -548,26 +528,6 @@
"print(gap)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false,
"nbgrader": {
"grade": true,
"grade_id": "exercise2",
"locked": true,
"points": "2",
"solution": false
}
},
"outputs": [],
"source": [
"###\n",
"### AUTOGRADER TEST - DO NOT REMOVE\n",
"###\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -628,26 +588,6 @@
"print(gap)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false,
"nbgrader": {
"grade": true,
"grade_id": "exercise3",
"locked": true,
"points": "1",
"solution": false
}
},
"outputs": [],
"source": [
"###\n",
"### AUTOGRADER TEST - DO NOT REMOVE\n",
"###\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
......
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# **Quantum Computing: Lab 10**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this lab we are going to work on creating Error Correction Codes on Quantum Circuits."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Before you begin, execute this cell to import numpy and packages from the D-Wave Ocean suite, and all necessary functions for the gate-model framework you are going to use, whether that is the Forest SDK or Qiskit. In the case of Forest SDK, it also starts the qvm and quilc servers."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%run -i \"assignment_helper_QML.py\"\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# **Classical Hamming Code** #"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import math\n",
"\n",
"G = np.array([[1, 0, 0, 0, 1, 1, 1],\n",
" [0, 1, 0, 0, 1, 1, 0],\n",
" [0, 0, 1, 0, 1, 0, 1],\n",
" [0, 0, 0, 1, 0, 1, 1]])\n",
"# G corrects one error\n",
"\n",
"\n",
"# Codewords are row space(mod 2)\n",
"# Encode: mG\n",
"\n",
"# Suppose is m = [1, 1, 0, 1]\n",
"m = np.array([[0, 1, 1, 1]])\n",
"\n",
"\n",
"print(np.matmul(m,G) % 2)\n",
"\n",
"e = np.array([[0, 0, 0, 0, 0, 0, 0]])\n",
"e[0,3] = 1\n",
"\n",
"H = np.array([[1, 1, 1],\n",
" [1, 1, 0],\n",
" [1, 0, 1],\n",
" [0, 1, 1],\n",
" [1, 0, 0],\n",
" [0, 1, 0],\n",
" [0, 0, 1]])\n",
"\n",
"print(np.matmul((np.matmul(m,G) + e),H)%2)\n",
"print(np.matmul(e,H)%2)\n",
"# and hence we know that the error is at ith position of e\n",
"\n",
"#print(np.matmul(e,H))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# **Shor's Bit Error Correction Code** #\n",
"\n",
"\n",
"<img src=\"images/qubit.png\" style=\"width: 600px;\"/>\n",
"\n",
"_*try placing a qubit error between the barriers_"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## *Create some arbitrary quantum State* ##"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit\n",
"from qiskit.tools.visualization import circuit_drawer, plot_histogram\n",
"from qiskit import execute\n",
"from qiskit import BasicAer\n",
"import numpy as np\n",
"%matplotlib inline\n",
"backend = BasicAer.get_backend('qasm_simulator')\n",
"\n",
"q = QuantumRegister(1,'q')\n",
"c = ClassicalRegister(1,'c')\n",
"circuit = QuantumCircuit(q, c)\n",
"\n",
"\n",
"circuit.u3(np.pi/3, 2*np.pi/7, np.pi/5, q[0])\n",
"\n",
"amp = get_amplitudes(circuit)\n",
"print(amp)\n",
"circuit.measure(q[0],c[0])\n",
"\n",
"num_shots = 1000\n",
"job = execute(circuit, backend=backend, shots=num_shots)\n",
"\n",
"result = job.result()\n",
"res = result.get_counts()\n",
"plot_histogram(res)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## *Encoder* ##"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit\n",
"from qiskit.tools.visualization import circuit_drawer, plot_histogram\n",
"from qiskit import execute\n",
"from qiskit import BasicAer\n",
"import numpy as np\n",
"%matplotlib inline\n",
"backend = BasicAer.get_backend('qasm_simulator')\n",
"\n",
"q = QuantumRegister(3,'q')\n",
"c = ClassicalRegister(1,'c')\n",
"circuit = QuantumCircuit(q, c)\n",
"\n",
"\n",
"circuit.u3(np.pi/3, 2*np.pi/7, np.pi/5, q[0])\n",
"circuit.cx(q[0], q[1])\n",
"circuit.cx(q[0], q[2])\n",
"circuit.barrier()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## *Error* ##"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"circuit.x(q[0])\n",
"circuit.barrier()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## *Decoder* ##"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"circuit.cx(q[0], q[1])\n",
"circuit.cx(q[0], q[2])\n",
"circuit.ccx(q[1], q[2], q[0])\n",
"\n",
"amp = get_amplitudes(circuit)\n",
"\n",
"\n",
"circuit.measure(q[0],c[0])\n",
"# circuit.measure(q[1],c[1])\n",
"# circuit.measure(q[2],c[2])\n",
"\n",
"\n",
"circuit_drawer(circuit, output='mpl')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"num_shots = 20000\n",
"job = execute(circuit, backend=backend, shots=num_shots)\n",
"\n",
"result = job.result()\n",
"res = result.get_counts()\n",
"plot_histogram(res)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(amp)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# **Shor's Phase Error Correction Code** #\n",
"\n",
"\n",
"<img src=\"images/phase.png\" style=\"width: 700px;\"/>\n",
"\n",
"_*try placing a qubit error between the barriers_"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## *Encoder* ##"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit\n",
"from qiskit.tools.visualization import circuit_drawer, plot_histogram\n",
"from qiskit import execute\n",
"from qiskit import BasicAer\n",
"import numpy as np\n",
"%matplotlib inline\n",
"backend = BasicAer.get_backend('qasm_simulator')\n",
"#backend = BasicAer.get_backend('statevector_simulator')\n",
"\n",
"q = QuantumRegister(3,'q')\n",
"c = ClassicalRegister(3,'c')\n",
"circuit = QuantumCircuit(q, c)\n",
"\n",
"\n",
"circuit.u3(np.pi/3, 2*np.pi/7, np.pi/5, q[0])\n",
"circuit.cx(q[0], q[1])\n",
"circuit.cx(q[0], q[2])\n",
"circuit.h(q[0])\n",
"circuit.h(q[1])\n",
"circuit.h(q[2])\n",
"\n",
"circuit.barrier()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## *Error* ##"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"circuit.z(q[0])\n",
"circuit.barrier()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## *Decoder* ##"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"circuit.h(q[0])\n",
"circuit.h(q[1])\n",
"circuit.h(q[2])\n",
"circuit.cx(q[0], q[1])\n",
"circuit.cx(q[0], q[2])\n",
"circuit.ccx(q[1], q[2], q[0])\n",
"\n",
"#circuit.x(q[0])\n",
"amp = get_amplitudes(circuit)\n",
"\n",
"circuit.measure(q[0],c[0])\n",
"circuit.measure(q[1],c[1])\n",
"circuit.measure(q[2],c[2])\n",
"\n",
"\n",
"circuit_drawer(circuit, output='mpl')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"num_shots = 20000\n",
"job = execute(circuit, backend=backend, shots=num_shots)\n",
"\n",
"result = job.result()\n",
"res = result.get_counts()\n",
"plot_histogram(res)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(amp)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# **EXERCISE: Build Shor's 9-Qubit Bit-Phase Error Correction Code** #"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"images/shors9.png\" style=\"width: 800px;\"/>\n",
"\n",
"_*try placing a qubit error between the barriers_"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
......@@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# **Quantum Computing: Lab 10** #"
"# **Quantum Computing: Lab 11** #"
]
},
{
......@@ -335,7 +335,7 @@
{
"data": {
"text/plain": [
"[<matplotlib.lines.Line2D at 0x7f30e89d1860>]"
"[<matplotlib.lines.Line2D at 0x7fe4a2443668>]"
]
},
"execution_count": 9,
......@@ -425,9 +425,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Ancilla post-selection probability was found to be 0.732421875\n",
"Probability for class 0 is 0.6306666666666667\n",
"Probability for class 1 is 0.36933333333333335\n"
"Ancilla post-selection probability was found to be 0.72265625\n",
"Probability for class 0 is 0.6162162162162163\n",
"Probability for class 1 is 0.3837837837837838\n"
]
}
],
......@@ -459,9 +459,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Ancilla post-selection probability was found to be 0.912109375\n",
"Probability for class 0 is 0.5524625267665952\n",
"Probability for class 1 is 0.4475374732334047\n"
"Ancilla post-selection probability was found to be 0.908203125\n",
"Probability for class 0 is 0.535483870967742\n",
"Probability for class 1 is 0.4645161290322581\n"
]
}
],
......
This diff is collapsed.
......@@ -332,6 +332,13 @@
"print('Bob_Secret_Key: ',' '.join(map(str, BobSecretKey)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
......
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment