Development, Haskell, Quantum Computing, Quantum Programming, Quipper, Technology

The Quipper Quantum Programming Language

I finally got a chance today to play with Peter Selinger’s new quantum programming language, Quipper. Since it was built as an embedded, scalable functional programming language on Haskell, it was quite easy to get up and running; and naturally, with exponential overhead, you are able to simulate an actual quantum computer.

Note: Selinger’s quantum computer simulator within Quipper is designed after an ideal, gate-model quantum computer, which is a substantially different beast from D-Wave’s adiabatic quantum computer. For example, some members of the quantum computing community argue that D-Wave’s line of cryogenically-cooled superconducting chimera-graph flux-qubit processors aren’t ‘true’ quantum computers, primarily because they don’t follow the gate-model, and instead implement their own concept of energy programming to gain most-efficient-solution. The fact is, both are ‘true’ quantum computers, in the sense that they exploit quantum mechanics for computation—and when it comes down to it, flux-qubits are damn cool. In the early days of classical computing, there were a number of competing models of computation as well—but it was eventually discovered that all models that were Turing-complete were effectively equal, so as the most expedient option, the von Neumann model won-out. The question of what counts as a true quantum computer is less simple, of course—but suffice it to say that adiabatic and gate-model quantum computers serve different purposes.

With those differences in mind, we can continue.

You can get Quipper yourself from here:

Familiarity with Haskell, functional programming, and a working installation of The Haskell Platform will get you most of the way. There are a few additional dependencies to install, but everything you need to know is covered in the INSTALLING and README files listed on the language page. Once you’ve got everything set up, the Quipper scripts folder added to your PATH, and all the examples built, continue here.

To get a sense of how you’ll interact with your quantum programs once you start writing them, let’s try out some of the demos under the Algorithms/ directory.

From the top-level Quipper directory:

$ cd Algorithms/BWT
$ ./bwt -C -o orthodox -n 5 -s 1 -f pdf > bwt.pdf

This will dump a PDF file of the output from the bwt executable. Quipper’s built-in “Preview” mode doesn’t currently have support for Linux, so you have to manually pipe the PDF output to a file and then open it with your preferred PDF Viewer. For sake of simplicity here, you can pretty much always count on Xpdf:

$ xpdf bwt.pdf

Now that you’ve recovered from the staggeringly quick generation of that terrifyingly long quantum circuit diagram, let’s take a look at what we actually called.

First, the BWT example runs the Binary Welded Tree algorithm. The -C flag outputs the complete circuit. -o orthodox selects the Orthodox version of the oracle. -n sets the tree height, and -s sets the number of iterations. Obviously, -f sets the output format.

Now let’s take a look at the graph of the BWT algorithm:

$ ./bwt -G -o orthodox -n 5 -f pdf > bwt-graph.pdf
$ xpdf bwt-graph.pdf

Pretty neat, eh? Go ahead and explore the rest of the examples in Algorithms/. You can call --help on each of the binaries to see what they can do.

Moving on, lets go to the tests directory:

$ cd ../../tests
$ ls -la

There’s quite a few in this folder, so let’s start with the first one, And_gate.hs. An executable will already have been built for you when you ran make during the set-up process, but if you’re on Linux you’ll have to make a small change, recompile, and then run the examples.

In your favourite editor, open And_gate.hs. You should see the following:

-- This file is part of Quipper. Copyright (C) 2011-2013. Please see the
-- file COPYRIGHT for a list of authors, copyright holders, licensing,
-- and other details. All rights reserved.
-- ======================================================================

import Quipper

and_gate :: (Qubit, Qubit) -> Circ (Qubit)
and_gate (a, b) = do
  c <- qinit False
  qnot_at c `controlled` [a, b]
  return c

main =
  print_simple Preview and_gate

Under the main function, change Preview to PDF, and save. Then back at the command line:

$ quipper And_gate.hs
$ ./And_gate > And_gate.pdf
$ xpdf And_gate.pdf

You can now see an example of programming in Quipper for yourself. While Selinger cautions not to be deceived by the oh-so-familiar and comforting Haskell syntax, since that might lead to the temptation to fall back on traditional idioms and constructs, the fact remains that it could not be simpler to program a quantum AND_GATE.

Last up, let’s see the quantum computer simulator in action. Open up QFTAdder.hs in your favourite editor and review the code.

Done? Great. On line 20, find Preview and replace it with PDF again. Save the file, and recompile:

$ quipper QFTAdder.hs

QFTAdder can take either one or two arguments. If you read the code, you’ll see that providing only one argument will generate the PDF output, so you’ll have to pipe it to a file again:

$ ./QFTAdder 10 > QFTAdder_ex1.pdf

But as I promised, we want to run the simulator here:

$ ./QFTAdder 10 15
10 + 15 = 25

And there you have it, your own simulated quantum computer, crunching numbers! You may notice that it’s slightly slow, but since you’re running the quantum computer simulator on a classical computer (i.e., a PC), that’s to be expected. Obviously, the bigger the numbers, the longer the calculation takes—just to see how much longer, I decided to try a ridiculous example:

$ ./QFTAdder 10130300045 1598009123045

It’s been running for over three hours so far on my Haswell i7-4770K, and it’s still not done, but the process only ever peaked at 12.59%. I think performance could be greatly improved if the example was re-written to take advantage of multi-core processing. Comparatively, and obviously, SBCL and GHCi returned the result of 1608139423090 instantly. A better comparison would be to stack this against D-Wave’s Python Pack, which I’ll take a stab at tomorrow.

For a thorough introduction to Quipper, make sure you read and follow the tutorial. The PDF is available at:

EDIT: After running for nine and a half hours with still no result, I figured I might as well kill ./QFTAdder 10130300045 1598009123045. As it stands, an addition of much lower numbers still took a full minute:

$ ./QFTAdder 100 150
100 + 150 = 250

So, when using the quantum computer simulator, it’s worthwhile to scale down problems to their smallest parts and run them separately. This will be especially important for those designing applications that use the quantum algorithms instead of just printing out the circuits.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s