Editorial
The goal is to construct a quantum circuit that applies an gate to , conditioned on .
Based on the solution to Problem A2, it seems possible to apply an gate to only when .
So, we consider a method where we apply the gate twice when , and once when .
You can achieve this by performing the following steps in order.
- Step 1: Apply an gate to .
- Step 2: Apply an gate to only when , using the same method as in Problem A2.
Therefore, when , the following state transitions hold by using the fact that :
As a result, you can solve this problem by following these steps.
The circuit diagram for is as follows.

The circuit depth is .
Sample Code
Below is a sample program:
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import XGate
def solve(n: int) -> QuantumCircuit:
m, k = QuantumRegister(1), QuantumRegister(n)
qc = QuantumCircuit(m, k)
qc.x(m[0])
qc.x(k)
qc.compose(XGate().control(len(k)), [*k, m[0]], inplace=True)
qc.x(k)
return qc