generated from CQCL/pytemplate
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First implementation to generate PHIR from sample QASM circuits. -------------------------------------------------------------------------- * Update packages * routing integration * Fix linting and typing issues * Update todos * Add first pass to generate PHIR * Bit more cleanup --------- Co-authored-by: Neal Erickson <[email protected]>
- Loading branch information
1 parent
c0605d8
commit ac3c955
Showing
17 changed files
with
226 additions
and
104 deletions.
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.