How to use qsharp - 10 common examples

To help you get started, we’ve selected a few qsharp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github microsoft / iqsharp / src / Python / qsharp-core / qsharp / azure.py View on Github external
def submit(op : qsharp.QSharpCallable, **params) -> AzureJob:
    """
    Submits a job to an Azure Quantum workspace.
    See https://docs.microsoft.com/qsharp/api/iqsharp-magic/azure.submit for more details.
    """
    result = qsharp.client._execute_callable_magic("azure.submit", op, raise_on_stderr=False, **params)
    if "error_code" in result: raise AzureError(result)
    return AzureJob(result)
github microsoft / iqsharp / src / Python / qsharp-core / qsharp / azure.py View on Github external
def connect(**params) -> List[AzureTarget]:
    """
    Connects to an Azure Quantum workspace or displays current connection status.
    See https://docs.microsoft.com/qsharp/api/iqsharp-magic/azure.connect for more details.
    """
    result = qsharp.client._execute_magic(f"azure.connect", raise_on_stderr=False, **params)
    if "error_code" in result: raise AzureError(result)
    return [AzureTarget(target) for target in result]
github microsoft / iqsharp / src / Python / qsharp-core / qsharp / azure.py View on Github external
def target(name : str = '', **params) -> AzureTarget:
    """
    Sets or displays the active execution target for Q# job submission in an Azure Quantum workspace.
    See https://docs.microsoft.com/qsharp/api/iqsharp-magic/azure.target for more details.
    """
    result = qsharp.client._execute_magic(f"azure.target {name}", raise_on_stderr=False, **params)
    if "error_code" in result: raise AzureError(result)
    return AzureTarget(result)
github microsoft / iqsharp / build / docs / build_docs.py View on Github external
def main(output_dir : str, uid_base : str, package : List[str]):
    output_dir = Path(output_dir)
    # Make the output directory if it doesn't already exist.
    output_dir.mkdir(parents=True, exist_ok=True)

    import qsharp

    print("Adding packages...")
    for package_name in package:
        qsharp.packages.add(package_name)
    
    print("Generating Markdown files...")
    magics = qsharp.client._execute(r"%lsmagic")
    all_magics = {}
    for magic in magics:
        magic_doc = format_as_document(magic, uid_base)
        all_magics[magic_doc.name] = magic_doc
        with open(output_dir / f"{magic_doc.safe_name}.md", 'w', encoding='utf8') as f:
            f.write(magic_doc.content)

    toc_content = format_toc(all_magics)
    with open(output_dir / "toc.yml", 'w', encoding='utf8') as f:
        f.write(toc_content)
    
    index_content = format_index(all_magics, uid_base)
    with open(output_dir / "index.md", 'w', encoding='utf8') as f:
        f.write(index_content)
github microsoft / iqsharp / src / Python / qsharp-core / qsharp / loader.py View on Github external
#     >>> import Microsoft.Quantum.Intrinsic as mqi
        # Thus, we need to check if the full name is one that that we can
        # sensibly load before we proceed.

        # To check the full name, we ask the client rather than going through
        # the public API for the qsharp package, so that we can check if the
        # client is currently busy. This can happen if anything below us in
        # meta_path needs to handle an import during an execute; this is the
        # case when ZeroMQ needs to import additional functionality from a
        # Cython module to handle a message.
        # See https://github.com/Microsoft/QuantumLibraries/issues/69 for an
        # example of this failure modality.

        # If the client is busy, we'll want to forego this request to find a
        # module and return None early.
        if qsharp.client.busy:
            return None

        # At this point, we should be safe to rely on the public API again.
        ops = qsharp.get_available_operations_by_namespace()

        if full_name not in ops:
            # We may have been given part of the qualified name of a namespace.
            # E.g., if we try to import Microsoft.Quantum.Intrinsic, we'll
            # see calls with "Microsoft" and "Microsoft.Quantum" first.
            if not any(
                ns_name.startswith(full_name + ".")
                for ns_name in ops
            ):
                return None

        return QSharpModuleLoader()
github microsoft / iqsharp / src / Python / qsharp-core / qsharp / loader.py View on Github external
def simulate(self, **kwargs) -> Any:
        """
        Executes this function or operation on the QuantumSimulator target
        machine, returning its output as a Python object.
        """
        return qsharp.client.simulate(self, **kwargs)
github microsoft / iqsharp / src / Python / qsharp-core / qsharp / loader.py View on Github external
def estimate_resources(self, **kwargs) -> Dict[str, int]:
        return qsharp.client.estimate(self, **kwargs)
github microsoft / iqsharp / src / Python / qsharp-core / qsharp / loader.py View on Github external
def __getattr__(self, name):
        ops = qsharp.get_available_operations_by_namespace()
        if name in ops[self._qs_name]:
            op_cls = new_class(name, (QSharpCallable, ))

            # Copy over metadata from the operation's header.
            metadata = qsharp.client.get_operation_metadata(f"{self._qs_name}.{name}")
            op_cls.__doc__ = metadata.get('documentation', '')
            op_cls.__file__ = metadata.get('source', None)
            return op_cls(f"{self._qs_name}.{name}", "workspace")
        raise AttributeError(f"Q# namespace {self._qs_name} does not contain a callable {name}.")
github microsoft / iqsharp / src / Python / qsharp-core / qsharp / loader.py View on Github external
def toffoli_simulate(self, **kwargs) -> Any:
        """
        Executes this function or operation on the ToffoliSimulator target
        machine, returning its output as a Python object.
        """
        return qsharp.client.toffoli_simulate(self, **kwargs)
github microsoft / iqsharp / src / Python / qsharp-core / qsharp / azure.py View on Github external
def status(jobId : str = '', **params) -> AzureJob:
    """
    Displays status for a job in the current Azure Quantum workspace.
    See https://docs.microsoft.com/qsharp/api/iqsharp-magic/azure.status for more details.
    """
    result = qsharp.client._execute_magic(f"azure.status {jobId}", raise_on_stderr=False, **params)
    if "error_code" in result: raise AzureError(result)
    return AzureJob(result)