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?


1 Answer 1


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
from qiskit.circuit.library import VBERippleCarryAdder

num_state_qubits = 3
adder = VBERippleCarryAdder(num_state_qubits, name=' Adder ')
num_qubits = len(adder.qubits)

circ = QuantumCircuit(num_qubits)

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

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

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

# 2) add one (using carry-in)

# 3) do the addition
circ.append(adder, range(num_qubits))


enter image description here

If you run this circuit the output will be: $001{\color{red}0\color{red}1\color{red}0}1011$

  • $\begingroup$ Thank you very much $\endgroup$
    – f h
    Jul 23, 2023 at 20:34
  • $\begingroup$ Haw great, But in Laplacian's circuit, the subtrahend are not clear so that we can't calculate its inverse because We are still in the middle of the circuit and we have not measured $\endgroup$
    – f h
    Jul 23, 2023 at 21:02
  • 1
    $\begingroup$ 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). $\endgroup$ Aug 21, 2023 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.