amazon-braket-sdk-python icon indicating copy to clipboard operation
amazon-braket-sdk-python copied to clipboard

Error when using AutoQASM with job decorator

Open yitchen-tim opened this issue 2 years ago • 0 comments

To reproduce the error

from braket.jobs import hybrid_job
from braket.devices import Devices

import braket.experimental.autoqasm as aq
from braket.experimental.autoqasm import instructions as ops

@hybrid_job(
    device=Devices.Amazon.SV1,
    dependencies=["git+https://github.com/amazon-braket/amazon-braket-sdk-python.git@f64d3e6a15521e78025d10fae2b8dfde804f1403#egg=amazon-braket-sdk"],
    local=True
) 
def bell_circuit_job():
    @aq.main
    def bell_circuit():
        ops.h(0)
        ops.cnot(0, 1)

    aq_program = bell_circuit()
    print(aq_program.to_ir())

bell_circuit_job()

Executing this code in a jupyter notebook causes pyct parsing error

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/usr/local/lib/python3.10/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/opt/ml/code/braket_container.py", line 267, in wrapped_customer_code
    raise e
  File "/opt/ml/code/braket_container.py", line 258, in wrapped_customer_code
    return customer_method(**kwargs)
  File "/opt/braket/code/customer_code/extracted/decorator_job_qrd6a_fa/entry_point.py", line 72, in bell_circuit_job
    result = recovered()
  File "/var/folders/53/q_0l5g7j7h7fmq6hjstpwl1m0000gs/T/ipykernel_73151/1483909699.py", line 24, in bell_circuit_job
    from pathlib import Path
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/api.py", line 162, in _wrapper
    return converter_callback(f, options, args, kwargs, **converter_args)
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/api.py", line 207, in _convert_main
    aq_transpiler.converted_call(f, args, kwargs, options=options)
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/transpiler/transpiler.py", line 241, in converted_call
    raise exc
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/transpiler/transpiler.py", line 296, in _try_convert_actual
    converted_f = _convert_actual(target_entity, program_ctx)
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/transpiler/transpiler.py", line 169, in _convert_actual
    transformed, module, source_map = _TRANSPILER.transform(entity, program_ctx)
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/autograph/pyct/transpiler.py", line 282, in transform
    return self.transform_function(obj, user_context)
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/autograph/pyct/transpiler.py", line 466, in transform_function
    nodes, ctx = super(PyToPy, self).transform_function(fn, user_context)
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/autograph/pyct/transpiler.py", line 342, in transform_function
    node, source = parser.parse_entity(fn, future_features=future_features)
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/autograph/pyct/parser.py", line 164, in parse_entity
    return parse(source, preamble_len=len(future_features)), source
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/autograph/pyct/parser.py", line 341, in parse
    raise ValueError('expected exactly one node, got 
{}
'.format(nodes))

Executing this code in terminal causes pyct inaccessible source code error

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/usr/local/lib/python3.10/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/opt/ml/code/braket_container.py", line 267, in wrapped_customer_code
    raise e
  File "/opt/ml/code/braket_container.py", line 258, in wrapped_customer_code
    return customer_method(**kwargs)
  File "/opt/braket/code/customer_code/extracted/decorator_job_4f4vdbac/entry_point.py", line 72, in bell_circuit_job
    result = recovered()
  File "<stdin>", line 7, in bell_circuit_job
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/api.py", line 162, in _wrapper
    return converter_callback(f, options, args, kwargs, **converter_args)
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/api.py", line 207, in _convert_main
    aq_transpiler.converted_call(f, args, kwargs, options=options)
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/transpiler/transpiler.py", line 241, in converted_call
    raise exc
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/transpiler/transpiler.py", line 296, in _try_convert_actual
    converted_f = _convert_actual(target_entity, program_ctx)
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/transpiler/transpiler.py", line 169, in _convert_actual
    transformed, module, source_map = _TRANSPILER.transform(entity, program_ctx)
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/autograph/pyct/transpiler.py", line 282, in transform
    return self.transform_function(obj, user_context)
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/autograph/pyct/transpiler.py", line 466, in transform_function
    nodes, ctx = super(PyToPy, self).transform_function(fn, user_context)
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/autograph/pyct/transpiler.py", line 342, in transform_function
    node, source = parser.parse_entity(fn, future_features=future_features)
  File "/usr/local/lib/python3.10/site-packages/braket/experimental/autoqasm/autograph/pyct/parser.py", line 150, in parse_entity
    raise errors.InaccessibleSourceCodeError(
braket.experimental.autoqasm.autograph.pyct.errors.InaccessibleSourceCodeError: Unable to locate the source code of <function bell_circuit at 0x402da8ad40>. Note that functions defined in certain environments, like the interactive Python shell, do not expose their source code. If that is the case, you should define them in a .py source file. If you are certain the code is graph-compatible, wrap the call using @tf.autograph.experimental.do_not_convert. Original error: could not get source code.

yitchen-tim avatar Dec 18 '23 19:12 yitchen-tim