# 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
adder = CDKMRippleCarryAdder(n, kind = 'fixed')
print(adder.decompose().decompose().count_ops())


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

controlled_adder = adder.control(1)
print(controlled_adder.decompose().count_ops())


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

controlled_adder = adder.control(1)
print(controlled_adder.decompose().decompose().count_ops())


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 at 22:21

## 1 Answer

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

• 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 ?
– MMM
May 5 at 13:19
• 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 May 5 at 14:34