# Does subtraction circuit have a class like full or half Carry adder circuit in qiskit? one subtraction circuit code-example in qiskit?

I need a subtraction circuit to calculate Laplacian in edge processing, but I didn't find any class in qiskit.Do I have to write the code myself?

A full-adder circuit can be used for subtraction by using the fact that: $$a - b = a + \neg b + 1$$ So, you need to invert the subtrahend, increment it by one, then do the addition as usual. As an example, the following code uses VBERippleCarryAdder to subtract 3 from 5:

from qiskit import QuantumCircuit

num_state_qubits = 3

circ = QuantumCircuit(num_qubits)

# Five
circ.x([1, 3])

# Three
circ.x([num_state_qubits + 1, num_state_qubits + 2])

circ.barrier()
# Subtraction:
# 1) invert b:
circ.x(range(num_state_qubits + 1, 2 * num_state_qubits + 1))

# 2) add one (using carry-in)
circ.x(0)


If you run this circuit the output will be: $$001{\color{red}0\color{red}1\color{red}0}1011$$
• It's simpler to temporarily bit flip the target rather than the offset, as this avoids having to deal with the +1 offset. (a -= b) === (a ^= -1; a += b; a ^= -1). Aug 21, 2023 at 18:23