I created a DAG circuit in Qiskit and don't really understand how to see which gate is which.

I want to call a function like dag_circuit.gate_info(gate_number), which will then print, for example, that gate number 9 is a hadamard gate $H$, and that gate number 11 is a $U_3(\theta=0.1,\phi=0.2,\lambda=-0.5)$.

How do I get this information?


1 Answer 1


NOTE: At the moment this method is correct, however, this information might be out of date once the 0.9 release of Qiskit is deployed.

Hi Samuel, if you have a dag circuit and you want to access information about the nodes, you need to call dag_circuit.multi_graph.nodes[index_of_node]

The information you can get from a node in the circuit is a dictionary containing the following:

  • type (either "in", "out", or "op")
  • name (either a qubit, classical bit, or an operation name)
  • wire (the Register Object of the qubit or classical bit that the wire is attached to)

An "op" node will will have these other values in addition to the ones above (excluding the "wire" value):

  • 'op' (The data type of the operation)
  • qargs (The qubits this gate is applied to)
  • cargs (The classical bits this gate is applied to)
  • condition

In your example where you have an $H$ Gate at node 9 and a $U_3(\theta=0.1, \phi=0.2, \lambda=-0.5)$ Gate at node 11, if you were to call print(dag_circuit.multi_graph.nodes[8]) and print(dag_circuit.multi_graph.nodes[10]), you would receive the following output:

{'type': 'op', 'op': <qiskit.extensions.standard.h.HGate object at "some_memory_address">, 'name': 'h', 'qargs': [(QuantumRegister(1, 'q'), 0)], 'cargs': [], 'condition': None}
{'type': 'op', 'op': <qiskit.extensions.standard.u3.U3Gate object at "some_memory_address">, 'name': 'u3', 'qargs': [(QuantumRegister(1, 'q'), 0)], 'cargs': [], 'condition': None}

  • $\begingroup$ Hi Matthew, thank you for the quick reply. This is exactly what I was looking for. Now I have 2 more questions: 1. In the developer (GitHub) version of Qiskit, how do I access the same information, as the ".multi_graph.nodes[]" provides? 2. How do I change, for example, a H gate into a U_3 gate? $\endgroup$
    – Samuel
    Commented Apr 10, 2019 at 8:39
  • $\begingroup$ Here is what I mean: circ_dag.multi_graph.nodes[9]['op'] results in: <qiskit.extensions.standard.t.TGate at 0x12096fef0> but I would like to change this into some other gate (like U_3) $\endgroup$
    – Samuel
    Commented Apr 10, 2019 at 9:18
  • 1
    $\begingroup$ I do not think there is any quick and easy way to change 'op' nodes into different gates. The only thing I could think of would be to add a new 'op' node with the desired gate, connect the edges the old 'op' node had to this new one, and remove the old 'op' node. However, this will not fully replace the old node. The new node would be added as the last index, and, in your example, index 9 would be empty. $\endgroup$ Commented Apr 10, 2019 at 14:22
  • $\begingroup$ The following works for me: p = QuantumRegister(1,'p'); repl = QuantumCircuit(p); repl.s(p[0]); circ_dag_repl = qiskit.converters.circuit_to_dag(repl); circ_dag.multi_graph.nodes[i]['op'] = circ_dag_repl.multi_graph.nodes[3]['op'] $\endgroup$
    – Samuel
    Commented Apr 12, 2019 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.