Editorial
The goal is to construct a quantum circuit that applies a Hadamard gate to , conditioned on .
So, how can we apply a Hadamard gate to only when ?
You can achieve this by performing the following steps in order.
- Step 1: Apply an gate to each qubit in .
- Step 2: Apply a multi-controlled Hadamard gate1 with as the control qubits and as the target qubit.
- Step 3: Apply the inverse of Step 1 to restore to its original state.
Therefore, when , the following state transitions hold:
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 HGate
def solve(n: int) -> QuantumCircuit:
m, k = QuantumRegister(1), QuantumRegister(n)
qc = QuantumCircuit(m, k)
qc.x(k)
qc.compose(HGate().control(len(k)), [*k, m[0]], inplace=True)
qc.x(k)
return qc
The control gate is usually enabled when the control bit is set to , but can also be used.
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import HGate
def solve(n: int) -> QuantumCircuit:
m, k = QuantumRegister(1), QuantumRegister(n)
qc = QuantumCircuit(m, k)
qc.compose(HGate().control(len(k), ctrl_state=0), [*k, m[0]], inplace=True)
return qc