The easiest way to find the number of gates is to use QuantumCircuit.count_ops()
which will return a dictionary with the name of the operations present in the circuit as the key and the value is how many
times it appears in the circuit: https://qiskit.org/documentation/stubs/qiskit.circuit.QuantumCircuit.count_ops.html#qiskit.circuit.QuantumCircuit.count_ops
However, using that will require you to manually map the name of each operation in the circuit to the number of qubits it uses. For standard gates this is normally pretty easy especially post-compilation when there are only a few basis gates on the target backend. But if the circuit has custom gates or non-standard operations it can be trickier to do this mapping. In those cases the easiest thing you could do is something like:
from collections import defaultdict
counts = defaultdict(int)
for inst in circuit.data:
counts[len(inst.qubits)] += 1
print(counts)
(note: if you are using qiskit-terra < 0.21.0 the counts line should be counts[len(inst[1])]
instead of counts[len(inst.qubits)]
)
where circuit
is your QuantumCircuit
object. That will print a dictionary where the key is the number of qubits and the value is the number of operations in the circuit use that many qubits. This will include non-gates like measurements though if you want to limit this to just gates you can add an if isinstance(inst.operation, Gate):
(same note if qiskit-terra < 0.21.0 replace inst.operation
with inst[0]
) before the increment line to only increase the count for Gate objects.