C1: Generate Uniform Amplitude Superposition State I

Time Limit: 3 sec

Memory Limit: 512 MiB

Score: 300

Writer: admin

Editorial

Define the integer kk as the "smallest integer such that 2kL2^k \geq L". Now consider applying the Hadamard gate to the first kk qubits.

By doing so, the absolute values of the probability amplitudes ai|a_i| of the computational basis states 0, 1, ... L1\ket{0},\ \ket{1},\ ...\ \ket{L-1} are all equal, and satisfy:

a0=a1==aL1=12k\begin{equation} |a_0| = |a_1| = \cdots = |a_{L-1}| = \frac{1}{\sqrt{2^k}} \end{equation}

Since 2k1<L2k2^{k-1} < L \leq 2^k, the sum of the observation probabilities satisfies the following inequality:

i=0L1ai2=L×(12k)2=L2k>2k12k=0.5\begin{align} \sum_{i=0}^{L-1} |a_i|^2 &= L \times \left(\frac{1}{\sqrt{2^k}}\right)^2 \nonumber\\ &= \frac{L}{2^k} > \frac{2^{k-1}}{2^k} = 0.5\\ \end{align}

Note that the "smallest integer kk such that 2kL2^k \geq L" can be represented as log2(L)\lceil\log_2(L)\rceil, where ()\lceil(\cdot)\rceil denotes the ceiling function.

As a result, you can solve this problem by applying the Hadamard gate to the first log2(L)\lceil\log_2(L)\rceil qubits.

Pay attention to the corner case that occurs when L=1L = 1.

Sample Code

Below is a sample program:

from qiskit import QuantumCircuit
import math
 
 
def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
 
    if L == 1:
        return qc
 
    k = math.ceil(math.log2(L))
    qc.h(range(k))
 
    return qc

Supplements