1
$\begingroup$

Is there a command in Qiskit that returns the number of one and/or two qubit gates in a circuit? I need to estimate the cost of a job on Quantinuum's H1-1, and the cost is dependent on this information. My circuit is two large to manually count the gates, and even on IBM devices the QASM output is not printed, basically just to say I have tried the trivial, manual ideas already. Thank you in advance for stopping and helpingHere are the costs, for context so you see exactly what info I am looking for

Edit: I made some custom gates, so the QuantumCircuit.count_ops() command does not work in my case. I am also open to trying Qiskit Runtime, however I would like to know the cost to run the circuit beforehand as well. Thanks again for the help so far!

$\endgroup$

1 Answer 1

1
$\begingroup$

The easiest way to find the number of gates is to use QuantumCircuit.count_ops() which will return a dictionary with the name of the operations present in the circuit as the key and the value is how many times it appears in the circuit: https://qiskit.org/documentation/stubs/qiskit.circuit.QuantumCircuit.count_ops.html#qiskit.circuit.QuantumCircuit.count_ops

However, using that will require you to manually map the name of each operation in the circuit to the number of qubits it uses. For standard gates this is normally pretty easy especially post-compilation when there are only a few basis gates on the target backend. But if the circuit has custom gates or non-standard operations it can be trickier to do this mapping. In those cases the easiest thing you could do is something like:

from collections import defaultdict
counts = defaultdict(int)

for inst in circuit.data:
     counts[len(inst.qubits)] += 1
print(counts)

(note: if you are using qiskit-terra < 0.21.0 the counts line should be counts[len(inst[1])] instead of counts[len(inst.qubits)])

where circuit is your QuantumCircuit object. That will print a dictionary where the key is the number of qubits and the value is the number of operations in the circuit use that many qubits. This will include non-gates like measurements though if you want to limit this to just gates you can add an if isinstance(inst.operation, Gate): (same note if qiskit-terra < 0.21.0 replace inst.operation with inst[0]) before the increment line to only increase the count for Gate objects.

$\endgroup$
3
  • $\begingroup$ Thank you for the quick response Matthew. I tried running that code and this was returned: {'h': 6, 'measure': 6, 'x': 4, 'barrier': 4, 'circuit-1': 1, 'circuit-4': 1, 'circuit-7': 1, 'circuit-10': 1, 'circuit-13': 1, 'circuit-16': 1, 'QFT^-1': 1} defaultdict(<class 'int'>, {1: 0, 14: 0, 9: 0, 6: 0}) As you can see, it was a valiant effort, but did not return the values I was hoping for. I guess a follow up question would be do you know how to estimate how may Qiskit Runtime seconds a circuit would use? Then I could bypass Quantinuum and use IBM instead, if the cost is not prohibitive. $\endgroup$
    – Sam_QC
    Jul 14, 2022 at 13:17
  • 2
    $\begingroup$ @Sam_QC you need to call QuantumCircuit.decompose() prior to count_ops in order to get the “native” gate counts. Sometimes several times, e.g. QuantumCircuit.decompose().decompose().count_ops(). Qiskit groups the operators into “circuit-1” etc., so you have to request it to break it down. You can either print the circuit after decomposing or just look at the returned gate op dict to be sure you are “decomposed” enough $\endgroup$
    – jjgoings
    Jul 16, 2022 at 4:01
  • $\begingroup$ Thanks jjgoings, that worked! $\endgroup$
    – Sam_QC
    Jul 18, 2022 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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