I've started playing around with circuits only recently and decided to try out and implement a 4-qubit Grover's search using qiskit.
When I run my python script using the qasm_simulator, I get the expected results with a probability peak over the value 0010 as expected (the value marked by my oracle).
However, when I change the backend to basically anything else on IBMQ (ibmqx2, ibmq_essex, ibmq_vigo), the results get very different. There is no apparent peak in the distribution whatsoever, not even if I repeat the amplitude amplification process - I use the "iterations" variable for that further in the code - works nicely with qasm_simulator, but seems to have no real effect on a quantum computer.
I feel like I'm doing something fundamentally wrong, but I simply cannot figure it out.
Here's my code:
import math
from qiskit import IBMQ, BasicAer
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, execute
from qiskit.visualization import plot_histogram
pi = math.pi
provider = IBMQ.load_account()
def oracle(circuit, qr):
circuit.x(qr[0])
circuit.x(qr[2])
circuit.x(qr[3])
circuit.cu1(pi/4, qr[0], qr[3])
circuit.cx(qr[0], qr[1])
circuit.cu1(-pi/4, qr[1], qr[3])
circuit.cx(qr[0], qr[1])
circuit.cu1(pi/4, qr[1], qr[3])
circuit.cx(qr[1], qr[2])
circuit.cu1(-pi/4, qr[2], qr[3])
circuit.cx(qr[0], qr[2])
circuit.cu1(pi/4, qr[2], qr[3])
circuit.cx(qr[1], qr[2])
circuit.cu1(-pi/4, qr[2], qr[3])
circuit.cx(qr[0], qr[2])
circuit.cu1(pi/4, qr[2], qr[3])
circuit.x(qr[0])
circuit.x(qr[2])
circuit.x(qr[3])
def amplification(circuit, qr):
circuit.h(qr)
circuit.x(qr)
circuit.cu1(pi/4, qr[0], qr[3])
circuit.cx(qr[0], qr[1])
circuit.cu1(-pi/4, qr[1], qr[3])
circuit.cx(qr[0], qr[1])
circuit.cu1(pi/4, qr[1], qr[3])
circuit.cx(qr[1], qr[2])
circuit.cu1(-pi/4, qr[2], qr[3])
circuit.cx(qr[0], qr[2])
circuit.cu1(pi/4, qr[2], qr[3])
circuit.cx(qr[1], qr[2])
circuit.cu1(-pi/4, qr[2], qr[3])
circuit.cx(qr[0], qr[2])
circuit.cu1(pi/4, qr[2], qr[3])
circuit.x(qr)
circuit.h(qr)
qr = QuantumRegister(4)
cr = ClassicalRegister(4)
iterations = 1
groverCircuit = QuantumCircuit(qr, cr)
# apply Hadamard gate to all qubits
groverCircuit.h(qr)
while iterations > 0:
oracle(groverCircuit, qr)
amplification(groverCircuit, qr)
iterations -= 1
# measure
groverCircuit.measure(qr, cr)
provider.backends()
backend = provider.get_backend('ibmqx2')
# backend = BasicAer.get_backend('qasm_simulator')
shots = 1024
results = execute(groverCircuit, backend=backend, shots=shots).result()
answer = results.get_counts()
print(answer)
print(plot_histogram(answer))
For example, this is one of the probability distributions I got from a quantum computer, where the 0010 value should have been amplified: