为什么python 看不到quantumCircuit class qiskit 的成员

Why python doesn't see the members of quantumCircuit class qiskit

我正在尝试学习量子计算机的编程。 我已经在 VS Code 中安装了 qiskit(VS Code 市场上所有可用的 qiskit 扩展),python 编译器(来自 Vs Code 市场 "Python" 和 "Python for VSCode")。我已经设置了我的 qikit API 以便正常工作

当我 运行 我得到错误的例子: "Instance of 'QuantumCircuit' has no 'h' member"

我该怎么办?

代码:

from qiskit import ClassicalRegister, QuantumRegister
from qiskit import QuantumCircuit, execute

q = QuantumRegister(2)
c = ClassicalRegister(2)
qc = QuantumCircuit(q)
qc.h(q[0]) 
qc.cx(q[0], q[1])
qc.measure(q, c)

job_sim = execute(qc, 'local_qasm_simulator')

sim_result = job_sim.result()

print(sim_result.get_counts(qc))

======================== The same error after adding comment # pylint: disable=no-member

有问题的错误来自 pylint,一个 linter,而不是 python 本身。虽然 pylint 非常聪明,但某些结构(尤其是那些涉及动态添加的属性的结构)超出了它的理解能力。当你遇到这样的情况时,最好的做法是双重的:

  1. 检查文档、代码等以绝对确保您编写的代码是正确的(即验证 linter 结果是否为误报)
  2. 告诉 linter 你知道自己在做什么,它应该忽略误报

user2357112 处理了上面评论中的第一步,证明了 属性 是由库的另一部分动态设置的。

pylint 的第二步可以通过在每个有问题的行之后添加注释告诉它关闭对该特定行的特定检查来完成:

qc.h(q[0])  # pylint: disable=no-member