Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

[Workbook Added] Solutions provided for Task 1.1 and 1.2 #674

Merged
merged 5 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 16 additions & 2 deletions KeyDistribution_BB84/KeyDistribution_BB84.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 1.1. Diagonal Basis\n",
"### <a name=\"Task-1.1-diagonal-basis\" /> Task 1.1. Diagonal Basis\n",
"\n",
"Try your hand at converting qubits from the computational basis to the diagonal basis.\n",
"\n",
Expand Down Expand Up @@ -90,7 +90,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 1.2. Equal superposition\n",
"*Can't come up with a solution? See the explained solution in the [Key Distribution Workbook](./Workbook_KeyDistribution_BB84.ipynb#diagonal_basis).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <a name=\"Task-1.2-equal-superposition\"/> Task 1.2. Equal superposition\n",
"\n",
" \n",
"**Input**: A qubit in the $|0\\rangle$ state.\n",
Expand All @@ -113,6 +120,13 @@
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Key Distribution Workbook](./Workbook_KeyDistribution_BB84.ipynb#equal_superposition).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
217 changes: 217 additions & 0 deletions KeyDistribution_BB84/Workbook_KeyDistribution_BB84.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Key Distribution - BB84 Workbook\n",
"\n",
"**What is this workbook?**\n",
"A workbook is a collection of problems, accompanied by solutions to them. \n",
"The explanations focus on the logical steps required to solve a problem; they illustrate the concepts that need to be applied to come up with a solution to the problem, explaining the mathematical steps required. \n",
"\n",
"Note that a workbook should not be the primary source of knowledge on the subject matter; it assumes that you've already read a tutorial or a textbook and that you are now seeking to improve your problem-solving skills. You should attempt solving the tasks of the respective kata first, and turn to the workbook only if stuck. While a textbook emphasizes knowledge acquisition, a workbook emphasizes skill acquisition.\n",
"\n",
"This workbook describes the solutions to the problems offered in the [Key Distribution BB84 kata](./KeyDistribution_BB84.ipynb). \n",
"Since the tasks are offered as programming problems, the explanations also cover some elements of Q# that might be non-obvious for a first-time user.\n",
"\n",
"**What you should know for this workbook**\n",
"\n",
"You should be familiar with the following concepts before tackling the Key Distribution - BB84 Kata (and this workbook):\n",
"1. Basic Qubit Operations\n",
"2. The concept of measurement"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Background\n",
"\n",
"**What is problem of Key Distribution?**\n",
"In Symmetric Key Cryptography, Alice and Bob: the two parties who wishes to communicate in a secured manner, are required to share a key using which encryption and decryption would work. Alice would calculate the cipher text using this key and the message to be encrypted. Bob, on the other hand would require the same key to decrypt the cipher text into plain text for understanding. Thus, the problem of secure communication can be reduced to the problem to sharing a *key*.\n",
"\n",
"In classical computing, Assymmetric Cryptographic techniques like RSA, Elliptical Curve Cryptography were used to perform encryption. These techniques are based on the fact that it is not possible to solve the problem of prime factorization or discrete logarithm in polynomial time. Both of these can easily be solved using Quantum Shor's Algorithm and thus it is required to visit back the problem of Key Distribution.\n",
"\n",
"**Quantum Key Distribution**\n",
"Quantum Key Distribution can be thought of as a new tool in the toolbox of a Cryptographer. It nowhere eliminates the need for other cryptographic steps like authentication, but rather help build a system with new security properties. \n",
"\n",
"Today we will learn about the BB84 Technique of Quantum Key Distribution which was maded after its scientists **B**ennett and **B**rassard in the year 19**84**. It is proven to be uncoditionally secure in theory and thus remain a strong foundatation of Quantum Key Distribution techniques, even today."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part I. Preparation\n",
"\n",
"While preparing qubits in Quantum Key Distribution, Alice is required to choose a random bit string. For each bit, she has to choose the bases in which it should be measured so that in case Eve wants to read this data, she would be in equal probability of getting the correct or the wrong answer.\n",
"\n",
"We are required to choose two bases to achieve the the same. In pratical implementation, this refers to choosing different types of polarization. Each photon is either polarized in Rectilinear Basis or in Diagonal Basis.\n",
"\n",
"In the first type of basis (referred above as Rectilinear Basis or Computational Basis), Alice would prepares the states $|0\\rangle$ and $|1\\rangle$ where $|0\\rangle$ stands for bit value `0` and $|1\\rangle$ stands for bit value `1`. The second basis (referred above as the diagonal basis or Hadamard Basis) represents bit value `0` as the states $|+\\rangle = \\frac{1}{\\sqrt2}(|0\\rangle + |1\\rangle)$, and bit value `1` as $|-\\rangle = \\frac{1}{\\sqrt2}(|0\\rangle - |1\\rangle)$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <a name=\"diagonal_basis\"></a> Task 1.1. Diagonal Basis\n",
"\n",
"Try your hand at converting qubits from the computational basis to the diagonal basis.\n",
"\n",
"**Input:** $N$ qubits (stored in an array of length $N$). Each qubit is either in $|0\\rangle$ or in $|1\\rangle$ state.\n",
"\n",
"**Goal:** Convert the qubits to the diagonal basis: \n",
"* if `qs[i]` was in state $|0\\rangle$, it should be transformed to $|+\\rangle = \\frac{1}{\\sqrt2}(|0\\rangle + |1\\rangle)$,\n",
"* if `qs[i]` was in state $|1\\rangle$, it should be transformed to $|-\\rangle = \\frac{1}{\\sqrt2}(|0\\rangle - |1\\rangle)$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solution\n",
"\n",
"This task is similar to the one mentioned in [task 1.9 of Superposition kata](./../Superposition/Superposition.ipynb#superposition-of-all-basis-vectors). It is great if you took the hint from the explaination above otherwise, this task consists of applying Hadamard Gate to every Qubit resulting in $|+\\rangle = \\frac{1}{\\sqrt2}(|0\\rangle + |1\\rangle)$ and $|-\\rangle = \\frac{1}{\\sqrt2}(|0\\rangle - |1\\rangle)$ state based on the input provided as $|0\\rangle$ or $|1\\rangle$, respectively. \n",
"\n",
"\n",
"Let us start with a basic example where input string is a string of zeros, $|00....0\\rangle$.\n",
"\\begin{align*}\n",
" H|0\\rangle &= \\frac{1}{\\sqrt2}\\big(|0\\rangle + |1\\rangle\\big)\\\\\n",
" H|0\\rangle \\otimes H|0\\rangle &= \\frac{1}{\\sqrt2} \\big(|0\\rangle + |1\\rangle\\big) \\otimes \\frac{1}{\\sqrt2} \\big(|0\\rangle + |1\\rangle\\big)\\\\ \n",
" &= \\frac{1}{\\sqrt{2^2}}\\big(|00\\rangle + |01\\rangle+ |10\\rangle+ |11\\rangle\\big)\\\\\n",
" H|0\\rangle \\otimes H|0\\rangle \\otimes H|0\\rangle &= \\frac{1}{\\sqrt{2^2}}\\big(|00\\rangle + |01\\rangle+ |10\\rangle+ |11\\rangle\\big) \\otimes \\frac{1}{\\sqrt2}\\big(|0\\rangle + |1\\rangle\\big)\\\\\n",
" &= \\frac{1}{\\sqrt{2^3}}\\big(|000\\rangle + |001\\rangle + |010\\rangle+ |100\\rangle+ |110\\rangle + |101\\rangle+ |011\\rangle+ |111\\rangle\\big)\\\\\n",
" \\underset{N}{\\underbrace{H|0\\rangle \\otimes \\dots \\otimes H|0\\rangle}} \n",
" &= \\frac{1}{\\sqrt{2^{N-1}}} \\big( |\\underset{N-1}{\\underbrace{0 \\cdots 0}}\\rangle + \\cdots + |\\underset{N-1}{\\underbrace{1 \\cdots 1}}\\rangle \\big) \\otimes \\frac{1}{\\sqrt2}\\big(|0\\rangle + |1\\rangle\\big) = \\\\\n",
" &= \\frac{1}{\\sqrt{2^N}} \\big( |\\underset{N}{\\underbrace{0 \\cdots 0}}\\rangle + \\cdots + |\\underset{N}{\\underbrace{1 \\cdots 1}}\\rangle \\big)\\\\ \n",
"\\end{align*}\n",
"\n",
"Using the same principles, we can consider the case where we have a string of ones, $|11....1\\rangle$.\n",
"\\begin{align*}\n",
" H|1\\rangle &= \\frac{1}{\\sqrt2}\\big(|0\\rangle - |1\\rangle\\big)\\\\\n",
" H|1\\rangle \\otimes H|1\\rangle &= \\frac{1}{\\sqrt2} \\big(|0\\rangle - |1\\rangle\\big) \\otimes \\frac{1}{\\sqrt2} \\big(|0\\rangle - |1\\rangle\\big)\\\\ \n",
" &= \\frac{1}{\\sqrt{2^2}}\\big(|00\\rangle - |01\\rangle- |10\\rangle+ |11\\rangle\\big)\\\\\n",
" H|1\\rangle \\otimes H|1\\rangle \\otimes H|1\\rangle &= \\frac{1}{\\sqrt{2^2}}\\big(|00\\rangle - |01\\rangle- |10\\rangle+ |11\\rangle\\big) \\otimes \\frac{1}{\\sqrt2}\\big(|0\\rangle - |1\\rangle\\big)\\\\\n",
" &= \\frac{1}{\\sqrt{2^3}}\\big(|000\\rangle - |001\\rangle - |010\\rangle- |100\\rangle+ |110\\rangle + |101\\rangle+ |011\\rangle- |111\\rangle\\big)\\\\\n",
" \\underset{N}{\\underbrace{H|1\\rangle \\otimes \\dots \\otimes H|1\\rangle}} \n",
" &= \\frac{1}{\\sqrt{2^{N-1}}} \\big( |\\underset{N-1}{\\underbrace{0 \\cdots 0}}\\rangle + \\cdots + {(-1)^{N-1}} |\\underset{N-1}{\\underbrace{1 \\cdots 1}}\\rangle \\big) \\otimes \\frac{1}{\\sqrt2}\\big(|0\\rangle - |1\\rangle\\big) = \\\\\n",
" &= \\frac{1}{\\sqrt{2^N}} \\big( |\\underset{N}{\\underbrace{0 \\cdots 0}}\\rangle + \\cdots + {(-1)^{N}}|\\underset{N}{\\underbrace{1 \\cdots 1}}\\rangle \\big)\\\\ \n",
"\\end{align*}\n",
"\n",
"Combining the two equation, it is easy to calculate the H operation on $|0\\rangle$ and $|1\\rangle$.\n",
"\\begin{align*}\n",
" H|0\\rangle \\otimes H|1\\rangle &= \\frac{1}{\\sqrt2} \\big(|0\\rangle + |1\\rangle\\big) \\otimes \\frac{1}{\\sqrt2} \\big(|0\\rangle - |1\\rangle\\big)\\\\ \n",
" &= \\frac{1}{\\sqrt{2^2}}\\big(|00\\rangle - |01\\rangle+ |10\\rangle- |11\\rangle\\big)\\\\\n",
" H|0\\rangle \\otimes H|1\\rangle \\otimes H|0\\rangle &= \\frac{1}{\\sqrt{2^2}}\\big(|00\\rangle - |01\\rangle+ |10\\rangle- |11\\rangle\\big) \\otimes \\frac{1}{\\sqrt2}\\big(|0\\rangle + |1\\rangle\\big)\\\\\n",
" &= \\frac{1}{\\sqrt{2^3}}\\big(|000\\rangle + |001\\rangle - |010\\rangle+ |100\\rangle- |110\\rangle + |101\\rangle- |011\\rangle- |111\\rangle\\big)\\\\\n",
" \\underset{N}{\\underbrace{H|0\\rangle \\otimes \\dots \\otimes H|1\\rangle}} \n",
" &= \\frac{1}{\\sqrt{2^{N-1}}} \\big( |\\underset{N-1}{\\underbrace{0 \\cdots 0}}\\rangle + \\cdots + {(-1)^{N-1}} |\\underset{N-1}{\\underbrace{1 \\cdots 1}}\\rangle \\big) \\otimes \\frac{1}{\\sqrt2}\\big(|0\\rangle - |1\\rangle\\big) = \\\\\n",
" &= \\frac{1}{\\sqrt{2^N}} \\big( |\\underset{N}{\\underbrace{0 \\cdots 0}}\\rangle + \\cdots + {(-1)^{N}}|\\underset{N}{\\underbrace{1 \\cdots 1}}\\rangle \\big)\\\\ \n",
"\\end{align*}\n",
"\n",
"We can either iterate over all the qubits, one by one or use the [ApplyToEachA](https://docs.microsoft.com/en-us/qsharp/api/qsharp/microsoft.quantum.canon.applytoeacha) function to get the desired output."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T11_DiagonalBasis\n",
"\n",
"operation DiagonalBasis (qs : Qubit[]) : Unit { \n",
" \n",
" // You can also use the following:\n",
" // for q in qs {\n",
" // H(q);\n",
" // }\n",
" \n",
" //Otherwise use the inbuilt function of Q#\n",
" ApplyToEachA(H,qs);\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to task 1.1 of the KeyDistribution_BB84 kata.](./KeyDistribution_BB84.ipynb#Task-1.1-diagonal-basis)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <a name=\"equal_superposition\"/> Task 1.2. Equal superposition\n",
"\n",
" \n",
"**Input**: A qubit in the $|0\\rangle$ state.\n",
"\n",
"**Goal**: Change the qubit state to a superposition state that has equal probabilities of measuring 0 and 1. \n",
"\n",
"> Note that this is not the same as keeping the qubit in the $|0\\rangle$ state with 50% probability and converting it to the $|1\\rangle$ state with 50% probability!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solution\n",
"This task is similar to [Task 1.1 of Superposition kata](./../Superposition/Superposition.ipynb#plus-state) or [Task 1.2 of Superposition kata](./../Superposition/Superposition.ipynb#minus-state). \n",
"\n",
"This can also be thought as a specific use case of the previous step with **N** = 1.\n",
"\n",
"If the equal superposition is required with a know state of $|0\\rangle$, Hadamard gate should be an undoubtable choice. We can create any one of the following states to get the desired results from this function.\n",
"1. Plus State : $|+\\rangle = \\frac{1}{\\sqrt2}(|0\\rangle + |1\\rangle)$\n",
"2. Minus State : $|-\\rangle = \\frac{1}{\\sqrt2}(|0\\rangle - |1\\rangle)$.\n",
"\n",
"Both the states with provide equal superposition of both $|0\\rangle$ and $|1\\rangle$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T12_EqualSuperposition \n",
"\n",
"operation EqualSuperposition (q : Qubit) : Unit {\n",
" \n",
" // Straight forward solution to get the desired results. This would give plus state.\n",
" H(q);\n",
" \n",
" //Alternatively, we can also use the following. This would give minus state.\n",
" //X(q);\n",
" //H(q);\n",
" \n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to task 1.2 of the Key Distribution kata.](././KeyDistribution_BB84.ipynb#Task-1.2-equal-superposition)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Q#",
"language": "qsharp",
"name": "iqsharp"
},
"language_info": {
"file_extension": ".qs",
"mimetype": "text/x-qsharp",
"name": "qsharp",
"version": "0.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}