1
$\begingroup$

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

enter image description here

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.

$\endgroup$
4
  • $\begingroup$ 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")... $\endgroup$
    – hft
    Commented Mar 4, 2023 at 3:22
  • $\begingroup$ 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 $\endgroup$ Commented Mar 4, 2023 at 9:16
  • $\begingroup$ 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? $\endgroup$
    – hft
    Commented Mar 4, 2023 at 19:08
  • 1
    $\begingroup$ "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. $\endgroup$ Commented Mar 4, 2023 at 19:32

1 Answer 1

3
$\begingroup$

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

enter image description here

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)
$\endgroup$

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.