Skip to content

Commit

Permalink
refactor!: Make qubit type lower case (#165)
Browse files Browse the repository at this point in the history
Addresses #142 

---

Also: update 27 test cases from
```python
from guppylang.hugr.tys import Qubit
```
to
```python
from guppylang.prelude.quantum import qubit
```
and some whitespace trimming
  • Loading branch information
croyzor authored Mar 6, 2024
1 parent e57ee8d commit 0a42097
Show file tree
Hide file tree
Showing 72 changed files with 216 additions and 216 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ Guppy is a quantum programming language that is fully embedded into Python.
It allows you to write high-level hybrid quantum programs with classical control flow and mid-circuit measurements using Pythonic syntax:

```python
from guppylang import guppy, Qubit, quantum
from guppylang import guppy, qubit, quantum

guppy.load(quantum)

# Teleports the state in `src` to `tgt`.
@guppy
def teleport(src: Qubit, tgt: Qubit) -> Qubit:
def teleport(src: qubit, tgt: qubit) -> qubit:
# Create ancilla and entangle it with src and tgt
tmp = Qubit()
tmp = qubit()
tmp, tgt = cx(h(tmp), tgt)
src, tmp = cx(src, tmp)

# Apply classical corrections
if measure(h(src)):
tgt = z(tgt)
Expand Down
6 changes: 3 additions & 3 deletions examples/demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6733,13 +6733,13 @@
],
"source": [
"import guppylang.prelude.quantum as quantum\n",
"from guppylang.prelude.quantum import Qubit\n",
"from guppylang.prelude.quantum import qubit\n",
"\n",
"module = GuppyModule(\"test\")\n",
"module.load(quantum)\n",
"\n",
"@guppy(module)\n",
"def main(q: Qubit) -> tuple[Qubit, bool]:\n",
"def main(q: qubit) -> tuple[qubit, bool]:\n",
" q = rz(q, 3.1415)\n",
" return measure(q)\n",
"\n",
Expand Down Expand Up @@ -8336,7 +8336,7 @@
"module.load(quantum)\n",
"\n",
"@guppy(module)\n",
"def apply_v3(q: Qubit, a: Qubit, b: Qubit) -> tuple[Qubit, Qubit, Qubit]:\n",
"def apply_v3(q: qubit, a: qubit, b: qubit) -> tuple[qubit, qubit, qubit]:\n",
" while True:\n",
" a, b = h(a), h(b)\n",
"\n",
Expand Down
2 changes: 1 addition & 1 deletion guppylang/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from guppylang.module import GuppyModule
from guppylang.prelude import builtins, quantum
from guppylang.prelude.builtins import Bool, Float, Int, List, linst
from guppylang.prelude.quantum import Qubit
from guppylang.prelude.quantum import qubit
2 changes: 1 addition & 1 deletion guppylang/checker/expr_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ def python_value_to_guppy_type(
try:
import tket2 # type: ignore[import-untyped, import-not-found, unused-ignore] # noqa: F401

qubit = globals.types["Qubit"].build()
qubit = globals.types["qubit"].build()
return FunctionType(
[qubit] * v.n_qubits,
row_to_type([qubit] * v.n_qubits + [BoolType()] * v.n_bits),
Expand Down
46 changes: 23 additions & 23 deletions guppylang/prelude/quantum.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,89 +21,89 @@ def quantum_op(op_name: str) -> ops.OpType:
tys.Opaque(extension="prelude", id="qubit", args=[], bound=TypeBound.Any),
linear=True,
)
class Qubit:
class qubit:
pass


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


@guppy.hugr_op(quantum, quantum_op("CZ"))
def cz(control: Qubit, target: Qubit) -> tuple[Qubit, Qubit]: ...
def cz(control: qubit, target: qubit) -> tuple[qubit, qubit]: ...


@guppy.hugr_op(quantum, quantum_op("CX"))
def cx(control: Qubit, target: Qubit) -> tuple[Qubit, Qubit]: ...
def cx(control: qubit, target: qubit) -> tuple[qubit, qubit]: ...


@guppy.hugr_op(quantum, quantum_op("T"))
def t(q: Qubit) -> Qubit: ...
def t(q: qubit) -> qubit: ...


@guppy.hugr_op(quantum, quantum_op("S"))
def s(q: Qubit) -> Qubit: ...
def s(q: qubit) -> qubit: ...


@guppy.hugr_op(quantum, quantum_op("X"))
def x(q: Qubit) -> Qubit: ...
def x(q: qubit) -> qubit: ...


@guppy.hugr_op(quantum, quantum_op("Y"))
def y(q: Qubit) -> Qubit: ...
def y(q: qubit) -> qubit: ...


@guppy.hugr_op(quantum, quantum_op("Z"))
def z(q: Qubit) -> Qubit: ...
def z(q: qubit) -> qubit: ...


@guppy.hugr_op(quantum, quantum_op("Tdg"))
def tdg(q: Qubit) -> Qubit: ...
def tdg(q: qubit) -> qubit: ...


@guppy.hugr_op(quantum, quantum_op("Sdg"))
def sdg(q: Qubit) -> Qubit: ...
def sdg(q: qubit) -> qubit: ...


@guppy.hugr_op(quantum, quantum_op("ZZMax"))
def zz_max(q: Qubit) -> Qubit: ...
def zz_max(q: qubit) -> qubit: ...


@guppy.hugr_op(quantum, quantum_op("Measure"))
def measure_return(q: Qubit) -> tuple[Qubit, bool]: ...
def measure_return(q: qubit) -> tuple[qubit, bool]: ...


@guppy.hugr_op(quantum, quantum_op("RzF64"))
def rz(q: Qubit, angle: float) -> Qubit: ...
def rz(q: qubit, angle: float) -> qubit: ...


@guppy.hugr_op(quantum, quantum_op("RxF64"))
def rx(q: Qubit, angle: float) -> Qubit: ...
def rx(q: qubit, angle: float) -> qubit: ...


@guppy.hugr_op(quantum, quantum_op("RzF64"))
def phased_x(q: Qubit, angle1: float, angle2: float) -> Qubit: ...
def phased_x(q: qubit, angle1: float, angle2: float) -> qubit: ...


@guppy.hugr_op(quantum, quantum_op("RzF64"))
def zz_phase(q1: Qubit, q2: Qubit, angle: float) -> tuple[Qubit, Qubit]: ...
def zz_phase(q1: qubit, q2: qubit, angle: float) -> tuple[qubit, qubit]: ...


@guppy.hugr_op(quantum, quantum_op("RzF64"))
def tk1(q: Qubit, angle1: float, angle2: float, angle3: float) -> Qubit: ...
def tk1(q: qubit, angle1: float, angle2: float, angle3: float) -> qubit: ...


@guppy.hugr_op(quantum, quantum_op("QAlloc"), name="Qubit")
def _Qubit() -> Qubit: ...
@guppy.hugr_op(quantum, quantum_op("QAlloc"), name="qubit")
def _qubit() -> qubit: ...


@guppy.hugr_op(quantum, quantum_op("QFree"))
def discard(q: Qubit) -> None: ...
def discard(q: qubit) -> None: ...


@guppy.hugr_op(quantum, quantum_op("Reset"))
def reset(q: Qubit) -> Qubit: ...
def reset(q: qubit) -> qubit: ...


@guppy.custom(quantum, MeasureCompiler())
def measure(q: Qubit) -> bool: ...
def measure(q: qubit) -> bool: ...
8 changes: 4 additions & 4 deletions quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ allows you to write high-level hybrid quantum programs with classical control
flow and mid-circuit measurements using Pythonic syntax:

```python
from guppylang import guppy, Qubit, quantum
from guppylang import guppy, qubit, quantum

guppy.load(quantum)

# Teleports the state in `src` to `tgt`.
@guppy
def teleport(src: Qubit, tgt: Qubit) -> Qubit:
def teleport(src: qubit, tgt: qubit) -> qubit:
# Create ancilla and entangle it with src and tgt
tmp = Qubit()
tmp = qubit()
tmp, tgt = cx(h(tmp), tgt)
src, tmp = cx(src, tmp)

# Apply classical corrections
if measure(h(src)):
tgt = z(tgt)
Expand Down
4 changes: 2 additions & 2 deletions tests/error/comprehension_errors/capture1.err
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Guppy compilation failed. Error in file $FILE:13

11: @guppy(module)
12: def foo(xs: list[int], q: Qubit) -> linst[Qubit]:
12: def foo(xs: list[int], q: qubit) -> linst[qubit]:
13: return [q for x in xs]
^
GuppyTypeError: Variable `q` with linear type `Qubit` would be used multiple times when evaluating this comprehension
GuppyTypeError: Variable `q` with linear type `qubit` would be used multiple times when evaluating this comprehension
4 changes: 2 additions & 2 deletions tests/error/comprehension_errors/capture1.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.hugr.tys import Qubit
from guppylang.prelude.quantum import qubit
from guppylang.prelude.builtins import linst

module = GuppyModule("test")
module.load(quantum)


@guppy(module)
def foo(xs: list[int], q: Qubit) -> linst[Qubit]:
def foo(xs: list[int], q: qubit) -> linst[qubit]:
return [q for x in xs]


Expand Down
4 changes: 2 additions & 2 deletions tests/error/comprehension_errors/capture2.err
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Guppy compilation failed. Error in file $FILE:18

16: @guppy(module)
17: def foo(xs: list[int], q: Qubit) -> list[int]:
17: def foo(xs: list[int], q: qubit) -> list[int]:
18: return [x for x in xs if bar(q)]
^
GuppyTypeError: Variable `q` with linear type `Qubit` would be used multiple times when evaluating this comprehension
GuppyTypeError: Variable `q` with linear type `qubit` would be used multiple times when evaluating this comprehension
6 changes: 3 additions & 3 deletions tests/error/comprehension_errors/capture2.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.hugr.tys import Qubit
from guppylang.prelude.quantum import qubit
from guppylang.prelude.builtins import linst

module = GuppyModule("test")
module.load(quantum)


@guppy.declare(module)
def bar(q: Qubit) -> bool:
def bar(q: qubit) -> bool:
...


@guppy(module)
def foo(xs: list[int], q: Qubit) -> list[int]:
def foo(xs: list[int], q: qubit) -> list[int]:
return [x for x in xs if bar(q)]


Expand Down
4 changes: 2 additions & 2 deletions tests/error/comprehension_errors/guarded1.err
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Guppy compilation failed. Error in file $FILE:13

11: @guppy(module)
12: def foo(qs: linst[tuple[bool, Qubit]]) -> linst[Qubit]:
12: def foo(qs: linst[tuple[bool, qubit]]) -> linst[qubit]:
13: return [q for b, q in qs if b]
^
GuppyTypeError: Variable `q` with linear type `Qubit` is not used on all control-flow paths of the list comprehension
GuppyTypeError: Variable `q` with linear type `qubit` is not used on all control-flow paths of the list comprehension
4 changes: 2 additions & 2 deletions tests/error/comprehension_errors/guarded1.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.hugr.tys import Qubit
from guppylang.prelude.quantum import qubit
from guppylang.prelude.builtins import linst

module = GuppyModule("test")
module.load(quantum)


@guppy(module)
def foo(qs: linst[tuple[bool, Qubit]]) -> linst[Qubit]:
def foo(qs: linst[tuple[bool, qubit]]) -> linst[qubit]:
return [q for b, q in qs if b]


Expand Down
4 changes: 2 additions & 2 deletions tests/error/comprehension_errors/guarded2.err
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Guppy compilation failed. Error in file $FILE:18

16: @guppy(module)
17: def foo(qs: linst[tuple[bool, Qubit]]) -> list[int]:
17: def foo(qs: linst[tuple[bool, qubit]]) -> list[int]:
18: return [42 for b, q in qs if b if q]
^
GuppyTypeError: Variable `q` with linear type `Qubit` is not used on all control-flow paths of the list comprehension
GuppyTypeError: Variable `q` with linear type `qubit` is not used on all control-flow paths of the list comprehension
6 changes: 3 additions & 3 deletions tests/error/comprehension_errors/guarded2.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.hugr.tys import Qubit
from guppylang.prelude.quantum import qubit
from guppylang.prelude.builtins import linst

module = GuppyModule("test")
module.load(quantum)


@guppy.declare(module)
def bar(q: Qubit) -> bool:
def bar(q: qubit) -> bool:
...


@guppy(module)
def foo(qs: linst[tuple[bool, Qubit]]) -> list[int]:
def foo(qs: linst[tuple[bool, qubit]]) -> list[int]:
return [42 for b, q in qs if b if q]


Expand Down
4 changes: 2 additions & 2 deletions tests/error/comprehension_errors/multi_use1.err
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Guppy compilation failed. Error in file $FILE:13

11: @guppy(module)
12: def foo(qs: linst[Qubit], xs: list[int]) -> linst[Qubit]:
12: def foo(qs: linst[qubit], xs: list[int]) -> linst[qubit]:
13: return [q for q in qs for x in xs]
^
GuppyTypeError: Variable `q` with linear type `Qubit` would be used multiple times when evaluating this comprehension
GuppyTypeError: Variable `q` with linear type `qubit` would be used multiple times when evaluating this comprehension
4 changes: 2 additions & 2 deletions tests/error/comprehension_errors/multi_use1.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.hugr.tys import Qubit
from guppylang.prelude.quantum import qubit
from guppylang.prelude.builtins import linst

module = GuppyModule("test")
module.load(quantum)


@guppy(module)
def foo(qs: linst[Qubit], xs: list[int]) -> linst[Qubit]:
def foo(qs: linst[qubit], xs: list[int]) -> linst[qubit]:
return [q for q in qs for x in xs]


Expand Down
4 changes: 2 additions & 2 deletions tests/error/comprehension_errors/multi_use2.err
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Guppy compilation failed. Error in file $FILE:13

11: @guppy(module)
12: def foo(qs: linst[Qubit], xs: list[int]) -> linst[Qubit]:
12: def foo(qs: linst[qubit], xs: list[int]) -> linst[qubit]:
13: return [q for x in xs for q in qs]
^^
GuppyTypeError: Variable `qs` with linear type `linst[Qubit]` would be used multiple times when evaluating this comprehension
GuppyTypeError: Variable `qs` with linear type `linst[qubit]` would be used multiple times when evaluating this comprehension
4 changes: 2 additions & 2 deletions tests/error/comprehension_errors/multi_use2.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.hugr.tys import Qubit
from guppylang.prelude.quantum import qubit
from guppylang.prelude.builtins import linst

module = GuppyModule("test")
module.load(quantum)


@guppy(module)
def foo(qs: linst[Qubit], xs: list[int]) -> linst[Qubit]:
def foo(qs: linst[qubit], xs: list[int]) -> linst[qubit]:
return [q for x in xs for q in qs]


Expand Down
4 changes: 2 additions & 2 deletions tests/error/comprehension_errors/multi_use3.err
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Guppy compilation failed. Error in file $FILE:18

16: @guppy(module)
17: def foo(qs: linst[Qubit], xs: list[int]) -> list[int]:
17: def foo(qs: linst[qubit], xs: list[int]) -> list[int]:
18: return [x for q in qs for x in xs if bar(q)]
^
GuppyTypeError: Variable `q` with linear type `Qubit` would be used multiple times when evaluating this comprehension
GuppyTypeError: Variable `q` with linear type `qubit` would be used multiple times when evaluating this comprehension
Loading

0 comments on commit 0a42097

Please sign in to comment.