B2: Phase Shift Oracle

Time Limit: 3 sec

Memory Limit: 512 MiB

Score: 200

Writer: PSL

Editorial

This problem involves implementing a quantum circuit that applies a phase shift gate P(θ)P(\theta) exclusively to a specific computational basis state L\ket{L}.

So, how can we apply the phase shift gate P(θ)P(\theta) only to the specific computational basis state L\ket{L}within a superposition?

This can be achieved with the following steps:

  1. For L=L0L1L2...Ln1\ket{L} = \ket{L_0 L_1 L_2 ... L_{n-1}}, 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 phase shift gate, using n1n - 1 bits as control bits and the remaining 11 bit as the target bit. In this case, the phase shift 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}.

The circuit diagram for the case where n=3n = 3 and L=1L = 1 is shown as follows.

The circuit depth is 33.

Sample Code

Below is a sample program:

from qiskit import QuantumCircuit
 
 
def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
 
    for i in range(n):
        if not (1 << i) & L:
            qc.x(i)
 
    if n == 1:
        qc.p(theta, n - 1)
    else:
        qc.mcp(theta, list(range(n - 1)), n - 1)
 
    for i in range(n):
        if not (1 << i) & L:
            qc.x(i)
 
    return qc