Programming Languages ​​for a Quantum Computer The prototype of the core of an ionic quantum computer. Ion Quantum Technology Group , University of Sussex

Quantum computers from time to time get into the media. You hear how a person is approaching their creation step by step, although for most, the development of quantum computing remains a strange and mysterious art.

Fortunately, to solve this problem, excellent projects appear that attract the attention of a wide audience. For example, several years ago, IBM made it possible for anyone to connect to a 5-qubit computer. The project has registered 70,000 people.

However, the quantum computing industry is still in its infancy. Although many prototypes have already been created, they cannot do what an ordinary laptop cannot handle. The necessary hardware does not yet exist, but we have something else to learn - quantum programming languages.

A journey into the world of quantum computing is accessible to everyone and resembles the study of any ordinary language in normal programming.

Quantum computing To immerse yourself in the world of quantum computing, you don’t need to keep a quantum computer at home. There are projects that provide access to real quantum devices, but simple quantum programs can easily be modeled on a regular laptop.

Before we take up the programs, let's recall the basic things about quantum computing. And here, an article by Brian Hayes “ Programming Your Quantum Computer ” will help us .

A typical computer is based on bits: we are talking about variables that have only two possible values. We often call them 0 and 1, although in the context of Boolean algebra we can call them “True” and “False”.

You can perform simple Boolean operations on bits, such as NOT, AND, and OR. Any variable that is more complex than a bit (for example, int or float) is simply a collection of many bits.

Quantum computers are based on quantum bits or qubits. They also have two possible meanings, which we can call 0 and 1. But the laws of quantum mechanics also allow other meanings, which we call superposition states.

In a sense, superposition states are values ​​that exist between extremes 0 and 1. We can imagine a qubit as a sphere, with 0 and 1 located at opposite poles. Superposition states are all other possible points on the surface.

It is not that a qubit can have an intermediate value, for example 0.63; when the state of a qubit is measured, the result is always 0 or 1. But during the calculation, the qubit can act as if it was a mixture of states - for example, 63% zero and 37% units.

Another key aspect of qubit behavior is interference, a phenomenon well known in wave physics. When two waves overlap, they can amplify each other (if the peaks and wave-like decays coincide), or they can level each other (if the waves do not correspond to the phase). Mathematically, the intensity of combined waves at any point is determined by the square of the sum of the individual wave amplitudes. When two amplitudes have the same sign, interference is constructive; when one amplitude is positive and the other negative, the resulting destructive interference gives an intensity less than that of one wave.

Like waves, states 0 and 1 of qubit have amplitudes that can be either positive or negative. Depending on the signs of the amplitudes, quantum interference can either increase or decrease the probability that a certain state will be observed when we measure the state of a qubit.

Interference plays a role in all interesting algorithms for quantum computers, that is, algorithms that can allow such a machine to surpass a classical computer. The general idea to organize the evolution of a quantum system is that erroneous answers are suppressed by destructive interference, and correct answers are amplified by constructive interference. Thus, algorithms use a form of parallelism unique to quantum systems.

One of the latest aspects of quantum strangeness is entanglement. You cannot change one qubit inside a quantum register, leaving the rest unchanged. To begin with, each function calculated by a quantum system must be completely reversible. If the machine receives input A to output the result B, then it should be possible to restore A if there is B.

Each function must have the same number of inputs and outputs. In one fell swoop, this rule prohibits most of arithmetic. The usual addition algorithm, for example, is irreversible. You can add 3 and 4 to get 7, but you cannot “disconnect” 7 to restore the original 3 and 4.

Another prohibition in quantum computing is copying a qubit (this principle is called the clone prohibition theorem ). Also, you cannot arbitrarily install or reload qubits in the middle of the calculation. Attempting to do this would destroy quantum superposition.

In aggregate, restrictions on qubit operations imply that any quantum program must have a chimney architecture - information flows directly from one end to the other. It is especially important that there can be no cycles in the program structure where control is transferred back to an earlier point.

The answer to these difficulties is found in quantum programming languages. In fact, quantum computers are hybrid devices: partially quantum and partially classical computers. The use of elements of a conventional computer is necessary for processing inputs and outputs in order to interact with external applications. Thus, in one program, you can combine the classical code and the quantum code.

Open Quantum Assembly Language (OpenQASM)

OpenQASM source code was released as part of the IBM Quantum Information Software Kit (QISKit) for use with the Quantum Experience quantum computing platform . OpenQASM shares similarities with specialized programming languages ​​(such as Verilog) used to describe the structure and behavior of electronic circuits.

QASM programs actually always start the same way: we define all the bits that we need - both quantum and normal. The following is an example of OpenQASM source code. The program adds two four-bit numbers.

OPENQASM 2.0;
include "qelib1.inc";
gate majority a,b,c
{
cx c,b;
cx c,a;
ccx a,b,c;
}
gate unmaj a,b,c
{
ccx a,b,c;
cx c,a;
cx a,b;
}
qreg cin;
qreg a;
qreg b;
qreg cout;
creg ans;
// устанавливаем входящие значения
x a; // a = 0001
x b;    // b = 1111
// добавляем a к b, сохраняем результаты в b
majority cin,b,a;
majority a,b,a;
majority a,b,a;
majority a,b,a;
cx a,cout;
unmaj a,b,a;
unmaj a,b,a;
unmaj a,b,a;
unmaj cin,b,a;
measure b -> ans;
measure b -> ans;
measure b -> ans;
measure b -> ans;
measure cout -> ans;

Q #

The high-level programming language Q # eliminates the need to have in-depth knowledge in quantum physics. For those interested in the language textbook, information is given on the basic concepts of quantum computing, covering vector and matrix mathematics, qubits, Dirac notation , Pauli's principle, and quantum schemes .

operation Teleport(msg : Qubit, there : Qubit) : () {
body {
using (register = Qubit) {
let here = register;
H(here);
CNOT(here, there);
CNOT(msg, here);
H(msg);
if (M(msg) == One) { Z(there); }
if (M(here) == One) { X(there); }

}
}
}

Teleportation.qs script from the Q # tutorial. The tutorial is available here.

Q # does not look like most other programming languages, and is somewhat similar to C #.

The Quantum Development Kit is provided free of charge with detailed installation instructions and introductory training programs. Q # compiles on a Visual Studio quantum simulator, simulating a 32-qubit quantum processor. The simulator can simulate up to 40 qubits.

If you follow the tutorial from Microsoft, the learning process will go from observing entangled states from two qubits to modeling quantum teleportation.

LIQUi (Language-Integrated Quantum Operations)

The LIQUi platform, created by the Quantum Architectures and Computation Group team at Microsoft Research, includes a programming language, optimization and planning algorithms, as well as several quantum simulators. LIQUi can be used to translate a quantum algorithm written as a high-level program in F # from the .NET Framework family into low-level commands for a quantum computer.

LIQUi allows you to simulate up to 30 qubits on a single machine with 32 GB of RAM. The platform can be used to define, execute and display quantum schemes in various graphic formats. Using LIQUi, you can simulate simple quantum teleportation, the Shore factorization algorithm, quantum associative memory, and quantum linear algebra.

operation TeleportClassicalMessage(message : Bool) : Bool {
body {
mutable measurement = false;

using (register = Qubit) {
// Запросим несколько кубитов, которые можно использовать для телепортации.
let msg = register;
let there = register;

// Кодируем сообщение, которое мы хотим отправить.
if (message) { X(msg); }
Teleport(msg, there);

// Проверяем сообщение.
if (M(there) == One) { set measurement = true; }

ResetAll(register);
}

return measurement;
}
}

As you can see from the example above, LIQUi is very similar to Q #.

Quantum Computation Language (QCL)

QCL, or Quantum Computation Language, was created by Bernhard Omer in 1998. The development of the language continues now: there is an emulator that allows you to run quantum programs on a classic computer. Of course, an emulator cannot provide acceleration of quantum parallelism; on the other hand, it offers the programmer some useful functions, such as commands for checking the internal state of qubits (which is extremely difficult to do on real quantum equipment).

QCL borrows the syntax of C and Java, which are sometimes described as “imperative” languages ​​because they rely on direct commands to set and reset the values ​​of variables. Such commands are usually prohibited in quantum computing, so the main parts of the QCL program work only on classical equipment. The quantum system serves as an “oracle” that answers questions that can be asked in a format suitable for calculating qubits. Each oracle request must have the required chimney architecture, but it can be embedded in a loop in an external classic context.

Snippet of code created in QCL (discrete Fourier transform):

operator dft(qureg q) {
const n=#q;
int i; int j;
for i=1 to n {
for j=1 to i-1 {
V(pi/2^(i-j),q[n-i] & q[n-j]);
}
H(q[n-i]);
}
flip(q);
}

The discrete Fourier transform is a crucial step in the Shore factorization algorithm. In the Shore algorithm, the number to be factorized is considered as a wave-like, periodic signal. If N has coefficients u and v, then N consists of u repetitions of v or v repetitions of u. The Shore algorithm uses quantum parallelism to search for a period of such repetitions, although the process is not as simple and straightforward as it might seem in the example above.

Quipper

The language was created by a team of authors led by Peter Selinger. Quipper is designed for the same programming tasks as QCL, but has a different structure and appearance. The language is implemented as a Haskell extension that uses a functional rather than an imperative way of expression.

Consider classical quantum teleportation. It includes two sides - Alice and Bob. Alice's goal is to teleport q qubit to Bob. Alice and Bob should have access to qubits from an intricate pair (a, b). We can think of Alice's role in terms of a function that introduces two qubits q and a. The output of the function will be a pair of classic bits created by Alice:

alice :: Qubit -> Qubit -> Circ (Bit,Bit)
alice q a = do
a <- qnot a 'controlled' q
(x,y) <- measure (q,a)
return (x,y)

More information can be found in the document " An Introduction to Quantum Programming in Quipper ".

And here is an interesting example of raising to 17 degrees, by raising x to 16 degrees by the built-in squaring procedure and multiplying x and x ^ 16:

o4_POW17 :: QIntTF -> Circ (QIntTF,QIntTF)
o4_POW17 = box "o4" \$ \x -> do
comment_with_label "ENTER: o4_POW17" x "x"

(x, x17) <- with_computed_fun x
(\x -> do
(x,x2) <- square x
(x2,x4) <- square x2
(x4,x8) <- square x4
(x8,x16) <- square x8
return (x,x2,x4,x8,x16))
(\(x,x2,x4,x8,x16) -> do
(x,x16,x17) <- o8_MUL x x16
return ((x,x2,x4,x8,x16),x17))
comment_with_label "EXIT: o4_POW17" (x,x17) ("x","x17")
return (x, x17)

The Quipper system is a compiler, not an interpreter; he translates the complete program at a time, and does not follow instructions one after another. The compiler output consists of quantum circuits: networks of interconnected, reversible logic gates. The circuit may take the form of an electrical circuit, but also represents a sequence of instructions ready to be executed using suitable quantum equipment or a simulator.

Quipper, like QCL, automatically generates schemas from high-level source semantic constructs.

Other approaches Multi-colored squares tell IBM's five quantum bits what to do. By dragging and dropping you can create your own quantum computing

The IBM Quantum Experience project provides an opportunity for everyone to run an experimental program on a real quantum computer. Working with the IBM programming language is similar to writing music using an application. A programmer can simply drag quantum objects into a specific area to write a program.

Quantum Computing Playground is a WebGL Chrome experiment that allows you to simulate working with a quantum computer in a browser window. It has its own Qscript scripting language with debugging and 3D quantum visualization features. A quantum computing platform can effectively simulate quantum registers up to 22 qubits.

The Python QISKit SDK includes several tools that IBM Q engineers provided to illustrate the goals of quantum programming. In particular, the SDK shows how you can complete several tasks for complex experiments. As the name implies, QISKit allows developers to explore a quantum computer using Python.

Qbsolv is an open source project for working with qubits of the D-Wave quantum processor (suitable only for computers of this company).

There are already dozens of quantum programming languages ​​(and simulators) , but they all work in a virtual machine. Probably, IBM Q is the only project that offers access to a real quantum computer. However, in order to begin to engage in “quantum programming”, it is not at all necessary to have access to a real advanced device. Already now you can not only study the work of promising quantum algorithms, but also create working applications, for example, games. But this is a completely different story.

Sources: