Lumaktaw sa pangunahing nilalaman

Isulat ang iyong unang Qiskit Serverless na programa

Package versions

Ang code sa page na ito ay ginawa gamit ang mga sumusunod na requirements. Inirerekomenda naming gamitin ang mga bersyong ito o mas bago.

qiskit[all]~=1.3.1
qiskit-ibm-runtime~=0.34.0
qiskit-aer~=0.15.1
qiskit-serverless~=0.18.1
qiskit-ibm-catalog~=0.2
qiskit-addon-sqd~=0.8.1
qiskit-addon-utils~=0.1.0
qiskit-addon-mpf~=0.2.0
qiskit-addon-aqc-tensor~=0.1.2
qiskit-addon-obp~=0.1.0
scipy~=1.15.0
pyscf~=2.8.0
tip

Ang Qiskit Serverless ay nagpapabuti, at mabilis nagbabago ang mga feature nito. Sa panahon ng development phase na ito, hanapin ang mga release note at pinakabagong dokumentasyon sa pahina ng Qiskit Serverless GitHub.

Ang halimbawang ito ay nagpapakita kung paano gamitin ang mga tool ng qiskit-serverless para gumawa ng parallel transpilation na programa, at pagkatapos ay gamitin ang qiskit-ibm-catalog para i-upload ang iyong programa sa IBM Quantum Platform bilang reusable remote service.

Pangkalahatang-ideya ng workflow​

  1. Gumawa ng lokal na direktoryo at walang laman na program file (./source_files/transpile_remote.py)
  2. Magdagdag ng code sa iyong programa na, kapag na-upload sa Qiskit Serverless, ay nagta-transpile ng circuit
  3. Gamitin ang qiskit-ibm-catalog para mag-authenticate sa Qiskit Serverless
  4. I-upload ang programa sa Qiskit Serverless

Pagkatapos ma-upload ang iyong programa, maaari mo itong patakbuhin para ma-transpile ang circuit sa pamamagitan ng pagsunod sa gabay na Patakbuhin ang iyong unang Qiskit Serverless workload nang remote.

# Added by doQumentation β€” required packages for this notebook
!pip install -q qiskit qiskit-ibm-catalog qiskit-ibm-runtime qiskit-serverless

Halimbawa: Remote transpilation gamit ang Qiskit Serverless​

Ang halimbawang ito ay ginagabayan ka sa paglikha at pagdaragdag sa isang program file na, kapag na-upload mo sa Qiskit Serverless, ay nagta-transpile ng circuit laban sa isang ibinigay na backend at target na optimization_level.

tip

Kailangan ng Qiskit Serverless na i-set up ang mga .py file ng iyong workload sa isang dedicated na direktoryo. Ang sumusunod na istruktura ay halimbawa ng magandang gawi:

serverless_program
β”œβ”€β”€ program_uploader.ipynb
└── source_files
β”œβ”€β”€ transpile_remote.py
└── *.py

Ina-upload ng Serverless ang mga nilalaman ng isang partikular na direktoryo (sa halimbawang ito, ang source_files na direktoryo) para patakbuhin nang remote. Pagkatapos ma-set up ang mga ito, maaari mong baguhin ang transpile_remote.py para kumuha ng mga input at magbalik ng mga output.

Gumawa ng direktoryo at walang laman na program file​

Una, gumawa ng direktoryo na pinangalanang source_files, pagkatapos ay gumawa ng program file sa direktoryo, upang ang path nito ay ./source_files/transpile_remote.py. Ito ang file na ia-upload mo sa Qiskit Serverless.

Magdagdag ng code sa iyong program file​

Punan ang iyong program file ng sumusunod na code, pagkatapos i-save ito.

babala

Kung binabasa mo ang mga code cell nang lokal sa notebook, makikita mo ang %%writefile magic command. Ang pagpapatupad ng mga cell na may magic command na ito ay nagse-save ng mga ito sa disk sa halip na i-execute ang mga ito.

# This cell is hidden from users, it creates a new folder
from pathlib import Path

Path("./source_files").mkdir(exist_ok=True)
%%writefile ./source_files/transpile_remote.py
# If you include the preceding `%%writefile` command (visible only when you read this locally in a notebook), running this cell saves to disk rather than executing the code.

from qiskit.transpiler import generate_preset_pass_manager

def transpile_remote(circuit, optimization_level, backend):
"""Transpiles an abstract circuit into an ISA circuit for a given backend."""
pass_manager = generate_preset_pass_manager(
optimization_level=optimization_level,
backend=backend
)
isa_circuit = pass_manager.run(circuit)
return isa_circuit

Magdagdag ng code para makuha ang mga argumento ng programa​

Ngayon, idagdag ang sumusunod na code sa iyong program file, na nagtatakda ng mga argumento ng programa.

Ang iyong paunang transpile_remote.py ay may tatlong input: circuits, backend_name, at optimization_level. Limitado pa lang ang Serverless na tumatanggap lamang ng serializable na mga input at output. Dahil dito, hindi mo maaaring direktang ipasa ang backend, kaya gamitin ang backend_name bilang string sa halip.

%%writefile --append ./source_files/transpile_remote.py
# If you include the preceding `%%writefile` command (visible only when you read this locally in a notebook), running this cell saves to disk rather than executing the code.

from qiskit_serverless import get_arguments, save_result, distribute_task, get

# Get program arguments
arguments = get_arguments()
circuits = arguments.get("circuits")
backend_name = arguments.get("backend_name")
optimization_level = arguments.get("optimization_level")

Magdagdag ng code na tumatawag sa backend​

Idagdag ang sumusunod na code sa iyong program file, na tumatawag sa iyong backend gamit ang QiskitRuntimeService.

Ipinapalagay ng sumusunod na code na naisagawa mo na ang proseso ng pag-save ng iyong credentials gamit ang QiskitRuntimeService.save_account, at maglo-load ng iyong default na naka-save na account maliban kung mag-specify ka ng iba. Tingnan ang I-save ang iyong mga login credential at I-initialize ang iyong Qiskit Runtime service account para sa karagdagang impormasyon.

%%writefile --append ./source_files/transpile_remote.py
# If you include the preceding `%%writefile` command (visible only when you read this locally in a notebook), running this cell saves to disk rather than executing the code.

from qiskit_ibm_runtime import QiskitRuntimeService

service = QiskitRuntimeService()
backend = service.backend(backend_name)

Magdagdag ng code para mag-transpile​

Sa wakas, idagdag ang sumusunod na code sa iyong program file. Ang code na ito ay nagpapatakbo ng transpile_remote() sa lahat ng circuits na ipinasa, at ibinabalik ang transpiled_circuits bilang resulta:

%%writefile --append ./source_files/transpile_remote.py
# If you include the preceding `%%writefile` command (visible only when you read this locally in a notebook), running this cell saves to disk rather than executing the code.

# Each circuit is being transpiled and will populate the array
results = [
transpile_remote(circuit, 1, backend)
for circuit in circuits
]

save_result({
"transpiled_circuits": results
})

Mag-authenticate sa Qiskit Serverless​

Gamitin ang qiskit-ibm-catalog para mag-authenticate sa QiskitServerless gamit ang iyong API key (maaari mong gamitin ang iyong QiskitRuntimeService API key, o gumawa ng bagong API key sa IBM Quantum Platform dashboard).

from qiskit_ibm_catalog import QiskitServerless, QiskitFunction

# Authenticate to the remote cluster and submit the pattern for remote execution
serverless = QiskitServerless()

Patakbuhin ang code para mag-upload​

Patakbuhin ang sumusunod na code para i-upload ang programa. Kino-compress ng Qiskit Serverless ang mga nilalaman ng working_dir (sa kasong ito, source_files) sa isang tar, na ini-upload at nilinis pagkatapos. Tinutukoy ng entrypoint ang pangunahing executable na programa para patakbuhin ng Qiskit Serverless.

transpile_remote_demo = QiskitFunction(
title="transpile_remote_serverless",
entrypoint="transpile_remote.py",
working_dir="./source_files/",
)
serverless.upload(transpile_remote_demo)
QiskitFunction(transpile_remote_serverless)

I-verify ang upload​

Para tingnan kung matagumpay na na-upload, gamitin ang serverless.list(), gaya ng sa sumusunod na code:

# Get program from serverless.list() that matches the title of the one we uploaded
next(
program
for program in serverless.list()
if program.title == "transpile_remote_serverless"
)
QiskitFunction(transpile_remote_serverless)
# This cell is hidden from users, it checks the program uploaded correctly
assert _.title == "transpile_remote_serverless" # noqa: F821
# This cell is hidden from users, it checks the program executes correctly
from time import sleep
from qiskit import QuantumCircuit

qc = QuantumCircuit(2)
transpile_remote_serverless = serverless.load("transpile_remote_serverless")
job = transpile_remote_serverless.run(
circuits=[qc],
backend="ibm_sherbrooke",
optimization_level=1,
)
while True:
sleep(5)
status = job.status()
if status not in ["QUEUED", "INITIALIZING", "RUNNING", "DONE"]:
raise Exception(
f"Unexpected job status: '{status}'\n"
+ "Here are the logs:\n"
+ job.logs()
)
if status == "DONE":
break

Mga susunod na hakbang​

Mga rekomendasyon