Calculating Hamming distance with the IBM Q Experience

In this paper a quantum algorithm to calculate the Hamming distance of two binary strings of equal length (or messages in theory information) is presented. The algorithm calculates the Hamming weight of two binary strings in one query of an oracle. To calculate the hamming distance of these two strings we only have to calculate the Hamming weight of the xor operation of both strings. To test the algorithms the quantum computer prototype that IBM has given open access to on the cloud has been used to test the results.


Introduction
The advantage of the quantum computation to solve more efficiently than classical computation some algorithms is one of the most important key for scientists to continue researching and developing news quantum algorithms.Grover´s searching quantum algorithm is able to select one entry between 2  with output 1 in only O(log(n)) queries to an oracle.Shor´s factoring quantum algorithms is able to factorize one number faster than any classical algorithms known.Deutch-Jozsa algorithm is able to determinate if a function is constant or balanced using only one query to a quantum oracle.
The This quantum algorithms uses quantum parallelism as the fundamental feature to evaluate a function implemented in a quantum oracle for many different values of x at the same time because it exploits the ability of a quantum computer to be in superposition of different states.
Moreover it uses a property of quantum mechanics known as interference.In a quantum computer it is possible for the two alternatives to interfere with one another to yield some global property of a function f, by using the Hadamard gate to recombine the different alternatives.
The essence of the design of this algorithm is the choice of a function and a final transformation that allows efficient determination of the Hamming weight of a message (information which cannot be attained quickly on a classical computer).
The quantum algorithm is based on the Deutch-Jozsa algorithm [6].A special function of two binary strings is implemented in an oracle and we only one query to calculate the Hamming weight is needed.
IBM releases in 2016 a 5-qubit universal quantum computer prototype accessible on the cloud.To test the algorithm, two experiments have been simulated on the IBM Q Experience composer.

The Deutsch-Jozsa algorithm
The goal is to find an algorithm which tells if a function f is constant or balanced with the least possible number of evaluations of an oracle.
The algorithm is divided into the following steps: 1. Prepare an (n+1)-qubit register in the state |Ψ 0 ⟩ = |0⟩ ⨂ ⨂|1⟩.First n qubits work as input qubits of the function, while the (n+1)st is an ancilla qubit to store temporary information.2. Apply the Hadamard transformation to the register.Then we have the state: 3. Perform a quantum transformation Bf, associated to a function f defined by  f |⟩|⟩ = |⟩| ⊕ ()⟩ where f(x) is any function : {0,1}  → {0,1} Then we obtain the following state.
4. Apply a Hadamard transformation on the first n qubit.
5. The first n qubits are measured.Following the algorithm if the probability that the measurements all give outcome 0, is 1, the function f is constant and balanced is that probability is 0.

How to calculate the Hamming weight
The algorithm to calculate the Hamming weight is based on the Deutsch-Jozsa algorithm so the steps are the same described above.
The function f associated to the transformation Bf is the following.Suppose we have to calculate the Hamming weight of a binary string of length k.In that case we define a function from  =  2 () + 1 bits to 1 bit in the following way:  On the first 2 −1 inputs of the function we encode the binary string (message) based on the position of each bit on the string (message). On the last 2 −1 inputs of the function we fix the binary string with all zero´s.Suppose we want to calculate the Hamming weight of the string a = 10.Then function f will be: The transformation Bf: This transformation has associated a unitary permutation so it can be implemented in a quantum computer using a set of universal quantum gates.
Then we apply the Bf one time and the Hadamard transformations.After the n qubits are measured, we can observe: where Following the algorithm   will be the binary string with all zero´s.Now consider the summation for the state |0⟩ and |2 −1 ⟩ in   : Now if we consider the summation (3) for the state |0⟩ and |2 −1 ⟩ in   we will obtain the same result: where ℎ(  ) is the Hamming weight of the substring   .After that we consider the contribution of ( 4) and ( 6) to the amplitude associated with the state|0⟩ and we will obtain If we consider the contribution of ( 5) and ( 7) to the amplitude associated with the state|2 −1 ⟩, we will obtain when the state |0⟩ is measured we will obtain: from where we can easily obtain (1).And if the state |2 −1 ⟩ is measured we will obtain: from where we can easily obtain (2).

Algorithm to calculate the Hamming distance
Suppose we have to calculate the Hamming distance of two binary strings A y B of length k.The steps are the following: that encodes the binary string C in the first n/2 inputs and the binary string  ´ in the last n/2 inputs.

Prepare an (n+1)-qubit register in the state
|Ψ 0 ⟩ = |0⟩ ⨂ ⨂|1⟩.First n qubits work as input qubits of the function, while the (n+1)st qubit is an ancilla qubit to store temporary information.4. Apply the steps 2, 3, 4 and 5 of the Deutsch-Jozsa algorithm.5.The states |2 −1 ⟩ and |0⟩ are measured: Below you can see the probabilities obtained for the Hamming weight (hw) for different n values.

Comparing the Hamming weight of two binary strings
Suppose that we are given two binary strings ,  of equal length .The goal is to determinate if both strings have the same Hamming weight.Classically  queries are necessary and sufficient to solve the problem.Using this quantum algorithm, one query will be sufficient to solve it.

Proof
If we want to determinate if two binary strings ,  of equal length  have the same Hamming weight ( =  2 () + 2), we define   like the first 2 −1 bits and   the last 2 −1 bits of the message: Following the algorithm   will be "ancilla" string: n/4 bits all one´s and n/4 bits all zero´s.Now consider the summation (3) for the state |2 −2 ⟩ and |2 −2 + 2 −1 ⟩ in   : Now if we consider again the summation (3) for the state |2 −2 ⟩ and |2 −2 + 2 −1 ⟩ in   we will obtain if the Hamming weight of binary strings ,  are equals because the coefficients vanishes for half string being  and the other half  with the same numbers of zero´s and one´s.Otherwise these summation will not be zero.
If we consider the contribution of ( 11) and (13) to the amplitude associated with the state |2 −2 ⟩, we will obtain And if we consider the contribution of ( 12) and ( 14) to the amplitude associated with the state When the state |2 −2 ⟩ and |2 −2 + 2 −1 ⟩ are measured we will obtain: Moreover if ℎ(, ) = 0 (Hamming distance) we will obtain all outcomes 0 when we measure the following states:

Simulations on IBM Quantum Experience
Experiment 1(figure 1): calculate de Hamming distance of the following strings:  = 10100101  = 01100101 In this case n=4.The first step is to calculate  =  ⊕  = 11000000 and to define the function f   After define the transformation Bf and the oracle, the quantum circuit is showing below.
The results we obtain after 8192 shots that you can see below on figure 4, are: As we can see the probability of the states |4⟩ and |12⟩ are 0.25 so we can say that both strings have the same Hamming length and because (18) the outcomes of the states |5⟩|6⟩|7⟩ are all zero´s we can say too that both strings are equals.
Hamming weight of two binary strings (messages) of equal length is the number of symbols different of the zero-symbol used in the alphabet.It represents the number of 1´s in the string or the digit sum of the binary representation.The Hamming weight is widely used in different disciplines [1][2], including information theory, coding theory, and cryptography , and his efficient implementation has been widely studied [3][4][5].The Hamming distance in information theory between two strings is the minimum number of substitutions required to change one string into the other.It is used in coding theory to define the error detecting and the error correcting codes.The running time of classical algorithms is proportional to the Hamming distance or to the number of bits in the inputs.Using this quantum algorithm it´s possible to calculate the Hamming distance in only one query to a quantum oracle.The quantum algorithm calculates the Hamming weight between to binary strings.To calculate the Hamming distance of these two inputs we have to calculate previously the xor operation between them and after that to calculate the Hamming weight (hw) of the result.Suppose we have to calculate the Hamming distance between the binary string A and B (hd (A,B)): A = 1001 B = 1100 First we calculate A ⊕ B = 0101 and then hw (A ⊕ B) = hd (A,B) = 2

Figure 1
Figure 1 Quantum circuit to calculate de Hamming distance of  = 10100101   = 01100101

Figure 2
Figure 2 Quantum circuit to calculate de Hamming distance of  = 1001   = 0001

Figure 3 Figure 4
Figure 3 Quantum circuit to compare de Hamming weight of  = 1010   = 1010 is the probability of measure the state |2 −1 ⟩ and |0⟩ is the probability of measure the state |0⟩ for any number n.As you can see if the Hamming weight tell us the number of symbols different of the zero-symbol used in the alphabet, the number 2 −1 • (1 − √|0⟩) tell us the number of symbols equal to the zero-symbol in the string.
ProofIf we want to calculate de Hamming weight of a binary string  of length k ( =  2 () + 1), we define   like the first 2 −1 bits and   the last 2 −1 bits of the message: