I would like to know if there is a way to read the elements of a circuit. What I mean is if I have a QuantumCircuit object, can I obtain the gates that are applied to each qubit in order?

I would like to implement certain noise gate after each Hadamard and I would have therefore to identify each Hadamard in the QuantumCircuit object. So I need a function that gives me all the gates in a circuit and that allows me to modificate what it returns so I can construct a modified quantum circuit.


2 Answers 2


You can play with the circuit's data. First write:



from pprint import pprint

Then you'll understand the structure and how to modify it.

  • $\begingroup$ For example, I obtain this for a simple circuit: [(<qiskit.extensions.standard.x.XGate object at 0x00000241AA8F7E48>, [Qubit(QuantumRegister(2, 'q'), 0)], []), (<qiskit.extensions.standard.h.HGate object at 0x00000241AA8F78D0>, [Qubit(QuantumRegister(2, 'q'), 0)], []), (<qiskit.extensions.standard.cx.CnotGate object at 0x00000241AA967470>, [Qubit(QuantumRegister(2, 'q'), 0), Qubit(QuantumRegister(2, 'q'), 1)], [])] How would you add, for example, an X gate after the H one? $\endgroup$
    – Paula G
    Commented Feb 27, 2020 at 9:24
  • $\begingroup$ First get the qubit register by circ.qreg. Then create a new list of instructions new_data. Copy the elements of data to new_data, where after the H gate you insert a triplet (XGate(), [a reference to the qubit in the quantum register] ,[]). I agree that this requires some hacking, but I've done it several times in the past (just can't find an example right now) and it's much simpler than it looks. You need to play a little bit with the objects and it works. At the end circ.data = new_data. $\endgroup$ Commented Mar 1, 2020 at 8:46
  • $\begingroup$ I will try to do this. What I have to do is to insert this kind of triplet object, where [a reference to the qubit in the quantum register] would be for example [0] for the first qubit and so on, wouldn't it? $\endgroup$
    – Paula G
    Commented Mar 3, 2020 at 8:05
  • $\begingroup$ It should be an object of type Qubit, so [0] alone will probably not do $\endgroup$ Commented Mar 4, 2020 at 9:18

I would recommend doing this by converting the circuit to a DAGCircuit and then using the topological_op_nodes() property, which will return all the operations in the DAG in order. I would also suggest instead of trying to modify the circuit in place, you create a new one.

To convert a circuit to a DAG you can use the function

from qiskit.converter import circuit_to_dag
dag = circuit_to_dag(your_circuit)

And then you can iterate over the nodes

for node in dag.topological_op_nodes():
    if node.name == 'H':
       ~do something~

The objects returned are DAGNodes and contain all the information for you to be able to add the gate to a new circuit.

  • $\begingroup$ Thank you so much for your help! I was wondering how could I add some gates. I cannot create a new circuit as was I want to do is to create a function that when you give a quantum circuit as an argument, it modifies it by adding certain gates depending on the gates used in the circuit, so I need to used the old one and modify it. $\endgroup$
    – Paula G
    Commented Feb 27, 2020 at 9:11
  • $\begingroup$ Would it not be possible for the function to return a circuit which is the new circuit? $\endgroup$
    – met927
    Commented Feb 27, 2020 at 13:12
  • 1
    $\begingroup$ Yes, that could be possible, I will try a way to do this. $\endgroup$
    – Paula G
    Commented Mar 3, 2020 at 8:03

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.