generated from CQCL/pytemplate
-
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
PHIR generation #12
Merged
Merged
PHIR generation #12
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fb9eb04
Update packages
qartik 7332570
routing integration
nealerickson-qtm 45933f2
Fix linting and typing issues
qartik 71e29cf
Merge remote-tracking branch 'origin/integration-of-routing-into-api'…
qartik 16f2728
Update todos
qartik 7a13075
Add first pass to generate PHIR
qartik 6be9d37
Merge branch 'main' into phir-out
qartik 97b8531
Bit more cleanup
qartik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# mypy: disable-error-code="misc,no-any-unimported" | ||
|
||
import json | ||
|
||
from phir.model import ( # type: ignore [import-untyped] | ||
Cmd, | ||
DataMgmt, | ||
OpType, | ||
PHIRModel, | ||
QOp, | ||
) | ||
from pytket.circuit import Command | ||
from pytket.phir.sharding.shard import Shard | ||
|
||
|
||
def write_cmd(cmd: Command, ops: list[Cmd]) -> None: | ||
"""Write a pytket command to PHIR qop. | ||
|
||
Args: | ||
cmd: pytket command obtained from pytket-phir | ||
ops: the list of ops to append to | ||
""" | ||
gate = cmd.op.get_name().split("(", 1)[0] | ||
metadata, angles = ( | ||
({"angle_multiplier": "π"}, cmd.op.params) | ||
if gate != "Measure" | ||
else (None, None) | ||
) | ||
qop: QOp = { | ||
"metadata": metadata, | ||
"angles": angles, | ||
"qop": gate, | ||
"args": [], | ||
} | ||
for qbit in cmd.args: | ||
qop["args"].append([qbit.reg_name, qbit.index[0]]) | ||
if cmd.bits: | ||
qop["returns"] = [] | ||
for cbit in cmd.bits: | ||
qop["returns"].append([cbit.reg_name, cbit.index[0]]) | ||
ops.extend(({"//": str(cmd)}, qop)) | ||
|
||
|
||
def genphir(inp: list[tuple[list[int], list[Shard], float]]) -> str: | ||
"""Convert a list of shards to the equivalent PHIR. | ||
|
||
Args: | ||
inp: list of shards | ||
""" | ||
phir = { | ||
"format": "PHIR/JSON", | ||
"version": "0.1.0", | ||
"metadata": {"source": "pytket-phir"}, | ||
} | ||
ops: OpType = [] | ||
|
||
qbits = set() | ||
cbits = set() | ||
for _orders, shard_layers, layer_costs in inp: | ||
for shard in shard_layers: | ||
qbits |= shard.qubits_used | ||
cbits |= shard.bits_read | shard.bits_written | ||
for sub_commands in shard.sub_commands.values(): | ||
for sc in sub_commands: | ||
write_cmd(sc, ops) | ||
write_cmd(shard.primary_command, ops) | ||
ops.append( | ||
{ | ||
"mop": "Transport", | ||
"metadata": {"duration": layer_costs / 1000000}, # microseconds to secs | ||
}, | ||
) | ||
|
||
# TODO(kartik): this may not always be accurate | ||
qvar_dim: dict[str, int] = {} | ||
for qbit in qbits: | ||
qvar_dim.setdefault(qbit.reg_name, 0) | ||
qvar_dim[qbit.reg_name] += 1 | ||
|
||
cvar_dim: dict[str, int] = {} | ||
for cbit in cbits: | ||
cvar_dim.setdefault(cbit.reg_name, 0) | ||
cvar_dim[cbit.reg_name] += 1 | ||
|
||
decls: list[DataMgmt] = [ | ||
{ | ||
"data": "qvar_define", | ||
"data_type": "qubits", | ||
"variable": q, | ||
"size": d, | ||
} | ||
for q, d in qvar_dim.items() | ||
] | ||
|
||
decls += [ | ||
{ | ||
"data": "cvar_define", | ||
"variable": c, | ||
"size": d, | ||
} | ||
for c, d in cvar_dim.items() | ||
] | ||
|
||
phir["ops"] = decls + ops | ||
PHIRModel.model_validate(phir) | ||
return json.dumps(phir) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Looks great, but as a note for later, we should definitely come up with some good type aliases for some of these things to make the dataflow a bit more readable.