Skip to content

feat: add splice on HugrGraph ADT - #98

Merged
acl-cqc merged 53 commits into
mainfrom
acl/splice_hugr
Feb 25, 2026
Merged

acl-cqc merged 53 commits into
mainfrom
acl/splice_hugr

Conversation

@acl-cqc

@acl-cqc acl-cqc commented Dec 24, 2025

Copy link
Copy Markdown
Collaborator

Uses the HugrGraph ADT added in #97.

splice replaces a HoleOp (ignoring the index) with a DFG-rooted Hugr of matching signature, i.e. inserts the DFG.
inlineDFG flattens the result, if desired. However, it'd be better to combine them - see comment.

I wasn't sure what the best approach was for dealing with new/old keys, but the splice method is general over both key types by taking a translation function, which gives some guarantee that we are translating the keys. splice_prepend (both NodeID Hugr's) and splice_new (arbitrary-keyed into NodeID) offer two possibilities....take a look and see what you think?

My hope ATM is that we don't need to deal with order edges since these are only added for nonlocal edges, and so we can do splicing/inlining before adding order edges.

Tests are pretty basic (i.e. about the simplest possible case, with/without inline). These should be more thorough....

Base automatically changed from refactor/hugr_graph to main January 12, 2026 20:06
@acl-cqc
acl-cqc marked this pull request as ready for review January 13, 2026 18:57
Comment thread brat/Data/HugrGraph.hs Outdated
splice hole add non_root_k = modify $ \host -> case (M.lookup hole (nodes host) >>= isHole) of
Just (_, sig) -> case M.lookup (root add) (nodes add) of
Just (OpDFG (DFG sig' _)) | sig == sig' -> {-inlineDFG hole-} host {
-- prefer host entry for parent of (`hole` == root of `add`)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment implies M.union, but below it's actually union

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and I think it should be disjoint union

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good spot, renamed to disj_union

Comment thread brat/Data/HugrGraph.hs Outdated
splice :: forall m n. (Ord n, Ord m) => n -> HugrGraph m -> (m -> n) -> State (HugrGraph n) ()
splice hole add non_root_k = modify $ \host -> case (M.lookup hole (nodes host) >>= isHole) of
Just (_, sig) -> case M.lookup (root add) (nodes add) of
Just (OpDFG (DFG sig' _)) | sig == sig' -> {-inlineDFG hole-} host {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Just (OpDFG (DFG sig' _)) | sig == sig' -> {-inlineDFG hole-} host {
Just (OpDFG (DFG sig' _)) | sig == sig' -> host {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yah, but added a few comments, obviously it wasn't clear what I was doing....

Comment thread brat/Data/HugrGraph.hs
host_out = execState (splice hole add (keyMap M.!)) host
in (host_out, ns_out)

inlineDFG :: Ord n => n -> State (HugrGraph n) ()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need inlineDFG or is it vestigial from an earlier implementation of splice?

@acl-cqc acl-cqc Jan 16, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, separate transformation ATM. I could combine into a single, more complex but more efficient, function....

@acl-cqc
acl-cqc merged commit 06f7432 into main Feb 25, 2026
1 check passed
@acl-cqc
acl-cqc deleted the acl/splice_hugr branch February 25, 2026 14:50
acl-cqc added a commit that referenced this pull request Apr 24, 2026
…e tests (#99)

A few old commits moved out into #100.

* Rework the Hugr compiler to compile only Kernel boxes; `Compiler.hs`
(`brat -c`) calls this only for Kernel boxes that are inputs to the Id
nodes of top-level decls. "compile" produces a Hugr + list of splices,
where each splice is (the NodeId of a Hole op in the hugr, OutPort from
BratGraph) - not using the "index" of the Hole op here.

* Add `Machine.hs` with interpreter (commandline `--run <func>`). For
Hugrs, this also evaluates the outports and performs the splices using
routines from #98, and returns the final Hugr in JSON form.

* Includes reworking test framework to specify which tests are xfailed
for parsing/compilation, and allowing to specify expected outputs from
the interpreter (or that the output should be hugr-validated) *in the
test file* (`examples/*.brat`). We could do with a full audit of the
testsuite after this but not done here.

* Minor fixes: `solveVal` wiring, bad type in `adder.brat`
* Also add `CFull`; `Dummy` graph node; `hugrRotation`, Hugr float
extension, CR(x/y/z); HugrGraph root->getRoot+getNodes

---------

Co-authored-by: Craig Roy <craig.roy@quantinuum.com>
Co-authored-by: Conor McBride <conor.mcbride@quantinuum.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants