Editorial
This problem involves implementing a quantum circuit that applies a phase shift gate exclusively to a specific computational basis state .
So, how can we apply the phase shift gate only to the specific computational basis state within a superposition?
This can be achieved with the following steps:
- For , apply the gate at index where . This transforms into .
- Apply a multiple-control phase shift gate, using bits as control bits and the remaining bit as the target bit. In this case, the phase shift gate acts only on the computational basis state within the superposition.
- Perform the inverse of step 1 to transform back to .
The circuit diagram for the case where and is shown as follows.

The circuit depth is .
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