Soluzione: il codice eseguito in Python deve essere protetto, o meglio “guarded”, ciò significa che il QuantumProgram deve essere eseguito dentro ad un’istruzione if __name__ == “__main__”
Compilando il programma nell’esempio nel qiskit-sdk-py si incorre nel seguente errore durante l’esecuzione:
1 2 3 4 5 6 7 8 9 10 11 12 |
Traceback (most recent call last): File "C:\Users\....\...\first.py", line 35, in <module> print(result.get_data("bell")) File "D:\Python36\lib\site-packages\qiskit\_result.py", line 204, in get_data raise exception File "D:\Python36\lib\site-packages\qiskit\_jobprocessor.py", line 99, in _job_done_callback result = future.result() File "D:\Python36\lib\concurrent\futures\_base.py", line 425, in result return self.__get_result() File "D:\Python36\lib\concurrent\futures\_base.py", line 384, in __get_result raise self._exception concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending. |
Questo accade perché si tentano di avviare delle biforcazioni del processo originale senza aver utilizzato un idioma appropriato per il modulo. Per correggere il problema è sufficiente reimpostare il codice eseguibile in questo modo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
from qiskit import QuantumProgram, QISKitError, RegisterSizeError # Create a QuantumProgram object instance. Q_program = QuantumProgram() if __name__ == "__main__": backend = 'local_qasm_simulator' try: # Create a Quantum Register called "qr" with 2 qubits. qr = Q_program.create_quantum_register("qr", 2) # Create a Classical Register called "cr" with 2 bits. cr = Q_program.create_classical_register("cr", 2) # Create a Quantum Circuit called "qc" involving the Quantum Register "qr" # and the Classical Register "cr". qc = Q_program.create_circuit("bell", [qr], [cr]) # Add the H gate in the Qubit 0, putting this qubit in superposition. qc.h(qr[0]) # Add the CX gate on control qubit 0 and target qubit 1, putting # the qubits in a Bell state qc.cx(qr[0], qr[1]) # Add a Measure gate to see the state. qc.measure(qr, cr) # Compile and execute the Quantum Program in the local_qasm_simulator. result = Q_program.execute(["bell"], backend=backend, shots=1024, seed=1) # Show the results. print(result) print(result.get_data("bell")) except QISKitError as ex: print('There was an error in the circuit!. Error = {}'.format(ex)) except RegisterSizeError as ex: print('Error in the number of registers!. Error = {}'.format(ex)) |
Ambiente di prova:
- Version used: Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
- Environment name and version (e.g. Python 3.6.1): Python 3.6.3
- Operating System and version: Microsoft Windows 10 Pro 10.0.16299 build 16299
Pubblicato originariamente su Github.