Quilc: An optimizing quantum compiler written in Common Lisp
source link: https://www.tuicool.com/articles/hit/ymUJbqY
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
QUILC
Quilc is an advanced optimizing compiler for the quantum instruction language Quil, licensed under the Apache 2.0 license .
Quilc comprises two projects. The first, cl-quil
, does the heavy
lifting of parsing, compiling, and optimizing Quil code. The second, quilc
, presents an external interface for using cl-quil
, either using
the binary quilc
application directly, or alternatively by
communicating with a server (HTTP or RPCQ
).
Quil is the quantum instruction language
developed at Rigetti Computing
. In Quil quantum algorithms are expressed using Quil's
standard gates and instructions. One can also use Quil's DEFGATE
to
define new non-standard gates, and DEFCIRCUIT
to build a named circuit
that can be referenced elsewhere in Quil code (analogous to a function
in most other programming languages).
Quil Compiler
This directory contains the quilc
application. quilc
takes as input
arbitrary Quil code, either provided directly to the binary or to the quilc
server, and produces optimized Quil code. The compiled code is
optimized for the configured instruction set architecture (ISA),
targeting the native gates specified by the ISA.
Building the Quil Compiler
Prerequisites to building quilc
are:
- Standard UNIX build tools
- SBCL (a recent version) : Common Lisp compiler
- Quicklisp : Common Lisp library manager
- buildapp : Builds executable binaries from Common Lisp software
Follow these instructions to get started from scratch.
One notorious dependency is MAGICL
. It is available on Quicklisp,
but requires you to install some system libraries such as BLAS, LAPACK, and libffi. Follow MAGICL's
instructions carefully before proceeding with loading CL-QUIL or make
ing quilc.
Once these dependencies are installed, building should be easy. Building the quilc
binary is automated using the Makefile
:
$ make quilc
This will create a binary quilc
in the current directory
$ ./quilc --version
To install system-wide issue the command
$ make install
Using the Quil Compiler
The Quil Compiler provides two modes of interaction: (1) communicating
directly with the quilc
binary, providing your Quil code over stdin
;
or (2) communicating with the quilc
server.
quilc
The quilc
binary reads Quil code provided on stdin
:
$ echo "H 0" | quilc $ cat large_file.quil | quilc
Server
For various reasons (e.g. not having to repeatedly load the quilc
binary into memory, communicating over a network) quilc
provides a
server interface. quilc
currently supports two server modes:
HTTP
The HTTP server was the original implementation of the server mode. It is now deprecated in favour
of the RPCQ server mode. Do not depend on it. You can create the HTTP server with the -S
flag
$ quilc -S +-----------------+ | W E L C O M E | | T O T H E | | R I G E T T I | | Q U I L | | C O M P I L E R | +-----------------+ Copyright (c) 2016-2019 Rigetti Computing. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> IMPORTANT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< The HTTP endpoint has been deprecated in favor of the RPCQ endpoint. In the future, it will be removed. You're advised to modify your client code to talk to the RPCQ version instead. >>>>>>>>>>>>>>>>>>>>>>>>>>>>> END IMPORTANT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<<<<< [2019-01-29 13:59:18] Starting server: 0.0.0.0 : 6000.
RPCQ
RPCQ
is an open-source RPC framework developed at Rigetti for
efficient network communication through the QCS stack. The server is started in RPCQ-mode using
the -R
flag
$ quilc -R +-----------------+ | W E L C O M E | | T O T H E | | R I G E T T I | | Q U I L | | C O M P I L E R | +-----------------+ Copyright (c) 2016-2019 Rigetti Computing. <134>1 2019-01-29T22:03:08Z workstation.local ./quilc 4077 LOG0001 - Launching quilc. <134>1 2019-01-29T22:03:08Z workstation.local ./quilc 4077 - - Spawning server at (tcp://*:5555) .
The server-mode provides to high-level languages such as Python a way
to communicate with the Quil compiler, thus enabling high-level
abstractions and tools that are not directly available in Quil. The
pyquil
library provides such an interface to quilc
.
CL-QUIL
CL-QUIL
is the Lisp library that implements parsing and compiling
of Quil code. The code can be found under ./src/
. Other lisp libraries, including quilc
, can depend on it.
Usage
To get up and running quickly using the quilc
Docker image, head directly to the
section "Running the Quil Compiler with Docker" below. Otherwise, the following steps
will walk you through how to build the compiler from source.
Follow the instructions in QVM's doc/lisp-setup.md
to satisfy the
dependencies required to load the CL-QUIL
library. Afterwhich, the
library can be loaded
$ sbcl
* (ql:quickload :cl-quil) ;;; <snip>compilation output</snip> (:CL-QUIL) * (cl-quil:parse-quil-string "H 0") #<CL-QUIL:PARSED-PROGRAM {100312C643}>
A few good entry points to exploring the library are:
-
The functions
cl-quil::parse-quil
insrc/parser.lisp
, andcl-quil:parse-quil-string
insrc/cl-quil.lisp
and the various transforms therein. -
The function
cl-quil:compiler-hook
which constructs a control-flow graph (CFG) and then performs various optimizations on the CFG.
Automated Packaging with Docker
The CI pipeline for quilc
produces a Docker image, available at
rigetti/quilc
.
To get the latest stable version of quilc
, run docker pull rigetti/quilc
.
Running the Quil Compiler with Docker
As outlined above, the Quil Compiler supports two modes of operation: stdin and server.
To run quilc
in stdin mode, do one either of the following:
- The containerized compiler will then read whatever newline-separated Quil instructions you enter, waiting for an EOF signal (C+d) to compile it.
docker run --rm -it rigetti/quilc
-
You can alternatively pipe Quil instructions into the
quilc
container if you drop the-t
.
echo "H 0" | docker run --rm -i rigetti/quilc
To run quilc
in server mode, do the following:
docker run --rm -it -p 5555:5555 rigetti/quilc -R
This will spawn an RPCQ-mode quilc
server, that you can communicate with over TCP. If
you would like to change the port of the server to PORT, you can alter the command as follows:
docker run --rm -it -p PORT:PORT rigetti/quilc -R -p PORT
Release Process
-
Update
VERSION.txt
and dependency versions (if applicable) and push the commit tomaster
. -
Push a git tag
vX.Y.Z
that contains the same version number as inVERSION.txt
. - Verify that the resulting build (triggered by pushing the tag) completes successfully.
- Publish a release using the tag as the name.
- Close the milestone associated with this release, and migrate incomplete issues to the next one.
Get involved!
We welcome and encourage community contributions! Peruse our guidelines for contributing
to get you up to speed on
expectations. Once that's clear, a good place to start is the good first issue
section. If you have any find bugs, please create an issue
.
If you need help with some code or want to discuss some technical issues, you can find us in the #dev
channel on slack
.
We look forward to meeting and working with you!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK