# How to get the "number of columns" in a Qiskit quantum circuit?

I would like to compute the number of columns in a Qiskit QuantumCircuit. To be clear about what I mean by number of columns, 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 columns = 5 since I have 5 "serial steps" (in the first layer the $$CY_{q1 \rightarrow q0}$$ and $$SWAP_{q2, q3}$$ gates act in parallel, same for the $$RY$$ and $$I$$ gates in the second layer).

Note that the method QuantumCircuit.depth returns the "length of critical path" (i.e. maximum number of operations on a single qubit/wire). So the circuit depth is in general different from the number of columns I need; in particular, in the example above, qc.depth() evaluates to 3.

• I know you said you want to be clear about what you mean by "layers," but I'm afraid it isn't really clear to me. I mean, the picture you drew is clear and has five "columns," but each of those represents a unitary transformation and any and all of them can be combined into one or more different unitary transformation. For example, you could just define a single unitary transformation for the whole circuit and make a circuit diagram with a single "layer" (a single column/box labelled "U")...
– hft
Mar 4 at 3:22
• If you prefer to call them "columns" instead of "layers" it's fine.. Of course, the number of columns depend on how you group together the different gates (in an arbitrary number of unitary transformations) but I just need the number of columns once the layout of the circuit is fixed Mar 4 at 9:16
• I think I understand. You want a qiskit function, or some other function to return the number of layers specific to the qiskit drawing. Is that right?
– hft
Mar 4 at 19:08
• "I would expect number of layers = 5 since I have 5 serial steps in the computation" the circuit doesn't tell you always the computational steps. I took the liberty to change the question without the use of the word layer. Mar 4 at 19:32

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 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)