microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/getting_started/SimpleTeleportation.qs
54lines · modecode
| 1 | /// # Summary |
| 2 | /// Simple quantum teleportation sample |
| 3 | /// |
| 4 | /// # Description |
| 5 | /// This Q# program demonstrates how to teleport quantum state |
| 6 | /// by communicating two classical bits and using previously entangled qubits. |
| 7 | /// This code teleports one specific state, but any state can be teleported. |
| 8 | operation Main() : Bool { |
| 9 | // Allocate `qAlice`, `qBob` qubits |
| 10 | use (qAlice, qBob) = (Qubit(), Qubit()); |
| 11 | |
| 12 | // Entangle `qAlice`, `qBob` qubits |
| 13 | H(qAlice); |
| 14 | CNOT(qAlice, qBob); |
| 15 | |
| 16 | // From now on qubits `qAlice` and `qBob` will not interact directly. |
| 17 | |
| 18 | // Allocate `qToTeleport` qubit and prepare it to be |𝜓⟩≈0.9394|0⟩−0.3429𝑖|1⟩ |
| 19 | use qToTeleport = Qubit(); |
| 20 | Rx(0.7, qToTeleport); |
| 21 | |
| 22 | // Prepare the message by entangling `qToTeleport` and `qAlice` qubits |
| 23 | CNOT(qToTeleport, qAlice); |
| 24 | H(qToTeleport); |
| 25 | |
| 26 | // Obtain classical measurement results b1 and b2 at Alice's site. |
| 27 | let b1 = M(qToTeleport) == One; |
| 28 | let b2 = M(qAlice) == One; |
| 29 | |
| 30 | // At this point classical bits b1 and b2 are "sent" to the Bob's site. |
| 31 | |
| 32 | // Decode the message by applying adjustments based on classical data b1 and b2. |
| 33 | if b1 { |
| 34 | Z(qBob); |
| 35 | } |
| 36 | if b2 { |
| 37 | X(qBob); |
| 38 | } |
| 39 | |
| 40 | // Make sure that the obtained message is |𝜓⟩≈0.9394|0⟩−0.3429𝑖|1⟩ |
| 41 | Rx(-0.7, qBob); |
| 42 | // This state dump should show that the state of `qBob` is |0⟩ state, which means that the teleportation was successful. |
| 43 | Std.Diagnostics.DumpMachine(); |
| 44 | // We can further verify the teleport by measuring `qBob`, which should give us |0⟩ state with certainty. |
| 45 | // Note that verifying via measurement might require multiple runs or "shots" to investigate the distribution of outcomes. |
| 46 | let correct = M(qBob) == Zero; |
| 47 | Message($"Teleportation successful: {correct}."); |
| 48 | |
| 49 | // Reset all qubits to |0⟩ state. |
| 50 | ResetAll([qAlice, qBob, qToTeleport]); |
| 51 | |
| 52 | // Return indication if the measurement of the state was correct |
| 53 | correct |
| 54 | } |
| 55 | |