dcsimg
 

Exploring Microsoft Quantum and Q#

Wednesday Feb 21st 2018 by Hannes DuPreez

Get tiny! Learn about quantum computing, which is computing using superposition and entanglement.

Quantum Mechanics

Quantum mechanics explains the behavior of matter (any substance that has mass and takes up space by having volume) and its interactions with energy on the scale of atoms and subatomic particles.

Atoms

An atom is the smallest unit (100 picometers—ten-billionth of a meter) of ordinary matter that has the properties of a chemical element. Every liquid, solid, plasma, and gas is composed of ionized atoms.

Subatomic Particles

Subatomic particles are much smaller than atoms. There are two types of subatomic particles: composite particles and elementary particles (particle whose substructure is unknown).

For a full list of Particle types, please refer to this list.

Quantum Computing

Quantum computing is computing using superposition and entanglement. A quantum computer (a device that can perform quantum computing) differs from binary digital electronic computers based on transistors. Digital computing requires that data be encoded into binary digits (or bits), Quantum computing requires quantum bits.

A quantum bit (or qubit) is a two-state quantum-mechanical system, similar to a photon, where the two states are horizontal polarization and vertical polarization. This means that a qubit can be in a superposition of both states at the same time.

Microsoft Quantum Development Kit

The Microsoft Quantum Development Kit includes the following components:

  • Q# (Q-sharp) A quantum-focused programming language: Q# is fully integrated with Visual Studio and contains native type system for qubits, operators, and other abstractions.
  • Quantum simulators: To test any quantum algorithm and solutions written in Q#, a simulator easily available from within Visual Studio.
  • Samples: You can access the entire set of libraries and samples on GitHub.

Q#

Q# (Q-sharp) is a programming language used for expressing quantum algorithms. Q# is used for writing sub-programs that execute on a quantum processor. Q# contains a very small set of primitive types and arrays and tuples. Q# contains loops and if/then statements. Please visit the following links for more information on these topics:

Getting Started with the Microsoft Quantum Development Kit

Navigate to the Microsoft Quantum Development Kit link, and click the Download Now button, as shown in Figure 1.

Get started with Quantum development
Figure 1: Get started with Quantum development

You will be taken to a Registration page, where you must fill in your details. After you have filled in all your details, click "Download Now."

Register
Figure 2: Register

You then will be taken to the Visual Studio Marketplace where you can finally download your Microsoft Quantum Development Kit.

Visual Studio Marketplace
Figure 3: Visual Studio Marketplace

You also can open Visual Studio 2017 and install the Microsoft Quantum Developer Kit from there. Click File, New, Project and simply search for quantum as shown next:

Visual Studio Quantum Developer Kit
Figure 4: Visual Studio Quantum Developer Kit

You will be provided with a VSIX template file, which you should install.

Quantum Install
Figure 5: Quantum Install

Install Finished
Figure 6: Install Finished

Getting Samples

To get samples and libraries for Q#, you need to visit GitHub.

GitHub
Figure 7: GitHub

You will be able to download a huge library, which is ready to run and explore.

Note: To install the samples, you will need F# capabilities in your Visual Studio 2017 IDE. This you also can download quickly.

Install F#
Figure 8: Install F#

Code

Time for a little sample project. This project is loosely based on this one.

Open Visual Studio. Click File, New, Project. Select C# as the language and navigate to Q# Application in the list of given options.

New Project
Figure 9: New Project

Once you click OK and your project is loaded, you will enter the world of Quantum computing. Your Solution Explorer will look like the following:

Solution Explorer
Figure 10: Solution Explorer

The Solution Explorer contains two code files. Operation.qs is your Q# file which will handle all your Q# commands and operations. You may rename the file to anything you like. Driver.cs, which is quite aptly named, is your C# file that will act as a host for your Q# program.

Your Driver.cs file and Operation.qs files look like the following upon opening:

Driver.cs
Figure 11: Driver.cs

Quantum Operations file
Figure 12: Quantum Operations file

You will notice the Quantum namespaces included into your C# Driver file. A Q# program's coding consists mainly of operations. A Q# Operation is the equivalent of a C# function. Let's add some code! Open your Quantum file and replace the Q# Operation named Operation with the following:

   operation SetQbit (res: Result, bit: Qubit) : ()
   {
      body
      {
         let current = M(bit);

         if (res != current)
         {
            X(bit);
         }
      }


   }

This Q# Operation is named SetQbit. With the help of the built-in Q# function X, we flip the given Qubit into the desired known state (one or zero). With the help of the built-in Q# function M, you measure the Qubit first to see if it is in the desired known state.

Add the following Operation:

   operation EntagleBell (iterations : Int,
      originalvalue: Result) : (Int,Int)
   {
      body
      {
         mutable numbers = 0;

         using (bits = Qubit[2])
         {
            for (count in 1..iterations)
            {
               SetQbit (originalvalue, bits[0]);
               SetQbit (Zero, bits[1]);

               H(bits[0]);

               CNOT(bits[0],bits[1]);

               let res = M (bits[0]);

               if (res == One)
               {
                  set numbers = numbers + 1;
               }
            }
         SetQbit(Zero, bits[0]);
         SetQbit(Zero, bits[1]);
         }

         return (iterations-numbers, numbers);
      }
   }
}

The above code creates the Entanglement for the Qubit Bell State.

Making It Work

Add the following C# code into the Driver.cs file:

      static void Main(string[] args)
      {
         var sim = new QuantumSimulator
            (throwOnReleasingQubitsNotInZeroState: true);


         Result[] values = new Result[] { Result.Zero,
            Result.One };

         foreach (Result value in values)
         {
            var res = EntagleBell.Run(sim, 1000, value).Result;
            var (zeros, ones) = res;

            Console.WriteLine($"Original: {value,-4} 0:
               {zeros,-4} 1: {ones,-4}");
         }

         Console.ReadKey();
      }

First, you instantiate the simulator. You create a Result object that will identify each Qubit's value and loop through them to display them.

Note: Before you can run your project, you must ensure that your Configuration setting of your project is set to x64.

Configuration Manager
Figure 13: Configuration Manager

Results
Figure 14: Results

Code for this article is available on GitHub.

Conclusion

Microsoft keeps raising the bar with technology. Q# opens up so many possibilities. The future is now.

Home
Mobile Site | Full Site
Copyright 2018 © QuinStreet Inc. All Rights Reserved