Calculating Hamming Distance with the IBM Q Experience José Manuel Bravo [email protected] Prof
Total Page:16
File Type:pdf, Size:1020Kb
Preprints (www.preprints.org) | NOT PEER-REVIEWED | Posted: 16 April 2018 doi:10.20944/preprints201804.0164.v2 Calculating Hamming distance with the IBM Q Experience José Manuel Bravo [email protected] Prof. Computer Science and Technology, High School, Málaga, Spain Abstract 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. Keywords: quantum algorithm, Hamming weight, Hamming distance Introduction This quantum algorithms uses quantum parallelism as the fundamental feature to evaluate The advantage of the quantum computation a function implemented in a quantum oracle for to solve more efficiently than classical many different values of x at the same time computation some algorithms is one of the most because it exploits the ability of a quantum important key for scientists to continue researching computer to be in superposition of different states. and developing news quantum algorithms. Moreover it uses a property of quantum Grover´s searching quantum algorithm is able to mechanics known as interference. In a quantum select one entry between 2푛 with output 1 in only computer it is possible for the two alternatives to O(log(n)) queries to an oracle. Shor´s factoring interfere with one another to yield some global quantum algorithms is able to factorize one property of a function f, by using the Hadamard number faster than any classical algorithms gate to recombine the different alternatives. known. Deutch-Jozsa algorithm is able to The essence of the design of this algorithm is determinate if a function is constant or balanced the choice of a function and a final transformation using only one query to a quantum oracle. that allows efficient determination of the Hamming The Hamming weight of two binary strings weight of a message (information which cannot be (messages) of equal length is the number of attained quickly on a classical computer). symbols different of the zero-symbol used in the The quantum algorithm is based on the alphabet. It represents the number of 1´s in the Deutch-Jozsa algorithm [6]. A special function of string or the digit sum of the binary representation. two binary strings is implemented in an oracle and The Hamming weight is widely used in different we only one query to calculate the Hamming disciplines [1][2], including information theory, weight is needed. coding theory, and cryptography , and his efficient IBM releases in 2016 a 5-qubit universal implementation has been widely studied [3][4][5]. quantum computer prototype accessible on the The Hamming distance in information theory cloud. To test the algorithm, two experiments have between two strings is the minimum number of been simulated on the IBM Q Experience substitutions required to change one string into the composer. other. It is used in coding theory to define the error detecting and the error correcting codes. The The Deutsch-Jozsa algorithm running time of classical algorithms is proportional to the Hamming distance or to the number of bits The goal is to find an algorithm which tells if a in the inputs. Using this quantum algorithm it´s function f is constant or balanced with the least possible to calculate the Hamming distance in only possible number of evaluations of an oracle. one query to a quantum oracle. The algorithm is divided into the following The quantum algorithm calculates the steps: Hamming weight between to binary strings. To 1. Prepare an (n+1)-qubit register in the state ⨂푛 calculate the Hamming distance of these two |Ψ0⟩ = |0⟩ ⨂|1⟩. First n qubits work as inputs we have to calculate previously the xor input qubits of the function, while the (n+1)st operation between them and after that to calculate is an ancilla qubit to store temporary the Hamming weight (hw) of the result. Suppose information. we have to calculate the Hamming distance 2. Apply the Hadamard transformation to the between the binary string A and B (hd (A,B)): register. Then we have the state: A = 1001 B = 1100 2푛−1 1 1 First we calculate A ⊕ B = 0101 and then |Ψ1⟩ = ∑ |푥⟩ ⊗ (|0⟩ − |1⟩) √2푛 √2 hw (A ⊕ B) = hd (A,B) = 2 푥=0 © 2018 by the author(s). Distributed under a Creative Commons CC BY license. Preprints (www.preprints.org) | NOT PEER-REVIEWED | Posted: 16 April 2018 doi:10.20944/preprints201804.0164.v2 3. Perform a quantum transformation Bf, 1 0 0 0 0 0 0 0 associated to a function f defined by 0 0 0 0 1 0 0 0 퐵 |푥⟩|푦⟩ = |푥⟩|푦 ⊕ 푓(푥)⟩ 0 0 0 0 0 1 0 0 f where f(x) is any function 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 푛 푓: {0,1} → {0,1} 0 0 0 0 0 0 1 0 Then we obtain the following state. 0 0 0 1 0 0 0 0 (0 0 0 0 0 0 0 1) 푛 2 −1 1 푓(푥) 1 |Ψ2⟩ = ∑ (−1) |푥⟩ ⊗ (|0⟩ − |1⟩) Then we apply the Bf one time and the √2푛 √2 푥=0 Hadamard transformations. After the n qubits are measured, we can observe: 4. Apply a Hadamard transformation on the first n qubit. ℎ푤(푎) = 2푛−1 ∙ (1 − √푃|0⟩) (1) ℎ푤(푎) = 2푛−1 ∙ √푃|2푛−1⟩ (2) 2푛−1 1 푓(푥)+푥∙푦 1 |Ψ3⟩ = ∑ (−1) |푦⟩ ⊗ (|0⟩ − |1⟩) 푛−1 2푛 √2 where 푃|2 ⟩ is the probability of measure the 푥,푦 state |2푛−1⟩ and 푃|0⟩ is the probability of measure 푥 ∙ 푦 = 푥n-1푦n-1 ⊕ 푥n-2푦n-2 ⊕ … ⊕ 푥0푦0 the state |0⟩ for any number n. As you can see if the Hamming weight tell us the number of symbols 5. The first n qubits are measured. Following different of the zero-symbol used in the alphabet, the algorithm if the probability that the the number 2푛−1 ∙ (1 − 푃|0⟩) tell us the number measurements all give outcome 0, is 1, the √ function f is constant and balanced is that of symbols equal to the zero-symbol in the string. probability is 0. Proof If we want to calculate de Hamming weight of a How to calculate the Hamming weight binary string 푠 of length k (푛 = 푙표푔2(푘) + 1), we define 푚 like the first 2푛−1 bits and 푚 the last 퐿 퐻 푛−1 The algorithm to calculate the Hamming 2 bits of the message: weight is based on the Deutsch-Jozsa algorithm so the steps are the same described above. 푚 = 푚2푛−1 ⋯ 푚0 = 푚퐻 & 푚퐿 The function f associated to the 푚퐻 = 푚2푛−1 ⋯ 푚2푛−1 transformation Bf is the following. Suppose we 푚퐿 = 푚2푛−1−1 … 푚0 = 푠푘 ⋯ 푠1 have to calculate the Hamming weight of a binary string of length k. In that case we define a function Following the algorithm 푚퐻 will be the binary from 푛 = 푙표푔2(푘) + 1 bits to 1 bit in the following string with all zero´s. Now consider the summation way: 푛 On the first 2푛−1 inputs of the function we 2 −1 encode the binary string (message) based on ∑ (−1)푓(푥)+푥∙푦|푦⟩ (3) the position of each bit on the string (message). 푥 On the last 2푛−1 inputs of the function we fix 푛−1 the binary string with all zero´s. for the state |0⟩ and |2 ⟩ in 푚퐻: Suppose we want to calculate the Hamming weight of the string a = 10. Then function f will be: ∑ (−1)0+푥∙(0⋯0)|0⟩ = 2푛−1 (4) 푙표푔2(2)+1 푓: {0,1} → {0,1} 푥∈푚퐻 Input |푥⟩ F(x) 00 0 ∑ (−1)0+푥∙(10⋯0)|2푛−1⟩ = −2푛−1 (5) 01 1 푥∈푚퐻 10 0 11 0 Now if we consider the summation (3) for the state 푛−1 |0⟩ and |2 ⟩ in 푚퐿 we will obtain the same The transformation Bf: result: 퐵f|푥⟩|푦⟩ = |푥⟩|푦 ⊕ 푓(푥)⟩ ∑ (−1)푓(푥)+푥∙(0⋯0)|0⟩ = This transformation has associated a unitary (6) 푥∈푚퐿 permutation so it can be implemented in a quantum 푛−1 = 2 − 2 ∙ ℎ푤(푚퐿) computer using a set of universal quantum gates. Preprints (www.preprints.org) | NOT PEER-REVIEWED | Posted: 16 April 2018 doi:10.20944/preprints201804.0164.v2 qubit is an ancilla qubit to store temporary information. ∑ (−1)푓(푥)+푥∙(10⋯0)|2푛−1⟩ = 4. Apply the steps 2, 3, 4 and 5 of the Deutsch- (7) 푥∈푚퐿 Jozsa algorithm. 푛−1 푛−1 = 2 − 2 ∙ ℎ푤(푚퐿) 5. The states |2 ⟩ and |0⟩ are measured: 푛−1 푛−1 where ℎ푤(푚퐿) is the Hamming weight of the hd (A,B) = hw(C) = 2 ∙ √푃|2 ⟩ substring 푚퐿. After that we consider the hd (A,B) = hw(C) = 2푛−1 ∙ (1 − √푃|0⟩) contribution of (4) and (6) to the amplitude associated with the state|0⟩ and we will obtain Below you can see the probabilities obtained for the Hamming weight (hw) for different n 1 ( 푛−1 ( ) 푛−1) values. 푛 ∙ 2 − 2 ∙ ℎ푤 푚퐿 + 2 = 2 (8) ℎ푤(푚 ) = 1 − 퐿 hw 푷|ퟎ⟩ P|ퟐ풏−ퟏ⟩ 2푛−1 0 1,00 0,00 If we consider the contribution of (5) and (7) 1 0,25 0,25 to the amplitude associated with the state|2푛−1⟩, 2 0,00 1,00 we will obtain Table 1 n=1 1 ∙ (2푛−1 − 2 ∙ ℎ푤(푚 ) − 2푛−1) = 2푛 퐿 hw 푷|ퟎ⟩ P|ퟐ풏−ퟏ⟩ −ℎ푤(푚퐿) (9) 0 1,00000 0,00000 = 푛−1 2 1 0,56250 0,06250 when the state |0⟩ is measured we will obtain: 2 0,25000 0,25000 3 0,06250 0,56250 ℎ푤(푚 ) 2 푃|0⟩ = (1 − 퐿 ) (8) 4 0,00000 1,00000 2푛−1 Table 1 n=2 from where we can easily obtain (1).