0
$\begingroup$

Trying to do an application of Grover's algorithm. I am referring to Qiskit's video on the satisfiability problem however aqua does not exist so I am trying some work arounds.

from qiskit import BasicAer
from qiskit.algorithms import Grover, AmplificationProblem  
from qiskit.circuit.library import PhaseOracle 

log_expr = '((A & B) | (B & C)) & ~ (B & D)'
algorithm = Grover(PhaseOracle(log_expr))

backend = BasicAer.get_backend('qasm_simulator')
result = algorithm.run(backend)

and get:

      1 backend = BasicAer.get_backend('qasm_simulator')
----> 2 result = algorithm.run(backend)

AttributeError: 'Grover' object has no attribute 'run'
$\endgroup$

1 Answer 1

1
$\begingroup$

Just replace this line

algorithm = Grover(PhaseOracle(log_expr))

with

problem = AmplificationProblem(PhaseOracle(log_expr))

And

result = algorithm.run(backend)

with

grover = Grover(quantum_instance=backend)
result = grover.amplify(problem)

Note, however, that it is recommended to use Qiskit primitives to run the algorithms instead of dealing with backends directly (see here). So, for your code to be future-proof use Sampler primitive instead of QuantumInstance

from qiskit.primitives import Sampler
from qiskit.algorithms import Grover, AmplificationProblem  
from qiskit.circuit.library import PhaseOracle 

log_expr = '((A & B) | (B & C)) & ~ (B & D)'

problem = AmplificationProblem(PhaseOracle(log_expr))

sampler = Sampler()
grover = Grover(sampler=sampler)
result = grover.amplify(problem)
$\endgroup$
6
  • 2
    $\begingroup$ There are also a couple of tutorials for Grover here qiskit.org/documentation/tutorials.html#algorithms that use a Sampler primitive (in this case the reference one from Terra). $\endgroup$
    – Steve Wood
    Apr 11, 2023 at 13:11
  • $\begingroup$ neither of the above worked. I assume you mean 'result = grover.amplify(algorithm)' not problem? Even so, I get ' 'Grover' object has no attribute 'oracle' ' $\endgroup$ Apr 11, 2023 at 13:48
  • $\begingroup$ Sorry, I tested the code on my machine, but forgot to copy one line while writing the answer. I added the missing line now. $\endgroup$ Apr 11, 2023 at 14:04
  • $\begingroup$ hmm this makes it not callable for when I want to see the histogram: 'plot_histogram(result('measurement'), title = "Possible combos", bar_labels = True)' returns ''GroverResult' object is not callable' $\endgroup$ Apr 11, 2023 at 22:40
  • 1
    $\begingroup$ Use result.circuit_results instead of result('measurement') $\endgroup$ Apr 11, 2023 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.