4
$\begingroup$

How to make a half adder for N inputs and getting the output on another x+1 output. Where x is numbers of qubit and N=2^x. please take a example of a circuit(x must be greater then 4 or 5 for example purpose ) for more understanding . Here is the example for 2 qubit half adder.

$\endgroup$

1 Answer 1

4
$\begingroup$

Qiskit includes an adder in the circuit library. Here is a quick introduction to it.

Let's say you wan to add two numbers, one of them is three-bit-long, the other one two-bit-long numbers. You will need a 5 qubit adder. That's the first parameter to qiskit.circuit.library.WeightedAdder. The second parameter is the weight of each of these qubits. Because the fist value is composed by $q_0 \times 2^0 + q_1 \times 2^1 + q_2 \times 2^2$, each of the weights is $2^0$, $2^1$, and $2^2$. The same applies to the second two-bit summand.

from qiskit.circuit.library.arithmetic import WeightedAdder
circuit = WeightedAdder(5, [1, 2, 4, 1, 2])

This creates a circuit, partially printed here:

circuit.draw()
  state_0: ──■────■────■──────────■────■──────────■────■─────────■────────»
             │    │    │          │    │          │    │         │        »
  state_1: ──┼────┼────┼──────────┼────┼──────────┼────┼─────────┼────────»
             │    │    │          │    │          │    │         │        »
  state_2: ──┼────┼────┼──────────┼────┼──────────┼────┼─────────┼────────»
             │    │    │          │    │          │    │         │        »
  state_3: ──┼────┼────┼──────────┼────┼──────────┼────┼─────────┼────────»
             │    │    │          │    │          │    │         │        »
  state_4: ──┼────┼────┼──────────┼────┼──────────┼────┼─────────┼────────»
             │  ┌─┴─┐  │   ┌───┐  │    │          │    │         │        »
    sum_0: ──■──┤ X ├──┼───┤ X ├──┼────┼──────────┼────┼─────────┼────────»
             │  └───┘  │   └───┘┌─┴─┐  │   ┌───┐  │    │         │        »
    sum_1: ──┼─────────■────────┤ X ├──┼───┤ X ├──┼────┼─────────┼────────»
             │         │        └─┬─┘  │   └───┘┌─┴─┐  │  ┌───┐  │   ┌───┐»
    sum_2: ──┼─────────┼──────────┼────■────────┤ X ├──┼──┤ X ├──■───┤ X ├»
             │         │          │    │        └─┬─┘┌─┴─┐└───┘  │   └───┘»
    sum_3: ──┼─────────┼──────────┼────┼──────────┼──┤ X ├───────┼────────»
           ┌─┴─┐       │          │    │          │  └─┬─┘       │        »
  carry_0: ┤ X ├───────■──────────■────┼──────────┼────┼─────────┼────────»
           └───┘     ┌─┴──┐            │          │    │         │        »
  carry_1: ──────────┤0   ├────────────■──────────■────┼─────────■────────»
                     │    │          ┌─┴──┐            │       ┌─┴──┐     »
  carry_2: ──────────┤  X ├──────────┤0   ├────────────■───────┤0   ├─────»
                     │    │          │  X │                    │  X │     »
control_0: ──────────┤1   ├──────────┤1   ├────────────────────┤1   ├─────»
                     └────┘          └────┘                    └────┘     »

Let's now measure the quantum registers sum and carry to extract the result of the computation:

from qiskit import ClassicalRegister

sum_result = ClassicalRegister(4, name='sum_result')
carry_result = ClassicalRegister(3, name='carry_result')
circuit.add_register(sum_result)
circuit.add_register(carry_result)
q_sum = circuit.qregs[1]
q_carry = circuit.qregs[2]

circuit.measure(q_sum, sum_result)
circuit.measure(q_carry, carry_result)

And now, let's create value summands. For example, $4$ (three-bit length) and $2$ (two-bit length)

from qiskit import QuantumCircuit

value1 = QuantumCircuit(3, name='value1')
value2 = QuantumCircuit(2, name='value2')

# bin(4) == 0b100
value1.x(2)

# bin(2) == 0b10
value2.x(1)

The inputs should be appended to the front of the adder circuit, like this:

circuit.compose(value1.to_gate(), qubits=[0, 1 ,2], front=True, inplace=True)
circuit.compose(value2.to_gate(), qubits=[3, 4], front=True, inplace=True)

circuit.draw()
                ┌─────────┐                                                    »
       state_0: ┤0        ├──■────■────■──────────■────■──────────■────■───────»
                │         │  │    │    │          │    │          │    │       »
       state_1: ┤1 value1 ├──┼────┼────┼──────────┼────┼──────────┼────┼───────»
                │         │  │    │    │          │    │          │    │       »
       state_2: ┤2        ├──┼────┼────┼──────────┼────┼──────────┼────┼───────»
                ├─────────┤  │    │    │          │    │          │    │       »
       state_3: ┤0        ├──┼────┼────┼──────────┼────┼──────────┼────┼───────»
                │  value2 │  │    │    │          │    │          │    │       »
       state_4: ┤1        ├──┼────┼────┼──────────┼────┼──────────┼────┼───────»
                └─────────┘  │  ┌─┴─┐  │   ┌───┐  │    │          │    │       »
         sum_0: ─────────────■──┤ X ├──┼───┤ X ├──┼────┼──────────┼────┼───────»
                             │  └───┘  │   └───┘┌─┴─┐  │   ┌───┐  │    │       »
         sum_1: ─────────────┼─────────■────────┤ X ├──┼───┤ X ├──┼────┼───────»
                             │         │        └─┬─┘  │   └───┘┌─┴─┐  │  ┌───┐»
         sum_2: ─────────────┼─────────┼──────────┼────■────────┤ X ├──┼──┤ X ├»
                             │         │          │    │        └─┬─┘┌─┴─┐└───┘»
         sum_3: ─────────────┼─────────┼──────────┼────┼──────────┼──┤ X ├─────»
                           ┌─┴─┐       │          │    │          │  └─┬─┘     »
       carry_0: ───────────┤ X ├───────■──────────■────┼──────────┼────┼───────»
                           └───┘     ┌─┴──┐            │          │    │       »
       carry_1: ─────────────────────┤0   ├────────────■──────────■────┼───────»
                                     │    │          ┌─┴──┐            │       »
       carry_2: ─────────────────────┤  X ├──────────┤0   ├────────────■───────»
                                     │    │          │  X │                    »
     control_0: ─────────────────────┤1   ├──────────┤1   ├────────────────────»
                                     └────┘          └────┘                    »
  sum_result: 4/═══════════════════════════════════════════════════════════════»
                                                                               »
carry_result: 3/═══════════════════════════════════════════════════════════════»
                                                                               »

Finally, it is time to run the circuit and see how it works:

from qiskit import BasicAer, execute
backend = BasicAer.get_backend('qasm_simulator')

result = execute(circuit, backend).result()

result.get_counts()
{'000 0110': 1024}

The result is $4+2 = 6$ (int('0110', base=2)).

$\endgroup$

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.