In [1]:
from qiskit import *
from qiskit.visualization import plot_histogram
%matplotlib inline
In [2]:
circuit = QuantumCircuit(2,1)
In [3]:
circuit.h(0)
circuit.x(1)
circuit.h(1)
circuit.barrier()
circuit.draw(output='mpl')
Out[3]:
No description has been provided for this image
In [4]:
# We don't need to know about the function that we'll apply now
# We'll use CNOT here since we know it's a balanced function 
# So we expect to see |1> when we measured the first qubit
# This is to show we'll get |1> for a balanced function (and |0> for a constant function)
circuit.cx(0,1)
circuit.barrier()
circuit.h(0)
circuit.barrier()
circuit.draw('mpl')
Out[4]:
No description has been provided for this image
In [5]:
# Measure the first qubit
circuit.measure(0,0)
circuit.draw('mpl')
Out[5]:
No description has been provided for this image
In [6]:
# Run on the simulator
from qiskit_aer import Aer
simulator = Aer.get_backend("qasm_simulator")
result = simulator.run(circuit, shots=1024).result()
counts = result.get_counts()
print(counts)
{'1': 1024}
In [7]:
plot_histogram([counts])
Out[7]:
No description has been provided for this image
In [8]:
# Now run on the real quantum computer
from qiskit_ibm_runtime import QiskitRuntimeService

# Load the "open" credentials
service = QiskitRuntimeService(name="open")

# Display all backends you have access
service.backends()
Out[8]:
[<IBMBackend('ibm_brisbane')>,
 <IBMBackend('ibm_sherbrooke')>,
 <IBMBackend('ibm_kyiv')>]
In [9]:
# Print some information for the quantum computers 
for backend in service.backends():
    #print(backend.properties())
    try:
        qubit_count = len(backend.properties().qubits)
    except:
        qubit_count = "simulated"
    print(f"{backend.name} : {backend.status().pending_jobs} pending jobs & {qubit_count} qubits")
ibm_brisbane : 57 pending jobs & 127 qubits
ibm_sherbrooke : 133 pending jobs & 127 qubits
ibm_kyiv : 783 pending jobs & 127 qubits
In [10]:
#You can directly use the defined feature to find a least busy computer
backend_leastBusy = service.least_busy(operational=True, simulator=False, min_num_qubits=2)
backend_leastBusy.name
Out[10]:
'ibm_brisbane'
In [11]:
quantum_computer = service.get_backend("ibm_brisbane")
In [12]:
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import SamplerV2 as Sampler
In [13]:
pm = generate_preset_pass_manager(backend=quantum_computer, target=quantum_computer.target, optimization_level=1)
isa_circuit = pm.run(circuit)

sampler = Sampler(quantum_computer)
job = sampler.run([isa_circuit])
result = job.result()
In [14]:
# pub_result = job_result[<idx>].data.<classical register>.get_counts()
# where <idx> is the index of the pub and <classical register> is the name of the classical register. 
# You can use circuit.cregs to find the name of the classical registers.
pub_result = result[0].data.c.get_counts()
plot_histogram(pub_result)
Out[14]:
No description has been provided for this image