A2: Controlled Gate I

Time Limit: 3 sec

Memory Limit: 512 MiB

Score: 200

Writer: PSL

Editorial

The goal is to construct a quantum circuit that applies a Hadamard gate HH to m\ket{m}, conditioned on kn=00...0n\ket{k}_n = \ket{00 ... 0}_n.

So, how can we apply a Hadamard gate HH to m\ket{m} only when kn=00...0n\ket{k}_n = \ket{00 ... 0}_n?

You can achieve this by performing the following steps in order.

  • Step 1: Apply an XX gate to each qubit in kn=k0k1...kn1n\ket{k}_n = \ket{k_0 k_1 ... k_{n - 1}}_n.
  • Step 2: Apply a multi-controlled Hadamard gate1 with k0k1...kn1n\ket{k_0 k_1 ... k_{n - 1}}_n as the control qubits and m\ket{m} as the target qubit.
  • Step 3: Apply the inverse of Step 1 to restore kn\ket{k}_n to its original state.

Therefore, when kn=00...0n\ket{k}_n = \ket{00 ... 0}_n, the following state transitions hold:

m00...0nStep 1 m11...1nStep 2 Hm11...1nStep 3 Hm00...0n\begin{align} \ket{m}\ket{00 ... 0}_n &\xrightarrow{\text{Step 1 }} \ket{m}\ket{11 ... 1}_n \\ &\xrightarrow{\text{Step 2 }} H \ket{m}\ket{11 ... 1}_n \\ &\xrightarrow{\text{Step 3 }} H \ket{m}\ket{00 ... 0}_n \end{align}

As a result, you can solve this problem by following these steps.

The circuit diagram for n=3n = 3 is as follows.

The circuit depth is 33.

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 11, but 00 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

Footnotes

  1. In Qiskit, multiple controlled gates can be implemented using the control method of each gate class.