11#!/usr/bin/env python3
22"""
3- Deutsch-Josza Algorithm is one of the first examples of a quantum
3+ Deutsch-Jozsa Algorithm is one of the first examples of a quantum
44algorithm that is exponentially faster than any possible deterministic
55classical algorithm
66
2222"""
2323
2424import numpy as np
25- import qiskit as q
25+ import qiskit
2626
2727
28- def dj_oracle (case : str , num_qubits : int ) -> q .QuantumCircuit :
28+ def dj_oracle (case : str , num_qubits : int ) -> qiskit .QuantumCircuit :
2929 """
3030 Returns a Quantum Circuit for the Oracle function.
3131 The circuit returned can represent balanced or constant function,
3232 according to the arguments passed
3333 """
3434 # This circuit has num_qubits+1 qubits: the size of the input,
3535 # plus one output qubit
36- oracle_qc = q .QuantumCircuit (num_qubits + 1 )
36+ oracle_qc = qiskit .QuantumCircuit (num_qubits + 1 )
3737
3838 # First, let's deal with the case in which oracle is balanced
3939 if case == "balanced" :
@@ -43,7 +43,7 @@ def dj_oracle(case: str, num_qubits: int) -> q.QuantumCircuit:
4343 # Next, format 'b' as a binary string of length 'n', padded with zeros:
4444 b_str = format (b , f"0{ num_qubits } b" )
4545 # Next, we place the first X-gates. Each digit in our binary string
46- # correspopnds to a qubit, if the digit is 0, we do nothing, if it's 1
46+ # corresponds to a qubit, if the digit is 0, we do nothing, if it's 1
4747 # we apply an X-gate to that qubit:
4848 for index , bit in enumerate (b_str ):
4949 if bit == "1" :
@@ -70,13 +70,15 @@ def dj_oracle(case: str, num_qubits: int) -> q.QuantumCircuit:
7070 return oracle_gate
7171
7272
73- def dj_algorithm (oracle : q .QuantumCircuit , num_qubits : int ) -> q .QuantumCircuit :
73+ def dj_algorithm (
74+ oracle : qiskit .QuantumCircuit , num_qubits : int
75+ ) -> qiskit .QuantumCircuit :
7476 """
75- Returns the complete Deustch -Jozsa Quantum Circuit,
77+ Returns the complete Deutsch -Jozsa Quantum Circuit,
7678 adding Input & Output registers and Hadamard & Measurement Gates,
7779 to the Oracle Circuit passed in arguments
7880 """
79- dj_circuit = q .QuantumCircuit (num_qubits + 1 , num_qubits )
81+ dj_circuit = qiskit .QuantumCircuit (num_qubits + 1 , num_qubits )
8082 # Set up the output qubit:
8183 dj_circuit .x (num_qubits )
8284 dj_circuit .h (num_qubits )
@@ -95,7 +97,7 @@ def dj_algorithm(oracle: q.QuantumCircuit, num_qubits: int) -> q.QuantumCircuit:
9597 return dj_circuit
9698
9799
98- def deutsch_jozsa (case : str , num_qubits : int ) -> q .result .counts .Counts :
100+ def deutsch_jozsa (case : str , num_qubits : int ) -> qiskit .result .counts .Counts :
99101 """
100102 Main function that builds the circuit using other helper functions,
101103 runs the experiment 1000 times & returns the resultant qubit counts
@@ -104,14 +106,14 @@ def deutsch_jozsa(case: str, num_qubits: int) -> q.result.counts.Counts:
104106 >>> deutsch_jozsa("balanced", 3)
105107 {'111': 1000}
106108 """
107- # Use Aer's qasm_simulator
108- simulator = q .Aer .get_backend ("qasm_simulator " )
109+ # Use Aer's simulator
110+ simulator = qiskit .Aer .get_backend ("aer_simulator " )
109111
110112 oracle_gate = dj_oracle (case , num_qubits )
111113 dj_circuit = dj_algorithm (oracle_gate , num_qubits )
112114
113- # Execute the circuit on the qasm simulator
114- job = q .execute (dj_circuit , simulator , shots = 1000 )
115+ # Execute the circuit on the simulator
116+ job = qiskit .execute (dj_circuit , simulator , shots = 1000 )
115117
116118 # Return the histogram data of the results of the experiment.
117119 return job .result ().get_counts (dj_circuit )
0 commit comments