News

日本語 · English · 中文

[AI for Quantum Apps] Issue 5: Solving Shift Schedules with QAOA – The Reason the Hit Rate Halved Wasn’t Quantum

This is the fifth installment in the series. We solve the shift assignment problem for four people and four slots using QAOA. The hit rate reached approximately 47%, which is 3.8 times higher than the 12.5% obtained by random selection. However, the results fluctuated, starting at 45.6% and dropping to 27.2% in the next run. The cause was not quantum effects, but rather an insufficient number of measurements during the process of searching for knobs.

This is the fifth installment in the series. The previous four articles featured apps that merely threw coins—they were functional but useless. In this article, we tackle a problem that appears directly in business operations: Shift Table Assignment.

I will also share a story of a mistake I made, because I believe many people fall into the same trap.

What are we solving?

Assume there are four part-time workers with specific available time slots.

PersonAvailable Slots
Person A1st period, 2nd period
Person B2nd period, 3rd period
Person C3rd period, 4th period
Person D1st period, 4th period

There are two conditions. We want to hire as many people as possible. However, no more than one person can be assigned to the same slot. Hiring one person gives -1 point, and overlapping slots give +3 points each. We aim to find the combination that minimizes the total score.

Since there are only four people, there are only 16 possible combinations, so we can brute-force the answer. We will determine the correct answer in advance and observe whether the quantum approach can reach it.

Minimum cost by brute force: -2
Optimal assignment: [(0, 1, 0, 1), (1, 0, 1, 0)]
Hit rate with random selection: 12.5%

The optimal solution is to hire either Person A and Person C, or Person B and Person D. Putting all four people in causes all slots to overlap, which actually worsens the score.

Solving with QAOA

QAOA is a method of applying quantum circuits to problems that involve "selecting good options from a combination." There are three steps.

1. Hold all combinations simultaneously. Applying H gates to the four qubits representing the people creates a state where all 16 combinations exist at once.

2. Mark good combinations. The cost formula is implemented directly into the circuit. Hiring one person (-1 point) corresponds to a rotation on that qubit, and overlapping slots (+3 points) corresponds to a rotation connecting two qubits.

3. Bias towards marked states. Applying another rotation, called a mixer, increases the probability of good combinations appearing.

def execute(gamma, beta, shots):
    c = Circuit(4)
    for q in range(4):
        c.h[q]
    for q in range(4):
        c.rz(-1 * gamma)[q]
    for i, j in itertools.combinations(range(4), 2):
        if set(available_slots[i]) & set(available_slots[j]):
            c.cx[i, j].rz(3 * gamma)[j].cx[i, j]
    for q in range(4):
        c.rx(2 * beta)[q]
    return c.m[:].run(shots=shots)

The key point is that the cost formula is directly mirrored in the circuit structure. "Hiring is -1 point" corresponds to rz(-1 * gamma), and "overlapping slots is +3 points" corresponds to rz(3 * gamma). If you change the problem, you only need to change these numbers.

The Mistake

There are two knobs, gamma and beta, that must be tuned to find the solution. We search for these using classical computation, not quantum. I performed a brute-force search on a 12x12 grid, rotating 200 times per point to determine goodness or badness.

The initial result was a 45.6% hit rate. This is 3.6 times better than the random 12.5%. Thinking this was a good result, I intended to write it up in the article as is.

However, when I ran it once more, the result was 27.2%. Then, 24.8%. The code and the problem were identical.

I was about to write the result from a single run as the representative value.

The Nature of the Fluctuation

I changed the method of searching for the knobs and remeasured. I set the number of rotations per point to 200 and 2,000, and then ran each set of knobs 5 times.

Number of SearchesHit Rate (Sum of 5 runs)Average
200 rotations/point (1st time)45.5 / 49.0 / 47.0 / 47.6 / 46.1%47.1%
200 rotations/point (2nd time)25.3 / 26.8 / 25.9 / 25.0 / 25.1%25.6%
2,000 rotations/point (1st time)46.0 / 47.6 / 47.9 / 47.1 / 46.2%47.0%
2,000 rotations/point (2nd time)47.1 / 48.4 / 46.3 / 46.4 / 46.3%46.9%

The 5 runs after fixing the knobs show no fluctuation. Even with 200 rotations, the results stayed within the 25.0–26.8% range.

The fluctuation occurred during the process of searching for the knobs. With 200 rotations per point, the judgment of goodness/badness became rough, and we accidentally selected poor knobs. Running the actual experiment with these knobs cut the hit rate in half. With 2,000 rotations, both runs converged to the same result.

⚠️ It is not that "quantum results fluctuate." The fluctuation happened in the classical computation phase before calling the quantum computer. Insufficient measurement counts lead to incorrect judgments, which is a natural statistical fact.

Results

The hit rate with stable knobs is approximately 47%. This is 3.8 times the random 12.5%.

However, QAOA does not "always yield the correct answer." A 47% rate implies that half the time, an incorrect solution is produced.

The usage is as follows. Run the algorithm multiple times, score the generated candidates using classical computation, and select the best one. Instead of examining all 16 combinations, we only need to examine the top few.

As problems grow larger, this difference becomes significant. When the number of people is n, the number of combinations is 2 to the power of n. For 20 people, there are about 1 million combinations; for 30 people, about 1 billion. Examining all of them is not realistic. Quantum computers are currently positioned as tools for narrowing down candidates.

What We Did Not Do This Time

We did not run it on actual hardware. These are simulator results. As seen in the 3rd and 4th articles, actual hardware yields deviated results due to noise. The circuit in this article is deeper than in the 2nd article, with multiple rz and cx gates, so it would degrade even more on actual hardware. We will verify this next time.

The order of measurement results is read from left to right in the code. Since blueqat returns strings where the rightmost bit is the 0th qubit, reading from left to right reverses the order of people. This time, the optimal solutions were the two symmetric options (0101 and 1010), so the answer did not change. For asymmetric problems, this would lead to misidentification. Next time, we will explicitly state the bit order.

The knob search is still a brute-force grid search. In practice, optimization libraries would be used, but I left it as is to highlight the mistake described above.

The code is about 50 lines in total. By rewriting the cost formula, the same code can be applied directly to assignment problems other than shift tables.

Environment Measured

The numbers in this article were measured using blueqat 2.1.3, numpy 2.2.6, and the simulator. Different versions yield different numbers. In particular, blueqat 2.0.4 reverses the order of measurement results (the leftmost bit is the 0th qubit), so running the code above as-is will misalign the person mappings. You can check your version with the following line:

python3 -c "import blueqat; print(blueqat.__version__)"

If you are using version 2.0.4, add backend="numpy" to run(shots=...) (conversely, in version 2.1.3 and later, specifying this causes it to malfunction).

This article was translated from the Japanese original. 日本語で読む

← Latest articles