Skip to main contentIBM Quantum Documentation

Circuit Converters

qiskit.converters

circuit_to_dag

qiskit.converters.circuit_to_dag(circuit, copy_operations=True, *, qubit_order=None, clbit_order=None)GitHub(opens in a new tab)

Build a DAGCircuit object from a QuantumCircuit.

Parameters

  • circuit (QuantumCircuit) – the input circuit.
  • copy_operations (bool(opens in a new tab)) – Deep copy the operation objects in the QuantumCircuit for the output DAGCircuit. This should only be set to False if the input QuantumCircuit will not be used anymore as the operations in the output DAGCircuit will be shared instances and modifications to operations in the DAGCircuit will be reflected in the QuantumCircuit (and vice versa).
  • qubit_order (Iterable[Qubit] or None) – the order that the qubits should be indexed in the output DAG. Defaults to the same order as in the circuit.
  • clbit_order (Iterable[Clbit] or None) – the order that the clbits should be indexed in the output DAG. Defaults to the same order as in the circuit.

Returns

the DAG representing the input circuit.

Return type

DAGCircuit

Raises

ValueError(opens in a new tab) – if the qubit_order or clbit_order parameters do not match the bits in the circuit.

Example

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit.dagcircuit import DAGCircuit
from qiskit.converters import circuit_to_dag
 
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)
dag = circuit_to_dag(circ)

dag_to_circuit

qiskit.converters.dag_to_circuit(dag, copy_operations=True)GitHub(opens in a new tab)

Build a QuantumCircuit object from a DAGCircuit.

Parameters

Returns

the circuit representing the input dag.

Return type

QuantumCircuit

Example

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit.dagcircuit import DAGCircuit
from qiskit.converters import circuit_to_dag
from qiskit.circuit.library.standard_gates import CHGate, U2Gate, CXGate
from qiskit.converters import dag_to_circuit
 
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)
dag = circuit_to_dag(circ)
circuit = dag_to_circuit(dag)
circuit.draw('mpl')
../_images/converters-1.png

circuit_to_instruction

qiskit.converters.circuit_to_instruction(circuit, parameter_map=None, equivalence_library=None, label=None)GitHub(opens in a new tab)

Build an Instruction object from a QuantumCircuit.

The instruction is anonymous (not tied to a named quantum register), and so can be inserted into another circuit. The instruction will have the same string name as the circuit.

Parameters

  • circuit (QuantumCircuit) – the input circuit.
  • parameter_map (dict(opens in a new tab)) – For parameterized circuits, a mapping from parameters in the circuit to parameters to be used in the instruction. If None, existing circuit parameters will also parameterize the instruction.
  • equivalence_library (EquivalenceLibrary) – Optional equivalence library where the converted instruction will be registered.
  • label (str(opens in a new tab)) – Optional instruction label.

Raises

QiskitError – if parameter_map is not compatible with circuit

Returns

an instruction equivalent to the action of the input circuit. Upon decomposition, this instruction will yield the components comprising the original circuit.

Return type

qiskit.circuit.Instruction

Example

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit.converters import circuit_to_instruction
 
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)
circuit_to_instruction(circ)

circuit_to_gate

qiskit.converters.circuit_to_gate(circuit, parameter_map=None, equivalence_library=None, label=None)GitHub(opens in a new tab)

Build a Gate object from a QuantumCircuit.

The gate is anonymous (not tied to a named quantum register), and so can be inserted into another circuit. The gate will have the same string name as the circuit.

Parameters

  • circuit (QuantumCircuit) – the input circuit.
  • parameter_map (dict(opens in a new tab)) – For parameterized circuits, a mapping from parameters in the circuit to parameters to be used in the gate. If None, existing circuit parameters will also parameterize the Gate.
  • equivalence_library (EquivalenceLibrary) – Optional equivalence library where the converted gate will be registered.
  • label (str(opens in a new tab)) – Optional gate label.

Raises

QiskitError – if circuit is non-unitary or if parameter_map is not compatible with circuit

Returns

a Gate equivalent to the action of the input circuit. Upon decomposition, this gate will yield the components comprising the original circuit.

Return type

Gate

dagdependency_to_circuit

qiskit.converters.dagdependency_to_circuit(dagdependency)GitHub(opens in a new tab)

Build a QuantumCircuit object from a DAGDependency.

Parameters

dagdependency (DAGDependency) – the input dag.

Returns

the circuit representing the input dag dependency.

Return type

QuantumCircuit

circuit_to_dagdependency

qiskit.converters.circuit_to_dagdependency(circuit, create_preds_and_succs=True)GitHub(opens in a new tab)

Build a DAGDependency object from a QuantumCircuit.

Parameters

Returns

the DAG representing the input circuit as a dag dependency.

Return type

DAGDependency

dag_to_dagdependency

qiskit.converters.dag_to_dagdependency(dag, create_preds_and_succs=True)GitHub(opens in a new tab)

Build a DAGDependency object from a DAGCircuit.

Parameters

Returns

the DAG representing the input circuit as a dag dependency.

Return type

DAGDependency

dagdependency_to_dag

qiskit.converters.dagdependency_to_dag(dagdependency)GitHub(opens in a new tab)

Build a DAGCircuit object from a DAGDependency.

Parameters

dependency (dag) – the input dag.

Returns

the DAG representing the input circuit.

Return type

DAGCircuit

Was this page helpful?