Skip to content

Adjacent CX gates are not cancelled if arrays are used #1307

@CalMacCQ

Description

@CalMacCQ

This Hugr is expected to optimize to a single Hadamard gates but doesn't seem to work if arrays are used.

from guppylang import guppy
from guppylang.std.quantum import cx, h, qubit
from guppylang.std.builtins import array

from hugr.hugr.base import Hugr

from tket.passes import NormalizeGuppy, PytketPass
from pytket.passes import RemoveRedundancies

@guppy
def arr_cx(arr: array[qubit, 2]) -> None:
    h(arr[0])
    cx(arr[0], arr[1])
    cx(arr[0], arr[1])


normalize = NormalizeGuppy()
hugr_graph: Hugr = normalize(arr_cx.compile_function().modules[0])
opt_pass = PytketPass(RemoveRedundancies())
new_hugr = opt_pass(hugr_graph)

def count_ops(hugr: Hugr, string_name: str) -> int:
    count = 0
    for _, data in hugr.nodes():
        if string_name in data.op.name():
            count += 1

    return count


print(count_ops(new_hugr, "CX"))  # prints 2, should be zero

We can draw the Hugr as follows

from graphviz import Digraph
from hugr.hugr.render import DotRenderer, RenderConfig

# Configure Hugr visualizer
my_renderer = DotRenderer(RenderConfig(display_node_id=True, display_metadata=False))


def draw(hugr: Hugr) -> Digraph:
    """Draw a Hugr graph with the DotRenderer."""
    return my_renderer.render(hugr)


draw(new_hugr)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions