# Getting Started with LIQUi|> and Quantum Computing

Published Mar 21 2019 07:25 AM 592 Views
Microsoft

# Getting Started with LIQUi|> and Quantum Computing

First published on MSDN on Oct 11, 2017

Guest blog by Eric Chamoun Microsoft Student Partner at University College London

Hello, my name is Eric Chamoun and I am a second year Mathematical Computation student at University College London (UCL). I have always had a deep passion for solving complex problems and have always been extremely interested in technology. Also, I love to connect and exchange ideas with new people, participate in volunteering experiences and practice football.

I am currently the Clients and Sponsorship director of the UCL Technology Society where I have the opportunity to meet a lot of interesting people in the tech field as well as practice my communication and leadership skills.

If you would like to reach me, please do not hesitate to contact me on my LinkedIn profile.

### Introduction

Many of you might have heard of quantum computing. Many might be asking themselves: What exactly is quantum computing? Why are companies investing so much money in it? Why not just use the normal computers and keep upgrading them?

Well, at first, I was one of those people. Quantum Computing has always been intriguing and it took me a lot of research to realize how important it was for the future.

The purpose of this blog is, not only about finding out more on Quantum Computing, but also about learning on how you can get started with using Liquid to develop quantum applications!!

Excited yet? I really hope so!

### Definition of key terms

First of all, let’s briefly explain what Quantum Computing is.

Quantum Computing studies computations systems that make direct use of quantum-mechanical phenomena, such as superposition and entanglement, to perform operations on data.

Let’s clarify this by starting from the beginning.

A typical computer is a binary system, meaning that it operates in binary (store data and perform calculations using zeros and ones). Each single binary digit can be used to represent True (1) or False (0) but multiple binary digits can be used to perform complex functions. Therefore, the typical computer can be referred to as a True/ False device.

A quantum computer however, can be referred to as a True/ False/ Both device. That means that the quantum computer is able to assess more than one probability at a time when solving a problem where every answer is equally likely. In contrast, typical computers have to take time to individually assess each probability.

Basically, quantum computers use subatomic particles, instead of bits, that have the ability to exist in more than one state at a time. Indeed, these subatomic particles, or “qubits”, can exist in a “quantum superposition” of 0 and 1 to create “entangled states”. If you add more bits to a regular computer, it can still only deal with one state at a time. But as you add qubits, the power of your quantum computer grows exponentially, meaning that if you have “n” qubits, you can simultaneously represent 2 n states.

Therefore, because these quantum computers can contain these multiple states simultaneously, it has the potential to be millions of times more powerful than today’s most powerful computers. In fact, they have the ability to solve an operation that would take a classical computer billions of years in a few hours (for example: figure out the prime factors of a very large number) as well as store more information using less energy than classical computers. Having them would provide a huge boost in a lot of fields such as medicine and security but also energy and resources preservation as we will not have the capability to power all of the machines around the globe if we follow the trend that has been in place since computers were introduced! (According to a recent report by the Semiconductor Industry Association). Quantum computers will reduce power consumption anywhere from 100 up to 1000 times.

An important thing to note is that the quantum computers do not offer an improvement on the speed of each individual operation, but rather an improvement on the total number of operations to arrive at the result in particular cases and algorithms. Therefore, it is not a replacement for the classical computers that can be used as effectively, if not more, for basic operations.

Currently, a lot of companies such as Microsoft and IBM are investing a lot of money in research in order to develop quantum computers as they see them as the future. They believe “quantum computers will change everything”. The issue with qubits is that the more you add, the less stable it becomes. Microsoft’s efforts have been in trying to develop “topological qubits” that would be much more difficult to disturb than classical qubits. Even though this looks like still far away in the future, Microsoft has developed a new programming language for writing these quantum applications and released a simulator that will allow developers to figure out if their algorithms are actually feasible on quantum computers! Isn’t that super exciting?

Well, the purpose of this blog will be to teach you guys on how to get started with developing quantum applications!

Now that we have given context to the topic, let’s go into a bit more details.

You now surely have an idea of what qubits are but the only way to truly understand them is by looking at the representation below:

The qubit is defined as a pair of complex vectors pointing to a spot on a unit sphere. Traditionally, a qubit pointing directly up (positive on the axis) is denoted as the column vector |0⟩ and the vector pointing down is known as |1⟩. (For example, in this case, the qubit is |0⟩).

Additionally, we define Ket as the complete state of a quantum system, i.e. the combination of all the qubits of the system. Its length is always 2 n with n number of qubits in the system and the simulator does everything to avoid materializing the vector unless it needs to (when qubits are fully entangled).

Also, we define Gates as being data structures that, once wrapped in F# functions, become operators that can be applied to a set of qubits.

Now that we have defined what gates and kets are, an operation represents the operation of a gate on a ket. In other terms, it’s the signature of any function that takes in Qubits and alters it. Logically: |Ψ2 ⟩ = |Ψ1⟩

Finally, we define Circuits as being data structures that represent a list of operations on gates.

After defining the key terms of quantum computing, let’s have a look at what LIQUi|> exactly is.

LIQUi|> is the software architecture and toolsuite developed by Microsoft for quantum computing. It includes a programming language, optimization and scheduling algorithms and quantum simulators. LIQUi|> can be used to translate a quantum algorithm written in the form of a high-level program into low-level machine instructions for a quantum device. You can use LIQUi|> to define quantum circuits in a high-level functional language (F#), render them into a variety of graphical formats, and execute them using the simulator.

### LIQUI|> includes three simulators:

· Physical Modeling simulator which is a full state vector simulator that tracks the detailed evolution of the quantum state. It can only operate on a small number of bits and is slow.

· Universal Modeling simulator which is a highly-optimized full state vector simulator. It allows a wide range of operations to be executed but has a limitation on the number of qubits that can be entangled at the same time.

· Stabilizer Simulator which allows millions of operations on massive number of qubits but has limitations on the types of the operations.

### The simulations can be accomplished in several ways:

· Test Mode in which you invoke directly from the command lines built-in tests.

· Script Mode which can be run directly from a F# text script but in which the interactive debugging is difficult.

· Function Mode which is the normal development mode requiring a compilation environment and a .Net language (F#). The user can extend the environment as well as build his own applications.

· Circuit Mode which is a function mode that can be compiled into a circuit data structure.

### Getting Started with LIQUi|>

Having defined key terms and concepts and given some context to the topic, we can now (finally) proceed to installing the tools we need to develop those quantum applications.

Luckily, whether your computer is running windows, linux or macOS, you can install LIQUi|>.

You will only need to follow these basic instructions:

| Windows

| Ubuntu (14.04)

| OS X(10.11)

### Windows

1 Install Visual Studio

The best way to install Visual Studio is to download the official MSI installer . This will install the tools and put them on your PATH. Make sure you select F# as one of the languages to install.

2 Install LIQ⏐〉

Download the zip file from GitHub (there's a Download Zip button near the top right side of the screen). Then extract the contents into C:\ This will create C:\Liquid-master. Rename the directory to C:\Liquid and you're done!

> dir /b c:\Liquid

03/01/2016  10:14 AM    <DIR>          . 03/01/2016  10:14 AM    <DIR>          .. 03/01/2016  10:14 AM            10,086 AzureGuide.md 03/01/2016  10:14 AM    <DIR>          bin 03/01/2016  10:14 AM    <DIR>          docs 03/01/2016  10:14 AM            17,292 GettingStarted.md 03/01/2016  10:14 AM    <DIR>          img 03/01/2016  10:14 AM             6,118 LICENSE.md 03/01/2016  10:14 AM    <DIR>          linux 03/01/2016  10:14 AM            32,708 LiquidTikZ.tex 03/01/2016  10:14 AM             5,441 README.md 03/01/2016  10:14 AM    <DIR>          Samples 03/01/2016  10:14 AM    <DIR>          source 03/01/2016  10:14 AM    <DIR>          UserCode 5 File(s)         71,645 bytes

3 Run the app

Go into the C:\Liquid\bin directory , run the app and accept the license:

> cd \Liquid\bin
> Liquid.exe

0:0000.0/=================================================================================================== 0:0000.0/=    The Language-Integrated Quantum Operations (LIQUi|>) Simulator                               = 0:0000.0/=    is made available under license by Microsoft Corporation                                     = 0:0000.0/=    License terms may be viewed at https://github.com/msr-quarc/Liquid/blob/master/LICENSE.md    = 0:0000.0/=    Please type Y, followed by return, to indicate your acceptance of these terms                = 0:0000.0/=================================================================================================== Y

4 Try a sample

Now try running the Quantum "Hello World" ... Teleport:
(note that there are two underscores in front of Teleport)

> cd \Liquid\bin
> Liquid.exe __Teleport()

And you're set!

You now have LIQ⏐〉 running on your machine!

### Ubuntu (14.04)

1 Install mono and fsharp

Make sure you remove any previous installations of mono or fsharp on your machine to avoid incompatibilities.

Now do the install:

\$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF \$ echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list \$ sudo apt-get update \$ sudo apt-get install mono-complete fsharp

2 Install LIQ⏐〉

Download the zip file from GitHub (there's a Download Zip button near the top right side of the screen). Then extract the contents into your home directory. This will create ~/Liquid-master. Copy to the final destination:

\$ cd ~/Liquid-master

\$ sudo mkdir /Liquid

\$ sudo chown \$USER /Liquid

\$ cp * /Liquid --recursive

3 Run the app

Go into the /Liquid/linux directory , run the app and accept the license:

\$ cd /Liquid/linux
\$ mono Liquid.exe

0:0000.0/=================================================================================================== 0:0000.0/=    The Language-Integrated Quantum Operations (LIQUi|>) Simulator                               = 0:0000.0/=    is made available under license by Microsoft Corporation                                     = 0:0000.0/=    License terms may be viewed at https://github.com/msr-quarc/Liquid/blob/master/LICENSE.md    = 0:0000.0/=    Please type Y, followed by return, to indicate your acceptance of these terms                = 0:0000.0/=================================================================================================== Y

4 Try a sample

Now try running the Quantum "Hello World" ... Teleport:
(note that there are two underscores in front of Teleport and the program argument has quotation marks because parenthesis are special characters on Linux)

\$ cd /Liquid/linux
\$ mono Liquid.exe "__Teleport()"

And you're set!

You now have LIQ⏐〉 running on your machine!

### OS X(10.11)

1 Install mono and fsharp

Install mono and F# for OSX from here

2 Install LIQ⏐〉

1. You'll find ``` Liquid-master ``` in your Downloads, open the folder.
2. Type ``` Command-A ``` ``` Command-C ``` to select all the files and copy them.
3. Type ``` Shift-Command-G ``` and then a slash ( ``` / ``` ) to go to the root.
4. Type ``` Shift-Command-N ``` to create a new folder (you will have to enter your admin password). Then create the ``` Liquid ``` folder.
5. Double click on the ``` Liquid ``` folder to open it and then type ``` Command-V ``` to paste the contents of the downloaded ``` Liquid-master ``` tree into ``` \Liquid ``` .

3 Run the app

Go into the /Liquid/linux directory , run the app and accept the license:

\$ cd /Liquid/linux
\$ mono Liquid.exe

0:0000.0/=================================================================================================== 0:0000.0/=    The Language-Integrated Quantum Operations (LIQUi|>) Simulator                               = 0:0000.0/=    is made available under license by Microsoft Corporation                                     = 0:0000.0/=    License terms may be viewed at https://github.com/msr-quarc/Liquid/blob/master/LICENSE.md    = 0:0000.0/=    Please type Y, followed by return, to indicate your acceptance of these terms                = 0:0000.0/=================================================================================================== Y

4 Try a sample

Now try running the Quantum "Hello World" ... Teleport:
(note that there are two underscores in front of Teleport and the program argument has quotation marks because parenthesis are special characters on OSX)

\$ cd /Liquid/linux
\$ mono Liquid.exe "__Teleport()"

And you're set!

You now have LIQ⏐〉 running on your machine!

### Developing quantum applications

Now that we’re set, let’s go and explore what we got.

In the bin, you’ll find the executable, the library and what you need to run the system.

In the docs directory, you’ll find the user’s manual, a tutorial script as well as a compiled HTML file which is a system documentation.

In the img directory, you’ll find the images that are used in the website.

The linux directory is a bit similar to the bin directory but uses Mono instead of Visual Studio. (For OS and Linux users)

The Quantum Challenge directory contains the winners of the Quantum Challenge that was organized by Microsoft.

You’ll find a huge number of samples the uses of LIQUi|> in the Samples Directory.

The source directory is where users write their code.

Finally, the UserCode directory is where people can contribute by sharing their classes and projects.

### Let’s have a look at the Liquid executable.

· Open the Command Prompt

· Change the directory to the Liquid one by typing: “cd \Liquid\bin” then press the enter key

· Type and Enter “Liquid”

· The “TESTS (all start with two underscores) part is the built-in tests list, many of which have sources in the “Samples” directory if you want to understand them.

Note: All the built-in tests names start with two underscores (Naming convention)

· Also, we can see an error is thrown. The reason for this is that we called Liquid without asking it to do anything. Let’s try to give it an argument and see how it goes.

· One of the interesting tests is the Shor’s algorithm one. Shor’s algorithm is a quantum algorithm that takes an integer N and finds its prime factors. It is MUCH faster than the most efficient classical factoring algorithm.

· To run it, just write and enter “Liquid __Shor (N, bool) with N being the integer you want factored and bool being false (to just run the circuit and ignore optimization) or true (to try to optimize).

· As you can see, if you try to give an illegal number, you’re given a list of legal numbers.

· Let’s try to use Shor’s algorithm to factor a large number by running it first with “false” then with “true” to highlight the difference.

Algorithm run without optimization

Algorithm run with optimization

· You can see from the second column of numbers that indicates the minutes, the last operation in the first algorithm finishes at 13.2 minutes while the last operation of the second algorithm finishes at 6 minutes thanks to the optimization!!!

### Introduction to quantum computing development with LIQUi|>

After trying one of the built-in tests, let’s now make things interesting and jump into some development.

· Type and enter in the Command Prompt “cd ..\source” to change to the “source” directory

· Run the solution file by entering “liquid.sln”

· Double click on “Main.fs” in the right window

· The code is in F#. Similarly to other programming languages, there’s a “Main” that runs the API running the system.

· Just over the “Main”, there’s a UserSample() function over that prints “This module is a good place to put compiled user code” by using the show function which is similar to the print function in other programming languages with some extra benefits (duplicates everything to a log file, knows about thread IDs to identify the source which is 0 in the screenshot below…) Just over it, you can see that the “[<LQD>]” attribute is written. It means that any function that has that attribute is callable from the command line as we can see below.

· We’re going to create a simple function and run it to explain the steps needed to program with LIQUi|>. This function will allow us to do superposition by using the Hadamard gate.

· The Hadamard gate maps the qubit-basis states |0> and |1> to two superposition states with equal weight of the computational basis sates |0> and |1>. It is used as the first step in many quantum algorithm since it maps n qubits initialized with |0> to a superposition of all 2 n orthogonal states in the |0>, |1> basis with equal weight

· The code looks like this.( picture below) Let’s break it into subparts and explain each one of them.

Before we start, please note that F# is an indentation-based language, so make sure to check your indentation before running the program!

First thing we are going to do is create a function which is a list of qubits that operates the Hadamard gate on the head of the list then measures it.

· By doing this, you are creating the function that is a list of qubits. “Qubits” is a built-in data type that is a list of qubits.

· Next step is the content of the function. By writing “H qs” then “M qs”, you are first applying the Hadamard gate on the head of the list then measuring it. (Measure and Hadamard gates both take only one qubit)

Second thing we’ll do is modifying the UserSample() function to get statistics of the effect of the Hadamard gate and to better understand it.

· This will create “stats”, an array of 2 integers that we’ll use for statistics. At position 0, we’ll have the number of |0> registered and position 1 the number of |1>.

· This will create “k”, a Ket vector of one qubit

· Here, we’re doing 10 000 loops, in which each will:

o 1- Reset the qubit in the Ket to the initial state (|0>)

o 2- Call the qFunc on the qubit.

o 3- Store the integer value (0 or 1) to describe the state of the qubit at the head of the list

o 4- Check the value of v and add 1 to the position 0 of the “stats” array or to the position 1 of the “stats” array depending on the state of the measured qubit.

· Last step is to just use the built-in “show” function to print the results and see how many qubits are in state 0 and in state 1. We should get very close numbers given that the Hadamard operator allows us to get a 50/50 probability of getting qubits in state |0> or state |1>.

· Now, to test our code, go to Properties by right-clicking on Liquid in the right window

· In the Debug Section, write the name of the function “__UserSample()” in “Command line arguments” then exit the properties.

· Choose “Release” instead of “Debug” in the toolbar on the right of the “Re-do” button

· Click on “Debug” then “Start Without Debugging” or simply “ Ctrl + F5”

· Congrats! You developed your first program with LIQUi|>. As we can see, we got the results we were expecting as the numbers are really close to each other.

Now that we have had a quick introduction to LIQUi|> and how to start developing quantum application, we reach the last and most important part of this article. Why should I learn to use LIQUi|>? Why would it even be useful? Wouldn’t be a waste of time? Well, I can guarantee you that you’ll be much more motivated to learn how to user LIQUi|> after reading this section.

### Why learn quantum computing and LIQUi|>?

To reply to this question, we must first reply to the question: In which fields will quantum computers be used? Well, if you’re interested in one of the fields there’s a high chance that you’ll need to learn quantum computing as their future will depend a lot on these computers:

Online Security: Quantum Computers will change the landscape of data security. Currently, all the encryption methods count on the fact that it takes an extraordinary amount of time by classical computers to decrypt a code but this will change with the introduction of quantum computers as they have the ability of processing data much faster. Therefore, there will be a need to develop quantum encryption methods to solve this problem.

Big Data Analysis: A significant amount of time is required by classical computers to analyze large sets of data. However, quantum computers perform large calculations nearly instantaneously. Therefore, they will be a revolution for this field.

Artificial Intelligence: The efficiency with which quantum computers analyze data may well prove to be a gamechanger in the AI field. Indeed, this will provide artificial machines with the feedback needed to improve the performance!

Logistics: We have previously explained that quantum computers can analyze more than one probability at a time, in contrast with classical computers that can only assess one at a time. Well, a typical example of this is the calculation of the optimal route. Quantum computers will analyze millions of routes at the same time while normal computers can only examine one at a time.

Medicine: Quantum computing may allow doctors to detect cancer earlier! Indeed, the computational models will help determine how diseases develop.

Quantum Computing will hugely affect these fields but also many more such as astronomy, aeronautics …

Do you notice now the impact that the introduction of quantum computers will have on the world?? Do you understand now, why everyone is describing it as the “next big thing”?

### Next question is “Why LIQUi|>” specifically?

Compared to other quantum simulators on the market, as Dave Wecker, the chief architect of the Microsoft’s research team says, “LIQUi|> ‘s strength is its industrial strength as well as the possibility for academics or researchers to extend it by adding their quantum calculations as needed”. Indeed, as we saw previously, there’s a “UserCode” directory containing the contributions of users who may well have participated in the building of one of Microsoft and the world’s biggest projects. If quantum computing is a field that really interests you, using LIQUi|> is one of the biggest opportunities to contribute to the development of this field and write your name in the history of its building!

Further, the simulator is 30 qubits, which is equivalent to 32 GB of memory, meaning you can run LIQUi|> on your local computer! Even more, if you have big requirements, you can run simulations on the Microsoft Azure, Amazon Web Services or even Google Cloud Platform!

Additionally, as we’ve seen with the built-in tests, the examples that can be used with LIQUi|> are various:

• Simple quantum teleportation
• Shor's factoring algorithm
• Quantum chemistry: computing the ground state energy of a molecule
• Quantum error correction
• Quantum associative memory
• Quantum linear algebra

Another point that could encourage a lot of you is the “Microsoft Quantum Challenge”. This challenge is for motivated students around the world who are interested in solving problems facing the quantum universe by using LIQUi|>. Winning it gets you big prizes and offers you a lot of opportunities such as visiting Microsoft Research and even sometimes gaining an internship!

### Conclusion

In conclusion, we first defined important terms and concepts before showing how to install and use LIQUi|> then getting started with developing quantum applications and finally explaining why learning to develop with LIQUi|> is extremely rewarding.

To summarize the last point, Quantum Computing “will accelerate highly complex calculations beyond our wildest dreams” as Satya Nadella, CEO of Microsoft, says. The introduction of this technology will revolutionize the world. By learning how to develop using LIQUi|>, you will be joining the small cohort of people who will not only have the ability to develop quantum applications but also participate to the development of this field! So, what are you waiting for?