Skip to content

Commit

Permalink
feat!: qubits are now reset on allocation, and dirty_qubit added (#…
Browse files Browse the repository at this point in the history
…325)

Closes #322 

BREAKING CHANGE: `qubit`s are now reset on allocation

---------

Co-authored-by: Mark Koch <[email protected]>
  • Loading branch information
doug-q and mark-koch authored Jul 25, 2024
1 parent 314409c commit 4a9e205
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
22 changes: 21 additions & 1 deletion guppylang/prelude/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@
from guppylang.tys.builtin import bool_type, int_type, list_type
from guppylang.tys.const import ConstValue
from guppylang.tys.subst import Inst, Subst
from guppylang.tys.ty import FunctionType, NoneType, NumericType, Type, unify
from guppylang.tys.ty import (
FunctionType,
NoneType,
NumericType,
Type,
type_to_row,
unify,
)


class ConstInt(BaseModel):
Expand Down Expand Up @@ -442,3 +449,16 @@ def compile(self, args: list[OutPortV]) -> list[OutPortV]:
quantum_op("QFree"), inputs=[measure.add_out_port(qubit.ty)]
)
return [measure.add_out_port(bool_type())]


class QAllocCompiler(CustomCallCompiler):
"""Compiler for the `qubit` function."""

def compile(self, args: list[OutPortV]) -> list[OutPortV]:
from .quantum import quantum_op

[qubit_ty] = type_to_row(get_type(self.node))
qalloc = self.graph.add_node(quantum_op("QAlloc"), inputs=args)
qalloc_result = qalloc.add_out_port(qubit_ty)
reset = self.graph.add_node(quantum_op("Reset"), inputs=[qalloc_result])
return [reset.add_out_port(qubit_ty)]
8 changes: 6 additions & 2 deletions guppylang/prelude/quantum.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from guppylang.decorator import guppy
from guppylang.hugr_builder.hugr import UNDEFINED
from guppylang.module import GuppyModule
from guppylang.prelude._internal import MeasureCompiler
from guppylang.prelude._internal import MeasureCompiler, QAllocCompiler

quantum = GuppyModule("quantum")

Expand All @@ -28,10 +28,14 @@ def quantum_op(op_name: str) -> ops.OpType:
linear=True,
)
class qubit:
@guppy.hugr_op(quantum, quantum_op("QAlloc"))
@guppy.custom(quantum, QAllocCompiler())
def __new__() -> "qubit": ...


@guppy.hugr_op(quantum, quantum_op("QAlloc"))
def dirty_qubit() -> qubit: ...


@guppy.hugr_op(quantum, quantum_op("H"))
def h(q: qubit) -> qubit: ...

Expand Down
19 changes: 19 additions & 0 deletions tests/integration/test_qalloc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.prelude.quantum import qubit

import guppylang.prelude.quantum as quantum
from guppylang.prelude.quantum import cx, measure, dirty_qubit


def test_dirty_qubit(validate):
module = GuppyModule("test")
module.load(quantum)

@guppy(module)
def test() -> tuple[bool, bool]:
q1, q2 = qubit(), dirty_qubit()
q1, q2 = cx(q1, q2)
return (measure(q1), measure(q2))

validate(module.compile())

0 comments on commit 4a9e205

Please sign in to comment.