iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🚚

Getting Started with TYTAN SDK (1) — An Absolute Beginner's Guide

に公開
2023/07/01

Objective

I'm not an expert, so my terminology or phrasing might not be perfectly accurate, but there is a concept called QUBO (Quadratic Unconstrained Binary Optimization), and it seems people want to optimize this using solvers to solve combinatorial optimization problems.

I decided to start writing a series of articles to grasp the basics of combinatorial optimization through QUBO optimization. The goal of this first article is to at least cover the minimum usage of the SDK.

Introduction

Putting aside what combinatorial optimization using QUBO actually is for a moment, there are tools like D-Wave Ocean SDK and PyQUBO[1]. While I haven't compared them thoroughly, TYTAN SDK seems to be something similar. I wanted to try using it for a typical reason: I don't know much about it, but it happened to catch my eye, and since it had a tutorial, it looked decent enough. Note that, excluding actual D-Wave hardware, this method likely falls under "simulated quantum annealing."

What you do is simple:

  1. Describe the combinatorial problem you want to solve as an expression called a QUBO expression.
  2. Throw it into a solver.

If the description is valid and you're lucky, you'll get an optimal or near-optimal solution.

Typical problems that can be addressed include:

  • Route optimization such as delivery routes
  • Shift optimization for work schedules

Brief Overview

In solving combinatorial problems using QUBO, you solve the problem using appropriate variables, but there are some quite strict conditions.

  1. As the name Binary suggests, variables q_0, q_1, ... can only take two values: 0 or 1.
  2. Furthermore, as the name Quadratic suggests, the degree of the polynomial describing the problem must be up to the second degree (quadratic).

In other words, q_0^2 is OK, but q_0^3 or q_0^4 is not. Since linear combinations are allowed:

  • OK:
    • H = q_0 + q_1^2,
    • H = (2 q_0 - q_1 + 3 q_2)^2 + 3 q_0 q_1
  • NG:
    • H = q_0 + q_1^3
    • H = (2 q_0 - q_1 + 3 q_2)^3 + 3 q_0 q_1 q_2

It looks like that. Note that since q_0 \in \{0, 1\}, q_0^2 = q_0 holds. That is,

\begin{align*} H = (q_0^2 + q_1^2) q_2 \end{align*}

appears to be a cubic expression at first glance, but when simplified, it becomes H = q_0 q_2 + q_1 q_2, which is a quadratic expression and thus OK.

I believe that constraints like binary values or quadratic degrees can be mitigated to some extent with certain techniques, but I will leave that for future articles.

A Simple Problem

Under these strict conditions, "let's try to find integers that add up to 2." In other words, the problem we want to solve is to find q_0, q_1 \in \{0, 1\} such that:

\begin{align*} q_0 + q_1 = 2 \tag{1} \end{align*}

The answer is likely trivial, but putting that aside, your brain probably tested the following in an instant:

  • q_0=0, q_1=0
  • q_0=0, q_1=1
  • q_0=1, q_1=0
  • q_0=1, q_1=1

Or so we shall assume[2].

Imagine that—calculating as many as four times! Doing that exhaustively every time is a hassle, and we don't want to do it. The motivation for wanting to solve such problems with a solver is likely the desire to leave it to a PC.

By the way, since we are solving this as a so-called "optimization problem," we need to prepare something like a cost function—a real-valued function where the case that takes the minimum value gives the optimal solution. In this case, two cost functions, H_1 and H_2, could be considered:

\begin{align*} H_1(q_0, q_1) = 2 - (q_0 + q_1) \tag{2} \end{align*}

and

\begin{align*} H_2(q_0, q_1) = (q_0 + q_1 - 2)^2 \tag{3} \end{align*}

Equation (2) feels a bit unpleasant because you can't really put it in this form (especially the sign) unless you already have a hunch about the answer. Equation (3) can be designed without thinking about signs or anything else, requiring no brainpower. This one is likely more versatile. On the other hand, squaring it immediately consumes the quadratic nature of QUBO, meaning you can only use linear expressions inside the parentheses.

Referring to (Stealing) the Tutorial

The TYTAN SDK tutorial is available at tytan_tutorial, and it is updated quite frequently. Since the official policy for the TYTAN SDK is to use the latest version from the main branch, I will follow that. However, for this article, I will use the latest version where CI is currently passing:

Solving the Problem

!pip install -q git+https://github.com/tytansdk/tytan.git@69d2415

Since the SDK is updated rapidly, the syntax changes slightly over time. Generally, new and simpler ways of writing are listed in the latest tutorials. Here, I will refer to TYTAN tutorial Recommended 10 (Linear Regression).

Import the necessary modules.

from tytan import symbols, Compile, sampler

Now, let's define each cost function:

q0 = symbols('q0')
q1 = symbols('q1')

H1 = 2 - q0 - q1
H2 = (q0 + q1 - 2)**2

We have defined the cost functions in a trivial way; reaching this stage is apparently called "formulating the QUBO."

Now, to make these ready to be passed to the API, we compile each of them:

qubo1, offset1 = Compile(H1).get_qubo()
qubo2, offset2 = Compile(H2).get_qubo()

Since constant terms do not affect the optimization, they are removed as offset variables. In the expression for H_1, the constant term is 2, so offset1 = 2. When the expression for H_2 is expanded, the constant term is 4, so offset2 = 4.

If you look at them with the print function, the output will look slightly different, but essentially, we obtain qubo1 = -q0 -q1 and qubo2 = -3*q0 -3*q1 + 2*q0*q1, with constant terms excluded. You might wonder where the -3 came from! If you expand the expression and note that q_0^2 = q_0, it appears as q0**2 - 4*q0 = q0 -4*q0 = -3*q0.

It works out to be like H1 = qubo1 + offset1 and H2 = qubo2 + offset2.

Now that the preparation is complete, we can throw them into the solver without thinking too much and solve the problem:

solver = sampler.SASampler()

result = solver.run(qubo1)

for r in result[:2]:
    print(r)

[{'q0': 1, 'q1': 1}, -2.0, 100]

Thus, q_0 = 1 and q_1 = 1 provide the optimal solution. "-2.0" is the value of H_1 at the optimal solution, and since qubo1 = -q0 -q1, it is indeed -2. The "100" part is the number of times that result was obtained. The SASampler.run method has an argument called shots, which specifies how many times to try the annealing experiment. The current default value is 100. Therefore, as a result of 100 trials, q_0 = 1 and q_1 = 1 were obtained every time.[3]

Since H1 = qubo1 + offset1 and offset1 = 2, it resulted in H_1(1, 1) = (2 - q_0 - q_1) \big|_{q_0=1,q_1=1} = 0.

Let's also solve for H_2:

result = solver.run(qubo2)

for r in result[:2]:
    print(r)

[{'q0': 1, 'q1': 1}, -4.0, 100]

Similarly, it can be seen that q_0 = 1 and q_1 = 1 provide the optimal solution. "-4.0" is the value of H_2 at the optimal solution, and since qubo2 = -3*q0 -3*q1 + 2*q0*q1, it is indeed -4. This also resulted in H_2(1, 1) = (q_0 + q_1 - 2)^2 = 0.

Summary

In this very simple problem setting, we performed a basic calculation using optimization technology. We covered the minimum usage of the API and two ways of formulating QUBO.

脚注
  1. Developed 'PyQUBO', a domain-specific language that automatically constructs QUBOs for solving 'combinatorial optimization problems' on quantum annealing machines, etc. ↩︎

  2. I don't know much about biology or neuroscience, so I'm not sure. ↩︎

  3. For example, with the QUBO expression H3 = (q0 + q1 - 1)**2, both q_0 = 0, q_1 = 1 and q_0 = 1, q_1 = 0 provide optimal solutions. In this case, which one achieves the optimal value is determined probabilistically and will be roughly 50:50. For instance, an actual measurement was 54:46. ↩︎

GitHubで編集を提案

Discussion