# Counting the number of CCX and CX gates in controlled modular addition

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

n=3


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?).

• Could you provide some more context for your question? And perhaps some example code of what you've tried so far? May 4, 2022 at 22:21

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

• 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 ?