In [1]:
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()
In [2]:
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
In [3]:
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
In [4]:
n = 4
dj_circuit = dj_algorithm(n,"constant")
dj_circuit.draw()
Out[4]:
In [5]:
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)
Out[5]:

Como analizar o Resultado Final?

Se o resultado for bitstream: 0000..0 (todos zeros) => f(0)=f(1) : i.e., função é constante

Se o resultado for bitstream for diferente de 0000..0 ==> f(x): função é balanceada

In [25]:
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)
Backend menos ocupado:  ibmq_manila
In [26]:
%qiskit_job_watcher
dj_circuit = dj_algorithm(n)  #Escolhe circuito Randomicamente!!
job = execute(dj_circuit, backend=bckend2, shots=shots, optimization_level=3)
In [ ]:
results = job.result()
resposta = results.get_counts()
plot_histogram(resposta)
In [6]:
#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)
Out[6]:
In [7]:
#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)
Out[7]:
In [8]:
#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)
Out[8]:
In [ ]:
 
In [ ]: