guppylang icon indicating copy to clipboard operation
guppylang copied to clipboard

AssertionError if x is conditionally redefined then used as an index

Open jane-garvin-q opened this issue 11 months ago • 1 comments

The attached guppy code causes an AssertionError when evaluated. It seems to be related to the fact that the variable x shadows the x from the library but not on all control flow paths.

@guppy
def main() -> None:
    q = [qubit() for _ in range(1)]
    for i in range(1):
        if i % 2 == 0:
            x = 0
        else:
            for _ in range(1):
                rx(q[x], angle(0.5))
Traceback (most recent call last):
  File "/Users/jane.garvin/Code/eldarion/bug.py", line 35, in <module>
    hugr = guppy.compile_module()
  File "/Users/jane.garvin/.venv/lib/python3.10/site-packages/guppylang/decorator.py", line 463, in compile_module
    return module.compile()
  File "/Users/jane.garvin/.venv/lib/python3.10/site-packages/guppylang/error.py", line 101, in pretty_errors_wrapped
    return f(*args, **kwargs)
  File "/Users/jane.garvin/.venv/lib/python3.10/site-packages/guppylang/module.py", line 346, in compile
    self.check()
  File "/Users/jane.garvin/.venv/lib/python3.10/site-packages/guppylang/module.py", line 323, in check
    other_defs = self._check_defs(
  File "/Users/jane.garvin/.venv/lib/python3.10/site-packages/guppylang/module.py", line 293, in _check_defs
    return {
  File "/Users/jane.garvin/.venv/lib/python3.10/site-packages/guppylang/module.py", line 295, in <dictcomp>
    defn.check(parsed_globals) if isinstance(defn, CheckableDef) else defn
  File "/Users/jane.garvin/.venv/lib/python3.10/site-packages/guppylang/definition/function.py", line 106, in check
    cfg = check_global_func_def(self.defined_at, self.ty, globals)
  File "/Users/jane.garvin/.venv/lib/python3.10/site-packages/guppylang/checker/func_checker.py", line 80, in check_global_func_def
    return check_cfg(cfg, inputs, ty.output, generic_params, func_def.name, globals)
  File "/Users/jane.garvin/.venv/lib/python3.10/site-packages/guppylang/checker/cfg_checker.py", line 108, in check_cfg
    check_rows_match(input_row, compiled[bb].sig.input_row, bb)
  File "/Users/jane.garvin/.venv/lib/python3.10/site-packages/guppylang/checker/cfg_checker.py", line 247, in check_rows_match
    assert map1.keys() == map2.keys()
AssertionError

bug.py.txt

jane-garvin-q avatar Jan 31 '25 18:01 jane-garvin-q

Duplicate of #772

mark-koch avatar Feb 03 '25 09:02 mark-koch