Skip to content

Commit

Permalink
Katas UI review - QKD (#1846)
Browse files Browse the repository at this point in the history
Co-authored-by: Mariia Mykhailova <[email protected]>
  • Loading branch information
SoniaLopezBravo and tcNickolas authored Aug 20, 2024
1 parent d1fb2a1 commit 0d1737a
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 23 deletions.
14 changes: 7 additions & 7 deletions katas/content/key_distribution/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Quantum key distribution is a type of quantum communication protocol that allows

Quantum key distribution protocols include two parties, commonly referred to as Alice and Bob, that have two communication channels between them, one quantum channel that allows Alice to send qubits to Bob and one bidirectional classical channel.
The quantum channel in such protocols is usually implemented with photons acting as qubits.
Note that the classical communication channel has to be authenticated, so that both parties can verify that the classical messages they receive indeed come from the party they are communicating with and are not tampered with in transit.
Note that the classical communication channel has to be authenticated, so that both parties can verify that the classical messages they receive indeed come from the party they are communicating with and aren't tampered with in transit.

@[section]({
"id": "key_distribution__bb84",
Expand All @@ -20,7 +20,7 @@ BB84 protocol, named after its inventors Charles H. Bennett and Gilles Brassard

BB84 protocol consists of the two main phases:

1. During the first phase, Alice prepares individual qubits following a certain procedure and then sends them to Bob via the quantum channel to be measured. Alice takes notes of the classical decisions she made when preparing the qubits, and Bob - of his decisions and the measurement results.
1. During the first phase, Alice prepares individual qubits following a certain procedure and then sends them to Bob via the quantum channel to be measured. Alice takes notes of the classical decisions she made when preparing the qubits, and Bob takes notes of his decisions when measuring the qubits and the measurement results.

2. The second phase is entirely classical post-processing and communication: Alice and Bob discuss their data from the first phase and extract a classical, random bit string they can use as a shared key.

Expand Down Expand Up @@ -55,13 +55,13 @@ If Alice decides to send $N$ qubits, she needs to make $2N$ random choices, usua

Once Bob receives the qubits from Alice, he needs to decide in which basis, computational or Hadamard, to measure each of them, and these decisions are also random, with each basis chosen with $50\%$ probability.

Finally, at the end of the first phase of the protocol Alice has a list of the bit values she sent as well as what basis she prepared each qubit in, and Bob has a list of bases he used to measure each qubit.
Finally, at the end of the first phase of the protocol, Alice has a list of the bit values she sent as well as what basis she prepared each qubit in, and Bob has a list of bases he used to measure each qubit.

To extract the shared key, they need to figure out when they both used the same basis, and toss the data from qubits where they used different bases. If Alice and Bob did not use the same basis to prepare and measure the qubits in, the measurement results Bob got will be just random bits with $50\%$ probability for both the `Zero` and `One` outcomes. But if they used the same basis, Bob's measurement result will match the bit Alice sent.
To extract the shared key, they need to figure out when they both used the same basis, and toss the data from qubits where they used different bases. If Alice and Bob didn't use the same basis to prepare and measure the qubits in, the measurement results Bob got will be just random bits with $50\%$ probability for both the 0 and 1 outcomes. But if they used the same basis, Bob's measurement result will match the bit Alice sent.

This means that by exchanging information about the bases Alice and Bob used for preparation and measurements via a public classical communication channel, they can deduce which parts of their lists of bits they kept private are identical, and use them as their shared key!

Now that we've learned the theory behind the BB84 protocol, let's implement its steps to see it in action!
Now that you've learned the theory behind the BB84 protocol, let's implement its steps to see it in action!

@[exercise]({
"id": "key_distribution__random_array",
Expand Down Expand Up @@ -95,7 +95,7 @@ Now that we've learned the theory behind the BB84 protocol, let's implement its

In this lesson, your goal is to put together the code from the previous exercises to simulate the complete BB84 protocol, from Alice choosing her bits and sending qubits to Bob to them figuring out the shared key.

> This is an open-ended task that is not tested automatically, unlike the previous exercises. Follow the suggestions in the comments to write your code and test it!
> This is an open-ended task that isn't tested automatically, unlike the previous exercises. Follow the suggestions in the comments to write your code and test it!
@[example]({"id": "key_distribution__bb84_demo", "codePath": "./examples/BB84Demo.qs"})

Expand All @@ -105,7 +105,7 @@ In this lesson, your goal is to put together the code from the previous exercise
"title": "Detecting an Eavesdropper"
})

Now, let's consider adding an eavesdropper Eve in the protocol.
Now, let's consider adding an eavesdropper, known as Eve, in the protocol.

Eve can intercept a qubit from the quantum channel that Alice and Bob are using.
She can try to get some information from it by measuring it. Then she prepares a new qubit and sends it back to the channel for Bob to receive.
Expand Down
2 changes: 1 addition & 1 deletion katas/content/key_distribution/measure_qubits/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
* `true`: use the basis $\ket{+}$ / $\ket{-}$ (Hadamard).

**Goal:** Measure each qubit in the corresponding basis and return an array of results as Boolean values, encoding measurement result `Zero` as `false` and `One` as `true`.
The state of the qubits at the end of the operation does not matter.
The state of the qubits at the end of the operation doesn't matter.
8 changes: 4 additions & 4 deletions katas/content/key_distribution/measure_qubits/solution.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
If the `bases[i]` array element is `true`, it means that we are choosing the Hadamard basis for this qubit, and thus an $H$ gate needs to be applied. Otherwise, we choose computational basis and don't need to apply the $H$ gate before measuring the qubit.
If the `bases[i]` array element is `true`, it means that you're choosing the Hadamard basis for this qubit, and thus an $H$ gate needs to be applied. Otherwise, you choose the computational basis and don't need to apply the $H$ gate before measuring the qubit.

Now, the output is expected to be a Boolean array, and thus we need to measure the each qubit and convert this measurement to a `Bool`.
1. To measure each of the qubits in one operation call, we can use Q# library operation `MeasureEachZ`.
2. To convert these measurement results into a Boolean array, we can use the function `ResultArrayAsBoolArray` that takes an array of `Result` type as an input and returns the required array of `Bool` type.
Now, the output is expected to be a Boolean array, and thus you need to measure the each qubit and convert this measurement to a `Bool`.
1. To measure each of the qubits in one operation call, you can use Q# library operation `MeasureEachZ`.
2. To convert these measurement results into a Boolean array, you can use the function `ResultArrayAsBoolArray` that takes an array of `Result` type as an input and returns the required array of `Bool` type.

@[solution]({
"id": "key_distribution__measure_qubits_solution",
Expand Down
10 changes: 5 additions & 5 deletions katas/content/key_distribution/prepare_qubits/solution.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
There are a total of 4 types of states that Alice can prepare before sending to Bob, each corresponds to the unique combination of bits and bases bool array.

1. State $\ket{0}$ corresponds to `bases[i]` be in computational basis, i.e., `false`, and `bits[i]` equal to 0, i.e., `false`.
2. State $\ket{1}$ corresponds to `bases[i]` in computational basis, i.e., `false`, and `bits[i]` equal to 1, i.e., `true`.
3. State $\ket{+}$ corresponds to `bases[i]` in Hadamard basis, i.e., `true`, and `bits[i]` equal to 0, i.e., `false`.
4. State $\ket{-}$ corresponds to `bases[i]` in Hadamard basis, i.e., `true` and `bits[i]` equal to 1, i.e., `true`.
1. State $\ket{0}$ corresponds to `bases[i]` being in the computational basis (that is, `false`) and `bits[i]` being equal to 0 (that is, `false`).
2. State $\ket{1}$ corresponds to `bases[i]` being in the computational basis (that is, `false`) and `bits[i]` being equal to 1 (that is, `true`).
3. State $\ket{+}$ corresponds to `bases[i]` being in the Hadamard basis (that is, `true`) and `bits[i]` being equal to 0 (that is, `false`).
4. State $\ket{-}$ corresponds to `bases[i]` being in the Hadamard basis (that is, `true`) and `bits[i]` being equal to 1 (that is, `true`).

So, in case `bits[i]` is set to `true`, we need to apply the $X$ gate to the i-th qubit, and then if `bases[i]` is set to `true`, the $H$ gate needs to be applied to the i-th qubit.
So, in case `bits[i]` is set to `true`, you need to apply the $X$ gate to the i-th qubit, and then if `bases[i]` is set to `true`, the $H$ gate needs to be applied to the i-th qubit.

@[solution]({
"id": "key_distribution__prepare_qubits_solution",
Expand Down
2 changes: 1 addition & 1 deletion katas/content/key_distribution/random_array/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
**Input:** An integer $N$.

**Output** : A `Bool` array of length N, where each element is chosen at random as `true` or `false` with $50\%$ probability.
**Output** : A `Bool` array of length $N$, where each element is chosen at random as `true` or `false` with $50\%$ probability.

> This will be used by both Alice and Bob to choose either the sequence of bits to send or the sequence of bases
> to use when encoding/measuring the bits.
4 changes: 2 additions & 2 deletions katas/content/key_distribution/random_array/solution.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
We need to do three steps to generate an array of random values:
You need to do three steps to generate an array of random values:

1. Create a mutable array of size $N$ - it will need a default value that can be `false`.
2. For each index from $0$ to $N-1$, choose one of the two values `true` or `false` at random, and assign that value to the array element at that index. We could choose a random bit by allocating a qubit, preparing it in the $\ket{+}$ state, and measuring it. However, we don't need those bits to have quantum origin, so we can use a Q# library operation `DrawRandomInt` instead.
2. For each index from $0$ to $N-1$, choose one of the two values `true` or `false` at random, and assign that value to the array element at that index. You could choose a random bit by allocating a qubit, preparing it in the $\ket{+}$ state, and measuring it. However, you don't need those bits to have quantum origin, so you can use a Q# library operation `DrawRandomInt` instead.
3. Finally, return the generated array.

@[solution]({
Expand Down
6 changes: 3 additions & 3 deletions katas/content/key_distribution/shared_key/solution.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
If Alice and Bob exchanged a qubit and used the same bases for preparing and measuring it, the bit produced by Bob's measurement would be the same as the one Alice encoded. Thus, they do not need to share the bits they chose or obtained over the classical communication channel. Sharing the bases used for each of the qubit is sufficient to understand if their bits match or not.
If Alice and Bob exchanged a qubit and used the same bases for preparing and measuring it, the bit produced by Bob's measurement would be the same as the one Alice encoded. Thus, they don't need to share the bits they chose or obtained over the classical communication channel. Sharing the bases used for each of the qubit is sufficient to understand if their bits match or not.

To complete this task, we need to perform the following steps:
To complete this task, you need to perform the following steps:

1. Declare an empty mutable array, let's name it `key`.
2. Decide which bits we can add to our key based on the comparison between bases used by Alice and Bob. You can iterate using an index in the range from $0$ to $N - 1$ and compare the bases in the corresponding positions.
2. Decide which bits you can add to the key based on the comparison between bases used by Alice and Bob. You can iterate using an index in the range from $0$ to $N - 1$ and compare the bases in the corresponding positions.
3. Return the required `key`.

@[solution]({
Expand Down

0 comments on commit 0d1737a

Please sign in to comment.