-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
fb8bac5
1e47906
a2843cc
596b1e0
7c704d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: | ||
|
@@ -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 | ||
if cmd.bits: | ||
qop["returns"] = [] | ||
for cbit in cmd.bits: | ||
|
@@ -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] = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No better types we can give than There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
"format": "PHIR/JSON", | ||
"version": "0.1.0", | ||
"metadata": {"source": "pytket-phir"}, | ||
} | ||
ops: OpType = [] | ||
ops: list[dict[str, Any]] = [] | ||
|
||
qbits = set() | ||
cbits = set() | ||
|
@@ -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", | ||
|
@@ -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) |
There was a problem hiding this comment.
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.