If we do a controlled modular addition of an adder with 2n ccx and 4n cx gates for example the CDKMRippleCarryAdder, what will be the resultant count of ccx and cx gates ? Exactly speaking what will be the count of number of ccx and cx gates when we call the circuit with CDKMRippleCarryAdder.control(1).

adder = CDKMRippleCarryAdder(n, kind = 'fixed')

gives OrderedDict([('cx', 12), ('ccx', 6)])

controlled_adder = adder.control(1)

gives OrderedDict([('ccx', 48), ('cp', 42), ('cu', 12)])

controlled_adder = adder.control(1)

gives OrderedDict([('cx', 396), ('t', 192), ('p', 162), ('tdg', 144), ('h', 96), ('u', 24)])

What is the correct way to measure cx and ccx gates ? How should we actually measure the number of gates required for these circuits? And what will be or how to find the depth of adder and controlled_adder in terms of $n$ (what level of decompose should be applied?).

  • 1
    $\begingroup$ Could you provide some more context for your question? And perhaps some example code of what you've tried so far? $\endgroup$
    – ryanhill1
    May 4 at 22:21

1 Answer 1


Usually, we care about number of CX gates because CCX gates can be decomposed into CX and single-qubit gates.

That said, you can use Unroller[1] transpiler pass to decompose the circuit with CCX included as one of the basis gates. Then use QuantumCircuit.count_ops()[2] method to get number of gates of each type, and QuantumCircuit.depth()[3] to get the circuit depth, for a given value of $n$:

from qiskit.circuit.library.arithmetic.adders import CDKMRippleCarryAdder
from qiskit.transpiler.passes import Unroller
from qiskit.converters import circuit_to_dag, dag_to_circuit

n = 5
adder = CDKMRippleCarryAdder(n, 'full', 'Full Adder')
controlled_adder = adder.control(1)

# Specify the target basis gate set. include CCX
unroller = Unroller(basis=['u', 'cx', 'ccx'])
unrolled_controlled_adder = dag_to_circuit(unroller.run(circuit_to_dag(controlled_adder)))

print('Operations:', dict(unrolled_controlled_adder.count_ops()))
print('Depth:', unrolled_controlled_adder.depth())
  • $\begingroup$ Yes this works. But the result won't give me the count of cx gates, if I decompose again, I see cx gates with other gates. When we find the gate resource estimates for $controlled\_adder$ what should be the correct value ? $\endgroup$
    – MMM
    May 5 at 13:19
  • $\begingroup$ I updated my answer to cover this point. Note that, the controlled version of the adder contains gates other than CX and CCX because of how Qiskit's implements multi-cx gate $\endgroup$ May 5 at 14:34

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.