2019-10-27 量子计算模拟

基于IBM提供的qiskit库进行量子计算模拟

以下为安装指令

pip install qiskit

请使用https源例如清华源执行pip安装
Windows 换源方法 - 创建文件 User/YourUserName/pip/pip.ini 内容如下

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn

以下为示例代码

import numpy as np
from qiskit import(
  QuantumCircuit,
  execute,
  Aer)
from qiskit.visualization import plot_histogram

# Use Aer's qasm_simulator
simulator = Aer.get_backend('qasm_simulator')

# Create a Quantum Circuit acting on the q register
circuit = QuantumCircuit(2, 2)

# Add a H gate on qubit 0
circuit.h(0)

# Add a CX (CNOT) gate on control qubit 0 and target qubit 1
circuit.cx(0, 1)

# Map the quantum measurement to the classical bits
circuit.measure([0,1], [0,1])

# Execute the circuit on the qasm simulator
job = execute(circuit, simulator, shots=1000)

# Grab results from the job
result = job.result()

# Returns counts
counts = result.get_counts(circuit)
print("\nTotal count for 00 and 11 are:",counts)

# Draw the circuit
circuit.draw()

预期输出

Total count for 00 and 11 are: {'11': 500, '00': 500}
image.png

更多见:官方文档

你可能感兴趣的:(2019-10-27 量子计算模拟)