58 lines
2.3 KiB
Plaintext
58 lines
2.3 KiB
Plaintext
---
|
||
title: dump
|
||
description: API reference for qiskit.qpy.dump
|
||
in_page_toc_min_heading_level: 1
|
||
python_api_type: function
|
||
python_api_name: qiskit.qpy.dump
|
||
---
|
||
|
||
# qiskit.qpy.dump
|
||
|
||
<Function id="qiskit.qpy.dump" isDedicatedPage={true} github="https://github.com/qiskit/qiskit/tree/stable/0.23/qiskit/qpy/interface.py" signature="dump(programs, file_obj, metadata_serializer=None)">
|
||
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:
|
||
|
||
```python
|
||
from qiskit.circuit import QuantumCircuit
|
||
from qiskit import qpy
|
||
|
||
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:
|
||
|
||
```python
|
||
with open('bell.qpy', 'wb') as fd:
|
||
qpy.dump(qc, fd)
|
||
```
|
||
|
||
or a gzip compressed file:
|
||
|
||
```python
|
||
import gzip
|
||
|
||
with gzip.open('bell.qpy.gz', 'wb') as fd:
|
||
qpy.dump(qc, fd)
|
||
```
|
||
|
||
Which will save the qpy serialized circuit to the provided file.
|
||
|
||
**Parameters**
|
||
|
||
* **programs** (`Union`\[`List`\[`Union`\[[`QuantumCircuit`](qiskit.circuit.QuantumCircuit "qiskit.circuit.quantumcircuit.QuantumCircuit"), [`ScheduleBlock`](qiskit.pulse.ScheduleBlock "qiskit.pulse.schedule.ScheduleBlock")]], [`QuantumCircuit`](qiskit.circuit.QuantumCircuit "qiskit.circuit.quantumcircuit.QuantumCircuit"), [`ScheduleBlock`](qiskit.pulse.ScheduleBlock "qiskit.pulse.schedule.ScheduleBlock")]) – QPY supported object(s) to store in the specified file like object. QPY supports [`QuantumCircuit`](qiskit.circuit.QuantumCircuit "qiskit.circuit.QuantumCircuit") and [`ScheduleBlock`](qiskit.pulse.ScheduleBlock "qiskit.pulse.ScheduleBlock"). Different data types must be separately serialized.
|
||
* **file\_obj** (`BinaryIO`) – The file like object to write the QPY data too
|
||
* **metadata\_serializer** (`Optional`\[`Type`\[`JSONEncoder`]]) – An optional JSONEncoder class that will be passed the `.metadata` attribute for each program in `programs` and will be used as the `cls` kwarg on the json.dump()\` call to JSON serialize that dictionary.
|
||
|
||
**Raises**
|
||
|
||
* **QpyError** – When multiple data format is mixed in the output.
|
||
* **TypeError** – When invalid data type is input.
|
||
</Function>
|
||
|