2
$\begingroup$

Studying Grover on IBM lab using Qiskit. But the following code produces unexpected outputs. Note that `PhaseOracle produces 2 CCZ gates. Could anyone check (and explain) why are those unexpected counts/outputs(given at the bottom) being produced?

Note that when I try a similar code with similar logical expressions with aqua (which is deprecated, the expected outputs are produced)

from qiskit import *
from qiskit.tools.visualization import plot_histogram
from qiskit.circuit.library import PhaseOracle
from qiskit.algorithms import Grover, AmplificationProblem
%matplotlib inline

matplotlib inline
oracle = PhaseOracle('((A & C) | (B & D)) & ~(C & D)')
problem = AmplificationProblem(oracle=oracle, is_good_state=oracle.evaluate_bitstring)
backend = Aer.get_backend('qasm_simulator')
grover = Grover(quantum_instance=backend)
result = grover.amplify(problem)
print(result.circuit_results[0])
{'0111': 259, '1100': 244, '1101': 234, '0011': 287}
$\endgroup$
2
  • $\begingroup$ Note that; if I set up the circuit manually, I can achieve correct results with expected probabilities. For example, I can catch 100% "111" with a manually created circuit with 3 qbits. (for a logical expression like (A&B&C) However, the code given above below not return 111 with 100% probability. $\endgroup$
    – EA_Oracle
    Jan 24 at 9:02
  • $\begingroup$ from qiskit import * from qiskit.tools.visualization import plot_histogram from qiskit.circuit.library import PhaseOracle from qiskit.algorithms import Grover, AmplificationProblem %matplotlib inline matplotlib inline oracle = PhaseOracle('((A & B & C)') problem = AmplificationProblem(oracle=oracle, is_good_state=oracle.evaluate_bitstring) backend = Aer.get_backend('qasm_simulator') grover = Grover(quantum_instance=backend) result = grover.amplify(problem) print(result.circuit_results[0]) $\endgroup$
    – EA_Oracle
    Jan 24 at 9:02

1 Answer 1

3
$\begingroup$

Qiskit Terra internally uses Tweedledum library to parse logical expressions. By default, Tweedledum sort the variables by the order of their appearance in the logical expressions. In your case, the order is A then C then B and finally D. So, you should interpret the output bitstring as DBCA.

On the other hand, in Qiskit Aqua's LogicalExpressionOracle the variables used to be sorted alphabetically.

$\endgroup$
3
  • $\begingroup$ Thank you. It is clear now. $\endgroup$
    – EA_Oracle
    Jan 26 at 16:30
  • $\begingroup$ What about A & B & C ? ** Following does not produce the result 111 with 100% certainty.. ** --> from qiskit import * from qiskit.tools.visualization import plot_histogram from qiskit.circuit.library import PhaseOracle from qiskit.algorithms import Grover, AmplificationProblem %matplotlib inline matplotlib inline oracle = PhaseOracle('((A & B & C)') problem = AmplificationProblem(oracle=oracle, is_good_state=oracle.evaluate_bitstring) backend = Aer.get_backend('qasm_simulator') grover = Grover(quantum_instance=backend) result = grover.amplify(problem) print(result.circuit_results[0]) $\endgroup$
    – EA_Oracle
    Jan 26 at 16:55
  • $\begingroup$ **However, following circuit (manually created) produce the result 111 with %100 probability. ** ----> circuit2=QuantumCircuit(3,3) circuit2.h(0) circuit2.h(1) circuit2.h(2) circuit2.h(2) circuit2.ccx(0,1,2) circuit2.h(2) circuit2.z(0) circuit2.z(1) circuit2.z(2) circuit2.h(2) circuit2.ccx(0,1,2) circuit2.h(2) circuit2.h(0) circuit2.h(1) circuit2.h(2) circuit2.measure([0,1,2],[0,1,2]) $\endgroup$
    – EA_Oracle
    Jan 26 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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