2023年11月Qiskit安装过程以及安装过程中错误解决(最新)
Qiskit安装过程以及错误解决
本文主要参照
https://zhuanlan.zhihu.com/p/414399506
1.安装anaconda
Anaconda安装地址
https://www.anaconda.com/download#Downloads
下载安装包后安装,不一定要安装在默认路径。
2.创建新环境,在环境中安装Qiskit
由于我想在一个独立的环境里安装qiskit,避免和其他编程环境互相影响,所以选择新建一个环境quantum。
(1)在Anaconda中创建一个新环境,名为Quantum
(2)关闭Anaconda,打开Anaconda Prompt
首先启动我们新安装的环境Quantum
conda activate 'Quantum'
设置清华源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
再安装qiskit
pip install qiskit
安装过程如下图所示,可见由于设置了清华源,速度较快
3.安装辅助部分
(1)安装qiskit_textbook
先安装git
conda install git
再安装qiskit-textbook
pip install git+https://github.com/qiskit-community/qiskit-textbook.git#subdirectory=qiskit-textbook-src
安装过程中报错
问题解决:参考https://blog.csdn.net/weixin_53742691/article/details/130931339
即,先执行
pip install --upgrade setuptools
再回去安装qiskit-textbook
(2)安装qiskit可视化模块
pip install qiskit[visualization]
(3)安装qiskit-aer(用于电路仿真)
pip install qiskit-aer
示例代码
from qiskit import *
import numpy as np
from qiskit import Aer
from qiskit.visualization import plot_state_city
import matplotlib.pyplot as plt
# Create a Quantum Circuit acting on a quantum register of three qubits
circ = QuantumCircuit(3)
# Add a H gate on qubit $q_{0}$, putting this qubit in superposition.
circ.h(0)
# Add a CX (CNOT) gate on control qubit $q_{0}$ and target qubit $q_{1}$, putting
# the qubits in a Bell state.
circ.cx(0, 1)
# Add a CX (CNOT) gate on control qubit $q_{0}$ and target qubit $q_{2}$, putting
# the qubits in a GHZ state.
circ.cx(0, 2)
circ.draw('mpl')
plt.show()
backend = Aer.get_backend('statevector_simulator')
# Create a Quantum Program for execution
job = backend.run(circ)
result = job.result()
outputstate = result.get_statevector(circ, decimals=3)
print(outputstate)
plot_state_city(outputstate)
plt.show()
运行结果