2
$\begingroup$

I would like to know the number and type of all the gates on the wire/qubit with the largest operations in a quantum circuit with multiple qubits. Is there any inbuilt functionality, or what is the shortest way to do so?

$\endgroup$
4
$\begingroup$

You can do this as follow:

  1. Specify your circuit then transpile it to the backend of interst.

transpile_circuit = transpile(circuit, backend)

  1. Convert this to DAG (Directed Acylic Graph)

dag_circuit = circuit_to_dag(transpile_circuit)

  1. count all the operation in the longest path:

dag_circuit.count_ops_longest_path()


Explicit example:

Says you have the circuit

qc = QuantumCircuit(4,4)
for i in range(4):
    qc.h(i)
qc.cx(0,1)
qc.cx(2,3)
qc.ry(2,0)
qc.cx(1,0)
qc.measure([0,1,2,3],[0,1,2,3])

enter image description here

you can transpile this onto ibmq_16_melbourne and to get:

backend = provider.get_backend('ibmq_16_melbourne')
transpile_circuit = transpile(qc, backend)

enter image description here

Now, convert to DAG and counts ops give you:

dag_circuit = circuit_to_dag(transpile_circuit)
dag_circuit.count_ops_longest_path()

Output: {'rz': 4, 'sx': 3, 'cx': 2, 'measure': 1}


It seems to me you want to find out the runtime of the circuit by finding out the longest path, and the operations on that path, then using the device properties to calculate the theoretical runtime.

If that is the case, then you need to extract the gate duration time for all the gate in that longest path, and work out the calculation.


Make sure to import:

from qiskit.dagcircuit import DAGCircuit
from qiskit.converters import circuit_to_dag
from qiskit.compiler import transpile
$\endgroup$
1
  • $\begingroup$ You caught me on my heels: yes, I wanted the theoretical runtime! Note however that the circuit with the longest path need not have the longest runtime, since single and two-qubit gates have orders of magnitude of difference in their execution times :) $\endgroup$
    – quantumdip
    May 1 at 19:22
0
$\begingroup$

Your description sounds like the notion of depth. You can get the depth of a circuit in Qiskit with the depth method: https://qiskit.org/documentation/stubs/qiskit.circuit.QuantumCircuit.html#qiskit.circuit.QuantumCircuit.depth

circuit.depth()
$\endgroup$
2
  • 1
    $\begingroup$ note that depth is just the longest path length. It does not return the operations within the path. $\endgroup$
    – KAJ226
    Apr 29 at 23:30
  • $\begingroup$ Yeap. You answer is more complete. $\endgroup$
    – luciano
    Apr 30 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.