0
$\begingroup$

I am trying to solve the boolean problem (x1 or x2) and (~x1 or ~x2 or x3) using Grover's algorithm. The only measured bits are q0, q1, and q2. This is my oracle:

def prob_6_oracle():
    num_qubits = 7

    qc = QuantumCircuit(num_qubits)
    #(x1 or x2) = ~(~x1 and ~x2) stored in q3
    qc.x([0,1])
    qc.ccx(0,1,3)
    qc.x([0,1])
    qc.x(3)

    qc.barrier()
    
    #(~x1 or ~x2) = ~(x1 and x2) stored in q4
    qc.ccx(0,1,4)
    qc.x(4)

    qc.barrier()

    #(q4 or x3) = ~(~q4 and ~x3)
    qc.x([2,4])
    qc.ccx(2,4,5)
    qc.x([2,4])
    qc.x(5)

    qc.barrier()

    qc.ccx(3,5,6)

    qc.barrier()

    #qc.z(6)
    qc.cz(6, [0,1,2])

    # Decompute
    qc.barrier()

    qc.ccx(3,5,6)
    

    qc.x(5)
    qc.x([2,4])
    qc.ccx(2,4,5)
    qc.x([2,4])

    qc.x(4)
    qc.ccx(0,1,4)

    
    qc.x(3)
    qc.x([0,1])
    qc.ccx(0,1,3)
    qc.x([0,1])
    
    
    return qc

Both qc.z() and qc.cz() does not work to provide me with the correct solutions. I am instead receiving an equal distribution of solutions. I am using the Qiskit GroverOperator, and have it iterating twice.

$\endgroup$

1 Answer 1

1
$\begingroup$

The issue is that your use of the controlled $Z$ gate is incorrect. I am not sure what you were trying to accomplish by doing:

 qc.cz(6, [0,1,2])

but that is is not what you need.

What you want is for the states that set q6 equal to 1 to flip in phase. You can do this by keeping the exact code you have, but remove the cz gate and instead wrap around the target of the ccx with $XH$ gates. This will guarantee phase kickback on the states marked by the oracle: enter image description here

$\endgroup$

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.