B3: Less Than Oracle I

Time Limit: 3 sec

Memory Limit: 512 MiB

Score: 300

Writer: admin

Editorial

Consider multiplying each probability amplitude aia_i of the computational basis states 0, 1, ... L1\ket{0},\ \ket{1},\ ...\ \ket{L-1} by 1-1.

The operation of multiplying by 1-1 can be achieved using the ZZ gate, which leaves the computational basis state 0\ket{0} unchanged, and transforms 1\ket{1} into 1-\ket{1}.

So, how can you apply the ZZ gate to only a specific computational basis state l\ket{l} within a superposition state?

This can be accomplished as follows:

  1. For l=l1l2l3...ln\ket{l} = \ket{l_1 l_2 l_3 ... l_n}, apply the XX gate at index ii where li=0l_i = 0. This transforms l\ket{l} into 2n1=1...1\ket{2^n - 1} = \ket{1...1}.
  2. Apply a multiple-control ZZ gate1, using n1n - 1 bits as control bits and the remaining 11 bit as the target bit. In this case, the ZZ gate acts only on the computational basis state l\ket{l} within the superposition.
  3. Perform the inverse of step 1 to transform 2n1\ket{2^n - 1} back to l\ket{l}.

Therefore, by performing this operation for each computational basis state 0, 1, ... L1\ket{0},\ \ket{1},\ ...\ \ket{L-1}, you can solve this problem.

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 l in range(L):
        for i in range(n):
            # check if i-th bit of l is 0 or 1
            if not ((l >> i) & 1):
                qc.x(i)
        if n == 1:
            qc.z(0)
        else:
            # apply multiple controlled Z gate
            qc.append(ZGate().control(n - 1), range(n))
        for i in range(n):
            if not ((l >> i) & 1):
                qc.x(i)
    return qc

Footnotes

  1. In Qiskit, multi-controlled gates can be implemented using the control method available in each gate class. Please refer to the sample code for usage.