Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 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()
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}.")