# Quantum Dilation and Erosion
## 4 Implementation on IBM QE
### Dilation and Erosion  
After all of the preparatory work, now we can implement the dilation and erosion operations and obtain the result. Recall that the dilation is in fact the OR operation and the erosion is the AND operation; by applying the quantum OR and AND gates (Figure 3) we can obtain the results of the dilation and erosion, as shown in Figure 10.  
Figure 8: The “add 1” subroutine, to add one to a three-bit binary number. The number is denoted by the qubits as \((\mathbf{abc})_{\mathbf{b}}\). As explained before (Figure 5b), the “ccX” gate is the Toffoli gate in IBM quantum score with \(\mathbf{a}\) and \(\mathbf{c}\) are control qubits while \(\mathbf{c}\) is the target. The green X gate is the quantum NOT gate, which simply flips the quantum state.  
Figure 9: The “sub 1” subroutine, to subtract one from a three-bit binary number. The number is denoted by the qubits as \((\mathbf{abc})_{\mathbf{b}}\). The “ccX” gate is the Toffoli gate (Figure 5b). Where the qubits denoted by “a” and “b” are control qubits while “c” is the target.  
In the quantum circuit, we use qubit \(q[1]\) for \(|G_{Y,X}\rangle\), \(q[0]\) and \(q[2]\) for \(|G_{Y-1,X-1}\rangle\)and \(|G_{Y+1,X+1}\rangle\), \(q[3]q[4]q[5]\) for \(|Y\rangle\), \(q[6]q[7]q[8]\) for \(|X\rangle\) and \(q[9]\) for the result \(|O\rangle\). To the far left side, the series of Hadamard gates for \(|Y\rangle\) and \(|X\rangle\) convert the all \(|0\rangle\) state to \(|I_{0}\rangle=\frac{1}{2^{n}}\sum_{Y=0}^{2^{n}-1}|Y\rangle\otimes\sum_{X=0}^{ 2^{n}-1}|X\rangle\), so as to prepare the initial state of the qubits. At this moment, the position states \(|Y\rangle\) and \(|X\rangle\) have not entangled with the color information state \(|G\rangle\). The second part of the implementation is the coding subroutine, as discussed in Section 4.2. Through this operation the color information state \(|G\rangle\) is coded according to the image information. After this step the quantum state of the whole image is obtained.  
As we have stated in Section 3.7, we cannot use the same state for different shifting. So, when we use three different states to store the two shifted images, they share the same position qubits. In the third and fourth parts of the circuit the \(|G_{Y-1,X-1}\rangle\) and \(|G_{Y+1,X+1}\rangle\) are prepared. After all of the three states are prepared, we apply quantum OR or AND operations to accomplish the dilation and erosion processes, and then measure the results.  
Figure 10: The circuits for dilation and erosion operation to the image (Equation (13)) by the structure element shown by Figure 7. The qubit \(q[1]\) is for \(|G_{Y,X}\rangle\), \(q[0]\) for \(|G_{Y-1,X-1}\rangle\), \(q[2]\) for \(|G_{Y,X}\rangle\), \(q[3]q[4]q[5]\) for \(|Y\rangle\), \(q[6]q[7]q[8]\) for \(|X\rangle\) and \(q[9]\) for the result \(|O\rangle\). To the very left side the Hadamard gates for \(|Y\rangle\) and \(|X\rangle\) convert the all \(|0\rangle\) state to \(|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 can code the \(|G_{Y,X}\rangle\) by the location states while retain the quantum states for position qubits after the operation. The blue blocks with “H” are the Hadamard gates, and the yellow blocks with “id” are identical operations that actually do nothing to the qubits. They are put there only for the symmetry of the circuit considering the possible errors from applying operations.The subroutines “coding”, “add1” and “sub1” are discussed above. (See Figures 6, 8, and 9). The “cccx” subroutine is simply a NOT gate with three control qubits, which is one more than a normal Toffoli gate, where \(\mathbf{a}\)\(\sim\)\(\mathbf{c}\) are control qubits and \(\mathbf{d}\) is the target. The pink blocks denote the measurements, where we can see the grey arrows showing that information is passed to classical bits. Then we can get the result \(|O\rangle\) in \(q[9]\) which is read out in the classical bit 6.