-
Notifications
You must be signed in to change notification settings - Fork 153
Invoking Q# callables from Python
The qsharp Python package makes it easy to call Q# operations and functions from within Python. Each Q# callable defined in %%qsharp magic cells, through calls to qsharp.eval(), or defined in a Q# project loaded via qsharp.init() is automatically added to the qsharp.code module in Python:
These callables can then be invoked as normal Python functions, which will run them in the Q# simulator just as if they were invoked from within a normal Q# context. Any output produced from calls to Message or diagnostics like DumpMachine will appear as normal and the return from the function marshalled into Python:
These callables can also be imported within the Python environment:
Python literals and variables can be passed directly to Q# callables like any other Python function, and types that support conversion into the equivalent Q# types will work as expected:
If a Python value cannot be converted into the correct Q# type or the wrong number of arguments are provided, an exception is raised:
Not all Q# types support conversion from/to Python; if a callable has arguments or return values of these types, invoking the function will trigger a runtime exception:
Unsupported interop types include Qubits, structs/user-defined-types, and callables.
When using projects, Q# callables will be with a module hierarchy matching the namespace hierarchy of the project:
When qsharp.init() is called the current compilation and simulator state is reset, so all callables are removed from the qsharp.code module:
Any functions previously imported from qsharp.code are also invalidated:
Q# Wiki
Overview
Q# language & features
- Q# Structs
- Q# External Dependencies (Libraries)
- Differences from the previous QDK
- V1.3 features
- Curated list of Q# libraries
- Advanced Topics and Configuration
- QDK Profile Selection
OpenQASM support
VS Code
Python
Circuit diagrams
Azure Quantum
For contributors