A4: Cyclic Shift Oracle III

Time Limit: 3 sec

Memory Limit: 512 MiB

Score: 400

Writer: PSL

Editorial

You can solve this problem by effectively using the Swap gate.

Let L=n+1L = n + 1. You can obtain the desired quantum state by performing the following steps in order.

  • Step 1: For j=0,2,4,...(<L1)j = 0, 2, 4, ... \left( < L - 1 \right), swap the jj th and (j+1)(j + 1) th qubits.
  • Step 2: For j=0,4,8,...(<L2)j = 0, 4, 8, ... \left( < L - 2 \right), swap the jj th and (j+2)(j + 2) th qubits.
  • Step 3: For j=0,8,16,...(<L4)j = 0, 8, 16, ... \left( < L - 4 \right), swap the jj th and (j+4)(j + 4) th qubits.

Therefore, when n=4n = 4, letting k4=k0k1k2k34\ket{k}_4 = \ket{k_0 k_1 k_2 k_3}_4, the following state transitions hold:

k0k1k2k3mStep 1 k1k0k3k2mStep 2 k3k0k1k2mStep 3 mk0k1k2k3\begin{align} \ket{k_0 k_1 k_2 k_3}\ket{m} &\xrightarrow{\text{Step 1 }} \ket{k_1 k_0 k_3 k_2}\ket{m} \\ &\xrightarrow{\text{Step 2 }} \ket{k_3 k_0 k_1 k_2}\ket{m} \\ &\xrightarrow{\text{Step 3 }} \ket{m}\ket{k_0 k_1 k_2 k_3} \\ \end{align}

As a result, you can solve this problem by following these steps.

The circuit diagram for n=4n = 4 is as follows.

Each step has circuit depth 11, so the total circuit depth is log2n\log_2 n.

Sample Code

Below is a sample program:

from qiskit import QuantumCircuit
 
 
def solve(n: int) -> QuantumCircuit:
    L = n + 1
    qc = QuantumCircuit(L)
 
    for i in range(L.bit_length()):
        for j in range(0, L - (2 ** i), 2 ** (i + 1)):
            qc.swap(j, j + (2 ** i))
 
    return qc