From 931a9dde69be91117448a189165da77c2d8d669e Mon Sep 17 00:00:00 2001 From: Abhishek Jaisingh Date: Sat, 17 Oct 2020 17:37:05 +0530 Subject: [PATCH 1/4] Implement Deutsch-Jozsa Algorithm In Qiskit Signed-off-by: Abhishek Jaisingh --- quantum/deutsch_jozsa.py | 125 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100755 quantum/deutsch_jozsa.py diff --git a/quantum/deutsch_jozsa.py b/quantum/deutsch_jozsa.py new file mode 100755 index 000000000000..bb7cc33c3e05 --- /dev/null +++ b/quantum/deutsch_jozsa.py @@ -0,0 +1,125 @@ +#!/usr/bin/env python3 +""" +Deutsch-Josza Algorithm is one of the first examples of a quantum +algorithm that is exponentially faster than any possible deterministic +classical algorithm + +Premise: +We are given a hidden Boolean function f , +which takes as input a string of bits, and returns either 0 or 1: + +f({x0,x1,x2,...}) -> 0 or 1 , where xn is 0 or 1 + +The property of the given Boolean function is that it is guaranteed to +either be balanced or constant. A constant function returns all 0 's +or all 1 's for any input, while a balanced function returns 0 's for +exactly half of all inputs and 1 's for the other half. Our task is to +determine whether the given function is balanced or constant. + +References: +- https://en.wikipedia.org/wiki/Deutsch-Jozsa_algorithm +- https://qiskit.org/textbook/ch-algorithms/deutsch-jozsa.html +""" + +import numpy as np +import qiskit as q + + +def dj_oracle(case: str, n: int) -> q.QuantumCircuit: + """ + Returns a Quantum Circuit for the Oracle function. + The circuit returned can represent balanced or constant function, + according to the arguments passed + """ + # This circuit has n+1 qubits: the size of the input, + # plus one output qubit + oracle_qc = q.QuantumCircuit(n+1) + + # First, let's deal with the case in which oracle is balanced + if case == "balanced": + # First generate a random number that tells us which CNOTs to + # wrap in X-gates: + b = np.random.randint(1,2**n) + # Next, format 'b' as a binary string of length 'n', padded with zeros: + b_str = format(b, '0'+str(n)+'b') + # Next, we place the first X-gates. Each digit in our binary string + # correspopnds to a qubit, if the digit is 0, we do nothing, if it's 1 + # we apply an X-gate to that qubit: + for qubit in range(len(b_str)): + if b_str[qubit] == '1': + oracle_qc.x(qubit) + # Do the controlled-NOT gates for each qubit, using the output qubit + # as the target: + for qubit in range(n): + oracle_qc.cx(qubit, n) + # Next, place the final X-gates + for qubit in range(len(b_str)): + if b_str[qubit] == '1': + oracle_qc.x(qubit) + + # Case in which oracle is constant + if case == "constant": + # First decide what the fixed output of the oracle will be + # (either always 0 or always 1) + output = np.random.randint(2) + if output == 1: + oracle_qc.x(n) + + oracle_gate = oracle_qc.to_gate() + oracle_gate.name = "Oracle" # To show when we display the circuit + return oracle_gate + + +def dj_algorithm(oracle: q.QuantumCircuit, n: int) -> q.QuantumCircuit: + """ + Returns the complete Deustch-Jozsa Quantum Circuit, + adding Input & Output registers and Hadamard & Measurement Gates, + to the Oracle Circuit passed in arguments + """ + dj_circuit = q.QuantumCircuit(n+1, n) + # Set up the output qubit: + dj_circuit.x(n) + dj_circuit.h(n) + # And set up the input register: + for qubit in range(n): + dj_circuit.h(qubit) + # Let's append the oracle gate to our circuit: + dj_circuit.append(oracle, range(n+1)) + # Finally, perform the H-gates again and measure: + for qubit in range(n): + dj_circuit.h(qubit) + + for i in range(n): + dj_circuit.measure(i, i) + + return dj_circuit + + +def deutsch_jozsa(case: str, n: int) -> q.result.counts.Counts: + """ + Main function that builds the circuit using other helper functions, + runs the experiment 1000 times & returns the resultant qubit counts + >>> deutsch_jozsa("constant", 3) + {'000': 1000} + >>> deutsch_jozsa("balanced", 3) + {'111': 1000} + """ + # Use Aer's qasm_simulator + simulator = q.Aer.get_backend("qasm_simulator") + + oracle_gate = dj_oracle(case, n) + dj_circuit = dj_algorithm(oracle_gate, n) + + # Execute the circuit on the qasm simulator + job = q.execute(dj_circuit, simulator, shots=1000) + + # Return the histogram data of the results of the experiment. + return job.result().get_counts(dj_circuit) + + +if __name__ == "__main__": + counts = deutsch_jozsa("constant", 3) + print(f"Deutsch Jozsa - Constant Oracle: {counts}") + + counts = deutsch_jozsa("balanced", 3) + print(f"Deutsch Jozsa - Balanced Oracle: {counts}") From 4e2a95b1741988764891e6bf358a68664b338146 Mon Sep 17 00:00:00 2001 From: Abhishek Jaisingh Date: Sat, 17 Oct 2020 19:11:00 +0530 Subject: [PATCH 2/4] Add Changes Requested In Review Signed-off-by: Abhishek Jaisingh --- quantum/deutsch_jozsa.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/quantum/deutsch_jozsa.py b/quantum/deutsch_jozsa.py index bb7cc33c3e05..8193948cc934 100755 --- a/quantum/deutsch_jozsa.py +++ b/quantum/deutsch_jozsa.py @@ -5,15 +5,15 @@ classical algorithm Premise: -We are given a hidden Boolean function f , -which takes as input a string of bits, and returns either 0 or 1: +We are given a hidden Boolean function f, +which takes as input a string of bits, and returns either 0 or 1: -f({x0,x1,x2,...}) -> 0 or 1 , where xn is 0 or 1 +f({x0,x1,x2,...}) -> 0 or 1, where xn is 0 or 1 The property of the given Boolean function is that it is guaranteed to -either be balanced or constant. A constant function returns all 0 's -or all 1 's for any input, while a balanced function returns 0 's for -exactly half of all inputs and 1 's for the other half. Our task is to +either be balanced or constant. A constant function returns all 0's +or all 1's for any input, while a balanced function returns 0's for +exactly half of all inputs and 1's for the other half. Our task is to determine whether the given function is balanced or constant. References: From 1a33ec4b33005c7e359f0d5ffd03dbb5451e31c3 Mon Sep 17 00:00:00 2001 From: Abhishek Jaisingh Date: Sun, 18 Oct 2020 20:12:16 +0530 Subject: [PATCH 3/4] Address Further Review Comments --- quantum/deutsch_jozsa.py | 57 +++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/quantum/deutsch_jozsa.py b/quantum/deutsch_jozsa.py index 8193948cc934..e0a1ac216cbf 100755 --- a/quantum/deutsch_jozsa.py +++ b/quantum/deutsch_jozsa.py @@ -25,37 +25,37 @@ import qiskit as q -def dj_oracle(case: str, n: int) -> q.QuantumCircuit: +def dj_oracle(case: str, num_qubits: int) -> q.QuantumCircuit: """ Returns a Quantum Circuit for the Oracle function. The circuit returned can represent balanced or constant function, according to the arguments passed """ - # This circuit has n+1 qubits: the size of the input, + # This circuit has num_qubits+1 qubits: the size of the input, # plus one output qubit - oracle_qc = q.QuantumCircuit(n+1) + oracle_qc = q.QuantumCircuit(num_qubits+1) # First, let's deal with the case in which oracle is balanced if case == "balanced": # First generate a random number that tells us which CNOTs to # wrap in X-gates: - b = np.random.randint(1,2**n) + b = np.random.randint(1,2**num_qubits) # Next, format 'b' as a binary string of length 'n', padded with zeros: - b_str = format(b, '0'+str(n)+'b') + b_str = format(b, f"0{num_qubits}b") # Next, we place the first X-gates. Each digit in our binary string # correspopnds to a qubit, if the digit is 0, we do nothing, if it's 1 # we apply an X-gate to that qubit: - for qubit in range(len(b_str)): - if b_str[qubit] == '1': - oracle_qc.x(qubit) + for index, bit in enumerate(b_str): + if bit == '1': + oracle_qc.x(index) # Do the controlled-NOT gates for each qubit, using the output qubit # as the target: - for qubit in range(n): - oracle_qc.cx(qubit, n) + for index in range(num_qubits): + oracle_qc.cx(index, num_qubits) # Next, place the final X-gates - for qubit in range(len(b_str)): - if b_str[qubit] == '1': - oracle_qc.x(qubit) + for index, bit in enumerate(b_str): + if bit == '1': + oracle_qc.x(index) # Case in which oracle is constant if case == "constant": @@ -63,39 +63,39 @@ def dj_oracle(case: str, n: int) -> q.QuantumCircuit: # (either always 0 or always 1) output = np.random.randint(2) if output == 1: - oracle_qc.x(n) + oracle_qc.x(num_qubits) oracle_gate = oracle_qc.to_gate() oracle_gate.name = "Oracle" # To show when we display the circuit return oracle_gate -def dj_algorithm(oracle: q.QuantumCircuit, n: int) -> q.QuantumCircuit: +def dj_algorithm(oracle: q.QuantumCircuit, num_qubits: int) -> q.QuantumCircuit: """ Returns the complete Deustch-Jozsa Quantum Circuit, adding Input & Output registers and Hadamard & Measurement Gates, to the Oracle Circuit passed in arguments """ - dj_circuit = q.QuantumCircuit(n+1, n) + dj_circuit = q.QuantumCircuit(num_qubits+1, num_qubits) # Set up the output qubit: - dj_circuit.x(n) - dj_circuit.h(n) + dj_circuit.x(num_qubits) + dj_circuit.h(num_qubits) # And set up the input register: - for qubit in range(n): + for qubit in range(num_qubits): dj_circuit.h(qubit) # Let's append the oracle gate to our circuit: - dj_circuit.append(oracle, range(n+1)) + dj_circuit.append(oracle, range(num_qubits+1)) # Finally, perform the H-gates again and measure: - for qubit in range(n): + for qubit in range(num_qubits): dj_circuit.h(qubit) - for i in range(n): + for i in range(num_qubits): dj_circuit.measure(i, i) return dj_circuit -def deutsch_jozsa(case: str, n: int) -> q.result.counts.Counts: +def deutsch_jozsa(case: str, num_qubits: int) -> q.result.counts.Counts: """ Main function that builds the circuit using other helper functions, runs the experiment 1000 times & returns the resultant qubit counts @@ -107,8 +107,8 @@ def deutsch_jozsa(case: str, n: int) -> q.result.counts.Counts: # Use Aer's qasm_simulator simulator = q.Aer.get_backend("qasm_simulator") - oracle_gate = dj_oracle(case, n) - dj_circuit = dj_algorithm(oracle_gate, n) + oracle_gate = dj_oracle(case, num_qubits) + dj_circuit = dj_algorithm(oracle_gate, num_qubits) # Execute the circuit on the qasm simulator job = q.execute(dj_circuit, simulator, shots=1000) @@ -118,8 +118,5 @@ def deutsch_jozsa(case: str, n: int) -> q.result.counts.Counts: if __name__ == "__main__": - counts = deutsch_jozsa("constant", 3) - print(f"Deutsch Jozsa - Constant Oracle: {counts}") - - counts = deutsch_jozsa("balanced", 3) - print(f"Deutsch Jozsa - Balanced Oracle: {counts}") + print(f"Deutsch Jozsa - Constant Oracle: {deutsch_jozsa('constant', 3)}") + print(f"Deutsch Jozsa - Balanced Oracle: {deutsch_jozsa('balanced', 3)}") From c818db884cf4d3285accaaceb8ffddc4c0059ad1 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 18 Oct 2020 14:43:43 +0000 Subject: [PATCH 4/4] fixup! Format Python code with psf/black push --- quantum/deutsch_jozsa.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/quantum/deutsch_jozsa.py b/quantum/deutsch_jozsa.py index e0a1ac216cbf..da1b6e4e9434 100755 --- a/quantum/deutsch_jozsa.py +++ b/quantum/deutsch_jozsa.py @@ -33,28 +33,28 @@ def dj_oracle(case: str, num_qubits: int) -> q.QuantumCircuit: """ # This circuit has num_qubits+1 qubits: the size of the input, # plus one output qubit - oracle_qc = q.QuantumCircuit(num_qubits+1) - + oracle_qc = q.QuantumCircuit(num_qubits + 1) + # First, let's deal with the case in which oracle is balanced if case == "balanced": # First generate a random number that tells us which CNOTs to # wrap in X-gates: - b = np.random.randint(1,2**num_qubits) + b = np.random.randint(1, 2 ** num_qubits) # Next, format 'b' as a binary string of length 'n', padded with zeros: - b_str = format(b, f"0{num_qubits}b") - # Next, we place the first X-gates. Each digit in our binary string + b_str = format(b, f"0{num_qubits}b") + # Next, we place the first X-gates. Each digit in our binary string # correspopnds to a qubit, if the digit is 0, we do nothing, if it's 1 # we apply an X-gate to that qubit: for index, bit in enumerate(b_str): - if bit == '1': + if bit == "1": oracle_qc.x(index) - # Do the controlled-NOT gates for each qubit, using the output qubit + # Do the controlled-NOT gates for each qubit, using the output qubit # as the target: for index in range(num_qubits): oracle_qc.cx(index, num_qubits) # Next, place the final X-gates for index, bit in enumerate(b_str): - if bit == '1': + if bit == "1": oracle_qc.x(index) # Case in which oracle is constant @@ -64,19 +64,19 @@ def dj_oracle(case: str, num_qubits: int) -> q.QuantumCircuit: output = np.random.randint(2) if output == 1: oracle_qc.x(num_qubits) - + oracle_gate = oracle_qc.to_gate() - oracle_gate.name = "Oracle" # To show when we display the circuit + oracle_gate.name = "Oracle" # To show when we display the circuit return oracle_gate def dj_algorithm(oracle: q.QuantumCircuit, num_qubits: int) -> q.QuantumCircuit: """ Returns the complete Deustch-Jozsa Quantum Circuit, - adding Input & Output registers and Hadamard & Measurement Gates, + adding Input & Output registers and Hadamard & Measurement Gates, to the Oracle Circuit passed in arguments """ - dj_circuit = q.QuantumCircuit(num_qubits+1, num_qubits) + dj_circuit = q.QuantumCircuit(num_qubits + 1, num_qubits) # Set up the output qubit: dj_circuit.x(num_qubits) dj_circuit.h(num_qubits) @@ -84,20 +84,20 @@ def dj_algorithm(oracle: q.QuantumCircuit, num_qubits: int) -> q.QuantumCircuit: for qubit in range(num_qubits): dj_circuit.h(qubit) # Let's append the oracle gate to our circuit: - dj_circuit.append(oracle, range(num_qubits+1)) + dj_circuit.append(oracle, range(num_qubits + 1)) # Finally, perform the H-gates again and measure: for qubit in range(num_qubits): dj_circuit.h(qubit) - + for i in range(num_qubits): dj_circuit.measure(i, i) - + return dj_circuit def deutsch_jozsa(case: str, num_qubits: int) -> q.result.counts.Counts: """ - Main function that builds the circuit using other helper functions, + Main function that builds the circuit using other helper functions, runs the experiment 1000 times & returns the resultant qubit counts >>> deutsch_jozsa("constant", 3) {'000': 1000}