Skip to main content
IBM Quantum Platform
This page is from an old version of Qiskit SDK and does not exist in the latest version. We recommend you migrate to the latest version. See the release notes for more information.

qiskit.circuit.qpy_serialization.dump

dump(circuits, file_obj)

GitHub

Write QPY binary data to a file

This function is used to save a circuit to a file for later use or transfer between machines. The QPY format is backwards compatible and can be loaded with future versions of Qiskit.

For example:

from qiskit.circuit import QuantumCircuit
from qiskit.circuit import qpy_serialization
 
qc = QuantumCircuit(2, name='Bell', metadata={'test': True})
qc.h(0)
qc.cx(0, 1)
qc.measure_all()

from this you can write the qpy data to a file:

with open('bell.qpy', 'wb') as fd:
    qpy_serialization.dump(qc, fd)

or a gzip compressed file:

import gzip
 
with gzip.open('bell.qpy.gz', 'wb') as fd:
    qpy_serialization.dump(qc, fd)

Which will save the qpy serialized circuit to the provided file.

Parameters

  • circuits (list or QuantumCircuit) – The quantum circuit object(s) to store in the specified file like object. This can either be a single QuantumCircuit object or a list of QuantumCircuits.
  • file_obj (file) – The file like object to write the QPY data too
Was this page helpful?
Report a bug or request content on GitHub.