import numpy as np
# Importing standard Qiskit libraries
from qiskit import QuantumCircuit, transpile, Aer, IBMQ, execute
from qiskit.tools.jupyter import *
from qiskit.visualization import *
from ibm_quantum_widgets import *
from qiskit.providers.aer import QasmSimulator
from qiskit.providers.ibmq import least_busy #Para verificar qual é o QComputer mais disponÃvel
# Loading your IBM Quantum account(s)
provider = IBMQ.load_account()
def dj_oracle(case, n):
oracle_qc = QuantumCircuit(n+1)
if case == "balanced":
for qubit in range(n):
oracle_qc.cx(qubit, n)
if case == "constant":
output = np.random.randint(2)
if output == 1:
oracle_qc.x(n)
oracle_gate = oracle_qc.to_gate()
oracle_gate.name = "Oraculo"
return oracle_gate
def dj_algorithm(n, case='random'):
dj_circuit = QuantumCircuit(n+1, n)
for qbit in range(n):
dj_circuit.h(qbit)
dj_circuit.x(n)
dj_circuit.h(n)
if case == "random":
random = np.random.randint(2)
if random == 0:
case = 'constant'
else:
case = 'balanced'
oracle = dj_oracle(case, n)
dj_circuit.append(oracle, range(n+1))
for i in range(n):
dj_circuit.h(i)
dj_circuit.measure(i, i)
return dj_circuit
n = 4
dj_circuit = dj_algorithm(n,"constant")
dj_circuit.draw()
bckend = Aer.get_backend('qasm_simulator')
shots = 1024
#dj_circuit = dj_algorithm(n,'constant')
#dj_circuit = dj_algorithm(n,'balanced')
dj_circuit = dj_algorithm(6)
results = execute(dj_circuit, backend=bckend, shots=shots).result()
resposta = results.get_counts()
plot_histogram(resposta)
bckend2 = least_busy(provider.backends(filters=lambda x: x.configuration().n_qubits >= (n+1) and not x.configuration().simulator and x.status().operational==True))
print("Backend menos ocupado: ", bckend2)
%qiskit_job_watcher
dj_circuit = dj_algorithm(n) #Escolhe circuito Randomicamente!!
job = execute(dj_circuit, backend=bckend2, shots=shots, optimization_level=3)
results = job.result()
resposta = results.get_counts()
plot_histogram(resposta)
#Resultado anterior - round 3
bck = provider.get_backend('ibmq_lima')
old_job = bck.retrieve_job("61d261fc525f9e2b6f398bb3")
resultado3 = old_job.result()
resposta3 = resultado3.get_counts()
plot_histogram(resposta3)
#Resultado anterior - round 1
bck = provider.get_backend('ibmq_lima')
old_job = bck.retrieve_job("61c07da520e2c46b48ead8ef")
resultado = old_job.result()
resposta1 = resultado.get_counts()
plot_histogram(resposta1)
#Resultado anterior - round 2
bck2 = provider.get_backend('ibmq_manila')
old_job = bck2.retrieve_job("61d25da4525f9eefe9398ba7")
resultado2 = old_job.result()
resposta2 = resultado2.get_counts()
plot_histogram(resposta2)