TransWikia.com

How to extend Qiskit Quantum Counting with a Truthtable Oracle?

Quantum Computing Asked by ix-stefstet on April 27, 2021

I’m a newbie in Quantum Computing. So please be indulgent with me.

I’m trying to adapt the Qiskit Quantum Counting Example, which defines its oracle in a programmed quantum circuit, to use a truthtable oracle instead.
I want to provide a bitstring (e.g. ‘01110000’) and see as a result that there are 3 solutions (amount of ones in the bitstring).

The idea is, to put a truthtable oracle in a Grover Search Algo extract the Grover Operator, make a controlled gate out of it and use this as Grover Iteration in Quantum Counting example. But it doesn’t seem to work. The number of solutions calculated at the end doesn’t correlate somehow with the provided bitstring.

There are several differences between the oracle in the example and the truthtable oracle.
My truthtable oracle has an flipping qbit (output) which is feeded with state |-> and ancilla qbits.

I’m not sure how to handle these flipping and ancilla qubits. Can I reuse these qbits in the grover iterations (‘hang in series’) or do I have to seperate them per Grover Iteration?

I’m not sure, if I have an issue with bit-flipping oracles vs phase-flipping ones in the current scenario.

Hopefully someone has a hint for me. Thank you in advance.

Here’s my code so far (99% copied from the example):

from qiskit import QuantumCircuit, Aer, BasicAer, transpile, assemble
from qiskit.aqua import QuantumInstance
from qiskit.aqua.components.oracles import TruthTableOracle
from qiskit.aqua.algorithms import Grover
from qiskit.visualization import plot_histogram
import numpy as np
import math

def calculateTruthTableString(**kid):
    
     
    return '00100000'


## Main
t = 4   # no. of counting qubits
n = 3   # no. of searching qubits

# https://qiskit.org/documentation/stubs/qiskit.circuit.QuantumCircuit.html
qc = QuantumCircuit(n+t+2, t) # Circuit with n+t qubits and t classical bits

# https://qiskit.org/documentation/stubs/qiskit.aqua.components.oracles.TruthTableOracle.html
truthtable = calculateTruthTableString(amountSearchQbits = n)
oracle = TruthTableOracle(truthtable)
oracle.circuit.draw()

# https://qiskit.org/documentation/stubs/qiskit.aqua.algorithms.Grover.html
grover = Grover(oracle)

grover.grover_operator.draw()

result = grover.run(QuantumInstance(BasicAer.get_backend('qasm_simulator'), shots=1024))
plot_histogram(result.measurement)

grover_iteration = grover.grover_operator.to_gate()
ctl_grover_iteration = grover_iteration.control()
ctl_grover_iteration.label = "Ctl GrovIt"

def qft(n):
    """Creates an n-qubit QFT circuit"""
    circuit = QuantumCircuit(n)
    def swap_registers(circuit, n):
        for qubit in range(n//2):
            circuit.swap(qubit, n-qubit-1)
        return circuit
    def qft_rotations(circuit, n):
        """Performs qft on the first n qubits in circuit (without swaps)"""
        if n == 0:
            return circuit
        n -= 1
        circuit.h(n)
        for qubit in range(n):
            circuit.cp(np.pi/2**(n-qubit), qubit, n)
        qft_rotations(circuit, n)
    
    qft_rotations(circuit, n)
    swap_registers(circuit, n)
    return circuit

qft_dagger = qft(t).to_gate().inverse()
qft_dagger.label = "QFT†"

# https://qiskit.org/textbook/ch-algorithms/quantum-counting.html#2.4-Putting-it-Together-

# Initialise all qubits to |+>
for qubit in range(t+n):
    qc.h(qubit)

 # Begin controlled Grover iterations
iterations = 1
for qubit in range(t):
    for i in range(iterations):
        qc.append(ctl_grover_iteration, [qubit] + [*range(t, n+t+2)])
    iterations *= 2


# Do inverse QFT on counting qubits
qc.append(qft_dagger, range(t))

qc.measure(range(t), range(t))

# Display the circuit
qc.draw()

qasm_sim = Aer.get_backend('qasm_simulator')
transpiled_qc = transpile(qc, qasm_sim)
qobj = assemble(transpiled_qc)
job = qasm_sim.run(qobj)
hist = job.result().get_counts()
plot_histogram(hist) 

measured_str = max(hist, key=hist.get)
measured_int = int(measured_str,2)
print("Register Output = %i" % measured_int)
theta = (measured_int/(2**t))*math.pi*2
print("Theta = %.5f" % theta)

N = 2**(n-1)
M = N * (math.sin(theta/2)**2)
print("No. of Solutions = %.1f" % (N-M)) 

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP