Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate function names in exported HUGR #765

Open
zrho opened this issue Jan 13, 2025 · 1 comment
Open

Duplicate function names in exported HUGR #765

zrho opened this issue Jan 13, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@zrho
Copy link

zrho commented Jan 13, 2025

Guppy appears to generate hugr modules that contain multiple functions with the same name and parent node. This is currently accepted by hugr-core but is considered an error in hugr-model to ensure that functions can be unambiguously referred to by their name.

Concretely, consider the following guppy code:

The guppy code
import itertools
import random
from typing import no_type_check

from guppylang import guppy
from guppylang.std.angles import angle
from guppylang.std.builtins import array, py, result
from guppylang.std.qsystem import phased_x, rz, zz_phase
from guppylang.std.quantum import measure_array, qubit

random.seed(2)

N = 100
rounds = N

angles = [
    [[random.uniform(0, 2 * 3.14159) for _ in range(15)] for _ in range(N // 2)]  # noqa: S311
    for _ in range(rounds)
]
qb_ids = [list(range(N)) for _ in range(N)]
for qs in qb_ids:
    random.shuffle(qs)

qb_pairs = [list(itertools.batched(qs, 2)) for qs in qb_ids]


@guppy
@no_type_check
def main() -> None:
    qs = array(qubit() for _ in range(py(N)))

    pair_lists = py(qb_pairs)
    all_angles = py(angles)

    for rond in range(len(all_angles)):
        pairs = pair_lists[rond]
        round_angles = all_angles[rond]

        for pair_id in range(len(pairs)):
            a, b = pairs[pair_id]
            pair_angles_flt = round_angles[pair_id]
            pair_angles = array(angle(a) for a in pair_angles_flt)
            rz(qs[a], pair_angles[0])
            rz(qs[b], pair_angles[1])
            phased_x(qs[a], pair_angles[2], pair_angles[3])
            phased_x(qs[b], pair_angles[4], pair_angles[5])

            zz_phase(qs[a], qs[b], pair_angles[6])
            zz_phase(qs[a], qs[b], pair_angles[7])
            zz_phase(qs[a], qs[b], pair_angles[8])

            phased_x(qs[a], pair_angles[9], pair_angles[10])
            phased_x(qs[b], pair_angles[11], pair_angles[12])
            rz(qs[a], pair_angles[13])
            rz(qs[b], pair_angles[14])

    for b in measure_array(qs):
        result("c", b)


with open("qv_hugr.json", "w") as f:
    f.write(guppy.compile_module().package.to_json())

The first hugr in the output JSON contains two functions named __hasnext__, two functions called __next__ and three functions called init_none. It appears that all of these functions are different from each other, so it is not a case of the same function being exported twice.

@zrho zrho added the bug Something isn't working label Jan 13, 2025
@zrho zrho changed the title Duplicate function names Duplicate function names in exported HUGR Jan 13, 2025
@ss2165
Copy link
Member

ss2165 commented Jan 13, 2025

warning about the attached example: set N to a small value (2) otherwise this will take a long time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants