Suppose I have a QuantumCircuit object named FC, which explicitly contains the identity gate, and convert it into a gate using .to_instruction():

gate = FC.to_instruction()

and I use this to create a bigger QuantumCircuit object named circuit, which looks like this:

enter image description here

Now, when I iterate over the elements of the circuit, using this code:

for instruction, qarg, carg in circuit:

I get this:

Instruction(name='gate', num_qubits=3, num_clbits=0, params=[])
Instruction(name='gate', num_qubits=3, num_clbits=0, params=[])

However, gate itself is made up of its constituent elements, which don't show up in the above list. How do I "collapse" the circuit, so that I can iterate over all the components present in circuit, rather than just gate? One way you can go about doing this is to transpile the circuit like this:

from qiskit import Aer, transpile

simulator = Aer.get_backend('aer_simulator')
circ = transpile(circ, simulator)

and iterate over circ, but when I do that, the identity gate does not show up.


1 Answer 1


You can achieve that by calling QuantumCircuit's decompose() method as follows:

for instruction, qarg, carg in circuit.decompose():

Another option is to add FC to your circuit using compose method intead of append:

circuit = circuit.compose(FC, [0, 1, 2])

Note that you should use FC directly without converting it into a gate.


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.