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?
2 Answers
You can do this as follow:
- Specify your circuit then transpile it to the backend of interst.
transpile_circuit = transpile(circuit, backend)
- Convert this to DAG (Directed Acylic Graph)
dag_circuit = circuit_to_dag(transpile_circuit)
- 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])
you can transpile this onto ibmq_16_melbourne
and to get:
backend = provider.get_backend('ibmq_16_melbourne')
transpile_circuit = transpile(qc, backend)
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
-
$\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$– aimedacaCommented May 1, 2021 at 19:22
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()