Remove references to depreciated QasmSimulator (#7417)

* Fix typos

* Replace depreciated QasmSimulator in Deutsch-Jozsa algorithm

* Replace depreciated QasmSimulator in half adder algorithm

* Replace depreciated QasmSimulator in not gate algorithm

* Replace depreciated QasmSimulator in full adder algorithm

* Simplify qiskit import

* Make formatting more consistent

* Replace depreciated QasmSimulator in quantum entanglement algorithm

* Replace depreciated QasmSimulator in ripple adder algorithm

* Replace depreciated QasmSimulator in qubit measure algorithm

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* updating DIRECTORY.md

* updating DIRECTORY.md

* Remove qiskit import alias for clarity

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Tianyi Zheng 2022-10-19 16:12:44 -04:00 committed by GitHub
parent 50da472ddc
commit 2859d4bf3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 67 additions and 58 deletions

View File

@ -27,6 +27,7 @@
* [Hamiltonian Cycle](backtracking/hamiltonian_cycle.py) * [Hamiltonian Cycle](backtracking/hamiltonian_cycle.py)
* [Knight Tour](backtracking/knight_tour.py) * [Knight Tour](backtracking/knight_tour.py)
* [Minimax](backtracking/minimax.py) * [Minimax](backtracking/minimax.py)
* [Minmax](backtracking/minmax.py)
* [N Queens](backtracking/n_queens.py) * [N Queens](backtracking/n_queens.py)
* [N Queens Math](backtracking/n_queens_math.py) * [N Queens Math](backtracking/n_queens_math.py)
* [Rat In Maze](backtracking/rat_in_maze.py) * [Rat In Maze](backtracking/rat_in_maze.py)
@ -157,6 +158,7 @@
* [Binary Tree Mirror](data_structures/binary_tree/binary_tree_mirror.py) * [Binary Tree Mirror](data_structures/binary_tree/binary_tree_mirror.py)
* [Binary Tree Node Sum](data_structures/binary_tree/binary_tree_node_sum.py) * [Binary Tree Node Sum](data_structures/binary_tree/binary_tree_node_sum.py)
* [Binary Tree Traversals](data_structures/binary_tree/binary_tree_traversals.py) * [Binary Tree Traversals](data_structures/binary_tree/binary_tree_traversals.py)
* [Diff Views Of Binary Tree](data_structures/binary_tree/diff_views_of_binary_tree.py)
* [Fenwick Tree](data_structures/binary_tree/fenwick_tree.py) * [Fenwick Tree](data_structures/binary_tree/fenwick_tree.py)
* [Inorder Tree Traversal 2022](data_structures/binary_tree/inorder_tree_traversal_2022.py) * [Inorder Tree Traversal 2022](data_structures/binary_tree/inorder_tree_traversal_2022.py)
* [Lazy Segment Tree](data_structures/binary_tree/lazy_segment_tree.py) * [Lazy Segment Tree](data_structures/binary_tree/lazy_segment_tree.py)
@ -513,6 +515,7 @@
* [Gamma](maths/gamma.py) * [Gamma](maths/gamma.py)
* [Gamma Recursive](maths/gamma_recursive.py) * [Gamma Recursive](maths/gamma_recursive.py)
* [Gaussian](maths/gaussian.py) * [Gaussian](maths/gaussian.py)
* [Gaussian Error Linear Unit](maths/gaussian_error_linear_unit.py)
* [Greatest Common Divisor](maths/greatest_common_divisor.py) * [Greatest Common Divisor](maths/greatest_common_divisor.py)
* [Greedy Coin Change](maths/greedy_coin_change.py) * [Greedy Coin Change](maths/greedy_coin_change.py)
* [Hamming Numbers](maths/hamming_numbers.py) * [Hamming Numbers](maths/hamming_numbers.py)
@ -601,6 +604,7 @@
* [Inverse Of Matrix](matrix/inverse_of_matrix.py) * [Inverse Of Matrix](matrix/inverse_of_matrix.py)
* [Matrix Class](matrix/matrix_class.py) * [Matrix Class](matrix/matrix_class.py)
* [Matrix Operation](matrix/matrix_operation.py) * [Matrix Operation](matrix/matrix_operation.py)
* [Max Area Of Island](matrix/max_area_of_island.py)
* [Nth Fibonacci Using Matrix Exponentiation](matrix/nth_fibonacci_using_matrix_exponentiation.py) * [Nth Fibonacci Using Matrix Exponentiation](matrix/nth_fibonacci_using_matrix_exponentiation.py)
* [Rotate Matrix](matrix/rotate_matrix.py) * [Rotate Matrix](matrix/rotate_matrix.py)
* [Searching In Sorted Matrix](matrix/searching_in_sorted_matrix.py) * [Searching In Sorted Matrix](matrix/searching_in_sorted_matrix.py)

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
""" """
Deutsch-Josza Algorithm is one of the first examples of a quantum Deutsch-Jozsa Algorithm is one of the first examples of a quantum
algorithm that is exponentially faster than any possible deterministic algorithm that is exponentially faster than any possible deterministic
classical algorithm classical algorithm
@ -22,10 +22,10 @@ References:
""" """
import numpy as np import numpy as np
import qiskit as q import qiskit
def dj_oracle(case: str, num_qubits: int) -> q.QuantumCircuit: def dj_oracle(case: str, num_qubits: int) -> qiskit.QuantumCircuit:
""" """
Returns a Quantum Circuit for the Oracle function. Returns a Quantum Circuit for the Oracle function.
The circuit returned can represent balanced or constant function, The circuit returned can represent balanced or constant function,
@ -33,7 +33,7 @@ def dj_oracle(case: str, num_qubits: int) -> q.QuantumCircuit:
""" """
# This circuit has num_qubits+1 qubits: the size of the input, # This circuit has num_qubits+1 qubits: the size of the input,
# plus one output qubit # plus one output qubit
oracle_qc = q.QuantumCircuit(num_qubits + 1) oracle_qc = qiskit.QuantumCircuit(num_qubits + 1)
# First, let's deal with the case in which oracle is balanced # First, let's deal with the case in which oracle is balanced
if case == "balanced": if case == "balanced":
@ -43,7 +43,7 @@ def dj_oracle(case: str, num_qubits: int) -> q.QuantumCircuit:
# Next, format 'b' as a binary string of length 'n', padded with zeros: # Next, format 'b' as a binary string of length 'n', padded with zeros:
b_str = format(b, f"0{num_qubits}b") b_str = format(b, f"0{num_qubits}b")
# Next, we place the first X-gates. Each digit in our binary string # 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 # corresponds to a qubit, if the digit is 0, we do nothing, if it's 1
# we apply an X-gate to that qubit: # we apply an X-gate to that qubit:
for index, bit in enumerate(b_str): for index, bit in enumerate(b_str):
if bit == "1": if bit == "1":
@ -70,13 +70,15 @@ def dj_oracle(case: str, num_qubits: int) -> q.QuantumCircuit:
return oracle_gate return oracle_gate
def dj_algorithm(oracle: q.QuantumCircuit, num_qubits: int) -> q.QuantumCircuit: def dj_algorithm(
oracle: qiskit.QuantumCircuit, num_qubits: int
) -> qiskit.QuantumCircuit:
""" """
Returns the complete Deustch-Jozsa Quantum Circuit, Returns the complete Deutsch-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 to the Oracle Circuit passed in arguments
""" """
dj_circuit = q.QuantumCircuit(num_qubits + 1, num_qubits) dj_circuit = qiskit.QuantumCircuit(num_qubits + 1, num_qubits)
# Set up the output qubit: # Set up the output qubit:
dj_circuit.x(num_qubits) dj_circuit.x(num_qubits)
dj_circuit.h(num_qubits) dj_circuit.h(num_qubits)
@ -95,7 +97,7 @@ def dj_algorithm(oracle: q.QuantumCircuit, num_qubits: int) -> q.QuantumCircuit:
return dj_circuit return dj_circuit
def deutsch_jozsa(case: str, num_qubits: int) -> q.result.counts.Counts: def deutsch_jozsa(case: str, num_qubits: int) -> qiskit.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 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:
>>> deutsch_jozsa("balanced", 3) >>> deutsch_jozsa("balanced", 3)
{'111': 1000} {'111': 1000}
""" """
# Use Aer's qasm_simulator # Use Aer's simulator
simulator = q.Aer.get_backend("qasm_simulator") simulator = qiskit.Aer.get_backend("aer_simulator")
oracle_gate = dj_oracle(case, num_qubits) oracle_gate = dj_oracle(case, num_qubits)
dj_circuit = dj_algorithm(oracle_gate, num_qubits) dj_circuit = dj_algorithm(oracle_gate, num_qubits)
# Execute the circuit on the qasm simulator # Execute the circuit on the simulator
job = q.execute(dj_circuit, simulator, shots=1000) job = qiskit.execute(dj_circuit, simulator, shots=1000)
# Return the histogram data of the results of the experiment. # Return the histogram data of the results of the experiment.
return job.result().get_counts(dj_circuit) return job.result().get_counts(dj_circuit)

View File

@ -10,10 +10,10 @@ https://en.wikipedia.org/wiki/Adder_(electronics)
https://qiskit.org/textbook/ch-states/atoms-computation.html#4.2-Remembering-how-to-add- https://qiskit.org/textbook/ch-states/atoms-computation.html#4.2-Remembering-how-to-add-
""" """
import qiskit as q import qiskit
def half_adder(bit0: int, bit1: int) -> q.result.counts.Counts: def half_adder(bit0: int, bit1: int) -> qiskit.result.counts.Counts:
""" """
>>> half_adder(0, 0) >>> half_adder(0, 0)
{'00': 1000} {'00': 1000}
@ -24,10 +24,10 @@ def half_adder(bit0: int, bit1: int) -> q.result.counts.Counts:
>>> half_adder(1, 1) >>> half_adder(1, 1)
{'10': 1000} {'10': 1000}
""" """
# Use Aer's qasm_simulator # Use Aer's simulator
simulator = q.Aer.get_backend("qasm_simulator") simulator = qiskit.Aer.get_backend("aer_simulator")
qc_ha = q.QuantumCircuit(4, 2) qc_ha = qiskit.QuantumCircuit(4, 2)
# encode inputs in qubits 0 and 1 # encode inputs in qubits 0 and 1
if bit0 == 1: if bit0 == 1:
qc_ha.x(0) qc_ha.x(0)
@ -48,9 +48,9 @@ def half_adder(bit0: int, bit1: int) -> q.result.counts.Counts:
qc_ha.measure(3, 1) # extract AND value qc_ha.measure(3, 1) # extract AND value
# Execute the circuit on the qasm simulator # Execute the circuit on the qasm simulator
job = q.execute(qc_ha, simulator, shots=1000) job = qiskit.execute(qc_ha, simulator, shots=1000)
# Return the histogram data of the results of the experiment. # Return the histogram data of the results of the experiment
return job.result().get_counts(qc_ha) return job.result().get_counts(qc_ha)

View File

@ -6,21 +6,23 @@ times and print the total count of the states finally observed.
Qiskit Docs: https://qiskit.org/documentation/getting_started.html Qiskit Docs: https://qiskit.org/documentation/getting_started.html
""" """
import qiskit as q import qiskit
def single_qubit_measure(qubits: int, classical_bits: int) -> q.result.counts.Counts: def single_qubit_measure(
qubits: int, classical_bits: int
) -> qiskit.result.counts.Counts:
""" """
>>> single_qubit_measure(2, 2) >>> single_qubit_measure(2, 2)
{'11': 1000} {'11': 1000}
>>> single_qubit_measure(4, 4) >>> single_qubit_measure(4, 4)
{'0011': 1000} {'0011': 1000}
""" """
# Use Aer's qasm_simulator # Use Aer's simulator
simulator = q.Aer.get_backend("qasm_simulator") simulator = qiskit.Aer.get_backend("aer_simulator")
# Create a Quantum Circuit acting on the q register # Create a Quantum Circuit acting on the q register
circuit = q.QuantumCircuit(qubits, classical_bits) circuit = qiskit.QuantumCircuit(qubits, classical_bits)
# Apply X (NOT) Gate to Qubits 0 & 1 # Apply X (NOT) Gate to Qubits 0 & 1
circuit.x(0) circuit.x(0)
@ -30,7 +32,7 @@ def single_qubit_measure(qubits: int, classical_bits: int) -> q.result.counts.Co
circuit.measure([0, 1], [0, 1]) circuit.measure([0, 1], [0, 1])
# Execute the circuit on the qasm simulator # Execute the circuit on the qasm simulator
job = q.execute(circuit, simulator, shots=1000) job = qiskit.execute(circuit, simulator, shots=1000)
# Return the histogram data of the results of the experiment. # Return the histogram data of the results of the experiment.
return job.result().get_counts(circuit) return job.result().get_counts(circuit)

View File

@ -11,7 +11,6 @@ https://www.quantum-inspire.com/kbase/full-adder/
import math import math
import qiskit import qiskit
from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute
def quantum_full_adder( def quantum_full_adder(
@ -78,12 +77,12 @@ def quantum_full_adder(
raise ValueError("inputs must be less or equal to 2.") raise ValueError("inputs must be less or equal to 2.")
# build registers # build registers
qr = QuantumRegister(4, "qr") qr = qiskit.QuantumRegister(4, "qr")
cr = ClassicalRegister(2, "cr") cr = qiskit.ClassicalRegister(2, "cr")
# list the entries # list the entries
entry = [input_1, input_2, carry_in] entry = [input_1, input_2, carry_in]
quantum_circuit = QuantumCircuit(qr, cr) quantum_circuit = qiskit.QuantumCircuit(qr, cr)
for i in range(0, 3): for i in range(0, 3):
if entry[i] == 2: if entry[i] == 2:
@ -102,8 +101,8 @@ def quantum_full_adder(
quantum_circuit.measure([2, 3], cr) # measure the last two qbits quantum_circuit.measure([2, 3], cr) # measure the last two qbits
backend = Aer.get_backend("qasm_simulator") backend = qiskit.Aer.get_backend("aer_simulator")
job = execute(quantum_circuit, backend, shots=1000) job = qiskit.execute(quantum_circuit, backend, shots=1000)
return job.result().get_counts(quantum_circuit) return job.result().get_counts(quantum_circuit)

View File

@ -29,8 +29,8 @@ def quantum_entanglement(qubits: int = 2) -> qiskit.result.counts.Counts:
""" """
classical_bits = qubits classical_bits = qubits
# Using Aer's qasm_simulator # Using Aer's simulator
simulator = qiskit.Aer.get_backend("qasm_simulator") simulator = qiskit.Aer.get_backend("aer_simulator")
# Creating a Quantum Circuit acting on the q register # Creating a Quantum Circuit acting on the q register
circuit = qiskit.QuantumCircuit(qubits, classical_bits) circuit = qiskit.QuantumCircuit(qubits, classical_bits)
@ -48,7 +48,7 @@ def quantum_entanglement(qubits: int = 2) -> qiskit.result.counts.Counts:
# Now measuring any one qubit would affect other qubits to collapse # Now measuring any one qubit would affect other qubits to collapse
# their super position and have same state as the measured one. # their super position and have same state as the measured one.
# Executing the circuit on the qasm simulator # Executing the circuit on the simulator
job = qiskit.execute(circuit, simulator, shots=1000) job = qiskit.execute(circuit, simulator, shots=1000)
return job.result().get_counts(circuit) return job.result().get_counts(circuit)

View File

@ -2,11 +2,11 @@
# https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder # https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder
# https://en.wikipedia.org/wiki/Controlled_NOT_gate # https://en.wikipedia.org/wiki/Controlled_NOT_gate
from qiskit import Aer, QuantumCircuit, execute import qiskit
from qiskit.providers import Backend from qiskit.providers import Backend
def store_two_classics(val1: int, val2: int) -> tuple[QuantumCircuit, str, str]: def store_two_classics(val1: int, val2: int) -> tuple[qiskit.QuantumCircuit, str, str]:
""" """
Generates a Quantum Circuit which stores two classical integers Generates a Quantum Circuit which stores two classical integers
Returns the circuit and binary representation of the integers Returns the circuit and binary representation of the integers
@ -21,10 +21,10 @@ def store_two_classics(val1: int, val2: int) -> tuple[QuantumCircuit, str, str]:
# We need (3 * number of bits in the larger number)+1 qBits # We need (3 * number of bits in the larger number)+1 qBits
# The second parameter is the number of classical registers, to measure the result # The second parameter is the number of classical registers, to measure the result
circuit = QuantumCircuit((len(x) * 3) + 1, len(x) + 1) circuit = qiskit.QuantumCircuit((len(x) * 3) + 1, len(x) + 1)
# We are essentially "not-ing" the bits that are 1 # We are essentially "not-ing" the bits that are 1
# Reversed because its easier to perform ops on more significant bits # Reversed because it's easier to perform ops on more significant bits
for i in range(len(x)): for i in range(len(x)):
if x[::-1][i] == "1": if x[::-1][i] == "1":
circuit.x(i) circuit.x(i)
@ -36,7 +36,7 @@ def store_two_classics(val1: int, val2: int) -> tuple[QuantumCircuit, str, str]:
def full_adder( def full_adder(
circuit: QuantumCircuit, circuit: qiskit.QuantumCircuit,
input1_loc: int, input1_loc: int,
input2_loc: int, input2_loc: int,
carry_in: int, carry_in: int,
@ -55,14 +55,14 @@ def full_adder(
# The default value for **backend** is the result of a function call which is not # The default value for **backend** is the result of a function call which is not
# normally recommended and causes flake8-bugbear to raise a B008 error. However, # normally recommended and causes flake8-bugbear to raise a B008 error. However,
# in this case, this is accptable because `Aer.get_backend()` is called when the # in this case, this is acceptable because `Aer.get_backend()` is called when the
# function is defined and that same backend is then reused for all function calls. # function is defined and that same backend is then reused for all function calls.
def ripple_adder( def ripple_adder(
val1: int, val1: int,
val2: int, val2: int,
backend: Backend = Aer.get_backend("qasm_simulator"), # noqa: B008 backend: Backend = qiskit.Aer.get_backend("aer_simulator"), # noqa: B008
) -> int: ) -> int:
""" """
Quantum Equivalent of a Ripple Adder Circuit Quantum Equivalent of a Ripple Adder Circuit
@ -104,7 +104,7 @@ def ripple_adder(
for i in range(len(x) + 1): for i in range(len(x) + 1):
circuit.measure([(len(x) * 2) + i], [i]) circuit.measure([(len(x) * 2) + i], [i])
res = execute(circuit, backend, shots=1).result() res = qiskit.execute(circuit, backend, shots=1).result()
# The result is in binary. Convert it back to int # The result is in binary. Convert it back to int
return int(list(res.get_counts())[0], 2) return int(list(res.get_counts())[0], 2)

View File

@ -6,25 +6,27 @@ finally prints the total count of the states finally observed.
Qiskit Docs: https://qiskit.org/documentation/getting_started.html Qiskit Docs: https://qiskit.org/documentation/getting_started.html
""" """
import qiskit as q import qiskit
def single_qubit_measure(qubits: int, classical_bits: int) -> q.result.counts.Counts: def single_qubit_measure(
qubits: int, classical_bits: int
) -> qiskit.result.counts.Counts:
""" """
>>> single_qubit_measure(1, 1) >>> single_qubit_measure(1, 1)
{'0': 1000} {'0': 1000}
""" """
# Use Aer's qasm_simulator # Use Aer's simulator
simulator = q.Aer.get_backend("qasm_simulator") simulator = qiskit.Aer.get_backend("aer_simulator")
# Create a Quantum Circuit acting on the q register # Create a Quantum Circuit acting on the q register
circuit = q.QuantumCircuit(qubits, classical_bits) circuit = qiskit.QuantumCircuit(qubits, classical_bits)
# Map the quantum measurement to the classical bits # Map the quantum measurement to the classical bits
circuit.measure([0], [0]) circuit.measure([0], [0])
# Execute the circuit on the qasm simulator # Execute the circuit on the simulator
job = q.execute(circuit, simulator, shots=1000) job = qiskit.execute(circuit, simulator, shots=1000)
# Return the histogram data of the results of the experiment. # Return the histogram data of the results of the experiment.
return job.result().get_counts(circuit) return job.result().get_counts(circuit)