Skip to content

Instantly share code, notes, and snippets.

@GDLMadushanka
Created May 2, 2020 07:23
Show Gist options
  • Save GDLMadushanka/ad31f7866f4881720007bc631b0162ee to your computer and use it in GitHub Desktop.
Save GDLMadushanka/ad31f7866f4881720007bc631b0162ee to your computer and use it in GitHub Desktop.
Grover full implementation
from matplotlib import pyplot as plt
import numpy as np
from qiskit import *
from qiskit.tools.visualization import plot_histogram
# Creating quantum circuit with 3 qubits and 3 classical bits
qc = QuantumCircuit(3,3)
# Preparing inputs
qc.h(0)
qc.h(1)
qc.h(2)
qc.barrier()
# Oracle function for number 5 = 101
qc.x(1)
qc.h(2)
qc.ccx(0,1,2)
qc.x(1)
qc.h(2)
qc.barrier()
# diffusion operator
qc.h(0)
qc.h(1)
qc.h(2)
qc.x(0)
qc.x(1)
qc.x(2)
qc.ccx(0,1,2)
qc.x(0)
qc.x(1)
qc.x(2)
qc.h(0)
qc.h(1)
qc.h(2)
qc.barrier()
# measuring the output
qc.measure(0,0)
qc.measure(1,1)
qc.measure(2,2)
qc.draw(output='mpl')
# Running the experiment in simulator
simulator = Aer.get_backend('qasm_simulator')
result = execute(qc,backend=simulator,shots=1024).result()
plot_histogram(result.get_counts())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment