# Length / transpilation issues with Grover's algorithm

I would like to discuss the discrepancies between what we see in the simulator versus what we see in the actual running of the code on any IBMQ machine for any qubits at 5 or above. I am doing a final project on grover's algorithm, and the premise would require an extremely large number of qubits. I have tested my theory on the simulators up to 26 qubits (took 6 hours), and they are ~100% accurate. When I went to test the theory using the exact same circuit, I couldn't get a circuit of 4 qubits to even deviate from statistical random distrubution.

First of all, I have used Qiskit and Q# to create the exact same algorithm. If you could provide me with a methodology that would allow for easier work / do the work for the transpiler on qiskit, or the ability to utilize a real quantum computer in Q#, I would appreciate it.

Currently the transpiler in qiskit / IBMQ for grover's algorithm is limited to around 6-7 qubits. I don't like that I don't have more gates available. Regardless of this issue, I get garbage results for an oracle / ancilla at or larger than 4 qubits total. Why is is that the transpiler / decoherance that is occuring within the circuit is so terrible that I cannot construct Grover's algorithm using gates at or larger than 4 qubits? The probabilties of the simulators themselves are upwards of 99%, but even after a two qubit Grover's algorithm, it shows as only 40ish %?

When I go to three qubits, the entire algorithm is garbage, and I cannot provide ANY accuracy other than a random distribution, and when I tabulated it the accuracy was actually less than the statistical random distrubution. I'm not happy with what is going on here, as I cannot feasibly run anything of meaning on any IBMQ machine. I don't understand why the transpiler is inserting such a ludicrous range in the possible number of gates (about a hundred different possible gate configurations for 5 qubit Grover's algorithm)

Is this an issue with the transpiler? Is this just an issue with general decoherance? I don't understand why the quantum computer can't peform a grover's algorithm of 5 or more.

Please help me to understand what I am not getting here. Are the physical devices that far off from where I felt the technology was? I was hoping that a 15 qubit machine would be able to actually perform better, esp. in the 5 qubit realm.

#initialization
import matplotlib.pyplot as plt
import math
import numpy as np

# importing Qiskit
from qiskit import ClassicalRegister, QuantumRegister
from qiskit.providers.ibmq import least_busy
from qiskit.quantum_info import Statevector
from qiskit.providers.aer import QasmSimulator

# import basic plot tools
from qiskit.visualization import plot_histogram

def initialize_s(qc,qubits):
"""Apply a H-gate to 'qubits' in qc"""
for q in qubits:
qc.h(q)
return qc

def numberofiterations(nqubits):
temp = 2 ** nqubits;
squareRoot = math.sqrt(temp)
iterations = round(squareRoot)
return iterations

def oracle(nqubits):
q = QuantumRegister(nqubits)
qc = QuantumCircuit(q)
#set the oracle's 0 bits
qc.x(q[1])
qc.x(q[2])
#qc.x(q[5])
#qc.x(q[7])
#qc.x(q[8])
#qc.x(q[12])
#qc.x(q[13])
#qc.x(q[15])
#qc.x(q[19])
#qc.x(q[20])
#qc.x(q[21])
#qc.x(q[22])
#qc.x(q[24])
qc.h(q[nqubits-1])
#[0,3,4,6,9,10,11,14,16,17,18,23,26,27],30,ancilla_qubits=None, mode='noancilla')
#qc.mcx([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24],q[25],ancilla_qubits=None, mode='noancilla')
#qc.cx([0],[1])
qc.mcx([0,1,2],q[nqubits-1],ancilla_qubits=None, mode='noancilla')
qc.x(q[1])
qc.x(q[2])
#qc.x(q[5])
#qc.x(q[7])
#qc.x(q[8])
#qc.x(q[12])
#qc.x(q[13])
#qc.x(q[15])
#qc.x(q[19])
#qc.x(q[20])
#qc.x(q[21])
#qc.x(q[22])
#qc.x(q[24])
qc.h(q[nqubits-1])
#return the oracle as a gate.
oracle = qc.to_gate()
oracle.name = "U$$_\omega$$"
return oracle

def diffuser(nqubits):
qc = QuantumCircuit(nqubits)
# Apply transformation |s> -> |00..0> (H-gates)
for qubit in range(nqubits):
qc.h(qubit)
# Apply transformation |00..0> -> |11..1> (X-gates)
for qubit in range(nqubits):
qc.x(qubit)
# Do multi-controlled-Z gate
qc.h(nqubits-1)
qc.mcx(list(range(nqubits-1)), nqubits-1)  # multi-controlled-toffoli
#qc.cx([0],[1])
qc.h(nqubits-1)
# Apply transformation |11..1> -> |00..0>
for qubit in range(nqubits):
qc.x(qubit)
# Apply transformation |00..0> -> |s>
for qubit in range(nqubits):
qc.h(qubit)
# We will return the diffuser as a gate
U_s = qc.to_gate()
U_s.name = "$$U_s$$"
return U_s

n = 4
qr = QuantumRegister(n-1, 'register')
anc = QuantumRegister(1, 'ancilla')
cr = ClassicalRegister(n-1, 'classical')
grover_circuit = QuantumCircuit(qr,anc)
grover_circuit = initialize_s(grover_circuit, list(range(n)))
iterations = numberofiterations(n-1)
for j in range(iterations ):
grover_circuit.append(oracle(n), list(range(n)))
grover_circuit.append(diffuser(n), list(range(n)))
measure_circuit = QuantumCircuit(qr,cr)
measure_circuit.measure(qr,cr)
$$$$
`
• Grover circuits tend to be fairly deep. What is the depth of the circuit you are running? Ie circuit.depth() Nov 19 '20 at 21:47
• @JoelAnthonyCollins IBM has a detailed tutorial walking through Grover's algorithm on 2 and 3 qubits in Qiskit: qiskit.org/textbook/ch-algorithms/grover.html. If you're looking for help on your particular implementation, you probably need to show your circuit. Nov 19 '20 at 23:31
• The depth is Apparently 7 for a 4 qubit system, @Paul Nation Nov 20 '20 at 1:54
• @Jonathan Trousdale, I have the IBM documentation. That is the way I implemented it except for some small things here and there. I use an MCX gate instead of the CX gate, etc. The issue is not with my implementation. If you take ANY implementation above 3 qubits, it falls flat on it's face. Nov 20 '20 at 1:55
• for the above code, you just run execute([grover_circuit+measure_circuit] with whatever settings you want and then plot the histogram. This will graph only the instances where the ancilla has been triggered. and will not include the ancilla in the answer. Nov 20 '20 at 2:17