【问题标题】:Why python doesn't see the members of quantumCircuit class qiskit为什么python看不到quantumCircuit类qiskit的成员
【发布时间】:2019-02-15 22:25:07
【问题描述】:

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

当我运行示例时出现错误:“'QuantumCircuit' 的实例没有'h' 成员”

我该怎么办?

代码:

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 消息,而不是实际错误。 Pylint 认为QuantumCircuit 的代码不会产生h 属性。 (查看code 我自己,我没有看到h 属性来自哪里,但文档说它应该可以工作。)
  • 抱歉这么多问题,但我从来没有在 pyhon 上写过任何东西(我知道 C++ 和 Java)。是的,QuantumCircuit 中没有 h 成员我是否必须对核心 qiskit 文件进行一些更改?
  • 我打开了QuantumCircuit文件,没有h成员。如何在那里添加h 函数?

标签: python quantum-computing qiskit


【解决方案1】:

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

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

user2357112 完成了上述 cmets 中的第一步,证明该属性由库的另一部分动态设置。

第二步可以为 pylint 完成,方法是在每个有问题的行之后添加一个注释,告诉它关闭对该特定行的特定检查:

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

【讨论】:

  • 感谢您的解释。我找到了那些库。添加评论后,pylint 不会随时看到这些功能。我得到了类似的东西:请参阅帖子末尾的图片
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 2019-09-03
  • 2016-02-14
  • 1970-01-01
  • 2014-05-25
相关资源
最近更新 更多