The atoms of computation 1/50
Study notes for The Atoms of Computation
Following the Prerequisites, I installed Python 3.9 and Jupyter Notebooks. Nothing fancy and difficult here. I am new to Jupyter Notebooks, but it seems to be fun and powerful. It took me no time to get why data scientist loves Jupyter Notebooks.
Atoms of computation gives a quick introduction of binary Adder, a concept I already forgot.
One interesting learning is that quantum computers may have some randomness in their results. The simulator by default runs the code 1000 times to show the result as a histogram. I assume high probability means high trustful result.
Now we have a mission to build a Quantum circuit for half adder
This circuits should have two bits input and two bits output. It contains two gates, XOR and AND.
By default, Qiksit qubits are always initialized to zero. To set the input bit to value 1, we can use a NOT gate. The NOT gate is called X
in qubits. This circuit set q_1, q_0 to 01.
qc = QuantumCircuit(2)
qc.x(0)
qc.draw()
The XOR gate is called CX
for Controlled-NOT. Why is it called a Controlled-NOT gate? This is because, using the diagram below for better understanding, q_0 is the control bit, flip q_1 only when q_0 is 1.
Here is an example of an XOR gate. q_0 is the control bit, q_1 is the output of q_0 XOR q_1.
from qiskit import QuantumCircuit
# A quantum circuit with 2 qubits and 2 classical bits(cbits)
qc = QuantumCircuit(2, 2)
# NOT on qubit0, i.e. qubit0=1
qc.x(0)
# Controlled-NOT, XOR on qubit0 and qubit1
qc.cx(0, 1)
# Two classical bit as the output of measure
# Alternatively, qc.measure([0, 1], [0, 1])
qc.measure(0,0) # measure qubit0 into cbit0
qc.measure(1,1) # measure qubit1 into cbit1
qc.draw()
This is the Quantum circuit. q_0=1, q_1=0, therefore the result should be 11.
The AND gate can be implemented using Toffoli gate, or ccx
for NOT gate with two control bits.
qc = QuantumCircuit(3)
qc.ccx(0,1,2)
qc.draw()
Now we have both XOR gate and AND gate. Let’s build the half adder circuit.
In the half adder circuit, we have two input bits and two output bits. The input bits are used twice, firstly as the input to the XOR gate, secondly as the input to the AND gate. There is a need to emulate such setup in quantum circuits.
Here is the half adder calculates 0+1 (q_1,q_0), sum=1(c_0), carry=0(c_1).
qc_ha = QuantumCircuit(4,2)
# set input q_1=0, q_0=1
qc_ha.x(0)
qc_ha.barrier()
# when we apply XOR on input bits,
# we don't want to change their original value
# use a CNOT gate to "copy" q_0 to q_2
qc_ha.cx(0,2)
# use a CNOT gate to get the result for q_2 XOR q_1
qc_ha.cx(1,2)
# note that q_0 and q_1 is not changed
# AND gate on q_0 and q_1
qc_ha.ccx(0,1,3)
qc_ha.barrier()
qc_ha.measure(2,0) # c_0 is the XOR oupput, i.e. sum
qc_ha.measure(3,1) # c_1 is the AND output, i.e. carry
qc_ha.draw()
In the circuit, barrier is a concept I don’t have a confident understanding of. This web page or this video give a good explanation of the concept of barrier. But this page.) answers my question.
References
[1] Qiskit textbook
[2] Difference between Half adder and full adder