# Quantum Dilation and Erosion
## 4 Implementation on IBM QE
### Image Coding  
Here we give examples to show every detail of implementation. Suppose an image of \(8\times 8=64\) pixels, which means we need three qubits for \(Y\) and three qubits for \(X\), in total six qubits for the location states. To simplify the system, we only use one qubit for the color information \(|G\rangle\).  
Suppose an image that has a white square of \(4\times 4\) pixels in the middle, and rest of the image is black. We denote black as \(|0\rangle\) and white as \(|1\rangle\) for \(|G\rangle\), we can obtain the state of the image as  
\[|I\rangle=\frac{1}{2^{n}}\sum_{Y=0}^{2^{n}-1}\sum_{X=0}^{2^{n}-1}|G_{YX} \rangle\otimes|Y\rangle\otimes|X\rangle. \tag{13}\]  
In order to accomplish this image state, it is easy to find that the circuit in Figure 6 can construct the state \(|I\rangle\) from the initial state \(|I_{0}\rangle=\frac{1}{2^{n}}\sum_{Y=0}^{2^{n}-1}|Y\rangle\otimes\sum_{X=0}^{ 2^{n}-1}|X\rangle\).  
The \"coding\" subroutine in Figure 6 accomplishes the target image state from the initial state with all \(|0\rangle\)s. Letters in the blue blocks denote the different qubits in this multi-qubit operation, where \(\mathbf{a}\) is the qubit for color information, \(|G\rangle\); the position qubits \(|Y\rangle\) and \(|X\rangle\) consist of \(\mathbf{bcd}\) and \(\mathbf{efg}\). The diagram in the middle of Figure 6 shows the composition in quantum IBM Q experience. Its counterpart is represented in quantum circuit notation on the right. This coding process including four CNOT gates and one Toffoli gate (\"ccX\"). By the first two CNOT gates, one can find that only the states after \(|0\rangle\) in Equation (13) can cause the target qubit to be flipped, so that the \(|G\rangle\) is coded. For example, if \(|Y\rangle=|101\rangle\) and \(|X\rangle=|010\rangle\) (i.e., \(\mathbf{bcd}=101\) and \(\mathbf{efg}=010\)), the target qubit \(\mathbf{c}\) of the upper CNOT is flipped from \(|0\rangle\) to \(|1\rangle\) because its control qubit \(\mathbf{b}=|1\rangle\), and for the lower CNOT, the target qubit \(\mathbf{f}\) remains the input value \(|1\rangle\) as the control qubit \(\mathbf{e}=|0\rangle\). While qubit \(\mathbf{c}\) and \(\mathbf{f}\) are the control qubits of the following Toffoli (\"ccX\") gate, the target qubit of the gate, which is qubit \(\mathbf{a}\), will be flipped because both qubit \(\mathbf{c}\) and \(\mathbf{f}\) are \(|1\rangle\) in this case. We put the other two CNOT gates at last just to counteract the influence from the previous CNOT gates, so that after the whole subroutine the states of qubit \(\mathbf{c}\) and \(\mathbf{f}\) will remain the same (if they are flipped by the first CNOT, they will be flipped back). In this way, the coding subroutine only encodes the color information qubit \(\mathbf{a}\) while remains the position qubits unchanged.  
Figure 5: Quantum operators in the quantum scores of IBM QE. (**a**) The CNOT gate. The little dot denotes the control qubit and the larger circle with a “+” denotes the target qubit. (**b**) The “ccX” subroutine, which is actually the Toffoli gate. Letters in the blue blocks denote the different qubits in the multi-qubit operation, like the \(\mathbf{a}\) and \(\mathbf{b}\) are the control qubits and the qubit \(\mathbf{c}\) is the target qubit.