microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
jan

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

samples/getting_started/SimpleTeleportation.qs

50lines · modeblame

c97c05a1DmitryVasilevsky1 years ago1/// # 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.
8operation Main() : Bool {
9// Allocate `qAlice`, `qBob` qubits
10use (qAlice, qBob) = (Qubit(), Qubit());
11
12// Entangle `qAlice`, `qBob` qubits
13H(qAlice);
14CNOT(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⟩
19use qToTeleport = Qubit();
20Rx(0.7, qToTeleport);
21
22// Prepare the message by entangling `qToTeleport` and `qAlice` qubits
23CNOT(qToTeleport, qAlice);
24H(qToTeleport);
25
26// Obtain classical measurement results b1 and b2 at Alice's site.
27let b1 = M(qToTeleport) == One;
28let 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.
33if b1 {
34Z(qBob);
35}
36if b2 {
37X(qBob);
38}
39
40// Make sure that the obtained message is |𝜓⟩≈0.9394|0⟩−0.3429𝑖|1⟩
41Rx(-0.7, qBob);
42let correct = Std.Diagnostics.CheckZero(qBob);
43Message($"Teleportation successful: {correct}.");
44
45// Reset all qubits to |0⟩ state.
46ResetAll([qAlice, qBob, qToTeleport]);
47
48// Return indication if the measurement of the state was correct
49correct
50}