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 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
19 changes: 17 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\"></a> 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 - BB84 Workbook](./Workbook_KeyDistribution_BB84.ipynb#diagonal_basis).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <a name=\"Task-1.2-equal-superposition\"></a> Task 1.2. Equal superposition\n",
"\n",
" \n",
"**Input**: A qubit in the $|0\\rangle$ state.\n",
Expand All @@ -110,9 +117,17 @@
"\n",
"operation EqualSuperposition (q : Qubit) : Unit {\n",
" // ...\n",
"\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Key Distribution - BB84 Workbook](./Workbook_KeyDistribution_BB84.ipynb#equal_superposition).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
170 changes: 170 additions & 0 deletions KeyDistribution_BB84/Workbook_KeyDistribution_BB84.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
{
"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 single-qubit gates\n",
"2. The concept of measurement"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part I. Preparation"
]
},
{
"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). \n",
"This task can be solved by applying a Hadamard gate to every qubit, resulting in $|+\\rangle = \\frac{1}{\\sqrt2}(|0\\rangle + |1\\rangle)$ and $|-\\rangle = \\frac{1}{\\sqrt2}(|0\\rangle - |1\\rangle)$ states depending on whether the starting state of each qubit was $|0\\rangle$ or $|1\\rangle$, respectively. \n",
"\n",
"To get the desired result, we will iterate over the given array of qubits using a foreach-style loop and apply a Hadamard gate to each qubit."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T11_DiagonalBasis\n",
"\n",
"operation DiagonalBasis (qs : Qubit[]) : Unit { \n",
" for q in qs {\n",
" H(q);\n",
" }\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Alternative, we can use the [ApplyToEach](https://docs.microsoft.com/en-us/qsharp/api/qsharp/microsoft.quantum.canon.applytoeach) library function from Microsoft.Quantum.Canon namespace to apply the given gate to each element of the array."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T11_DiagonalBasis\n",
"\n",
"operation DiagonalBasis (qs : Qubit[]) : Unit { \n",
" // Use the library 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\"></a> 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](./../Superposition/Superposition.ipynb#plus-state) and [Task 1.2](./../Superposition/Superposition.ipynb#minus-state) of the Superposition kata. \n",
"This can also be thought as a specific case of the previous task with $N = 1$.\n",
"\n",
"For a qubit state to have equal probabilities of producing 0 and 1 when measured, both basis states in the superposition need to have absolute values of amplitudes equal to $\\frac{1}{\\sqrt2}$.\n",
"This means that any state of the following form will provide the desired results, as the relative phase will not affect the measurement probabilities\n",
"\n",
"$$|\\psi\\rangle = \\frac{1}{\\sqrt2}(|0\\rangle + e^{i\\phi}|1\\rangle)$$\n",
"\n",
"There are multiple ways to prepare one of these states using rotation gates.\n",
"However, the simplest solution would be to reuse the previous task and to prepare a plus state $|+\\rangle = \\frac{1}{\\sqrt2}(|0\\rangle + |1\\rangle)$ or a minus state $|-\\rangle = \\frac{1}{\\sqrt2}(|0\\rangle - |1\\rangle)$ using the Hadamard gate."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T12_EqualSuperposition \n",
"\n",
"operation EqualSuperposition (q : Qubit) : Unit {\n",
" // The most straightforward solution - preparing the plus state.\n",
" H(q);\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to task 1.2 of the Key Distribution_BB84 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
}