Created
May 2, 2020 15:49
-
-
Save AbhishekAshokDubey/cd16c54a908658991805c240172ae848 to your computer and use it in GitHub Desktop.
Grover using IBM qiskit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import numpy as np | |
from qiskit import QuantumCircuit | |
from run_circuit import get_current_state, get_qc_simulation_results, get_qc_actual_results | |
from qiskit.visualization import plot_histogram | |
n = 2 | |
grover_circuit = QuantumCircuit(n) # if we use grover_circuit.measure_all() below | |
# grover_circuit = QuantumCircuit(n, n) # if we grover_circuit.measure() below | |
for qubit in range(n): | |
grover_circuit.h(qubit) | |
for qubit in range(n): | |
grover_circuit.x(qubit) | |
grover_circuit.cz(0, 1) | |
# Apply the Oracle for |00⟩ : | |
for qubit in range(n): | |
grover_circuit.x(qubit) | |
for qubit in range(n): | |
grover_circuit.h(qubit) | |
for qubit in range(n): | |
grover_circuit.z(qubit) | |
grover_circuit.cz(0, 1) | |
for qubit in range(n): | |
grover_circuit.h(qubit) | |
# # The code to show the current circuit, this line can be put anywhere in code above to visualize the circuit at that point | |
grover_circuit.draw('mpl') | |
# The code to get the current state vector, this line can be put anywhere in code above to get the state vector at that point | |
statevec = get_current_state(grover_circuit) | |
print(np.round(statevec,2)) | |
# Now before we run the quantum circuit on simulation or actual quantum device, we must add the mesurement units to our circuit above. | |
# You can choose to put measure to all the qubits i the circuit using 'measure_all' or you could sepcify the qubits you need to measure 'measure' | |
grover_circuit.measure_all() | |
# grover_circuit.measure([0,1], [0,1]) | |
grover_circuit.draw('mpl') | |
# The code below actually run the circuit with multiple times to get the probabilitstic output | |
# Note: 'get_current_state' func above just display non probabilitic/ Ideal state of the circuit, | |
# whereas below code actually simulate a quantum circuit with probabilities/ noises | |
state_count_dict = get_qc_simulation_results(grover_circuit) | |
print(state_count_dict) | |
# Now lets actually run the circuit above on a real Quantum Computer! | |
results = get_qc_actual_results(grover_circuit) #print(results) | |
# Below are two ways to get the mesaurement results. | |
ans_way1 = results.data() | |
ans_way2 = results.get_counts(grover_circuit) | |
print(ans_way1, ans_way2) | |
plot_histogram(ans_way2) | |
# resultplt = plot_histogram(ans_way2) | |
# resultplt.show() | |
# resultplt.savefig("results.png") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# https://pypi.org/project/qiskit-ibmq-provider/ | |
# https://github.com/Qiskit/qiskit-ibmq-provider | |
# https://qiskit.org/documentation/stubs/qiskit.providers.ibmq.IBMQFactory.enable_account.html | |
# https://qiskit-staging.mybluemix.net/documentation/release_notes.html | |
# https://qiskit.org/documentation/getting_started.html | |
# in case you have issue with inline plots, uncomment the below lines | |
# import matplotlib | |
# matplotlib.use('Agg') | |
# import matplotlib.pyplot as plt | |
# For Simulator | |
from qiskit import Aer | |
# For Actual Quantum Experience | |
from qiskit import IBMQ | |
from qiskit.providers.ibmq import least_busy | |
from qiskit.tools.monitor import job_monitor | |
# For executing Quantum circuit | |
from qiskit import execute | |
# https://qiskit.org/documentation/apidoc/aer_provider.html | |
# Ideal quantum circuit statevector simulator | |
statevector_sim = Aer.get_backend('statevector_simulator') | |
# Noisy quantum circuit simulator backend. | |
qasm_sim = Aer.get_backend('qasm_simulator') | |
def get_current_state(circuit, backend_sim = statevector_sim): | |
job_sim = execute(circuit, backend_sim) | |
result = job_sim.result() # print(result) | |
state_vec = result.get_statevector() | |
return state_vec | |
def get_qc_simulation_results(circuit, backend_sim = qasm_sim, sim_count = 1024): | |
# circuit.measure_all() | |
backend = Aer.get_backend('qasm_simulator') | |
all_sim_results = execute(circuit, backend=backend, shots=sim_count).result() #print(all_sim_results.to_dict()) | |
state_count_dict = all_sim_results.get_counts() | |
return state_count_dict | |
# Note, you could always visit: https://quantum-computing.ibm.com/results to see the status of your job submitted on Real Quantum Computer | |
def get_qc_actual_results(circuit, sim_count = 1024): | |
token = open(r"<path to token file>\ibm_token.txt",mode='r').read() # or just copy the token here | |
# In case you do not want to save credential on disk, just enable & disable them as required | |
provider = IBMQ.enable_account(token) | |
provider = IBMQ.get_provider() | |
# for keeping the credentioal on disk, just save once& then use laod | |
# IBMQ.save_account(token) | |
# provider = IBMQ.load_account() | |
# Load IBM Q account and get the least busy backend device | |
device = least_busy(provider.backends(simulator=False)) | |
print("Running on current least busy device: ", device) | |
# Run our circuit on the least busy backend. Monitor the execution of the job in the queue | |
job = execute(circuit, backend=device, shots=sim_count, max_credits=10) | |
job_monitor(job, interval = 2) | |
# Get the results from the computation, job.result() will wait untill your program is finished | |
results = job.result() | |
IBMQ.disable_account() | |
return results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment