Numbers and Adders
Part of Womanium Quantum + AI 2024 program
Introduction
Being able to represent numbers and manipulate them is a crucial part of computers. In classical computers we represent numbers in binary and we use gates such as half-adders and a full-adders to perform mathematical operations on them.
In quantum computers, we will also represent a number in binary, but we need to find a equivalent circuit for a half-adder.
Classical Adder
In classical computers we perform a half-adder on two bits and we output another two bits, summation result and a carry out. The following truth table shows the output of the half-adder for every possible permutation of the bits a and b.
a | b | sum | c_out |
---|---|---|---|
0 | 0 | 0 | 0 |
0 | 1 | 1 | 0 |
1 | 0 | 1 | 0 |
1 | 1 | 0 | 1 |
This gate is implemented using an XOR gate that takes in a and b and results in sum, and an AND gate that takes in a and b and results in c_out. The truth table of the XOR and AND gates proves the correctness of this implementation.
a | b | XOR | AND |
---|---|---|---|
0 | 0 | 0 | 0 |
0 | 1 | 1 | 0 |
1 | 0 | 1 | 0 |
1 | 1 | 0 | 1 |
Quantum Adder
We already know that we can implement a quantum AND gate using a Controlled-X gate where the control qubits are the input and the target qubit is the output.
Implementing a quantum XOR is similar and also uses Controlled-X gates, we first apply a CX from our first qubit to the output qubit then apply another CX from the second qubit to the target, this will flip the state of the target qubit if any of the input qubits are one, but if both are one then they will cancel each other.
Now we can create a half-adder circuit just like we do classically but with the quantum implementation of the XOR and AND gates.
The following circuit shows the implementation of a half-adder, where qubits
In-Place Addition
We notice that the half-adder we constructed stores the result in a new qubit, but we may want to store the result in one of the input qubits instead of using another qubit. We implement that by applying a CX gate where the target is the qubit that will hold the result and controlled by the other input qubit.
We could also add another qubit to store the carry out of the summation. However, since we are applying and in-place operation, it's important to compute the carry out before the sum because the sum will override one of our qubits.
The following circuit shows the implementation of
Counting
One important application of adders is counting the number of qubits that are in the
We may also call this array a quantum number and its size is the number of qubits that it uses.
To count with multiple qubits, we treat the first qubit as the sum and the second as the carry out and the third as the carry out for the last qubit and so on.
Example:
Let's say that we have 3 qubits in the state
We start by checking for an overflow to the last qubit, we do that by using CX gate controlled by the first 2 qubits of the number and the summed qubit, the following pseudo code shows this application
# Example: 1 + 011 -> 111
X(q5).controlled_by(q0, q3, q4)
Then we do the same for the qubit
# Example: 1 + 01 -> 11
X(q4).controlled_by(q0, q3)
and finally we add the qubit to the first qubit:
# Example 1 + 1 -> 0
X(q3).controlled_by(q0)
We apply the same operations to the qubits
For adding the first qubit to the number
The following circuit shows the full implementation of this operation.
Number Checking
After we have a quantum number we will probably need to base other operations of the value of the number, checking and comparing the value of the number works just like working with any binary number, the difference is that we use CX gates to check the value.
Checking for Equality
To check if our quantum number is equal to some value
Example:
Check if a quantum number of 3 qubits
First we convert 5 to binary:
Now we use a CX gate to check if our quantum number is equal to 101. But if we set the first and last qubits as the control and left out the second qubit then we would be checking if the number is equal to 5 or 7 since the second qubit can be either 0 or 1 without affecting the flag.
To solve that we flip all qubits that we expect to be in the zero state, using X gates, then we include them in the control of the CX gate, finally we apply X gates again to the same qubits to return them to their original values.
The following pseudo code show this operation.
X(q1) # flip 0 to 1
X(q4).controlled_by(q0, q1, q2)
X(q1) # return the flipped qubit to 0
And the following is the circuit implementation.
Checking for Inequality
We apply a similar process as we did in the checking for equality but we don't include all qubits in controlling the CX gate to let them be either 0 or 1 such that they still satisfy the condition.
Example:
Check if the a quantum number of 3 qubits
Convert 4 to binary.
We notice that at least the most significant bit (MSB) has to be one, if any other bit is also 1 then the number will be greater than 4.
The following pseudo code checks for the MSB of the quantum number and therefore checking if
X(q3).controlled_by(q2)
To check if the number is less than a number n we can either use the operation as checking for
We can build on the same concepts to check for