A3: Controlled Gate II

Time Limit: 3 sec

Memory Limit: 512 MiB

Score: 200

Writer: PSL

Editorial

The goal is to construct a quantum circuit that applies an XX gate to m\ket{m}, conditioned on kn00...0n\ket{k}_n \neq \ket{00 ... 0}_n.

Based on the solution to Problem A2, it seems possible to apply an XX gate to m\ket{m} only when kn=00...0n\ket{k}_n = \ket{00 ... 0}_n.

So, we consider a method where we apply the XX gate twice when kn=00...0n\ket{k}_n = \ket{00 ... 0}_n, and once when kn00...0n\ket{k}_n \neq \ket{00 ... 0}_n.

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

  • Step 1: Apply an XX gate to m\ket{m}.
  • Step 2: Apply an XX gate to m\ket{m} only when kn=00...0n\ket{k}_n = \ket{00 ... 0}_n, using the same method as in Problem A2.

Therefore, when kn=00...0n\ket{k}_n = \ket{00 ... 0}_n, the following state transitions hold by using the fact that XX=IXX = I:

m00...0nStep 1 Xm00...0nStep 2 XXm00...0n=m00...0n\begin{align} \ket{m}\ket{00 ... 0}_n \xrightarrow{\text{Step 1 }} &X \ket{m}\ket{00 ... 0}_n \\ \xrightarrow{\text{Step 2 }} &X X \ket{m}\ket{00 ... 0}_n \\ = &\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 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