Editorial
When applying the gates to all qubits, the zero state transitions to . In other words, can be expressed as
Due to the properties of the gate, we have
Using the properties of adjoint matrices, we can also derive
This allows us to transform as follows:
Therefore, by applying the gates to all qubits, implementing the circuit of problem B4, and applying the gate to all qubits again, we can solve this problem.
For the case where , the circuit diagram is shown below.
The Reflect_B4
in the circuit diagram indicates the circuit of problem B4.

Sample Code
Below is a sample program:
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
def reflect(n: int) -> QuantumCircuit:
qc = QuantumCircuit(n)
qc.x(range(n))
qc.append(ZGate().control(n - 1), range(n))
qc.x(range(n))
return qc
def solve(n: int) -> QuantumCircuit:
qc = QuantumCircuit(n)
qc.h(range(n))
qc.compose(reflect(n), inplace=True)
qc.h(range(n))
return qc