microsoft / qsharp-compiler

Q# compiler, command line tool, and Q# language server
https://docs.microsoft.com/quantum
MIT License
684 stars 170 forks source link

Cannot connect to Q# Kernel with Jupyter Notebook #1535

Closed raffaeleterribile closed 2 years ago

raffaeleterribile commented 2 years ago

Describe the bug

I've started studying Quantum Computing with Quantum Katas: with the first two notebooks (which uses ONLY Python code) I don't have problems; but with the third notebook (which uses ONLY Q# code) I cannot execute any cell. To investigate the issue I've created a simple Jupyter Notebook with some simple Q# code and have the same issue, so it's not a problem with the Quantum Katas, but some wrong configurations on my PC. When I try to execute the first Q# cell I see this message:

Connection to Q# Kernel: Waiting for the Jupyter session to be inactive

I've tried to execute simple Q# file to verifiy if there is a problem with QDK, but I haven't any problem: seems an error between Jupyter and Q# kernel. I've uninstalled QDK some times, I've created a conda environment for QDK (python 3.9.12) and upgraded all python packages, but I don't think it depends on Python.

To Reproduce

Execute any Q# cell

Expected behavior

Jupyter cell executed

Screenshots

I use a Visual Studio Code localized in Italian

image

System information

I have Windows 10, Python 3.9.12, last version of Visual Studio Code and last versione of Quantum Development Kit (uninstalled and reinstalled some times). In the Output Windows of Jupyter I've found these logs:

Visual Studio Code (1.70.1, undefined, desktop) Jupyter Extension Version: 2022.7.1102252217. Python Extension Version: 2022.12.0. Workspace folder d:\Progetti\VisualStudioCode\QuantumComputing\QuantumHelloJupyter info 18:50:13.654: ZMQ install verified. L'utente appartiene al gruppo di esperimenti 'jupyterTest' L'utente appartiene al gruppo di esperimenti 'jupyterEnhancedDataViewer' info 18:50:14.115: LSP Notebooks experiment is disabled -- not in treatment group info 18:50:14.128: Experiment status for python is {"enabled":true,"optInto":[],"optOutFrom":[]} info 18:50:14.272: Preferred Remote kernel for d:\Progetti\VisualStudioCode\QuantumComputing\QuantumHelloJupyter\QuantumHello.ipynb is undefined info 18:50:14.272: Find preferred kernel for d:\Progetti\VisualStudioCode\QuantumComputing\QuantumHelloJupyter\QuantumHello.ipynb with metadata {"kernelspec":{"display_name":"Q#","language":"qsharp","name":"iqsharp"},"language_info":{"name":"qsharp","version":"3.9.12"},"orig_nbformat":4,"vscode":{"interpreter":{"hash":"a27239699dac23b156ebe8060e45f338e727b914205219c8d2dd3847d8b55e86"}}} & preferred interpreter info 18:50:14.273: Preferred Remote kernel for d:\Progetti\VisualStudioCode\QuantumComputing\QuantumHelloJupyter\QuantumHello.ipynb is undefined info 18:50:14.273: Preferred kernel .iqsharp.c:\Users\Raffaele.dotnet\tools\dotnet-iqsharp.exe..c:\users\raffaele.dotnet\tools\dotnet-iqsharp.exe#kernel#--log-level#error#{connection_file} is exact match or top match for non python kernels, (false, undefined, true, true) info 18:50:14.275: PreferredConnection: .iqsharp.c:\Users\Raffaele.dotnet\tools\dotnet-iqsharp.exe..c:\users\raffaele.dotnet\tools\dotnet-iqsharp.exe#kernel#--log-level#error#{connection_file} found for NotebookDocument: d:\Progetti\VisualStudioCode\QuantumComputing\QuantumHelloJupyter\QuantumHello.ipynb info 18:50:16.510: Starting Jupyter Session id = 'startUsingLocalKernelSpec:.iqsharp.c:\Users\Raffaele.dotnet\tools\dotnet-iqsharp.exe..c:\users\raffaele.dotnet\tools\dotnet-iqsharp.exe#kernel#--log-level#error#{connection_file}' for 'd:\Progetti\VisualStudioCode\QuantumComputing\QuantumHelloJupyter\QuantumHello.ipynb' (disableUI=true) info 18:50:16.512: Creating raw notebook for resource 'd:\Progetti\VisualStudioCode\QuantumComputing\QuantumHelloJupyter\QuantumHello.ipynb' info 18:50:16.512: Computing working directory for resource 'd:\Progetti\VisualStudioCode\QuantumComputing\QuantumHelloJupyter\QuantumHello.ipynb' info 18:50:16.787: Starting raw kernel 'Q#' for interpreter info 18:50:16.923: Kernel launching with ports 9000,9001,9002,9003,9004. Start port is 9000 info 18:50:17.395: Registering dummy command feature info 18:50:17.500: Launching Raw Kernel & not daemon Q# # C:\Users\Raffaele.dotnet\tools\dotnet-iqsharp.exe info 18:50:17.500: No custom variables nor do we have a conda environment info 18:50:17.522: Process Execution: > ~.dotnet\tools\dotnet-iqsharp.exe kernel --log-level Error ~\AppData\Roaming\jupyter\runtime\kernel-v2-24056MsOISUEkaXaV.json

~.dotnet\tools\dotnet-iqsharp.exe kernel --log-level Error ~\AppData\Roaming\jupyter\runtime\kernel-v2-24056MsOISUEkaXaV.json info 18:50:17.522: Process Execution: cwd: d:\Progetti\VisualStudioCode\QuantumComputing\QuantumHelloJupyter cwd: d:\Progetti\VisualStudioCode\QuantumComputing\QuantumHelloJupyter info 18:50:22.146: Widget Message: Received IPyWidgetMessages.IPyWidgets_IsReadyRequest info 18:50:24.154: Process Execution: > ~.conda\envs\quantum\python.exe -m pip list ~.conda\envs\quantum\python.exe -m pip list info 18:50:26.251: Preferred Remote kernel for d:\Progetti\VisualStudioCode\QuantumComputing\QuantumHelloJupyter\QuantumHello.ipynb is undefined warn 18:50:29.487: Didn't get response for requestKernelInfo after 3014ms. info 18:50:29.490: kernel Q# avviato., (Raw session started and connected) info 18:50:29.491: Finished connecting df097af5-a637-49b4-89db-2978e1c2389f info 18:51:0.969: Execute Cell 0 d:\Progetti\VisualStudioCode\QuantumComputing\QuantumHelloJupyter\QuantumHello.ipynb

I've noticed in these logs the file "~\AppData\Roaming\jupyter\runtime\kernel-v2-24056MsOISUEkaXaV.json". The contents of this file is:

{ "key": "5b5caef4-8775-4672-824a-796a4b3e9acc", "signature_scheme": "hmac-sha256", "transport": "tcp", "ip": "127.0.0.1", "hb_port": 9000, "control_port": 9001, "shell_port": 9002, "stdin_port": 9003, "iopub_port": 9004, "kernel_name": "iqsharp" }

Additional context

I've activated VS Code Development Tools BEFORE running any cells and then I've tried to execute the first cell: nothing happens. However this is the message in the VS Code Development Tools:

log.ts:319 CRITI Extension 'ms-vsliveshare.vsliveshare' wants API proposal 'notebookDocumentEvents' but that proposal DOES NOT EXIST. Likely, the proposal has been finalized (check 'vscode.d.ts') or was abandoned. log.ts:307 WARN [redhat.vscode-yaml]: Non è possibile registrare 'redhat.telemetry.enabled'. Questa proprietà è già registrata. TMScopeRegistry.ts:47 Overwriting grammar scope name to file mapping for scope source.yaml. Old grammar file: file:///c%3A/Users/Raffaele/AppData/Local/Programs/Microsoft%20VS%20Code/resources/app/extensions/yaml/syntaxes/yaml.tmLanguage.json. New grammar file: file:///c%3A/Users/Raffaele/.vscode/extensions/redhat.vscode-yaml-1.9.1/syntaxes/yaml.tmLanguage.json register @ TMScopeRegistry.ts:47 log.ts:313 ERR [Extension Host] (node:19728) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead. (Use Code --trace-deprecation ... to show where the warning was created) console.ts:137 [Extension Host] (node:19728) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead. (Use Code --trace-deprecation ... to show where the warning was created) I @ console.ts:137 console.ts:137 [Extension Host] instantiating test adapters - workspace name: QuantumHelloJupyter I @ console.ts:137 localProcessExtensionHost.ts:298 Extension Host localProcessExtensionHost.ts:299 Debugger attached. log.ts:301 INFO UNRESPONSIVE extension host: starting to profile NOW log.ts:301 INFO UNRESPONSIVE extension host: received responsive event and cancelling profiling session log.ts:307 WARN UNRESPONSIVE extension host: 'quantum.quantum-devkit-vscode' took 91% of 2376.519ms, saved PROFILE here: 'file:///c%3A/Users/Raffaele/AppData/Local/Temp/exthost-ac1982.cpuprofile' (9) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] console.ts:137 [Extension Host] [qsharp-lsp] Activated! VM11:4 Registering custom require.js for Jupyter Kernel eval @ VM11:4 c @ notebookWebviewPreloads.js:3 o @ notebookWebviewPreloads.js:3 await in o (async) load @ notebookWebviewPreloads.js:3 (anonymous) @ notebookWebviewPreloads.js:3 postMessage (async) (anonymous) @ index.html?id=c81f8430-c4c5-49e6-a43a-726cfbffc610&origin=c81f8430-c4c5-49e6-a43a-726cfbffc610&swVersion=4&extensionId=&platform=electron&vscode-resource-base-authority=vscode-resource.vscode-cdn.net&parentOrigin=vscode-file%3A%2F%2Fvscode-app&purpose=notebookRenderer:1102 HostMessaging.channel.port1.onmessage @ index.html?id=c81f8430-c4c5-49e6-a43a-726cfbffc610&origin=c81f8430-c4c5-49e6-a43a-726cfbffc610&swVersion=4&extensionId=&platform=electron&vscode-resource-base-authority=vscode-resource.vscode-cdn.net&parentOrigin=vscode-file%3A%2F%2Fvscode-app&purpose=notebookRenderer:295 console.ts:137 [Extension Host] [qsharp-lsp] Using Language Server from global storage at "c:\Users\Raffaele\AppData\Roaming\Code\User\globalStorage\quantum.quantum-devkit-vscode\server\Microsoft.Quantum.QsLanguageServer.exe". notebookWebviewPreloads.js:3 Notebook preload (https://file%2B.vscode-resource.vscode-cdn.net/c%3A/Users/Raffaele/.vscode/extensions/ms-toolsai.jupyter-2022.7.1102252217/out/node_modules/%40vscode/jupyter-ipywidgets/dist/ipywidgets.js) looks like a module but does not export an activate function o @ notebookWebviewPreloads.js:3 await in o (async) load @ notebookWebviewPreloads.js:3 (anonymous) @ notebookWebviewPreloads.js:3 postMessage (async) (anonymous) @ index.html?id=c81f8430-c4c5-49e6-a43a-726cfbffc610&origin=c81f8430-c4c5-49e6-a43a-726cfbffc610&swVersion=4&extensionId=&platform=electron&vscode-resource-base-authority=vscode-resource.vscode-cdn.net&parentOrigin=vscode-file%3A%2F%2Fvscode-app&purpose=notebookRenderer:1102 HostMessaging.channel.port1.onmessage @ index.html?id=c81f8430-c4c5-49e6-a43a-726cfbffc610&origin=c81f8430-c4c5-49e6-a43a-726cfbffc610&swVersion=4&extensionId=&platform=electron&vscode-resource-base-authority=vscode-resource.vscode-cdn.net&parentOrigin=vscode-file%3A%2F%2Fvscode-app&purpose=notebookRenderer:295 console.ts:137 [Extension Host] [qsharp-lsp] Package is quantum-devkit-vscode with 0.25.222597 and 0.25.222597 console.ts:137 [Extension Host] [qsharp-lsp] State stopped -> starting console.ts:137 [Extension Host] [qsharp-lsp] Started LanguageClient object. console.ts:137 [Extension Host] [qsharp-lsp] Writing language server log to C:\Users\Raffaele\AppData\Local\Temp\qsharp-19728kU0atJ6FroyW.log. DevTools failed to load source map: Could not load content for https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/Raffaele/.vscode/extensions/ms-toolsai.jupyter-2022.7.1102252217/out/webviews/webview-side/ipywidgetsKernel/ipywidgetsKernel.js.map: Connection error: net::ERR_NAME_NOT_RESOLVED console.ts:137 [Extension Host] [qsharp-lsp] State starting -> running console.ts:137 [Extension Host] Starting WebSocket: RAW/api/kernels/e4c71cd8-c9b4-437b-b29c-4e6bde525f1c localProcessExtensionHost.ts:298 Extension Host localProcessExtensionHost.ts:299 Starting WebSocket: RAW/api/kernels/e4c71cd8-c9b4-437b-b29c-4e6bde525f1c

In the language server log found in this messages there are only thisi message:

Connecting via stdin and stdout. Listening...

I think there is some wrong configuration in the Q# kernel registration, but I cannot find what the problem is.

Can you help me?

Thanks in advance

raffaeleterribile commented 2 years ago

I resolved this issue. My antivirus was blocking the Q# kernel. I reinstalled IQSharp and launched a Jupyter notebook. My antivirs showd this message:

image

My antivirus is Avast and says that dotnet-iqsharp.exe is infected by IDC.Generic virus. But it's a false positive because I've instaled it with the command found on Quantum Development Kit site: dotnet iqsharp install. I've added an exception rule in my Antivirus and was able to execute Q# code inside a Jupyter Notebook.