microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
samples/Teleportation.qs
205lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | |
| 5 | namespace Microsoft.Quantum.Samples.Teleportation { |
| 6 | open Microsoft.Quantum.Canon; |
| 7 | open Microsoft.Quantum.Intrinsic; |
| 8 | open Microsoft.Quantum.Random; |
| 9 | |
| 10 | ////////////////////////////////////////////////////////////////////////// |
| 11 | // Introduction ////////////////////////////////////////////////////////// |
| 12 | ////////////////////////////////////////////////////////////////////////// |
| 13 | |
| 14 | // Quantum teleportation provides a way of moving a quantum state from one |
| 15 | // location to another without having to move physical particle(s) along |
| 16 | // with it. This is done with the help of previously shared quantum |
| 17 | // entanglement between the sending and the receiving locations and |
| 18 | // classical communication. |
| 19 | |
| 20 | ////////////////////////////////////////////////////////////////////////// |
| 21 | // Teleportation ///////////////////////////////////////////////////////// |
| 22 | ////////////////////////////////////////////////////////////////////////// |
| 23 | |
| 24 | /// # Summary |
| 25 | /// Sends the state of one qubit to a target qubit by using |
| 26 | /// teleportation. |
| 27 | /// |
| 28 | /// Notice that after calling Teleport, the state of `msg` is |
| 29 | /// collapsed. |
| 30 | /// |
| 31 | /// # Input |
| 32 | /// ## msg |
| 33 | /// A qubit whose state we wish to send. |
| 34 | /// ## target |
| 35 | /// A qubit initially in the |0〉 state that we want to send |
| 36 | /// the state of msg to. |
| 37 | operation Teleport (msg : Qubit, target : Qubit) : Unit { |
| 38 | use register = Qubit(); |
| 39 | // Create some entanglement that we can use to send our message. |
| 40 | H(register); |
| 41 | CNOT(register, target); |
| 42 | |
| 43 | // Encode the message into the entangled pair. |
| 44 | CNOT(msg, register); |
| 45 | H(msg); |
| 46 | |
| 47 | // Measure the qubits to extract the classical data we need to |
| 48 | // decode the message by applying the corrections on |
| 49 | // the target qubit accordingly. |
| 50 | if (M(msg) == One) { Z(target); } |
| 51 | // Correction step |
| 52 | if (M(register) == One) { |
| 53 | X(target); |
| 54 | // Reset register to Zero state before releasing |
| 55 | X(register); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // One can use quantum teleportation circuit to send an unobserved |
| 60 | // (unknown) classical message from source qubit to target qubit |
| 61 | // by sending specific (known) classical information from source |
| 62 | // to target. |
| 63 | |
| 64 | /// # Summary |
| 65 | /// Uses teleportation to send a classical message from one qubit |
| 66 | /// to another. |
| 67 | /// |
| 68 | /// # Input |
| 69 | /// ## message |
| 70 | /// If `true`, the source qubit (`here`) is prepared in the |
| 71 | /// |1〉 state, otherwise the source qubit is prepared in |0〉. |
| 72 | /// |
| 73 | /// ## Output |
| 74 | /// The result of a Z-basis measurement on the teleported qubit, |
| 75 | /// represented as a Bool. |
| 76 | operation TeleportClassicalMessage (message : Bool) : Bool { |
| 77 | // Ask for some qubits that we can use to teleport. |
| 78 | use (msg, target) = (Qubit(), Qubit()); |
| 79 | |
| 80 | // Encode the message we want to send. |
| 81 | if (message) { |
| 82 | X(msg); |
| 83 | } |
| 84 | |
| 85 | // Use the operation we defined above. |
| 86 | Teleport(msg, target); |
| 87 | |
| 88 | // Check what message was received. |
| 89 | let result = (M(target) == One); |
| 90 | |
| 91 | // Reset qubits to Zero state before releasing |
| 92 | Reset(msg); |
| 93 | Reset(target); |
| 94 | |
| 95 | return result; |
| 96 | } |
| 97 | |
| 98 | /// # Summary |
| 99 | /// Sets the qubit's state to |+⟩. |
| 100 | operation SetToPlus(q: Qubit) : Unit { |
| 101 | Reset(q); |
| 102 | H(q); |
| 103 | } |
| 104 | |
| 105 | /// # Summary |
| 106 | /// Sets the qubit's state to |−⟩. |
| 107 | operation SetToMinus(q: Qubit) : Unit { |
| 108 | Reset(q); |
| 109 | X(q); |
| 110 | H(q); |
| 111 | } |
| 112 | |
| 113 | /// # Summary |
| 114 | /// Returns true if qubit is |+⟩ (assumes qubit is either |+⟩ or |−⟩) |
| 115 | operation MeasureIsPlus(q: Qubit) : Bool { |
| 116 | // BLOCKED ON: within implementation. Measure, MapPauli. |
| 117 | // return (Measure([PauliX], [q]) == Zero); |
| 118 | H(q); |
| 119 | let result = M(q) == Zero; |
| 120 | H(q); |
| 121 | return result; |
| 122 | } |
| 123 | |
| 124 | /// # Summary |
| 125 | /// Returns true if qubit is |−⟩ (assumes qubit is either |+> or |−⟩) |
| 126 | operation MeasureIsMinus(q: Qubit) : Bool { |
| 127 | // BLOCKED ON: within implementation. Measure, MapPauli. |
| 128 | // return (Measure([PauliX], [q]) == One); |
| 129 | H(q); |
| 130 | let result = M(q) == One; |
| 131 | H(q); |
| 132 | return result; |
| 133 | } |
| 134 | |
| 135 | /// # Summary |
| 136 | /// Randomly prepares the qubit into |+⟩ or |−⟩ |
| 137 | operation PrepareRandomMessage(q: Qubit) : Unit { |
| 138 | let choice = DrawRandomInt(0, 1) == 1; |
| 139 | |
| 140 | if (choice) { |
| 141 | Message("Sending |->"); |
| 142 | SetToMinus(q); |
| 143 | } else { |
| 144 | Message("Sending |+>"); |
| 145 | SetToPlus(q); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // One can also use quantum teleportation to send any quantum state |
| 150 | // without losing any information. The following sample shows |
| 151 | // how a randomly picked non-trivial state (|-> or |+>) |
| 152 | // gets moved from one qubit to another. |
| 153 | |
| 154 | /// # Summary |
| 155 | /// Uses teleportation to send a randomly picked |-> or |+> state |
| 156 | /// to another. |
| 157 | operation TeleportRandomMessage () : Unit { |
| 158 | // Ask for some qubits that we can use to teleport. |
| 159 | use (msg, target) = (Qubit(), Qubit()); |
| 160 | PrepareRandomMessage(msg); |
| 161 | |
| 162 | // Use the operation we defined above. |
| 163 | Teleport(msg, target); |
| 164 | |
| 165 | // Report message received: |
| 166 | if (MeasureIsPlus(target)) { Message("Received |+>"); } |
| 167 | if (MeasureIsMinus(target)) { Message("Received |->"); } |
| 168 | |
| 169 | // Reset all of the qubits that we used before releasing |
| 170 | // them. |
| 171 | Reset(msg); |
| 172 | Reset(target); |
| 173 | } |
| 174 | |
| 175 | @EntryPoint() |
| 176 | operation Main () : Unit { |
| 177 | for idxRun in 1 .. 10 { |
| 178 | let sent = DrawRandomInt(0, 1) == 1; |
| 179 | let received = TeleportClassicalMessage(sent); |
| 180 | Message( |
| 181 | "Round " + AsString(idxRun) + |
| 182 | ": Sent " + AsString(sent) + |
| 183 | ", got " + AsString(received) + "."); |
| 184 | Message(sent == received ? "Teleportation successful!" | ""); |
| 185 | } |
| 186 | for idxRun in 1 .. 10 { |
| 187 | TeleportRandomMessage(); |
| 188 | } |
| 189 | |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // //////////////////////////////////////////////////////////////////////// |
| 194 | // Other teleportation scenarios not illustrated here |
| 195 | // //////////////////////////////////////////////////////////////////////// |
| 196 | |
| 197 | // ● Teleport a rotation. Rotate a basis state by a certain angle φ ∈ [0, 2π), |
| 198 | // for example by preparing Rₓ(φ) |0〉, and teleport the rotated state to the target qubit. |
| 199 | // When successful, the target qubit captures the angle φ [although, on course one does |
| 200 | // not have classical access to its value]. |
| 201 | // ● "Super dense coding". Given an EPR state |β〉 shared between the source and target |
| 202 | // qubits, the source can encode two classical bits a,b by applying Z^b X^a to its half |
| 203 | // of |β〉. Both bits can be recovered on the target by measurement in the Bell basis. |
| 204 | // For details refer to discussion and code in Unit Testing Sample, in file SuperdenseCoding.qs. |
| 205 | // //////////////////////////////////////////////////////////////////////// |