Understanding Q# Programming Language

In this article I going to give basic information to understand the Q# Programming Language. Before the introduction, you must have a basic understanding of what is Quantum Computing.

Please refer to Quantum Computing.

By changing the memory structure of the computer, we can change the computer's history. That’s what happing now in Quantum Computing.

Basically a classical computer is a computer has a memory that is made up of bits. Each represents either Zero or One.

But in a quantum computer it maintains a memory by a sequence of Qubits. Qubit is a bit which stores either Zero or One. But this single qubit can represent a One, a Zero or Any Quantum Superpostion.

Simply put, to represent two states in Bit we need two bits. But by using a Single Qubit we can represent either state. By using 3 Qubits we can represent 8 States of ordinary Bit Operation.

Two phenomena that we need to concentrate on to understand the quantum computing,’

  • Superposition
  • Entanglement

Q#(Q-sharp) Programming Language Introduction

Q# is a domain-specific Programming language used for expressing quantum algorithms. It is to be used for writing sub-programs that execute on an adjunct quantum processor, under the control of a classical host program and computer.

To practice Q#-sharp programming language, Microsoft introduced “Quantum Development Kit”.

To install a Quantum Development Kit click here.( https://docs.microsoft.com/en-us/quantum/quantum-installconfig?view=qsharp-preview)

To start programming in Q# please visit Getting Started with Q# Programming.

After we started the new Q# project in Visual Studio, we need to mainly concentrate on two files in solution explorer, .qs and .cs files. .qs consist of quantum programming and we are going run that code by normal C# .cs file.

I am going to take the sample Q# program from Microsoft website.


  1. namespace Quantum.Bell {  
  2.     open Microsoft.Quantum.Primitive;  
  3.     operation Set(desired: Result, q1: Qubit): () {  
  4.         body {  
  5.             let current = M(q1);  
  6.             if (desired != current) {  
  7.                 X(q1);  
  8.             }  
  9.         }  
  10.     }  
  11.     operation BellTest(count: Int, initial: Result): (Int, Int) {  
  12.         body {  
  13.             mutable numOnes = 0;  
  14.             using(qubits = Qubit[1]) {  
  15.                 for (test in 1..count) {  
  16.                     Set(initial, qubits[0]);  
  17.                     X(qubits[0]);  
  18.                     let res = M(qubits[0]);  
  19.                     H(qubits[0]);  
  20.                     let res = M(qubits[0]);  
  21.                     // Count the number of ones we saw:  
  22.                     if (res == One) {  
  23.                         set numOnes = numOnes + 1;  
  24.                     }  
  25.                 }  
  26.                 Set(Zero, qubits[0]);  
  27.             }  
  28.             // Return number of times we saw a |0> and number of times we saw a |1>  
  29.             return (count - numOnes, numOnes);  
  30.         }  
  31.     }  
  32. }  


Basically this program will give the number of times the 0’s and 1’s will come in output based on the quantum computing.

Here you can see the word “operation”. It is separate methods like C#,Java.


  1. operation Set (desired: Result, q1: Qubit) : ()  


Here, “Set” is the method name, “desired:Result,q1:Qubit” is the parameter with datatype, separated by colon’:’, and in last “()” is a return values of this method.

  1. operation BellTest (count : Int, initial: Result) : (Int,Int)  


This method has two return values, both are integer values.

And you can see the words like,

  • X(q1) – Pauli-X Not gate
  • M(q1) – Measure Qubit state
  • H(qubits[0]) – Hadamard Gate
  • CNOT(qubits[0]) – Controlled Gate

These H,X,M,CNOT  represent each “Quantum Gates” operation. To know more about Quantum Gates.

By these gate operations we can achieve the “Superposition” quantum computing phenomena that we need to concentrate on to understand the quantum computing,’


  1. Consider,  
  2. X(qubits[0]);  
  3. let res = M (qubits[0]);  


Init:Zero 0s=0 1s=1000
Init:One 0s=1000 1s=0

Here, we are doing NOT Gate operation, which means we are flipping from 0 to 1 or 1 to 0. But this is a classical operation.

Let’s move to Quantum result. So to achieve it change the code like follows

  1. H(qubits[0]);  
  2. let res = M (qubits[0]);  
  3. H—Hadamard Gate  



Init:Zero 0s=484 1s=516
Init:One 0s=522 1s=478

By giving H gate, Instead of flipping the Qubit, it will do all the way from 0 to 1, we will only flip it halfway.

This makes the qubit halfway between 0 and 1, so we can get (statistically) 0 half the time and 1 half the time. This is known as SuperPostion.

I hope this article is very useful.Thank You