2
$\begingroup$

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.

Thanks in advance for your help.

$\endgroup$

2 Answers 2

2
$\begingroup$

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[0], operand1[1]])
# and 2nd operand to equal 5
circ.x([operand2[0], operand2[2]])

# 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[0]] + operand1[0:3] + operand2[0:3] + [anc[1]])

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

enter image description here

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())[0])
$\endgroup$
1
  • 1
    $\begingroup$ Thank you so much for a very clear answer! I'll try this out! $\endgroup$ Jan 5, 2022 at 12:30
0
$\begingroup$

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)
circuit_with_adder = my_initial_circuit.compose(adder_circuit)

If the above appears complicated, I would definitely recommend Qiskit textbook (see chapter on Defining Quantum Circuits) as well as introductory tutorials (that you can also play with interactively in IBM Quantum Lab.)

$\endgroup$

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.