I would like to compute the number of layers in a Qiskit
QuantumCircuit
. To be clear about what I mean by number of layers, consider the following circuit as an example:from qiskit.circuit.random import random_circuit qc = random_circuit(4, 3, seed=0) qc.draw('mpl')
In this case, I would expect number of layers = 5 ...
In the ascii art TextDrawing (ascii art) representation of the same circuit, the number of "layers" (or "columns" in the updated question) corresponds to the number of nodes. Here is a function that will return the number of "layers" (or "columns") shown in the diagram:
def num_layers(qc):
return len(qc.draw(output='text').nodes)