Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ in
pkgs-unstable.uv
pkgs.commitizen
pkgs.git
pkgs.just
];

dotenv.enable = true;

# https://devenv.sh/scripts/
scripts.qfmt.exec = ''
echo -e "Running formatting, linting and typechecking 🧹 🔧 \n"

uv run ruff check --select I --fix
uv run ruff check
uv run ruff format
uv run mypy qnexus/ tests/ integration/
just qfmt
'';
scripts.qtest.exec = ''
echo -e "Running unit tests 📏🔍\n"
Expand Down
2 changes: 1 addition & 1 deletion integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def test_ref_serialisation(

@pytest.fixture(name="qa_hugr_package")
def qa_hugr_package_fixture() -> Package:
hugr_path = Path("tests/data/example.hugr").resolve()
hugr_path = Path("tests/data/example_hugr.hugr").resolve()
return Package.from_bytes(hugr_path.read_bytes())


Expand Down
42 changes: 6 additions & 36 deletions integration/test_qsys_jobs.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"""Tests related to running jobs against QSys devices."""

from typing import Any, Callable, ContextManager, cast
from typing import Callable, ContextManager, cast

import pytest
from guppylang import guppy
from guppylang.std.builtins import result
from guppylang.std.quantum import cx, h, measure, qubit, x, z
from hugr.package import Package
from hugr.qsystem.result import QsysResult
from pytket.backends.backendinfo import BackendInfo
from quantinuum_schemas.models.backend_config import (
Expand All @@ -24,36 +22,6 @@
)


def prepare_teleportation() -> Any:
"""Prepares the teleportation circuit."""

@guppy
def bell() -> tuple[qubit, qubit]:
"""Constructs a bell state."""
q1, q2 = qubit(), qubit()
h(q1)
cx(q1, q2)
return q1, q2

@guppy
def main() -> None:
src = qubit()
x(src)
alice, bob = bell()

cx(src, alice)
h(src)
if measure(alice):
x(bob)
if measure(src):
z(bob)

result("teleported", measure(bob))

main.check()
return main.compile()


@pytest.mark.parametrize(
"backend_config",
[
Expand All @@ -70,6 +38,7 @@ def test_guppy_execution(
test_case_name: str,
create_project: Callable[[str], ContextManager[ProjectRef]],
backend_config: BackendConfig,
qa_hugr_package: Package,
) -> None:
"""Test the execution of a guppy program
on a next-generation QSys device."""
Expand All @@ -78,7 +47,7 @@ def test_guppy_execution(
n_shots = 10

hugr_ref = qnx.hugr.upload(
hugr_package=prepare_teleportation(),
hugr_package=qa_hugr_package,
name=f"hugr for {test_case_name}",
project=project_ref,
)
Expand Down Expand Up @@ -127,12 +96,13 @@ def test_guppy_execution(
def test_hugr_costing(
test_case_name: str,
create_project: Callable[[str], ContextManager[ProjectRef]],
qa_hugr_package: Package,
) -> None:
"""Test the costing of a Hugr program on a cost checking device."""

with create_project(f"project for {test_case_name}") as project_ref:
hugr_ref = qnx.hugr.upload(
hugr_package=prepare_teleportation(),
hugr_package=qa_hugr_package,
name=f"hugr for {test_case_name}",
project=project_ref,
)
Expand Down
17 changes: 17 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# List the available commands
help:
@just --list --justfile {{justfile()}}

# Run basic formatting, linting and typechecking
qfmt:
echo -e "Running formatting, linting and typechecking 🧹 🔧 \n"

uv run ruff check --select I --fix
uv run ruff check
uv run ruff format
uv run mypy qnexus/ tests/ integration/

# Re-compile test hugr files.
recompile-test-files:
@echo "---- Recompiling example guppy programs ----"
just tests/data/recompile
8 changes: 8 additions & 0 deletions tests/data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Test programs

This directory contains simple quantum programs used for testing.

The compiled HUGR is stored (alongside the guppy python program) with a `.hugr` extension.

Run `just recompile` in this directory (or `just recompile-test-hugrs` on
Copy link
Collaborator

Choose a reason for hiding this comment

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

Small typo, the justfile command in the root is recompile-test-files.

the root) to recompile the `.hugr` files.
Binary file removed tests/data/example.hugr
Binary file not shown.
Binary file added tests/data/example_hugr.hugr
Binary file not shown.
38 changes: 38 additions & 0 deletions tests/data/example_hugr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Simple guppylang program from https://docs.quantinuum.com/guppy"""

from pathlib import Path
from sys import argv

from guppylang import guppy
from guppylang.std.builtins import result
from guppylang.std.quantum import cx, h, measure, qubit, x, z


@guppy
def bell() -> tuple[qubit, qubit]:
"""Constructs a bell state."""
q1, q2 = qubit(), qubit()
h(q1)
cx(q1, q2)
return q1, q2


@guppy
def main() -> None:
src = qubit()
x(src)
alice, bob = bell()

cx(src, alice)
h(src)
if measure(alice):
x(bob)
if measure(src):
z(bob)

result("teleported", measure(bob))


main.check()
program = main.compile_function()
Path(argv[0]).with_suffix(".hugr").write_bytes(program.to_bytes())
14 changes: 14 additions & 0 deletions tests/data/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# List the available commands
help:
@just --list --justfile {{justfile()}}

# Re-generate all test hugr files.
recompile: recompile-hugrs

# Re-generate all hugr files.
recompile-hugrs:
#!/usr/bin/env sh
for example in $(find . -type f -name "*.py"); do
Copy link
Collaborator

Choose a reason for hiding this comment

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

This might be a good opportunity to start using the proposed .gpy.py extension for guppy source files. This could also come handy if later the example guppy files get more complicated and they have to make use of utility functions in regular python files.

Though it means that the files would end up being example_hugr.gpy.py and example_hugr.gpy.hugr. Not ideal. I'd suggest changing the name of the example file to example_bell.gpy.py, which would generate a corresponding example_bell.gpy.hugr.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good suggestions, have adapted the code to save the test hugrs with just a .hugr extension - see what you think!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm looks like it is unhappy with the .gpy.py extension

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed

echo "Re-generating $example"
uv run --no-project "$example"
done