使用 IBM API 的 Python 多处理错误(Q 体验)



>我正在尝试使用 IBM Q Experience API,我安装了该库并编写了一个快速测试代码。 起初,它找不到该模块。 然后,我添加了整个系统的东西。 诚然,我只是从一个堆栈溢出帖子中复制了它,所以这可能是问题所在。 无论如何,我得到了"运行时错误",这导致了python多处理库。 IBM Q 没有一个很大的社区,所以我在研究了一段时间后找不到任何东西。 我完全不熟悉多处理、线程等,所以我想只看到代码和错误会比我能做的任何事情都更好。 提前谢谢。

import sys
sys.path.append("../../")
from qiskit import QuantumProgram
#visualization
from tools.visualization import plot_histogram
#set up registers
qp = QuantumProgram()
q = qp.create_quantum_register("q", 3)
c = qp.create_classical_register("c", 3)
#define our circuit
threeQ = qp.create_circuit("threeQ", [q], [c])
threeQ.measure(q[0], c[0])
threeQ.measure(q[1], c[1])
threeQ.measure(q[2], c[2])
#run
result = qp.execute(["threeQ"])
#plot
plot_histogram(result.get_counts("threeQ"))

我得到的错误(对不起,它很长(:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingspawn.py", line 106, in spawn_main
    exitcode = _main(fd)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingspawn.py", line 115, in _main
    prepare(preparation_data)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingspawn.py", line 226, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingspawn.py", line 278, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35librunpy.py", line 254, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35librunpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35librunpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:UsersEverybodyfirstquantum.py", line 21, in <module>
    print(qp.execute(["threeQ"]))
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libsite-packagesqiskit_quantumprogram.py", line 1413, in execute
    result = self.run(qobj, wait=wait, timeout=timeout)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libsite-packagesqiskit_quantumprogram.py", line 1232, in run
    timeout=timeout)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libsite-packagesqiskit_quantumprogram.py", line 1315, in _run_internal
    job_processor.submit()
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libsite-packagesqiskit_jobprocessor.py", line 118, in submit
    future = executor.submit(run_backend, q_job)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libconcurrentfuturesprocess.py", line 454, in submit
    self._start_queue_management_thread()
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libconcurrentfuturesprocess.py", line 415, in _start_queue_management_thread
    self._adjust_process_count()
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libconcurrentfuturesprocess.py", line 434, in _adjust_process_count
    p.start()
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingprocess.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingcontext.py", line 212, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingcontext.py", line 313, in _Popen
    return Popen(process_obj)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingpopen_spawn_win32.py", line 34, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingspawn.py", line 144, in get_preparation_data
    _check_not_importing_main()
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingspawn.py", line 137, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.
        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:
            if __name__ == '__main__':
                freeze_support()
                ...
        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingspawn.py", line 106, in spawn_main
    exitcode = _main(fd)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingspawn.py", line 115, in _main
    prepare(preparation_data)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingspawn.py", line 226, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingspawn.py", line 278, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35librunpy.py", line 254, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35librunpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35librunpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:UsersEverybodyfirstquantum.py", line 21, in <module>
    print(qp.execute(["threeQ"]))
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libsite-packagesqiskit_quantumprogram.py", line 1413, in execute
    result = self.run(qobj, wait=wait, timeout=timeout)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libsite-packagesqiskit_quantumprogram.py", line 1232, in run
    timeout=timeout)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libsite-packagesqiskit_quantumprogram.py", line 1315, in _run_internal
    job_processor.submit()
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libsite-packagesqiskit_jobprocessor.py", line 118, in submit
    future = executor.submit(run_backend, q_job)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libconcurrentfuturesprocess.py", line 454, in submit
    self._start_queue_management_thread()
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libconcurrentfuturesprocess.py", line 415, in _start_queue_management_thread
    self._adjust_process_count()
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libconcurrentfuturesprocess.py", line 434, in _adjust_process_count
    p.start()
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingprocess.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingcontext.py", line 212, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingcontext.py", line 313, in _Popen
    return Popen(process_obj)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingpopen_spawn_win32.py", line 34, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingspawn.py", line 144, in get_preparation_data
    _check_not_importing_main()
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingspawn.py", line 137, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.
        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:
            if __name__ == '__main__':
                freeze_support()
                ...
        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingspawn.py", line 106, in spawn_main
    exitcode = _main(fd)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingspawn.py", line 115, in _main
    prepare(preparation_data)
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingspawn.py", line 226, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35libmultiprocessingspawn.py", line 278, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:UsersEverybodyAppDataLocalProgramsPythonPython35librunpy.py", line 254, in run_path
ERROR

这是 Python on Windows 中多线程的普遍问题。解决方案是将顶级代码放在 if __name__ == "__main__"> 块中,如下所示:

if __name__ == '__main__':
    <your code here>

希望对您有所帮助!

相关内容

  • 没有找到相关文章

最新更新