# How to read the elements of a circuit in qiskit?

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.

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

print(circ.data)


or

from pprint import pprint
pprint(vars(circ.data))


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

• 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? Feb 27 '20 at 9:24
• 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. Mar 1 '20 at 8:46
• 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  for the first qubit and so on, wouldn't it? Mar 3 '20 at 8:05
• It should be an object of type Qubit, so  alone will probably not do Mar 4 '20 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.

• 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. Feb 27 '20 at 9:11
• Would it not be possible for the function to return a circuit which is the new circuit? Feb 27 '20 at 13:12
• Yes, that could be possible, I will try a way to do this. Mar 3 '20 at 8:03