Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typing in phirgen, fix measure args bug, pretty print PHIR #13

Merged
merged 5 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ repos:
args: [--fix, --exit-non-zero-on-fix]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.6.0'
rev: 'v1.6.1'
hooks:
- id: mypy
pass_filenames: false
Expand All @@ -39,5 +39,5 @@ repos:
pytket==1.21.0,
pytket-quantinuum==0.25.0,
types-setuptools,
"git+https://github.com/CQCL/phir#egg=phir",
"phir",
]
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include *.txt
include Makefile
include mypy.ini
recursive-include docs *.rst
recursive-include docs Makefile
recursive-include tests *.qasm
exclude Dockerfile
exclude .docker*
Expand Down
4 changes: 4 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ warn_unused_configs = true

[mypy-tests.*]
disallow_untyped_defs = false

[mypy-pytket.phir.phirgen.*]
disallow_any_expr = false
disallow_any_explicit = false
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Repository = "https://github.com/CQCL/pytket-phir.git"
[tool.setuptools.packages.find]
where = ["."]

[tool.setuptools.package-data]
"pytket.phir" = ["py.typed"]

[tool.pytest.ini_options]
addopts = "-s -vv"
pythonpath = [
Expand Down
10 changes: 7 additions & 3 deletions pytket/phir/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logging
from typing import TYPE_CHECKING

from rich import print

from phir.model import PHIRModel
from pytket.circuit import Circuit
from pytket.phir.phirgen import genphir
from pytket.phir.place_and_route import place_and_route
Expand Down Expand Up @@ -50,7 +53,8 @@ def pytket_to_phir(
msg = "no machine found"
raise ValueError(msg)

phir_output = genphir(placed)
phir_json = genphir(placed)

logger.info("Output: %s", phir_output)
return phir_output
if logger.getEffectiveLevel() <= logging.INFO:
print(PHIRModel.model_validate_json(phir_json, strict=True)) # type: ignore[misc]
return phir_json
27 changes: 11 additions & 16 deletions pytket/phir/phirgen.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
# mypy: disable-error-code="misc,no-any-unimported"

import json
from typing import Any

from phir.model import ( # type: ignore [import-untyped]
Cmd,
DataMgmt,
OpType,
PHIRModel,
QOp,
)
from phir.model import PHIRModel
from pytket.circuit import Command
from pytket.phir.sharding.shard import Shard


def write_cmd(cmd: Command, ops: list[Cmd]) -> None:
def write_cmd(cmd: Command, ops: list[dict[str, Any]]) -> None:
"""Write a pytket command to PHIR qop.

Args:
Expand All @@ -23,17 +16,19 @@ def write_cmd(cmd: Command, ops: list[Cmd]) -> None:
gate = cmd.op.get_name().split("(", 1)[0]
metadata, angles = (
({"angle_multiplier": "π"}, cmd.op.params)
if gate != "Measure"
if gate != "Measure" and cmd.op.params
else (None, None)
)
qop: QOp = {
qop: dict[str, Any] = {
"metadata": metadata,
"angles": angles,
"qop": gate,
"args": [],
}
for qbit in cmd.args:
qop["args"].append([qbit.reg_name, qbit.index[0]])
if gate == "Measure":
break
Copy link
Member Author

Choose a reason for hiding this comment

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

Previously, both the qbit and cbit were part of the args for Measure.

if cmd.bits:
qop["returns"] = []
for cbit in cmd.bits:
Expand All @@ -47,12 +42,12 @@ def genphir(inp: list[tuple[list[int], list[Shard], float]]) -> str:
Args:
inp: list of shards
"""
phir = {
phir: dict[str, Any] = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

No better types we can give than Any?

Copy link
Member Author

Choose a reason for hiding this comment

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

With the current approach, i.e., constructing Python dict's instead of a PHIRModel object, the best we can do is Any. Someday, we can consider directly constructing PHIRModel but I found it difficult to go that route in my first attempt.

"format": "PHIR/JSON",
"version": "0.1.0",
"metadata": {"source": "pytket-phir"},
}
ops: OpType = []
ops: list[dict[str, Any]] = []

qbits = set()
cbits = set()
Expand Down Expand Up @@ -82,7 +77,7 @@ def genphir(inp: list[tuple[list[int], list[Shard], float]]) -> str:
cvar_dim.setdefault(cbit.reg_name, 0)
cvar_dim[cbit.reg_name] += 1

decls: list[DataMgmt] = [
decls: list[dict[str, str | int]] = [
{
"data": "qvar_define",
"data_type": "qubits",
Expand All @@ -102,5 +97,5 @@ def genphir(inp: list[tuple[list[int], list[Shard], float]]) -> str:
]

phir["ops"] = decls + ops
PHIRModel.model_validate(phir)
PHIRModel.model_validate(phir, strict=True)
return json.dumps(phir)
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
black==23.10.0
build==1.0.3
mypy==1.6.0
phir @ git+https://github.com/CQCL/phir
mypy==1.6.1
phir==0.1.3
pre-commit==3.5.0
pytest==7.4.2
pytket-quantinuum==0.25.0
Expand Down
8 changes: 8 additions & 0 deletions tests/e2e_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from phir.model import PHIRModel
from rich import print

from pytket.phir.machine import Machine
from pytket.phir.phirgen import genphir
from pytket.phir.place_and_route import place_and_route
from pytket.phir.placement import placement_check
from pytket.phir.sharding.sharder import Sharder
Expand Down Expand Up @@ -35,3 +39,7 @@
cost_1 = output[1][2]
assert cost_0 == 2.0
assert cost_1 == 0.0

phir_json = genphir(output)

print(PHIRModel.model_validate_json(phir_json, strict=True)) # type: ignore[misc]