Skip to content

Commit

Permalink
Added teleportation task 1.7, 1.8 (microsoft#1607)
Browse files Browse the repository at this point in the history
Added teleportation task 1.7n8

---------

Co-authored-by: Mariia Mykhailova <[email protected]>
  • Loading branch information
devikamehra and tcNickolas authored Jun 7, 2024
1 parent f2b6e7b commit 5577430
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 0 deletions.
31 changes: 31 additions & 0 deletions katas/content/teleportation/entanglement_swapping/Placeholder.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Kata {
operation EntanglementSwapping() : ((Qubit, Qubit) => Int, (Qubit, Int) => Unit) {
return (SendMessageCharlie, ReconstructMessageBob);
}

// The skeleton operations for two parts of the protocol.
operation SendMessageCharlie(qAlice1 : Qubit, qBob1 : Qubit) : Int {
// Implement your solution here...
return -1;
}

operation ReconstructMessageBob(qBob2 : Qubit, resultCharlie : Int) : Unit {
// Implement your solution here...
}

// You might find these helper operations from earlier tasks useful.
operation SendMessage(qAlice: Qubit, qMessage: Qubit) : (Bool, Bool) {
CNOT(qMessage, qAlice);
H(qMessage);
return (M(qMessage) == One, M(qAlice) == One);
}

operation ReconstructMessage(qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {
if b1 {
Z(qBob);
}
if b2 {
X(qBob);
}
}
}
32 changes: 32 additions & 0 deletions katas/content/teleportation/entanglement_swapping/Solution.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Kata {
open Microsoft.Quantum.Convert;

operation EntanglementSwapping() : ((Qubit, Qubit) => Int, (Qubit, Int) => Unit) {
return (SendMessageCharlie, ReconstructMessageBob);
}

operation SendMessageCharlie(qAlice1 : Qubit, qBob1 : Qubit) : Int {
let (c1, c2) = SendMessage(qAlice1, qBob1);
return BoolArrayAsInt([c1, c2]);
}

operation ReconstructMessageBob(qBob2 : Qubit, resultCharlie : Int) : Unit {
let classicalBits = IntAsBoolArray(resultCharlie, 2);
ReconstructMessage(qBob2, (classicalBits[0], classicalBits[1]));
}

operation SendMessage(qAlice: Qubit, qMessage: Qubit) : (Bool, Bool) {
CNOT(qMessage, qAlice);
H(qMessage);
return (M(qMessage) == One, M(qAlice) == One);
}

operation ReconstructMessage(qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {
if b1 {
Z(qBob);
}
if b2 {
X(qBob);
}
}
}
48 changes: 48 additions & 0 deletions katas/content/teleportation/entanglement_swapping/Verification.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace Kata.Verification {
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Katas;

operation TeleportEntanglementSwappingTestLoop(
entanglementSwapping : ((Qubit, Qubit) => Int, (Qubit, Int) => Unit)
) : Bool {

for i in 1 .. 15 {
use (qAlice1, qAlice2) = (Qubit(), Qubit());
EntangleWrapper_Reference([qAlice1, qAlice2]);

use (qBob1, qBob2) = (Qubit(), Qubit());
EntangleWrapper_Reference([qBob1, qBob2]);

let (teleportOp, adjustOp) = entanglementSwapping;
// Apply the operations returned by the solution:
// first Charlie's side, then Bob's side.
let result = teleportOp(qAlice1, qBob1);
adjustOp(qBob2, result);

// Apply adjoint of the operation that prepares the |Φ⁺⟩ state:
// if the state of Alice's and Bob's qubits was |Φ⁺⟩,
// their state should become |00⟩ now.
Adjoint EntangleWrapper_Reference([qAlice2, qBob2]);

// Assert that Alice's and Bob's qubits end up in |0⟩ state.
if not CheckAllZero([qAlice2, qBob2]) {
Message($"Incorrect.");
Message($"Entanglement swapping was not successful, as qubits qAlice2 and qBob2 didn't end up in the state |Φ⁺⟩ = 1/sqrt(2) (|00⟩ + |11⟩)");
EntangleWrapper_Reference([qAlice2, qBob2]);
Message("The state of the qubits [qAlice1, qAlice2, qBob1, qBob2] after teleportation:");
DumpMachine();
ResetAll([qAlice1, qAlice2, qBob1, qBob2]);
return false;
}

Message($"Correct.");
ResetAll([qAlice1, qAlice2, qBob1, qBob2]);
return true;
}
}

@EntryPoint()
operation CheckSolution() : Bool {
return TeleportEntanglementSwappingTestLoop(Kata.EntanglementSwapping());
}
}
14 changes: 14 additions & 0 deletions katas/content/teleportation/entanglement_swapping/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Alice and Bob, independently from each other, each hold an entangled qubit pair in the state $\ket{\Phi^{+}} = \frac{1}{\sqrt{2}}(\ket{00} + \ket{11})$. They each hand off one part of their pair to Charlie.

Charlie can now teleport the state of Alice's qubit he holds onto Bob's remaining qubit, thus teleporting the entanglement. Just like in the "standard" teleportation, Bob still needs to apply the reconstruction steps - based on Charlie's measurement results - to the other qubit in his possession.

After this procedure the state $\ket{\Phi^{+}} = \frac{1}{\sqrt{2}}(\ket{00} + \ket{11})$ now spans across Alice's and Bob's other qubits - the ones they didn't send to Charlie. These qubits are now maximally entangled, even though they never interacted in the first place!

**Output:**
A tuple of two operations:

1. The first operation is Charlie's part of the protocol. It will take two qubits as input (the ones Alice and Bob sent to Charlie), and produce a message, encoded as an integer, that Charlie will send to Bob.

2. The second operation is Bob's part of the protocol. It will take the qubit that remained in Bob's possession and Charlie's encoded integer as input and use the message to adjust the state of Bob's qubit, so that Alice's and Bob's qubits end up in the shared state $\ket{\Phi^{+}}$.

> You will need to implement two separate helper operations, one for each part of the protocol, and return them, rather than implementing the solution in the body of the top-level operation.
15 changes: 15 additions & 0 deletions katas/content/teleportation/entanglement_swapping/solution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
The exercise demonstrate the capability of sharing a bell pair between Alice and Bob without any direct communication. This requires a third party Charlie who both Alice and Bob trust.

The implementation can be divided into following operations:

- Individually, Alice and Bob prepare a Bell state each, two qubits in the $\ket{\Phi^{+}} = \frac{1}{\sqrt{2}}(\ket{00} + \ket{11})$ state.
- Charlie now receives one qubit from Alice `qAlice1` and other from Bob `qBob1`.
- Now, Charlie treats Alice's qubit `qAlice1` as the message qubit and teleports its state to Bob. Using the `SendMessage` operation implemented in the earlier exercise, Charlie gets two `Bool` values and encodes them as one `Int` - the classical message. Based on Charlie's measurement results, the remaining two qubits, `qAlice2` and `qBob2` can be in one of the four Bell states.
- Finally, using the `ReconstructMessage` operation implemented in the earlier exercise, Bob can perform correction on the `qBob2` qubit to convert the state of the qubit pair of `qAlice2` and `qBob2` to $\ket{\Phi^{+}}$.

In this exercise you only need to implement the last two steps.

@[solution]({
"id": "teleportation__entanglement_swapping_solution",
"codePath": "./Solution.qs"
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
namespace Kata {
open Microsoft.Quantum.Diagnostics;

@EntryPoint()
operation TestTeleportation() : Unit {

// To define the different states, let us make use of PauliX, PauliY and PauliZ basis
let messages = [(PauliX, Zero, "|+⟩"),
(PauliX, One, "|-⟩"),
(PauliY, Zero, "|i⟩"),
(PauliY, One, "|-i⟩"),
(PauliZ, Zero, "|0⟩"),
(PauliZ, One, "|1⟩")];

// To effectively test the solution, experiment needs to be repeated multiple times
let numRepetitions = 100;

// Loop through all the states to test each one of them individually
for (basis, sentState, stateName) in messages {

// Loop through multiple iterations for each state
for j in 1 .. numRepetitions {
// 1. Initialize qubits for Alice and Bob
// ..

// 2. Prepare the entangled state between Alice and Bob
// ..

// 3. Prepare the Message qubit and send classical message
// ..

// 4. Reconstruct Bob's qubit using the classical message
// ..

// 5. Verify if the state was teleported correctly. If not, indicate failure
// ..

// 6. Reset the qubits
// ..
}
}

// 7. Indicate success if everything went well
// ..
}

// You might find these helper operations from earlier tasks useful.
operation PrepareAndSendMessage(qAlice : Qubit, basis : Pauli, state : Result) : (Bool, Bool) {
use qMessage = Qubit();
if state == One {
X(qMessage);
}
if basis != PauliZ {
H(qMessage);
}
if basis == PauliY {
S(qMessage);
}
let classicalBits = SendMessage(qAlice, qMessage);
Reset(qMessage);
return classicalBits;
}

operation ReconstructAndMeasureMessage(qBob : Qubit, (b1 : Bool, b2 : Bool), basis : Pauli) : Result {
ReconstructMessage(qBob, (b1, b2));
return Measure([basis], [qBob]);
}

operation Entangle(qAlice : Qubit, qBob : Qubit) : Unit is Adj + Ctl {
H(qAlice);
CNOT(qAlice, qBob);
}

operation SendMessage(qAlice: Qubit, qMessage: Qubit) : (Bool, Bool) {
CNOT(qMessage, qAlice);
H(qMessage);
return (M(qMessage) == One, M(qAlice) == One);
}

operation ReconstructMessage(qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {
if b1 {
Z(qBob);
}
if b2 {
X(qBob);
}
}
}
34 changes: 34 additions & 0 deletions katas/content/teleportation/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,38 @@ We split the teleportation protocol into several steps:
"../KatasLibrary.qs",
"./Common.qs"
]
})

@[section]({
"id": "teleportation__testing_standard_teleportation",
"title": "Testing standard quantum teleportation"
})

In this lesson, your goal is to put together the code from the previous exercises to teleport the states $\ket{0}$ and $\ket{1}$, as well as superposition states $\frac{1}{2}(\ket{0}+\ket{1})$, $\frac{1}{2}(\ket{0}-\ket{1})$, $\frac{1}{2}(\ket{0}+i\ket{1})$ and $\frac{1}{2}(\ket{0}-i\ket{1})$, and to verify that teleportation succeeds each time.

> 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!
@[example]({
"id": "teleportation__testing_standard_teleportation_example",
"codePath": "./examples/TestingStandardTeleportation.qs"
})

@[section]({
"id": "teleportation__three_parties",
"title": "Teleportation with Three Parties"
})

There are multiple variants of teleportation protocol that involve more than two parties. In this lesson, we'll take a look at two of them:

- Entanglement swapping allows us to propagate entanglement across space, enabling protocols such as quantum repeater.
- In teleportation with three entangled qubits, a state is transferred from Alice to a third participant Charlie, but this may only be accomplished if Charlie has the trust of the second participant Bob.

@[exercise]({
"id": "teleportation__entanglement_swapping",
"title": "Entanglement Swapping",
"path": "./entanglement_swapping/",
"qsDependencies": [
"../KatasLibrary.qs",
"./Common.qs"
]
})

0 comments on commit 5577430

Please sign in to comment.