Welcome to 21
st century

As students during our courses, we used to have a deep-rooted fear for the subject that had quantum in its name. Ardent followers of Star Trek franchise will connect with the character Q which was omnipresent and occurred in multiple episodes to trouble the crew aboard.

I just got reminded that this is the right way to start writing a post if I want to readers to read the blog post. So, let me reset and start afresh with the same theme. During your life as student this subject would have challenged your understanding but you might also remember the future of many niche employment opportunities related to the fields of quantum. Ardent followers of Star Trek franchise will connect with the many of the ship’s functioning be it matter replicator, warp engine and the ever-present computer (as they called) had quantum at its core of operations.

I hope this is inspiring enough. But this is also the core concept of quantum programming. We will touch upon at an appropriate time again but for the moment let us just proceed with optimism and let me remind you like software is eating the world. Quantum computers and the programming languages of that is going to make it true for; let’s just say galaxy for now. So, the new term of the future would be Software is eating the galaxy.

Pre-requisites

Before you begin you need a quantum computer!

Did that put you off? Well, it did to us. But behold there is a way out. Cloud – Many leading cloud service providers Azure, AWS etc. (currently in preview) offer the capability to launch various quantum computer e.g., IonQ or Honeywell’s hardware etc.

Our focus today is to be able to write quantum code so we will not too deep dive in IonQ and Honeywell’s architecture.

If you are perplexed with cloud; we recommend you make your acquittance with them at the earliest. However, there is another way out – Your own non-quantum computer can simulate a quantum computer. Very much like the mobile device emulator which simulated a mobile device when you develop mobile applications. Like there were limitations to the capabilities of those simulated mobile device there will be limitations on the simulated quantum computer. But for you to bootstrap your own computer is more than enough. But we strongly urge you to please get yourself acquainted with cloud and make yourselves a good friend to it.

Alright, once you are through with the concept of hardware let us focus on getting the basics ready for the development environment. We start with Q# (as the title goes) which is a Microsoft offering and has made the pre-requisites very easy. You need to install only one thing – The Software Development Kit (SDK) for quantum i.e., Quantum Development Kit (QDK). This might be a new family of development kits going into future.

You can get the QDK for either Visual Studio (any edition) or for Visual Studio Code. Once you have the QDK installed the only way to check if all is fine by creating the project using the QDK project template.

Verifying the pre-requisites

When you create a new project using the Q# project template it does many things in the background. It creates the folder and configures the correct version of the dependencies to project. Visual Studio (Code) launches the language server (as for typescript, C# or any other language) once you open the project in the IDE.

 

 

We encountered an error when we first did that – “There was an error activating the remote language server, Q# language Extension Visual Studio”. It is quite possible you might encounter this error. Don’t worry this is only momentary and you will have to update the VS (Code) which will take care of copying over any file that got missed out when installing the QDK.

Once this is done you are all set to work with the new project

Your first code

Typically, you would start with “Hello world” program. In quantumcomputing this is really not a challenge. The “Hello World” equivalent in quantum world is random number generation. Because, that is one with introduces certain basic concepts and is light weight to start.

Quantum random number generator

Before you start, please grasp the concept of superposition in quantum world. We will take a route where we put to simple words in English what we mean by that instead of getting into neck deep complex and abstract mathematics.

Superposition

Superposition is like the greyscale. Like in the greyscale you can have a mix of white and black at different proportions; in quantum computer you have a bit have value both 0 and 1 in any given point of time. Remember in typical computer you will have the value only as EITHER 0 or 1. This is a state for the bit and is typically addressed as Superposition state. To handle this kind of possibility regular bit-based computing is not used thus, paving the way for Qubit.

Quantum measurement

Stay with us to see through this as well. When you look at a box which is painted in greyscale you would only quote the colour as grey for a wide spectrum of greyscale value. Similarly, a quantum measurement is equivalent of the act of observation which settles the possibilities of both 0 and 1 (grey at some let us say 75% in greyscale) to a specific value e.g., either 0 or 1. Thus, giving determinism to such computers. This measurement is collapse of superposition state yielding a deterministic value.

With these two important concepts we will jump right into code and talk about the lines of code after that –

 

namespace QuantumExperiments {

   open Microsoft.Quantum.Canon;

   open Microsoft.Quantum.Intrinsic;

   open Microsoft.Quantum.Measurement;

   open Microsoft.Quantum.Math;

   open Microsoft.Quantum.Convert;

 

   @Entrypoint()

   operation GetSuperimposedBit() : Result {

       use q = Qubit();

       H(q);

       return M(q);

   }

}

 

 

We didn’t formally introduce to the language Q#. Let us do that now – Q# is a Domain Specific Language. What domain you ask, needless to say it uses lingua franca of quantum world. So, the concept of superposition and measurement flows in to the method names, in framework as well used by the practitioners.

C# aficionados will notice the lot of similarities but not as much as the F# aficionados do. So, namespace has the same meaning as in C#/F# but the act of importing namespace is similar to F# with the open keyword.

Keywords @Entrypoint and operation might by new but the meaning of it is comes across intuitively. We will not spend much now on the Result type. Rather focus on the obscure method H and M.

H is an operation which puts the Qubit the rather special bit in superposition state. The nerdy ones can follow up more on that by using the full form of it – Hadamard gate! Now the question which we asked ourselves when we were experimenting – Why would somebody use an obscure name in DSL? We do not yet know the answer. But we believe that is a curious cultural question to answer. Let us keep that in the back of our minds, might be one day we have an answer from Microsoft themselves.

M is an operation that performs the measurement and thereby kills the uncertainty in value and yields a specific value of 0 or 1. This is a process which makes it possible to have simulators build that runs on simple binary computers like ours along with many other importance. Nerdy ones might be disappointed with this operator as there is no specific vibe other than it stands for Measure operator.

Code above is good enough to run. Execute the code. You will undergo the similar process of compilation, linking and binary executable generation process you know for C# or Java or any other compile-able language.

You should see either “Zero” or “One” getting printed in the screen. Striking observation will be even quantum program is generated and saved as exe. We are sure the behaviour is similar even if you had opted to run the code directly in Azure.

We have seen the how superposition and measurement is programmed in Q#. In the next dispatch we will look at how that is used to generate random number.

picture courtesy- Quora