python 量子电路模拟库qiskit
程序员文章站
2024-03-22 12:12:34
...
import math
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
import warnings #忽略告警
warnings.filterwarnings('ignore')
#定义量子傅里叶变换函数qft
def qft(circ, q, n):
"""n-qubit QFT on q in circ."""
for j in range(n):
for k in range(j):
circ.cu1(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])
print(qft_n.qasm())
# 可视化
from qiskit.tools.visualization import circuit_drawer
circuit_drawer(qft_n,filename='./qft_circuit')#可视化
推荐阅读