PyCharm 不显示 Qiskit 可视化

问题描述 投票:0回答:2

即使我正确获取了数字,我也无法从

Qiskit
获取直方图以使用
PyCharm
显示。代码如下:

import numpy as np
from qiskit import QuantumCircuit, transpile
from qiskit.providers.aer import QasmSimulator
from qiskit.visualization import plot_histogram
from matplotlib import pyplot as plt

simulator = QasmSimulator()
circuit = QuantumCircuit(2, 2)
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])
compiled_circuit = transpile(circuit, simulator)
job = simulator.run(compiled_circuit, shots=1000)
result = job.result()
counts = result.get_counts(compiled_circuit)
print("\nTotal count for 00 and 11 are:",counts)
plot_histogram(counts)

我应该得到这样的图像:

this

python pycharm qiskit
2个回答
0
投票

plot_histogram
使用Matplotlib来显示直方图。 PyCharm 提供了在科学模式下渲染 Matplotlib 图形的选项,该选项仅存在于 PyCharm Professional 中


0
投票

您似乎忘记在plot_histogram(counts)之后添加plt.plot()。此外,您正在绘制整数计数而不是概率。我已将电路图添加到代码中。

import numpy as np
from qiskit import QuantumCircuit, transpile, Aer
# from qiskit.providers.aer import QasmSimulator
from qiskit.visualization import plot_histogram
from matplotlib import pyplot as plt


simulator = Aer.get_backend('qasm_simulator')
circuit = QuantumCircuit(2, 2)
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])

circuit.draw(output='mpl')

plt.show()

compiled_circuit = transpile(circuit, simulator)
job = simulator.run(compiled_circuit, shots=1000)
result = job.result()
counts = result.get_counts(compiled_circuit)
print("\nTotal count for 00 and 11 are:",counts)
plot_histogram(counts)

plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.