B4: Less Than Oracle II

Time Limit: 3 sec

Memory Limit: 512 MiB

Score: 500

Writer: admin

Editorial

Let us represent the ii-th bit of integer LL as LiL_i, such that L=L1,...,Ln\ket{L} = \ket{L_1, ..., L_n}.

First, we decompose the set of integers A={0, 1, ..., L1}A = \left\{0,\ 1,\ ...,\ L-1\right\} which are targeted by the oracle, as follows:

A=i=1n(Ai={X(Xi<Li)((Xi+1=Li+1)...(Xn=Ln))})\begin{equation} A = \bigcup_{i=1}^n \left(A_i = \left\{\ket{X} \mid (X_i < L_i) \land ((X_{i+1} = L_{i+1}) \land ... \land (X_n = L_n)) \right\}\right) \end{equation}

In other words, AA can be expresseed as the union of subsets AiA_i, where the ii-th bit is less than LL, and the bits from i+1i+1 to nn are all equal to LL. Note that (Xi<Li)(X_i < L_i) is equivalent to (Xi=0Li=1)(X_i = 0 \land L_i = 1).

For example, consider L=5=101\ket{L} = \ket{5} = \ket{101}:

  • For i=1i=1, A1={X(X1<1)((X2=0)(X3=1))}={001=4}A_1 = \left\{\ket{X} \mid (X_1 < 1) \land ((X_2 = 0) \land (X_3 = 1)) \right\} = \{\ket{001} = \ket{4}\}
  • For i=2i=2, A2={X(X2<0)(X3=1)}={}A_2 = \left\{\ket{X} \mid (X_2 < 0) \land (X_3 = 1) \right\} = \{\}
  • For i=3i=3, A3={X(X3<1)}={000=0,100=1,010=2,110=3}A_3 = \left\{\ket{X} \mid (X_3 < 1) \right\} = \{\ket{000} = \ket{0}, \ket{100} = \ket{1}, \ket{010} = \ket{2}, \ket{110} = \ket{3}\}

Thus, A=A1A2A3={0,1,2,3,4}A = A_1 \cup A_2 \cup A_3 = \{\ket{0}, \ket{1}, \ket{2}, \ket{3}, \ket{4}\}, and successfully contains all states preceding L=5\ket{L} = \ket{5}.

Based on this decomposition, an oracle can be implemented by applying a ZZ gate to each subset associated with index ii.

The two conditions for each index, Xi<LiX_i < L_i and ((Xi+1=Li+1)...(Xn=Ln))((X_{i+1} = L_{i+1}) \land ... \land (X_n = L_n)), can be implemented using quantum gates as follows:

  • Xi<LiX_i < L_i:
    1. If Li=0L_i = 0, do nothing (skip applying any gate).
  • ((Xi+1=Li+1)...(Xn=Ln))((X_{i+1} = L_{i+1}) \land ... \land (X_n = L_n)):
    1. Apply an XX gate to the jj-th qubit where jj runs from i+1i+1 to nn, if Lj=0L_j = 0.
    2. Apply an XX gate to the ii-th qubit (to apply 1-1 when Xi=0X_i = 0).
    3. Apply a multi-controlled ZZ gate with control bits from i+1i+1 to nn and target bit ii.
    4. Perform the inverse operations of steps 2 and 1.

Sample Code

Below is a sample program:

from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
 
 
def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
 
    for i in range(n):
        if not (L >> i) & 1:
            continue
        for j in range(i + 1, n):
            if not (L >> j) & 1:
                qc.x(j)
        qc.x(i)
        if i == n - 1:
            qc.z(i)
        else:
            qc.append(ZGate().control(n - i - 1), range(i, n))
        qc.x(i)
        for j in range(i + 1, n):
            if not (L >> j) & 1:
                qc.x(j)
 
    return qc

Supplements

  • In Problem B3, the expected solution involves applying a corresponding quantum gate to each state targeted by the oracle. This method, however, is a greedy approach and inefficient. To demonstrate the computational advantage of quantum algorithms over classical algorithms, it is necessary to implement an efficient oracle with a shallow circuit depth.
  • A more efficient method for implementing the oracle is proposed in [1].

References

  • [1] J. Sanchez-Rivero et al., “Automatic generation of an efficient less-than oracle for quantum amplitude amplification,” in 2023 IEEE/ACM 4th International Workshop on Quantum Software Engineering (Q-SE), May 2023, pp. 26–33.
    https://arxiv.org/abs/2303.07120