Make MultiCntrlFunction daggerable using struct modifiers

Open
#239 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Start by reading MultiCntrlFunction and MultiCntrlFunctionParams, then trace the _swap_forward and _swap_inverse adapters in SwapUp and inspect existing kernels that use measurement or randomness. The issue’s Guppy 1.2.4 Composer prototype shows the desired struct modifiers and generic Unitary callbacks. Done when daggering can reuse the forward operation while preserving predicate behavior, kernel workspace cleanup, and existing callback use cases.

Written by the indexing model from the issue text.

Description

SwapUp currently needs separate _swap_forward and _swap_inverse adapters because MultiCntrlFunction stores opaque Function callbacks. We should be able to reuse the forward operation:

operation = MultiCntrlFunction(controlled_swapup, cnx_kernel)

with dagger:
    operation.compose(controls, regs)

Update MultiCntrlFunction and MultiCntrlFunctionParams to retain both callbacks as generic types bounded by Unitary, with struct modifiers on compose.

This prototype works on Guppy 1.2.4:

from guppylang import guppy
from guppylang.std.builtins import Unitary, array, dagger, nat
from guppylang.std.quantum import discard, qubit


@guppy.struct
class Composer[
    m: nat,
    Regs,
    Op: Unitary[[qubit, Regs], None],
    Kernel: Unitary[[array[qubit, m], qubit], None],
]:
    operation: Op
    kernel: Kernel

    @guppy.unitary
    class compose:
        @guppy
        def __call__(
            self, controls: array[qubit, m], targets: Regs
        ) -> None:
            if m == 1:
                self.operation(controls[0], targets)
            else:
                work = qubit()
                self.kernel(controls, work)
                self.operation(work, targets)
                with dagger:
                    self.kernel(controls, work)
                discard(work)

        @guppy
        def daggered(
            self, controls: array[qubit, m], targets: Regs
        ) -> None:
            if m == 1:
                with dagger:
                    self.operation(controls[0], targets)
            else:
                work = qubit()
                self.kernel(controls, work)
                with dagger:
                    self.operation(work, targets)
                with dagger:
                    self.kernel(controls, work)
                discard(work)

The operation must preserve its predicate and act as identity when it is zero. The kernel must preserve the controls, compute their AND into a clean target, and restore internal workspace.

Migration needs care: both callbacks must be statically known, while explicit generic specialization currently produces opaque function pointers. Some existing kernels also use measurement or randomness; preserve those use cases or provide a compatible alternative.

Dominant language
Python
Stars
12
Forks
1
Avg merge
3d 1h
Merged PRs (30d)
19

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Quantinuum/guppyalgos

All issues in Quantinuum/guppyalgos

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.