# How does one use the Qiskit CDMKRippleCarryAdder?

How does one use (call from a main program) Qiskit's CDKMRippleCarryAdder in a quantum circuit, or for that matter, any other Qiskit library of a similar nature?
For example, how do I create a 3 bit half adder module using the library to be used as part of an oracle for Grover's algorithm?

Side-note: I've programmed for many years in traditional languages like C/C++ but I'm new to python and even newer to Qiskit and I'm very confused about how the parameters get passed in the case of the ripple carry adder library.

I've tried searching for forums, but haven't found any other than this one. There also seems to be many tutorials on basics of quantum gates, but that's not what I'm looking for at the moment.

Here is a code snippet with inline comments for using CDKMRippleCarryAdder

adder = CDKMRippleCarryAdder(3, 'full', 'Full Adder')

# Here we create a quantum register for each operand and another one for the carry in and carry out.
# We can also use a single 8-qubit register.
operand1 = QuantumRegister(3, 'o1')
operand2 = QuantumRegister(3, 'o2')
anc = QuantumRegister(2, 'a')
cr = ClassicalRegister(4)

circ = QuantumCircuit(operand1, operand2, anc, cr)

# For testing purposes we set 1st operand to equal 3
circ.x([operand1, operand1])
# and 2nd operand to equal 5
circ.x([operand2, operand2])

# Note 1: CDKMRippleCarryAdder expects the qubits to have the order: carry in - 1st operand - 2nd operand - carry out.
# Note 2: Python uses '+' operator to combine lists:
circ.append(adder, [anc] + operand1[0:3] + operand2[0:3] + [anc])

# The adder writes the result in place of 2nd operand and carry-out:
circ.measure(operand2[0:3] + [anc], cr)
circ.draw('mpl') To test the previous circuit:

simulator = Aer.get_backend('qasm_simulator')
tr_circ = transpile(circ, basis_gates = ['u3', 'cx'], optimization_level = 3)
result = simulator.run(tr_circ).result()
counts = result.get_counts()

# The result must be 8 (in binary, 1000)
print(list(counts.keys()))

• Thank you so much for a very clear answer! I'll try this out! Jan 5, 2022 at 12:30

CDKMRippleCarryAdder is a quantum circuit, meaning a set of quantum gates operating on some quantum registers consisting of qubits (quantum bits.) The inputs to your adder are two quantum registers, which store qubits representing the numbers you want to add, and some helper qubits depending on the type of the adder (for ex. the default full adder needs an additional qubit for carry-out, as specified in the documentation.)

To use CDKMRippleCarryAdder in your code, you can start by preparing a quantum circuit containing two registers representing your input numbers and necessary helper qubits, and compose it with the adder to create a single coherent circuit:

adder_circuit = CDKMRippleCarryAdder(number_of_qubits_in_input_number)