A series of Quantum Computing hands-on tutorial with Q#
First of all, for setting up the Q# programming environment on macOS, please refer to this article before you run the following code.
In order for the code to work, you need to create a new project for each of the demo code by ⌘ Cmd + ↑ Shift + P in VS Code, then use
Q#: Create new project... option.
After you type in the name of the project, VS Code will automatically create the local files and folder for you. All you need to do is writing code in the
Program.qs file. 🙂
A Qubit is a unit of quantum information that can be in superposition. At measurement, a Qubit can only be either 0 or 1. See this article. However, during execution, the state of the qubit represents the probability of reading either a 0 or a 1 with a measurement. This probabilistic state is known as superposition.
In the following Bloch sphere figure, the north pole represents the value 0 and the south pole, value 1. Any superposition can be represented as a point on the sphere, here we point the superposition using a red arrow from the center of the sphere. If the point is located on the equator of the sphere, the Qubit has a 50/50 chance to collapse 1 or 0 at measurement. Therefore, the closed the point to pole, the higher the probability the qubit collapses into the value assigned to that pole at measurement. For example, the qubit state represented by the red arrow below has a higher probability of giving the value 0 if we measure it.
The Q# code for simulating superposition is as following. I named my project
Q_superposition, so the namespace here is the same name.
Line 3 — 4 are generated by the VS Code, so we won’t talk about it.
Line 6 — Line 15 are the actual superposition simulation and measure code.
- Line 6: operation is to define a function, here I called my
Superposition(). The syntax of the Q# is very similar to the C#, you need to tell the compiler the return type of your function, or operation, here the type is
Result. So we can
- Line 7: define a mutable variable
stateto collect the results when measuring the Qubit. The default value is
Zero. We know that a Qubit is either in the superposition which we cannot measure them or in the classical 0/1 state when measuring, therefore the
statevariable is to receive the result at the measurement.
- Line 8:
usingblock is used to create a QuBit. I named mine
qubit, you can call it whatever you like. A
usingblock cleans up any resources that are initialized outside of the scope.
QuBit()is the constructor to simulate QuBit, and its basic state is always
- Line 9: now we need to put our QuBit into a superposition of both
One. To do so, we use the
H()operation, which is just fancy linear algebra under the hood, for simplicity, we can say, it `moves` the value of the QuBit from
One. This is the superposition!
- Line 10: when measuring the QuBit, it collapses to the basic state. To do so, we need to use
M()operation, which stands for “measure”, and captures the value in
- Line 12: In the Q#, we need reset the state of the Qubit to its default state after every measurement. If you don’t do this, the driver code could throw an exception at runtime, as all QuBit requires to be reset before disposed. In this case, at the end of our
- Line 14: return the captured state.
Line 17 — Line 21 are the driver code.
- Line 17:
EntryPointtells the compiler this is the entry point of the code, similar to the
- Line 18: Same as in line 7, we define the driver operation.
- Line 19: similar to the
print()function, we print out the message in the console.
- Line 20: run the simulation of
Superposition()and expose the result to the console.
$ dotnet build. The output should be like this:
$ dotnet build
Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.Restore completed in 68.44 ms for /Users/bill/Documents/sandbox/Q_superposition/Q_superposition.csproj.
Restore completed in 1.81 ms for /Users/bill/Documents/sandbox/Q_superposition/Q_superposition.csproj.
Q#: Success! (0 errors, 0 warnings)
Q_superposition -> <project location>/Q_superposition/bin/Debug/netcoreapp3.1/Q_superposition.dllBuild succeeded.
0 Error(s)Time Elapsed 00:00:15.65
$dotnet run --no-build. Since we already build it, so no newbuild is needed now. The output should be like.
If you run the
$dotnet run --no-build, a few times, you will find out you should find out it also output
And the probability it happens follows a random binomial distribution.
2. 9 Data Science and Machine Learning Courses by Harvard, IBM, Udemy, and Others
3. Can you avoid C++ in Computer Vision?
4. Deep learning with COVID-19 xray convolutional Neural Network.
if you run:
$ dotnet run
You will notice the probability of you run a 1000 simulation of Qubits, the results is almost 50–50 to get each of the classical /basic states, which are 0 and 1.
You learned what superposition is by using real examples. And you also learned that superposition is important because it’s where the QuBit gets its power from. You have simulated superposition using Q# and seen the results of the collapse of the superposition. Great job!