2
$\begingroup$

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?

$\endgroup$

2 Answers 2

3
$\begingroup$

For subtraction as mentioned earlier you have to negate the number, and than use an adder.

There are few possible implementations for that (adder can be with QFT or with ripple-carry adder with more or less auxiliaries)

Once you use a high-level compiler, you can subtract without the low-level implementation.

You can use the classiq compiler with the following code from this git repo. Also, using different optimization parameters for the compiler will automatically choose the implementation. Regarding the representation of the sign, once the variable is defined as a signed number using QNum[3, True, 1] (True for signed number) you can use the executor to interpret it correctly.

Here is an example:

from classiq import *

@qfunc
def main(a: Output[QNum[3, True,2]], b: Output[QNum[4,False,1]], res: Output[QNum]) -> None:
    allocate(3, a)
    allocate(4, b)
    hadamard_transform(a)
    hadamard_transform(b)

    res |= a-b

qmod = create_model(main)
qprog = synthesize(qmod)
show(qprog)

job = execute(qprog)
result = job.result()[0].value
print(result.parsed_counts)

job.open_in_ide()

You can see how to interpret the results in the IDE at platform.classiq.io or in Python according to the answer here

enter image description here

If you need the qiskit circuit object of, use this command to get the QASM:

QuantumProgram.parse_raw(qprog).transpiled_circuit.qasm

And convert it with the answer here

$\endgroup$
0
$\begingroup$

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

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)

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

circ.draw('mpl')

enter image description here

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

$\endgroup$
5
  • $\begingroup$ Thank you very much $\endgroup$
    – f h
    Commented 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
    Commented 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$ Commented Aug 21, 2023 at 18:23
  • $\begingroup$ You can try (a-b) = (a'+b)', where -a = a'+1, you just have to apply NOT gates in front of 'a' and finally NOT gates at the end in the resulting output. $\endgroup$ Commented Apr 17 at 10:46
  • $\begingroup$ Thank you very much haw excellent $\endgroup$
    – f h
    Commented May 13 at 9:17

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.