Putting it All Together: Teleportation

Let's return to the example of the teleportation circuit defined in Quantum Circuits. Shown below is a text-book quantum circuit that implements the teleportation, including the quantum part, the measurements, and the classically-controlled correction operations.

`Teleport(msg : Qubit, there : Qubit) : ()`

We can now translate each of the steps in this quantum circuit into Q#. First, we begin the definition of a new operation while will perform the teleportation given two qubits msg and there:

operation Teleport(msg : Qubit, there : Qubit) : () {
    body {

Next, we allocate a qubit here with a using block:

        using (register = Qubit[1]) {
            let here = register[0];

We can then create the entangled pair between here and there by using the H and CNOT operations:

            H(here);
            CNOT(here, there);

We then use the next $\operatorname{CNOT}$ and $H$ gates to move our message qubit:

            CNOT(msg, here);
            H(msg);

Finally, we use M to perform the measurements and feed them into classical control, as denoted by if statements:

            // Measure out the entanglement.
            if (M(msg) == One)  { Z(there); }
            if (M(here) == One) { X(there); }

This finishes the definition of our teleportation operator, so we can deallocate here, end the body, and end the operation.

        }
    }
}