דלג לתוכן הראשי
פרויקט זה בשלב בטא. מצאת באג? פתח issue ב-GitHub. יש שאלה או רעיון? שאל ב-Discussions — נשמח לשמוע ממך!
מה איכות התרגום בדף זה?

שמירת Circuit-ים לדיסק

Package versions

הקוד בעמוד זה פותח באמצעות הדרישות הבאות. אנו ממליצים להשתמש בגרסאות אלו או חדשות יותר.

qiskit[all]~=2.5.2

השתמש ב-QPY serialization כדי לשמור את ה-Circuit שלך לקובץ. קבצי QPY שומרים את אובייקט ה-Circuit המלא של Qiskit ויהיו תואמים לגרסאות חדשות יותר של Qiskit (אך לא בהכרח לגרסאות ישנות יותר של Qiskit).

להדגמה, התא הבא יוצר Circuit קוונטי פשוט.

# Added by doQumentation — installs the packages this notebook needs if they are missing
import importlib.util

_needed = {"qiskit": "qiskit"}
_missing = [pip for module, pip in _needed.items()
if importlib.util.find_spec(module) is None]
# One at a time, so a package that fails to install does not block the others
for _pip in _missing:
%pip install -q {_pip}
if not _missing:
print("\u2713 All required packages are installed")
from qiskit import QuantumCircuit

qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()

כדי לשמור קובץ זה לדיסק, השתמש בפונקציה qpy.dump. אתה יכול גם לשמור רשימה של Circuit-ים.

from qiskit import qpy

with open("test.qpy", "wb") as file:
qpy.dump(qc, file)

Circuit זה נשמר כעת לקובץ test.qpy. אם תפעיל מחדש את ה-kernel של Python שלך, תוכל לטעון מחדש את ה-Circuit באמצעות הפונקציה qpy.load. שים לב שפונקציה זו תמיד מחזירה רשימה של Circuit-ים, גם אם ביצעת סריאליזציה ל-Circuit אחד בלבד.

with open("test.qpy", "rb") as handle:
qc = qpy.load(handle)

qc[0].draw("mpl")

Output of the previous code cell