54 lines
1.4 KiB
Plaintext
54 lines
1.4 KiB
Plaintext
---
|
||
title: ast_to_dag
|
||
description: API reference for qiskit.converters.ast_to_dag
|
||
in_page_toc_min_heading_level: 1
|
||
python_api_type: function
|
||
python_api_name: qiskit.converters.ast_to_dag
|
||
---
|
||
|
||
# qiskit.converters.ast\_to\_dag
|
||
|
||
<Function id="qiskit.converters.ast_to_dag" isDedicatedPage={true} github="https://github.com/qiskit/qiskit/tree/stable/0.18/qiskit/converters/ast_to_dag.py" signature="ast_to_dag(ast)">
|
||
Build a `DAGCircuit` object from an AST `Node` object.
|
||
|
||
**Parameters**
|
||
|
||
**ast** (*Program*) – a Program Node of an AST (parser’s output)
|
||
|
||
**Returns**
|
||
|
||
the DAG representing an OpenQASM’s AST
|
||
|
||
**Return type**
|
||
|
||
[DAGCircuit](qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit")
|
||
|
||
**Raises**
|
||
|
||
**QiskitError** – if the AST is malformed.
|
||
|
||
**Example**
|
||
|
||
```python
|
||
from qiskit.converters import ast_to_dag
|
||
from qiskit import qasm, QuantumCircuit, ClassicalRegister, QuantumRegister
|
||
from qiskit.visualization import dag_drawer
|
||
%matplotlib inline
|
||
|
||
q = QuantumRegister(3, 'q')
|
||
c = ClassicalRegister(3, 'c')
|
||
circ = QuantumCircuit(q, c)
|
||
circ.h(q[0])
|
||
circ.cx(q[0], q[1])
|
||
circ.measure(q[0], c[0])
|
||
circ.rz(0.5, q[1]).c_if(c, 2)
|
||
qasm_str = circ.qasm()
|
||
ast = qasm.Qasm(data=qasm_str).parse()
|
||
dag = ast_to_dag(ast)
|
||
dag_drawer(dag)
|
||
```
|
||
|
||

|
||
</Function>
|
||
|