2021-04-13 The atoms of computation

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值