guppylang icon indicating copy to clipboard operation
guppylang copied to clipboard

Allow implicit discard of qubits at the end of main

Open ss2165 opened this issue 11 months ago • 4 comments

Qubits leftover at the end of main are being discarded anyway, users shouldn't have to do it manually just to satisfy the type checker. This should significantly reduce the need for discard calls - in programs that don't use ancillas.

ss2165 avatar Jan 27 '25 18:01 ss2165

Note that we still need to generate Hugr discards which is not entirely trivial when considering nested structs, arrays, and lists.

Also, would this be a special case for the qubit type only?

mark-koch avatar Jan 27 '25 22:01 mark-koch

Note that we still need to generate Hugr discards which is not entirely trivial when considering nested structs, arrays, and lists.

Also, would this be a special case for the qubit type only?

I think it should apply to all linear types. An executable should be allowed to exit without having to clean up after itself.

But this makes me wonder if we should change the type checker rather than guppy (so it would need to understand the concept of a main program).

cqc-alec avatar Jan 28 '25 09:01 cqc-alec

  • If we are doing all linear types, should we have a __drop__/__leak__ implementation that gets called automatically in these cases? (For qubit it would be a noop).

  • Seyon mentioned elsewhere to add a @guppy.entrypoint marker that would trigger this behaviour.

aborgna-q avatar Jan 28 '25 16:01 aborgna-q

  • If we are doing all linear types, should we have a __drop__/__leak__ implementation that gets called automatically in these cases? (For qubit it would be a noop).

Seems a good idea -- for example, an output stream would want to flush itself when dropped. Not sure if we have any non-trivial examples in guppy at the moment?

  • Seyon mentioned elsewhere to add a @guppy.entrypoint marker that would trigger this behaviour.

Yes, better than relying on a function name.

cqc-alec avatar Jan 29 '25 10:01 cqc-alec