Quantum teleportation do it yourself with Q#


Quantum computing nowadays is the one of the hottest topics in the computer science world.
Recently IBM unveiled the IBM Q System One: a 20-qubit quantum computer which is touting as “the world’s first fully integrated universal quantum computing system designed for scientific and commercial use”.

In this article I’d like how to show the quantum teleportation phenomenon. I will use the Q# language designed by Microsoft to simplify creating quantum algorithms.

In this example I have used the quantum simulator which I have wrapped with the REST api and put into the docker image.

Quantum teleportation allows moving a quantum state from one location to another. Shared quantum entanglement between two particles in the sending and receiving locations is used to do this without having to move physical particles along with it.

1. Theory

Let’s assume that we want to send the message, specific quantum state described using Dirac notation:


Additionally we have two entangled qubits, first in Laboratory 1 and second in Laboratory 2:


thus we starting with the input state:

|\psi\rangle|\phi^+\rangle=(\alpha|0\rangle+\beta|1\rangle)(\frac{1}{\sqrt{2}}(|00\rangle+|11\rangle)) |\psi\rangle|\phi^+\rangle=\frac{\alpha}{\sqrt{2}}|000\rangle + \frac{\alpha}{\sqrt{2}}|011\rangle + \frac{\beta}{\sqrt{2}}|100\rangle + \frac{\beta}{\sqrt{2}}|111\rangle

To send the message we need to start with two operations applying CNOT and then Hadamard gate.

CNOT gate flips the second qubit only if the first qubit is 1.

Applying CNOT gate will modify the first qubit of the input state and will result in:

\frac{\alpha}{\sqrt{2}}|000\rangle + \frac{\alpha}{\sqrt{2}}|011\rangle + \frac{\beta}{\sqrt{2}}|110\rangle + \frac{\beta}{\sqrt{2}}|101\rangle

Hadamard gate changes states as follows:

|0\rangle \rightarrow \frac{1}{\sqrt{2}}(|0\rangle+|1\rangle))


|1\rangle \rightarrow \frac{1}{\sqrt{2}}(|0\rangle-|1\rangle))

Applying Hadmard gate results in:

\frac{\alpha}{\sqrt{2}}(\frac{1}{\sqrt{2}}(|0\rangle+|1\rangle))|00\rangle + \frac{\alpha}{\sqrt{2}}(\frac{1}{\sqrt{2}}(|0\rangle+|1\rangle))|11\rangle + \frac{\beta}{\sqrt{2}}(\frac{1}{\sqrt{2}}(|0\rangle-|1\rangle))|10\rangle + \frac{\beta}{\sqrt{2}}(\frac{1}{\sqrt{2}}(|0\rangle-|1\rangle))|01\rangle



which we can write as:


Then we measure the states of the first two qubits (message qubit and Laboratory 1 qubit) where we can have four results:

  • |00\rangle which simplifies equation to: |00\rangle(\alpha|0\rangle+\beta|1\rangle) and indicates that the qubit in the Laboratory 2 is \alpha|0\rangle+\beta|1\rangle

  • |01\rangle which simplifies equation to: |01\rangle(\alpha|1\rangle+\beta|0\rangle) and indicates that the qubit in the Laboratory 2 is \alpha|1\rangle+\beta|0\rangle

  • |10\rangle which simplifies equation to: |10\rangle(\alpha|0\rangle-\beta|1\rangle) and indicates that the qubit in the Laboratory 2 is \alpha|0\rangle-\beta|1\rangle

  • |11\rangle which simplifies equation to: |11\rangle(\alpha|1\rangle-\beta|0\rangle) and indicates that the qubit in the Laboratory 2 is \alpha|1\rangle-\beta|0\rangle

Now we have to send the result classical way from Laboratory 1 to Laboratory 2.

Finally we know what transformation we need to apply to qubit in the Laboratory 2
to make its state equal to message qubit:


if Laboratory 2 qubit is in state:

  • \alpha|0\rangle+\beta|1\rangle we don’t need to do anything.

  • \alpha|1\rangle+\beta|0\rangle we need to apply NOT gate.

  • \alpha|0\rangle-\beta|1\rangle we need to apply Z gate.

  • \alpha|1\rangle-\beta|0\rangle we need to apply NOT gate followed by Z gate

This operations will transform Laboratory 2 qubit state to initial message qubit state thus we moved the particle state from Laboratory 1 to Laboratory 2 without moving particle.

2. Code

Now it’s time to show the quantum teleportation using Q# language. I have used Microsoft Quantum Development Kit to run the Q# code inside the .NET Core application. Additionally I have added the nginx proxy with the angular gui which will help to show the results.
Everything was put inside the docker to simplify the setup.

Before you will start you will need git, docker and docker-compose installed on your machine (https://docs.docker.com/get-started/)

To run the project we have to clone the repository and run it using docker compose:

git clone https://github.com/qooba/quantum-teleportation-qsharp.git
cd quantum-teleportation-qsharp
docker-compose -f app/docker-compose.yml up

Now we can run the http://localhost:8020/ in the browser:

Then we can put the message in the Laboratory 1, click the Teleport button, trigger for the teleportation process which sends the message to the Laboratory 2.

The text is converted into array of bits and each bit is sent to the Laboratory 2 using quantum teleportation.

In the first step we encode the incoming message using X gate.

if (message) {

Then we prepare the entanglement between the qubits in the Laboratory 1 and Laboratory 2.

CNOT(here, there);

In the second step we apply CNOT and Hadamard gate to send the message:

CNOT(msg, here);

Finally we measure the message qubit and the Laboratory 1 qubit:

if (M(msg) == One) {

if (M(here) == One) {

If the message qubit has state |1\rangle then we need to apply the Z gate to the Laboratory 2 qubit.
If the Laboratory 1 qubit has state |1\rangle then we need to apply the X gate to the Laboratory 2 qubit. This information must be sent classical way to the Laboratory 2.

Now the Laboratory 2 qubit state is equal to the initial message qubit state and we can check it:

if (M(there) == One) {
    set measurement = true;

This kind of communication is secure because even if someone will take over the information sent classical way it is still impossible to decode the message.

Tensorflow meets C# Azure function


Tensorflow meets C# Azure function and … . In this post I would like to show how to deploy tensorflow model with C# Azure function. I will use the TensorflowSharp the .NET bindings to the tensorflow library. The InterceptionInterface will be involved to create http endpoint which will recognize the images.


I will start with creating .net core class library and adding TensorFlowSharp package:

dotnet new classlib
dotnet add package TensorFlowSharp -v 1.9.0

Then create file TensorflowImageClassification.cs:

Here I have defined the http entrypoint for the AzureFunction (Run method). The q query parameter is taken from the url and used as a url of the image which will be recognized.

The solution will analyze the image using the convolutional neural network arranged with the Interception architecture.

The function will automatically download the trained interception model thus the function first run will take little bit longer. The model will be saved to the D:\home\site\wwwroot\.

The convolutional neural network graph will be kept in the memory (graphCache) thus the function don’t have to read the model every request. On the other hand the input image tensor has to be prepared and preprocessed every time (ConstructGraphToNormalizeImage).

Finally I can run command:

dotnet publish

which will create the package for the function deployment.

Azure function

To deploy the code I will create the Azure Function (Consumption) with the http trigger. Additionally I will set the function entry point, the function.json will be defined as:

The kudu will be used to deploy the already prepared package. Additionally I have to deploy the libtensorflow.dll from /runtimes/win7-x64/native (otherwise the Azure Functions won’t load it). The bin directory should look like:

Finally I can test the azure function:

The function recognize the image and returns the label with the highest probability.