1 qiskit库:
========
1.1 目前IBM推出的qiskit是非常棒的Python量子模拟器。
1.2 量子学,我们国家目前世界领先,量子计算,量子计算机,量子通信逐步在应用,今天就是介绍python也能进入该领域,讲讲皮毛,一秒入门。
1.3 官网
https://qiskit.org/
https://github.com/Qiskit
1.4 安装:
pip install qiskit #官方推荐
sudo pip3.8 install qiskit #本机安装,太慢了
#本机实际安装
sudo pip3.8 install -i https://pypi.tuna.tsinghua.edu.cn/simple qiskit
2 说明:
=====
2.1 官网代码基于Jupyter Notebook。
2.2 本机是deepin-linux操作系统,python3.8和微软vscode编辑器。
2.3 想用matplotlib显示图片,需要额外安装:
pip install pylatexenc
#本机实际如下安装
sudo pip3.8 install pylatexenc
3 你好量子:
========
3.1 代码:1-hello_quantum.py代码
#导入模块
from qiskit import QuantumCircuit
from qiskit.tools.visualization import circuit_drawer
import matplotlib.pyplot as plt
#量子线圈设置
#新建一个量子线圈(线路图)
qc = QuantumCircuit(2, 2)
#在q0上设置h,qubit 0上的h,也可以设置在qubit 1
qc.h(0)
# Add a CX (CNOT) gate on control qubit 0 and target qubit 1, putting
# the qubits in a Bell state.
#qubit 0和qubit 1设置控制开关,由0-1
#也可以有1-0设置
qc.cx(0, 1)
# Add a Measure gate to see the state.
qc.measure([0, 1], [0, 1])
#图片保存和展示
#文件名
picsave='1.png'
#保存在根目录下
circuit_drawer(qc,output='mpl',filename=picsave)
#根目录下
imge = plt.imread('/home/xgj/' picsave)
plt.imshow(imge)
plt.show()
3.2 图
3.3 代码还可以写成:
from qiskit import QuantumCircuit
from qiskit.tools.visualization import circuit_drawer
import matplotlib.pyplot as plt
def build_bell_circuit():
qc = QuantumCircuit(2, 2)
qc.h(0)
qc.cx(0, 1)
qc.measure([0, 1], [0, 1])
return qc
bell_circuit = build_bell_circuit()
#文件名
picsave='1-1.png'
#保存在根目录下
circuit_drawer(bell_circuit,output='mpl',filename=picsave)
#根目录下
imge = plt.imread('/home/xgj/' picsave)
plt.imshow(imge)
plt.show()
4 傅里叶变换函数qft:
================
4.1 qft.py代码:
import math
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit.tools.visualization import circuit_drawer
import matplotlib.pyplot as plt
# 定义量子傅里叶变换函数qft
def qft(circ, q, n):
for j in range(n):
for k in range(j):
# 套用傅里叶就换
circ.cp(math.pi/float(2**(j-k)), q[j], q[k])
circ.h(q[j])
# 量子比特数量
n = 3
q = QuantumRegister(n)
c = ClassicalRegister(n)
# 定义量子电路,后面可以可视化
qft_n = QuantumCircuit(q, c)
qft(qft_n, q, n)
for i in range(n):
qft_n.measure(q[i], c[i])
#文件名
picsave='2.png'
#保存在根目录下
circuit_drawer(qft_n,output='mpl',filename=picsave)
#根目录下
imge = plt.imread('/home/xgj/' picsave)
plt.imshow(imge)
plt.show()
4.2 图
5 通信关系:
========
5.1 代码:commutation_relation.py
from qiskit import QuantumCircuit
from qiskit.transpiler import PassManager
from qiskit.transpiler.passes import CommutationAnalysis, CommutativeCancellation
from qiskit.tools.visualization import circuit_drawer
import matplotlib.pyplot as plt
circuit = QuantumCircuit(5)
circuit.cx(0, 1)
circuit.cx(2, 1)
circuit.cx(4, 3)
circuit.cx(2, 3)
circuit.z(0)
circuit.z(4)
circuit.cx(0, 1)
circuit.cx(2, 1)
circuit.cx(4, 3)
circuit.cx(2, 3)
circuit.cx(3, 2)
#print(circuit) #终端画图
pm = PassManager()
pm.append([CommutationAnalysis(), CommutativeCancellation()])
new_circuit = pm.run(circuit)
#print(new_circuit) #终端画图
#文件名
picsave1='3-1.png'
circuit_drawer(circuit,output='mpl',filename=picsave1)
picsave2='3-2.png'
circuit_drawer(new_circuit,output='mpl',filename=picsave2)
#根目录下
imge1 = plt.imread('/home/xgj/' picsave1)
imge2 = plt.imread('/home/xgj/' picsave2)
plt.imshow(imge1)
plt.imshow(imge2)
5.2 图:
6 量子隐形传态:
============
6.1 代码:quantum teleportation.py
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit.tools.visualization import circuit_drawer
import matplotlib.pyplot as plt
coupling_map = [[0, 1], [0, 2], [1, 2], [3, 2], [3, 4], [4, 2]]
q = QuantumRegister(3, "q")
c0 = ClassicalRegister(1, "c0")
c1 = ClassicalRegister(1, "c1")
c2 = ClassicalRegister(1, "c2")
qc = QuantumCircuit(q, c0, c1, c2, name="teleport")
qc.u(0.3, 0.2, 0.1, q[0])
qc.h(q[1])
qc.cx(q[1], q[2])
qc.barrier(q)
qc.cx(q[0], q[1])
qc.h(q[0])
qc.measure(q[0], c0[0])
qc.measure(q[1], c1[0])
qc.barrier(q)
qc.z(q[2]).c_if(c0, 1)
qc.x(q[2]).c_if(c1, 1)
qc.measure(q[2], c2[0])
#文件名
picsave='4.png'
#保存在根目录下
circuit_drawer(qc,output='mpl',filename=picsave)
imge = plt.imread('/home/xgj/' picsave)
plt.imshow(imge)
plt.show()
6.2 图:
7 备注:
=====
7.1 源代码来自github,进行代码的修改和图形可视化,谢谢作者和官网的来源。
7.2 简单介绍到这里,喜欢的点个赞,加油,中国量子力学。
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved