Site preferences
Preferences



*The preferences are saved using cookies because this website does not use Javascript.
To clear the cookies, select Clear Cookies and click submit, this will also reset all the preferences to default.
The cookies are not used for analytics, read the privacy policy for more details.

Experimenting with Quantum Computation Language

My first attempt to learn QCL, a C-based high level, architecture independent programming language for quantum computers.

This page was published on .
Tags:


Prologue

Feel free to skip this and start reading from the setting up QCL section, I am going to talk a bit about my fascination here.

When I was around 14-years-old, I read a book in Malayalam titled Sourayoodhavum(സൗരയൂഥവും) athinappuravum (അതിനപ്പുറവും) by Professor K.(കെ.) Sreedharan(ശ്രീധരൻ). The title translates to The solar system and beyond, and was published by the Kerala(കേരള) Sasthra (ശാസ്ത്ര) Sahithya (സാഹിത്യ)Parishad(പരിഷദ്) (Kerala Science Literature Movement).

The book was written to create a grasp of our universe. It covered our solar system, neighbouring stars, galaxies, nebulas, black holes and so on. It was a well-written book, and easy to grasp for someone in their puberty. Well, I believe it was aimed at a younger audience.

Note: I'm just recalling things, my brain might be making some of it up. Sadly, I do not have that book with me anymore to cross-check, if you do, please let me know if I exaggerated about its content.

This book made me curious about how our universe works, and was the very reason for me to start watching science documentaries and read more about the universe.

What made me even more curious was how the General relativity, while explains how big things work, does not play well along with the small stuff, i.e, subatomic particles.

Enter quantum, the smallest physical entity involved in an interaction. While the concept of quantum and quantum physics has been around for a while, the way it works is still confusing, and the there are many misconceptions about it, like thinking that quantum entanglement can allow communication faster than light — which simply is not true.

Just like anyone else, I was fascinated by many things in my life. One thing led to another, and I ended up taking a career path in Software development. But whenever I see news or articles about quantum physics or general relativity, I become that kid again, just interested, curious, and overall fascinated.

Quantum in Computers

Somewhere around 2014, when I was working at my first software job, I read a bit about QCL (Quantum Computation Language), I had a Windows machine at that time and had to install QCL using cygwin. QCL had some bugs. Well, it still has some annoying bugs that I will mention in the second part of this blog, but it wonderfully demonstrated the concepts of quantum computing.

There are some promissing quantum computers being developed by now, and the most famous ones are created by well-known industry giants, like IBM, Google, and Baidu.

But I do not want to talk about quantum computers right now, it is a whole another blog. I want to talk about quantum Computing, and Dr. Bernhard Ömer, the creator of QCL, can explain it better. The below two paragraphs are copied from the QCL project page.

Despite many common concepts with classical computer science, quantum computing is still widely considered as a special discipline within the broad field of theoretical physics. One reason for the slow adoption of QC by the computer science community is the confusing variety of formalisms (Dirac notation, matrices, gates, operators, etc.), none of which has any similarity with classical programming languages, as well as the rather 'physical' terminology in most of the available literature.

QCL (Quantum Computation Language) tries to fill this gap: QCL is a high level, architecture independent programming language for quantum computers, with a syntax derived from classical procedural languages like C or Pascal. This allows for the complete implementation and simulation of quantum algorithms (including classical components) in one consistent formalism.

—Dr. Bernhard Ömer, QCL - A Programming Language for Quantum Computers

Quantum computation is still in its baby steps, it has great potential in securing communication, improving privacy, and preventing man-in-the-middle attacks. Another good implementation, due to the randomness in quantum, is the creation of advanced AI and ML models. I hope that we can leverage quantum in the near future for better modals that can do things humans have never been able to, like curing deceases that are currently considered terminal.

Setting up QCL

To set up QCL, you need either a GNU/Linux system or a UNIX based system with gcc, flex, biosn, and optionally GNU readline. You may also need libplotter if you want to see some fancy graphs (I won't be using that here).

If you are on Windows, you could try it in cygwin or WSL, but I will not cover that here as I don't have a Windows system with me.

After installing those dependencies, you can get the latest QCL from the project website. I am going to install it from the source using QCL version 0.6.7 (tar file). Once the file is downloaded, untar that file using the command tar -xvf qcl-<version-number>.tgz.

After extracting, navigate to the extracted folder, the installation instructions are provided in the README file, basically, you just need just to run make and make install. If you do not have libplotter or Xorg libraries installed, you will need to comment out line 42 and 43 (PLOPT = -DQCL_PLOT and PLLIB = -L/usr/X11/lib -lplotter) in the Makefile.

Note: If you are on Gentoo GNU/Linux, I have an ebuild for this project at my cowaybuilds repository, feel free to use it.

Once installed, to see everyting is setup correctly, run qcl --version. The output should resumble something like this:

QCL version 0.6.4, (c) by Bernhard Oemer, 1998-2014
This program is free software under the terms of the GPL

Doing classical computing using QCL

Before we dive into the advanced quantum concepts, we should first get familiarized with doing basic operations in QCL. Like assignments, loops, functions, etc. QCL has all of them and a bit more (if you know what I mean 😉)!

The QCL programs can be run in two ways.

  1. Interactive mode
  2. Non-interactive mode

The interactive mode is similar to the Python or JavaScript console, it gives you an interactive shell to run commands on. Just type qcl in your terminal to enter the interactive mode, you will see something like this:

QCL Quantum Computation Language (64 qubits, seed 1712768575)
[0/64] 1 |0>
qcl> 

you can type exit or press Ctrl+D to exit the interactive mode.

In the interactive prompt, you will be able to see the number of qubits (quantum bits) that you can use, and the seed to reproduce the same randomness. I will talk about them in the next section.

The non-interactive mode can take one or more files, parse it and execute it as a qcl script. The syntax is qcl file-name.

Everything I write from here onwards can be done in either interactive or non-interactive modes, but I prefer having a backup of everything I do, so I will be using the non-interactive one.

There is a good enough documentation on how the statements work, the data types, and some quirks at Quantum Programming in QCL (PDF document) in the project website. I couldn't find an HTML version of the document, So I will create one and share the link here later.

In order to assist the programmers familiar with classical computing to use quantum computing, QCL is endowed with several features from most other languages like data types, including int, boolean, string, vector, and matrices. QCL also includes support for complex and real numbers.

There are also some mathematical functions built-in to QCL, like sin, cos, tan, etc. But using them is fairly straight forward, you can find some examples of all basic things you can do in QCL at my codeberg repository.

Quantum specific programming

The Quantum computers, are based on qubits, or a quantum bit. This means that there is a 0, 1, and a state that can be either 0 or 1. So none of the software that’s been written for a traditional computer will function on an entirely qubit-based processor.

I am no quantum expert here, but I read the third state as the spin or polarization of the electron, which you wouldn't know until measured, but when you do, superposition is disturbed and you know its state.

The qubits are stored in QCL as qureg, it is considered to be the foundation quantum data type in QCL. A qureg can store one or more qubits, yes it is an array of qubits, like how a string is an array of chars.

The code qureg qubits[2]; will declare a qureg of size two, you can think of as an empty array of type qureg of size 2.

dump is a command that dumps out the current state of all the qubits in the program in terms of their basis states. Let us try calling dump before and after creating a qureg of size 5:

dump;

qureg qubits[5];

dump;

When you execute the above code, you will see the quantum bits you can use and how many are allocated:

$ qcl qureg-example.qcl
QCL Quantum Computation Language (64 qubits, seed 1712837174)
: STATE: 0 / 64 qubits allocated, 64 / 64 qubits free
1 |0>
: STATE: 5 / 64 qubits allocated, 59 / 64 qubits free
1 |0>

From the output, you can see that I can use 64 qubits, and at the end of the program I used 5 qubits, so I have only 59 left.

The seed you see at the beginning of the program is the seed for randomness. Note that QCL in your system is just simulating how a quantum program will behave, so you get the privilege of creating predictable randomness!

Let us watch this in action, I will create a simple for loop and call the random() function in it.

int number;
for number = 0 to 10 {
  if random() >= 0.5 {
    print "red";
  } else {
    print "black";
  }
}

If you run the program, you will see an output with 10 lines, with the text red or black appearing randomly. Something like this:

$ qcl seed-example.qcl
QCL Quantum Computation Language (64 qubits, seed 1712837723)
: red 
: black 
: black 
: black 
: red 
: black 
: black 
: black 
: black 
: red 
: black

If you run this again, you will see a different output, something like this:

$ qcl seed-example.qcl
QCL Quantum Computation Language (64 qubits, seed 1712837875)
: black 
: red 
: black 
: red 
: red 
: red 
: red 
: red 
: black 
: red 
: red

But, if you copy the seed value, and run the qcl --seed=<seed-value> command, it will always show the result. Here, I am going to use the seed value from my last output with qcl --seed=1712837875.

$ qcl --seed=1712837875 seed-example.qcl
QCL Quantum Computation Language (64 qubits, seed 1712837875)
: black 
: red 
: black 
: red 
: red 
: red 
: red 
: red 
: black 
: red 
: red

This is helpful because it will help you test various scenarios in your program without depending on luck. Pretty cool, right?

It is also possible to limit the number of qubits that you can use by adding qcl --bits=<qubit-count>. This is set to 64 by default if your system can handle it.

If the number of qubits used exceeds the number of qubits allocated, QCL with throw out of memory error.

In the following example, we will create 12 qubits.

qureg a[4]; // allocate 4 qubits to register

qureg b[3]; // allocate 3 qubits to register

procedure createAndPrintQbits() {
  qureg c[2]; // allocate 2 qubits to register local to this procedure
  print a,b,c; // print all qubits allocated
}

qureg c[5]; // Re-allocate c to a 5-qbit register

print c; // Print new register c

dump; //show actual qubit mappings
$ qcl memory-example.qcl
QCL Quantum Computation Language (64 qubits, seed 1712853382)
: <7,8,9,10,11>
: STATE: 12 / 64 qubits allocated, 52 / 64 qubits free
1 |0>

Now I am going to restrict the qubit count to 10 and run it again with qcl --bits=10 filename.qcl

$ qcl --bits=10 memory-example.qcl
QCL Quantum Computation Language (10 qubits, seed 1712853723)
! memory error: out of quantum memory

You can try out these and more using my codeberg repository. I will have a part 2 of this blog to deep dive into quantum programming. Stay tuned!


About me

Coding Otaku Logo

I'm Rahul Sivananda (he/him), the Internet knows me by the name Coding Otaku. I work as a Full-Stack Developer at IBM in London.

I care about Accessibility, Minimalism, and good user experiences. Sometimes I write stories and draw things.

Get my cURL card with curl -sL https://codingotaku.com/cc
Find me on Mastodon, Codeberg, or Peertube.
Subscribe to my feeds.

Continue Reading

Next

All my blogs can be subscribed to using RSS(Atom) or JSON feeds, if you do not know how to use feeds, I have a page with instructions on how to do that.

Comments

You can comment on this post here, all fields except the comment and answer are optional.









Recent Blogs

Subscribe via Atom or JSON feeds.