회로를 디스크에 저장하기
패키지 버전
이 페이지의 코드는 다음 요구 사항을 사용하여 개발되었습니다. 이 버전 이상을 사용하길 권장합니다.
qiskit[all]~=2.6.1
QPY 직렬화를 사용하여 회로를 파일로 저장하세요. QPY 파일은 전체 Qiskit 회로 객체를 저장하며 이후(더 새로운) 버전의 Qiskit과 호환돼요 (단, 이전 버전의 Qiskit과 반드시 호환되는 것은 아니에요).
예시로, 다음 셀에서 간단한 양자 회로를 만들어요.
# 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 함수를 사용하세요. 회로 목록을 저장할 수도 있습니다.
from qiskit import qpy
with open("test.qpy", "wb") as file:
qpy.dump(qc, file)
이 회로는 이제 test.qpy 파일에 저장됐어요. Python 커널을 재시작하면 qpy.load 함수를 사용하여 회로를 다시 불러올 수 있어요. 회로를 하나만 직렬화했더라도 항상 회로 목록이 반환된다는 점에 유의하세요.
with open("test.qpy", "rb") as handle:
qc = qpy.load(handle)
qc[0].draw("mpl")