Portování kódu do Qiskit Serverless
Qiskit Serverless prochází vylepšením a jeho funkce se rychle mění. V průběhu této vývojové fáze najdeš poznámky k vydání a nejnovější dokumentaci na stránce Qiskit Serverless GitHub.
Následující příklad ukazuje, jak portovat existující kód tak, aby využíval Qiskit Serverless.
Následující kód předpokládá, že máš uložené přihlašovací údaje. Pokud ne, postupuj podle pokynů v části Nastavení účtu IBM Cloud a ověř se pomocí svého API klíče.
Aktualizace experimentu
Ve verzi Serverless vytvoř runtime službu pomocí get_runtime_service() (z qiskit_serverless) místo QiskitRuntimeService(), aby úlohy a Sessions IBM Quantum® Compute Service, které spustí, byly sledovány vůči nadřazené úloze Qiskit Serverless. Podrobnosti najdeš v Napiš svůj první program Qiskit Serverless.
- Lokální experiment
- Serverless
from qiskit.transpiler import generate_preset_pass_manager
from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit.circuit.random import random_circuit
qc_random = [(random_circuit(20, 20, measure=True)) for _ in range(30)]
optimization_level = 3
service = QiskitRuntimeService(channel="ibm_quantum_platform")
backend = service.get_backend(backend_name)
pass_manager = generate_preset_pass_manager(
optimization_level=optimization_level, backend=backend
)
# @distribute_task(target={"cpu": 1})
def transpile_parallel(circuit, pass_manager):
"""Distributed transpilation for an abstract circuit into an ISA circuit for a given backend."""
isa_circuit = pass_manager.run(circuit)
return isa_circuit
transpiled_circuits = [
transpile_parallel(circuit, pass_manager)
for circuit in circuits
]
print(transpiled_circuits)
# transpile_remote.py
from qiskit.transpiler import generate_preset_pass_manager
from qiskit_serverless import get_arguments, save_result, distribute_task, get, get_runtime_service
# Získat argumenty programu
arguments = get_arguments()
circuits = arguments.get("circuits")
backend_name = arguments.get("backend_name")
optimization_level = arguments.get("optimization_level")
pass_manager = generate_preset_pass_manager(
optimization_level=optimization_level, backend=backend_name
)
# Distribuovat úlohu mezi pracovníky (workers)
@distribute_task(target={"cpu": 1})
def transpile_parallel(circuit, pass_manager):
"""Distributed transpilation for an abstract circuit into an ISA circuit for a given backend."""
isa_circuit = pass_manager.run(circuit)
return isa_circuit
try:
# Get backend
service = get_runtime_service()
backend = service.get_backend(backend_name)
# spustit distribuované úlohy jako asynchronní funkci
# jako návratový typ získáme reference na úlohy
sample_task_references = [
transpile_parallel(circuit, pass_manager)
for circuit in circuits
]
# now we need to collect results from task references
results = get(sample_task_references)
# Vrátit výsledky
save_result({
"transpiled_circuits": results
})
except Exception as e:
# Exception handling
import traceback
print(traceback.format_exc())
Upload to Qiskit Serverless
Follow the instructions on the Introduction to Qiskit Functions page to authenticate with your API key.
from qiskit_ibm_catalog import QiskitServerless, QiskitFunction
# Authenticate to the remote cluster and submit the pattern for remote execution.
serverless = QiskitServerless()
transpile_remote_demo = QiskitFunction(
title="transpile_remote_serverless",
entrypoint="transpile_remote.py",
working_dir="./source_files/",
)
serverless.upload(transpile_remote_demo)
Výstup
'transpile_remote_serverless'
Vzdálené spuštění v Qiskit Serverless
from qiskit.circuit.random import random_circuit
from qiskit_ibm_runtime import QiskitRuntimeService
# Setup inputs
qc_random = [(random_circuit(20, 20, measure=True)) for _ in range(30)]
backend = "ibm_brisbane"
optimization_level = 3
# Running program
transpile_remote_serverless = serverless.load('transpile_remote_serverless')
job = transpile_remote_serverless.run(
circuits=qc_random,
backend=backend,
optimization_level=optimization_level
)
job.job_id
Výstup
'727e921d-512d-4b7d-af97-fe29e93ce7ea'
Další kroky
- Přečti si článek, ve kterém výzkumníci využili Qiskit Serverless a kvantové superpočítání zaměřené na kvantové systémy k prozkoumání kvantové chemie.