# Programming

Programming

Before quantum computers existed, quantum algorithms were defined either mathematically, in terms of the transitions on quantum states, or using a graphical circuit notation for individual gates. Nowadays, quantum computers can be programmed using a GUI for the graphical notation, an assembly language-like text notation for the equivalent gates, or richer programming languages that may also provide low-level access.

The effects of the gates described earlier are similar to the logic gates we use to build a classical computer, like AND and OR. These days, few people bother designing their own programs for classical computers using those low-level gates. In fact, due to the fixed structure of a classical computer, it is hard to select which gates are used. Instead, we program computers using programming languages that humans can read, which are then turned into instructions for the computer, like ADD or MULTIPLY.

When we construct quantum algorithms, we will use gates. Each gate will operate on one, two or three qubits, and we will apply a sequence of gates to our register to form a complete circuit, an implementation of an algorithm. In most quantum computer implementations, the qubits are stationary, and gates are executed on the qubits where they sit, allowing us to treat them something like low-level computer instructions.

## Graphical Notation

Each of the gates discussed back in Step 2.15 has a symbol, shown in the figure at the top of this article. Circuits are composed to run left to right, with a horizontal line for each qubit. The visual resemblance to music has lead IBM to referring to such a diagram as a “score”. IBM’s web-based interface uses drag-and-drop programming with colorful, stylized gate symbols.

This approach can show conditional operation, basing the choice to execute individual gates on the outcome of prior measurements. It is also common to draw circuits in terms of blocks of primitive gates. However, it is inherently difficult to create a loop or complex control structures.

## A Simple “Assembly” Language

During my (Van Meter’s) Ph.D. thesis work, I developed a simple “assembly” language that lists gates by name, and the qubits on which they operate by a variable name, rather than a physical location. Other low-level languages might make similar choices, or might more directly specify the unitary transform and might identify qubits by physical location, but the goal and program structure will be similar. One unique feature of my language is that the programmer explicitly specifies which gates may be executed at the same time, denoted by the numeric labels. Other assembly languages depend on the compilation tools to determine this parallelism.

1: CCNOT A0 B0 C0 CCNOT A1 B1 C1 CCNOT A2 B2 C22: CNOT A1 B1 CNOT A2 B2 CNOT A3 B33: CCNOT C0 B1 C14: CCNOT C1 B2 C2

One modern tool is OpenQASM,
developed by Andrew Cross and others at IBM.

## Programming Languages

The notion of quantum programming languages goes back to the earliest
days of quantum computing research, but much of the early work was
very abstract. Omer’s QCL was one of the first concrete, publicly
available languages, and also provided a simple simulator.

Several years ago, the IARPA Quantum Computer Science program resulted
in the development of several languages.
Quipper (by a team from Dalhousie
University, U. Penn, and IAS in Princeton) builds on the Haskell
programming language.
Scaffold
(from a team including Princeton University) is a C-like language.
QuaFL (from BBN)
is a from-scratch functional language.

Microsoft is now on their second-generation language, known as Q#
(Q-sharp)
.

More recently, efforts have turned toward simple library extensions
for the popular programming language Python.
QISKit and OpenQASM, for connecting to the
IBM machines, looks like this:

# quantum circuit to make an entangled bell statebell = Q_program.create_circuit('bell', [qr], [cr])# H (Hadamard) gate to qubit 0 in the Quantum Register "qr"bell.h(qr[0])# CNOT (Controlled-NOT) gate from qubit 0 to qubit 1bell.cx(qr[0], qr[1])

Rigetti, a startup company making superconducting quantum computers,
has developed PyQuil and
the Forest programming environment.

## Common Subroutines

Of course, quantum algorithms depend on a number of common building
blocks, or subroutines. (Just to confuse things, quantum
researchers occasionally call an entire block of gates a “gate”,
also.) Some of these build on classical notions of simple circuits
such as adders, or are quantum-specific routines such as the quantum
Fourier transform (QFT).

The image below is a circuit for adding two eight-qubit registers
together. This design, by Vedral, Barenco and Ekert, is based on a
classical form of circuit known as a ripple-carry adder, a binary
form of the right-to-left method you learned as a child. Extra qubits
are used to hold partial, temporary results. The “V” shape comes from
the need to disentangle those temporary qubits from the important data
qubits, and the severe restriction that the entire block must be
unitary, or reversible.

We can expect that such low-level routines will be provided as
libraries of functions in programming toolkits, allowing us to compose
our own algorithms at a high level of abstraction.

# プログラミング

## 視覚的表記法

このページの冒頭にある画像のように、2.15 で述べた各ゲートには対応した専用の記号が存在しています。 回路は各量子ビットから水平に伸びる線上に描かれ、左から右へ順番に計算されていきます。 楽譜と視覚的に似ていることから、IBMはこのような回路図を「スコア」と呼んでいます。 IBMのWebベース・インターフェースでは、カラフルでスタイリッシュなゲートのシンボルによるドラッグ・アンド・ドロップ・プログラミングが可能です。

この方法を使って、先に行った測定の結果に基づいたゲートの実行、いわゆる条件付き操作を表すこともできます。 基本的なゲートの集合を単位とした回路を描くことも一般的です。 しかし、ループや複雑な制御構造を表現することは本質的に困難です。

## 単純なアセンブリ言語

1: CCNOT A0 B0 C0 CCNOT A1 B1 C1 CCNOT A2 B2 C22: CNOT A1 B1 CNOT A2 B2 CNOT A3 B33: CCNOT C0 B1 C14: CCNOT C1 B2 C2

## プログラミング言語

また、ポピュラーなプログラミング言語であるPythonでつかえる簡単なライブラリの拡張にも力が入っています。例えば、 QISKitとOpenQASMは、IBMマシンに接続するためのPython ライブラリで、具体的には以下のような記述が可能です。

# quantum circuit to make an entangled bell statebell = Q_program.create_circuit('bell', [qr], [cr])# H (Hadamard) gate to qubit 0 in the Quantum Register "qr"bell.h(qr[0])# CNOT (Controlled-NOT) gate from qubit 0 to qubit 1bell.cx(qr[0], qr[1])

## 共通サブルーチン

もちろん、量子アルゴリズムは、共通に利用可能な構成要素（あるいはサブルーチン）を積極的に活用してします。 （混乱を招きますが、量子計算の研究者は、ゲートの組み合わせ(構成要素)全体を「ゲート」と呼ぶこともあります。） これらの中には、加算器などの単純な回路の古典的概念に基づくものもあれば、量子フーリエ変換 (QFT)のような量子特有のものもあります。

このような低レベルのルーチンがプログラミングツールキットの関数ライブラリとして提供されるようになれば、我々は、高い抽象度で独自のアルゴリズムを作成することに注力できるようになります。