Mkao Ufij
Mkao Ufij

Reputation: 1

Problem on import libraries on qiskit (python) python and import from qiskit problem

I want to run this code but the latest version of qiskit contains several updates to the arrangement of libraries. Can someone help me run the code correctly compatible with the latest version of qiskit? The most important problem is the execute problem.

from qiskit import Aer, QuantumCircuit, transpile, assemble, execute
from qiskit.visualization import plot_histogram
from qiskit.aqua.algorithms import Grover
from qiskit.aqua.components.oracles import LogicalExpressionOracle
import time
import matplotlib.pyplot as plt

database = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig', 'grape']
target = 'cherry'

def classical_search(database, target):
    for i in range(len(database)):
        if database[i] == target:
            return i
    return -1

def quantum_search(target):
    oracle = LogicalExpressionOracle(f'v0 & v1 & v2')
    grover = Grover(oracle)
    backend = Aer.get_backend('qasm_simulator')
    result = grover.run(backend)
    return result['top_measurement']

start_time = time.time()
classical_result = classical_search(database, target)
classical_time = time.time() - start_time

start_time = time.time()
quantum_result = quantum_search(target)
quantum_time = time.time() - start_time

print(f"Classical Search Result: Index {classical_result} (Time: {classical_time:.6f} seconds)")
print(f"Quantum Search Result: {quantum_result} (Time: {quantum_time:.6f} seconds)")

labels = ['Classical Search', 'Quantum Search']
times = [classical_time, quantum_time]

plt.bar(labels, times, color=['blue', 'green'])
plt.ylabel('Time (seconds)')
plt.title('Classical vs Quantum Search Time')
plt.show()

Upvotes: 0

Views: 23

Answers (0)

Related Questions