Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teleportation Task 1.2-1.4 added #1556

Merged
merged 5 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion katas/content/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
"oracles",
"deutsch_algo",
"deutsch_jozsa",
"qec_shor"
"qec_shor",
"teleportation"
tcNickolas marked this conversation as resolved.
Show resolved Hide resolved
]
98 changes: 55 additions & 43 deletions katas/content/teleportation/Common.qs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Kata.Verification {

open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Math;

operation EntangleWrapper_Reference(qs : Qubit[]) : Unit is Adj + Ctl {
let (qAlice, qBob) = (qs[0], qs[1]);
H(qAlice);
Expand All @@ -14,7 +14,7 @@ namespace Kata.Verification {
// 1 - |Φ⁻⟩ = (|00⟩ - |11⟩) / sqrt(2)
// 2 - |Ψ⁺⟩ = (|01⟩ + |10⟩) / sqrt(2)
// 3 - |Ψ⁻⟩ = (|01⟩ - |10⟩) / sqrt(2)
operation StatePrep_BellState (q1 : Qubit, q2 : Qubit, state : Int) : Unit {
operation StatePrep_BellState(q1 : Qubit, q2 : Qubit, state : Int) : Unit {
H(q1);
CNOT(q1, q2);

Expand All @@ -30,18 +30,15 @@ namespace Kata.Verification {
}

// ------------------------------------------------------
// Helper operation that runs teleportation using two building blocks
// specified as first two parameters.
operation ComposeTeleportation (
setupPsiOp : (Qubit => Unit is Adj),
// Helper operation that run teleportation using the given operations to prepare the message qubit
// and the entangled pair, and to run sender and receiver parts of the protocol.
operation ComposeTeleportation(
bellPrepOp : ((Qubit, Qubit) => Unit),
getDescriptionOp : ((Qubit, Qubit) => (Bool, Bool)),
reconstructOp : ((Qubit, (Bool, Bool)) => Unit),
reconstructOp : ((Qubit, (Bool, Bool)) => Unit),
qAlice : Qubit,
qBob : Qubit,
qMessage : Qubit) : Unit {

setupPsiOp(qMessage);

bellPrepOp(qAlice, qBob);
let classicalBits = getDescriptionOp(qAlice, qMessage);
Expand All @@ -50,13 +47,14 @@ namespace Kata.Verification {
// Bob uses these bits to transform his part of the entangled pair into the message.
reconstructOp(qBob, classicalBits);
}

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

operation ReconstructMessage_Reference (qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {
operation ReconstructMessage_Reference(qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {
if b1 {
Z(qBob);
}
Expand All @@ -65,47 +63,61 @@ namespace Kata.Verification {
}
}

operation CheckTeleportationWithFeedback(protocolTeleportation : (((Qubit => Unit is Adj),Qubit,Qubit,Qubit) => Unit)) : Bool {
let numRepetitions = 100;
let setupPsiOps = [I, X, H, Ry(42.0, _)];
let operationName = ["|0>","|1>","|+>","Unequal Superposition"];
mutable index = 0;
while index < Length(setupPsiOps) {
use (qAlice, qBob, qMessage) = (Qubit(),Qubit(),Qubit());
protocolTeleportation(setupPsiOps[index],qAlice,qBob,qMessage);
Adjoint (setupPsiOps[index])(qBob);
if not CheckZero(qBob){
Message($"Incorrect. {operationName[index]} state was teleported incorrectly.");
ResetAll([qMessage, qAlice, qBob]);
return false;
}
// ------------------------------------------------------
// Helper operation that runs a teleportation operation (specified by teleportOp).
// The state to teleport is set up using an operation (specified by setupPsiOp).
//
// Specifying the state to teleport through an operation allows to get the inverse
// which makes testing easier.
operation TeleportTestHelper(
teleportOp : ((Qubit, Qubit, Qubit) => Unit),
setupPsiOp : (Qubit => Unit is Adj)) : Bool {

use (qMessage, qAlice, qBob) = (Qubit(), Qubit(), Qubit());
setupPsiOp(qMessage);

// This should modify qBob to be identical to the state
// of qMessage before the function call.
teleportOp(qAlice, qBob, qMessage);

// Applying the inverse of the setup operation to qBob
// should make it Zero.
Adjoint setupPsiOp(qBob);
if not CheckZero(qBob){
Message("Incorrect. The state was teleported incorrectly.");
DumpMachine();
tcNickolas marked this conversation as resolved.
Show resolved Hide resolved
ResetAll([qMessage, qAlice, qBob]);
set index += 1;
return false;
}
ResetAll([qMessage, qAlice, qBob]);
Message("Correct.");
devikamehra marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

operation CheckTeleportationCompleteWithFeedback(protocolTeleportation : ((Qubit,Qubit,Qubit) => Unit)) : Bool {
// ------------------------------------------------------
// Run teleportation for a number of different states.
// After each teleportation success is asserted.
// Also repeats for each state several times as
// code is expected to take different paths each time because
// measurements done by Alice are not deterministic.
operation TeleportTestLoop(teleportOp : ((Qubit, Qubit, Qubit) => Unit)) : Bool {
// Define setup operations for the message qubit
// on which to test teleportation: |0⟩, |1⟩, |0⟩ + |1⟩, unequal superposition.
let setupPsiOps = [I, X, H, Ry(ArcCos(0.6) * 2.0, _)];
devikamehra marked this conversation as resolved.
Show resolved Hide resolved

// As part of teleportation Alice runs some measurements
// with nondeterministic outcome.
// Depending on the outcomes different paths are taken on Bob's side.
// We repeat each test run several times to ensure that all paths are checked.
let numRepetitions = 100;
let setupPsiOps = [I, X, H, Ry(42.0, _)];
let operationName = ["|0>","|1>","|+>","Unequal Superposition"];
mutable index = 0;
while index < Length(setupPsiOps) {
use (qAlice, qBob, qMessage) = (Qubit(),Qubit(),Qubit());
(setupPsiOps[index]) (qMessage);
protocolTeleportation(qAlice,qBob,qMessage);
Adjoint (setupPsiOps[index])(qBob);
if not CheckZero(qBob){
Message($"Incorrect. {operationName[index]} state was teleported incorrectly.");
ResetAll([qMessage, qAlice, qBob]);
return false;
for psiOp in setupPsiOps {
for j in 1 .. numRepetitions {
let validation = TeleportTestHelper(teleportOp, psiOp);
if not validation{
devikamehra marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
}
ResetAll([qMessage, qAlice, qBob]);
set index += 1;
}
Message("Correct.");
return true;
}

}
8 changes: 4 additions & 4 deletions katas/content/teleportation/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ We split the teleportation protocol into several steps:

@[exercise]({
"id": "teleportation__send_message",
"title": "Send the message (Alice's task)",
"path": "./send_the_message/",
"title": "Send Message (Alice's Task)",
"path": "./send_message/",
"qsDependencies": [
"../KatasLibrary.qs",
"./Common.qs"
Expand All @@ -46,8 +46,8 @@ We split the teleportation protocol into several steps:

@[exercise]({
"id": "teleportation__reconstruct_message",
"title": "Reconstruct the message (Bob's task)",
"path": "./reconstruct_the_message/",
"title": "Reconstruct Message (Bob's Task)",
"path": "./reconstruct_message/",
"qsDependencies": [
"../KatasLibrary.qs",
"./Common.qs"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Kata {
operation ReconstructMessage(qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {
// ...
devikamehra marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Kata {
operation ReconstructMessage (qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {
operation ReconstructMessage(qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {
if b1 {
Z(qBob);
}
Expand Down
10 changes: 10 additions & 0 deletions katas/content/teleportation/reconstruct_message/Verification.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Kata.Verification {
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Katas;

@EntryPoint()
operation CheckSolution() : Bool {
let teleport = ComposeTeleportation(StatePrep_BellState(_, _, 0), SendMessage_Reference, Kata.ReconstructMessage, _, _, _);
return TeleportTestLoop(teleport);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Transform Bob's qubit into the required state using the two classical bits received from Alice.

**Input:**
1. Bob's part of the entangled pair of qubits qBob.
**Inputs:**
1. Bob's part of the entangled pair of qubits `qBob`.
2. The tuple of classical bits received from Alice, in the format used in previous exercise.

**Output:**
**Output:**
Transform Bob's qubit qBob into the state in which the message qubit had been originally.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Kata {
operation SendMessage (qAlice : Qubit, qMessage : Qubit) : (Bool, Bool) {
operation SendMessage(qAlice : Qubit, qMessage : Qubit) : (Bool, Bool) {
// Implement your solution here...
return (false, false);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Kata {
operation SendMessage (qAlice : Qubit, qMessage : Qubit) : (Bool, Bool) {
operation SendMessage(qAlice : Qubit, qMessage : Qubit) : (Bool, Bool) {
CNOT(qMessage, qAlice);
H(qMessage);
return (M(qMessage) == One, M(qAlice) == One);
Expand Down
10 changes: 10 additions & 0 deletions katas/content/teleportation/send_message/Verification.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Kata.Verification {
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Katas;

@EntryPoint()
operation CheckSolution() : Bool {
let teleport = ComposeTeleportation(StatePrep_BellState(_, _, 0), Kata.SendMessage, ReconstructMessage_Reference, _, _, _);
return TeleportTestLoop(teleport);
}
}
8 changes: 8 additions & 0 deletions katas/content/teleportation/send_message/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Entangle the message qubit with Alice's qubit and extract two classical bits to be sent to Bob.

**Inputs:**
1. Alice's part of the entangled pair of qubits `qAlice`.
2. The message qubit `qMessage`.

**Output:**
Two classical bits Alice will send to Bob via classical channel as a tuple of Boolean values. The first bit in the tuple should hold the result of measurement of the message qubit, the second bit - the result of measurement of Alice's qubit. Represent measurement result `One` as `true` and `Zero` as `false`. The state of the qubits in the end of the operation doesn't matter.
15 changes: 0 additions & 15 deletions katas/content/teleportation/send_the_message/Verification.qs

This file was deleted.

8 changes: 0 additions & 8 deletions katas/content/teleportation/send_the_message/index.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
namespace Kata {
operation StandardTeleport (qAlice : Qubit, qBob : Qubit, qMessage : Qubit) : Unit {
// ...
operation StandardTeleport(qAlice : Qubit, qBob : Qubit, qMessage : Qubit) : Unit {
//..
devikamehra marked this conversation as resolved.
Show resolved Hide resolved
}

// You might find these helper operations from earlier tasks useful.
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);
}
}
}
devikamehra marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
namespace Kata {
operation StandardTeleport(qAlice : Qubit, qBob : Qubit, qMessage : Qubit) : Unit {
Entangle(qAlice, qBob);
let classicalBits = SendMessage(qAlice, qMessage);
ReconstructMessage(qBob, classicalBits);
}

operation Entangle(qAlice : Qubit, qBob : Qubit) : Unit is Adj + Ctl {
H(qAlice);
Expand All @@ -11,17 +16,12 @@ namespace Kata {
return (M(qMessage) == One, M(qAlice) == One);
}

operation ReconstructMessage (qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {
operation ReconstructMessage(qBob : Qubit, (b1 : Bool, b2 : Bool)) : Unit {
if b1 {
Z(qBob);
}
if b2 {
X(qBob);
}
}
operation StandardTeleport (qAlice : Qubit, qBob : Qubit, qMessage : Qubit) : Unit {
Entangle(qAlice, qBob);
let classicalBits = SendMessage(qAlice, qMessage);
ReconstructMessage(qBob, classicalBits);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace Kata.Verification {
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Katas;

@EntryPoint()
operation CheckSolution() : Bool {
return CheckTeleportationCompleteWithFeedback(Kata.StandardTeleport);
operation CheckSolution() : Bool {
let teleport = Kata.StandardTeleport(_, _, _);
tcNickolas marked this conversation as resolved.
Show resolved Hide resolved
return TeleportTestLoop(teleport);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Put together the steps implemented in previous three exercises to implement the full teleportation protocol.

**Input:**
1. The two qubits qAlice and qBob in $\ket{0}$ state.
2. The message qubit qMessage in the state $\ket{\psi}$ to be teleported.
**Inputs:**
1. The two qubits `qAlice` and `qBob` in $\ket{0}$ state.
2. The message qubit `qMessage` in the state $\ket{\psi}$ to be teleported.

**Output:**
**Goal:**
Transform Bob's qubit qBob into the state $\ket{\psi}$. The state of the qubits qAlice and qMessage in the end of the operation doesn't matter.
devikamehra marked this conversation as resolved.
Show resolved Hide resolved
Loading