autoqasm.transpiler.transpiler module

The PyToOqpy transpiler.

TODO @shaffry: this file is mostly copied from the class PyToTF in the TensorFlow implementation of autograph. Consider refactoring to reduce duplication if possible.

class autoqasm.transpiler.transpiler.PyToOqpy[source]

Bases: PyToPy

The AutoQASM transpiler which converts a Python function into an oqpy program.

get_transformed_name(node: Lambda | FunctionDef) str[source]

Returns a name for the output function. Subclasses may override this.

get_extra_locals() dict[source]

Returns extra static local variables to be made to transformed code.

Returns:

dict – Additional variables to make available to the transformed code.

get_caching_key(ctx: ControlStatusCtx) ConversionOptions[source]

Returns a unique key to use for caching.

Subclasses must override this.

Calls made to transform_function with functions that have the same code object and caching key will return a cached instance on subsequent invocations.

Parameters:

user_context – The context object which was passed to transform.

Returns:

extra_locals – A hashable.

transform_ast(node: Lambda | FunctionDef, ctx: ControlStatusCtx) Lambda | FunctionDef[source]

Performs an actual transformation of a function’s AST.

Parameters:
  • node (Lambda | FunctionDef) – One or more ast.AST nodes representing the AST to be transformed.

  • ctx (ControlStatusCtx) – transformer context.

Returns:

Lambda | FunctionDef – The root of the transformed AST.

autoqasm.transpiler.transpiler.converted_call(f: Callable, args: tuple, kwargs: dict | None, caller_fn_scope: FunctionScope | None = None, options: ConversionOptions | None = None) Any[source]

Converts a function call inline.

For internal use only.

Note: The argument list is optimized for readability of generated code, which may look like this:

ag__.converted_call(f, (arg1, arg2), None, fscope) ag__.converted_call(f, (), dict(arg1=val1, **kwargs), fscope) ag__.converted_call(f, (arg1, arg2) + varargs, dict(**kwargs), lscope)

Parameters:
  • f (Callable) – The function to convert.

  • args (tuple) – the original positional arguments of f.

  • kwargs (dict | None) – the original keyword arguments of f.

  • caller_fn_scope (FunctionScope | None) – the function scope of the converted function in which this call was originally made. Defaults to None.

  • options (ConversionOptions | None) – conversion options. If not specified, the value of caller_fn_scope.callopts is used. Either options or caller_fn_scope must be present. Defaults to None.

Returns:

Any – the result of executing a possibly-converted f with the given arguments.