microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
samples/getting_started/SimpleTeleportation.qs
50lines · modeblame
c97c05a1DmitryVasilevsky1 years ago | 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 | let correct = Std.Diagnostics.CheckZero(qBob); | |
| 43 | Message($"Teleportation successful: {correct}."); | |
| 44 | | |
| 45 | // Reset all qubits to |0⟩ state. | |
| 46 | ResetAll([qAlice, qBob, qToTeleport]); | |
| 47 | | |
| 48 | // Return indication if the measurement of the state was correct | |
| 49 | correct | |
| 50 | } |