From bffdec82d3fce61cc62662e063832c72270e03fe Mon Sep 17 00:00:00 2001 From: Kartik Singhal Date: Fri, 10 Nov 2023 11:36:13 -0600 Subject: [PATCH] feat: add phirc CLI driver --- pyproject.toml | 10 ++++++-- pytket/phir/cli.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 pytket/phir/cli.py diff --git a/pyproject.toml b/pyproject.toml index 18aa6d7..450aaef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,11 +1,10 @@ [build-system] -requires = ["setuptools", "wheel"] +requires = ["setuptools>=64", "setuptools_scm>=8", "wheel"] build-backend = "setuptools.build_meta" # https://packaging.python.org/en/latest/specifications/declaring-project-metadata/ [project] name = "pytket-phir" -version = "0.0.1" description = "A circuit analyzer and translator from pytket to PHIR" readme = "README.md" requires-python = ">=3.10" @@ -23,6 +22,7 @@ classifiers = [ "Typing :: Typed", ] +dynamic = ["version"] dependencies = ["phir>=0.1.6", "pytket"] [project.optional-dependencies] @@ -30,6 +30,9 @@ tests = ["pytest"] docs = ["sphinx", "sphinx-rtd-theme"] +[project.scripts] +phirc = "pytket.phir.cli:main" + [project.urls] Repository = "https://github.com/CQCL/pytket-phir.git" @@ -49,3 +52,6 @@ log_cli_level = "INFO" filterwarnings = ["ignore:::lark.s*"] log_format = "%(asctime)s.%(msecs)03d %(levelname)s %(message)s" log_date_format = "%Y-%m-%d %H:%M:%S" + +[tool.setuptools_scm] +version_scheme = "python-simplified-semver" diff --git a/pytket/phir/cli.py b/pytket/phir/cli.py new file mode 100644 index 0000000..672501b --- /dev/null +++ b/pytket/phir/cli.py @@ -0,0 +1,59 @@ +# mypy: disable-error-code="misc" + +from argparse import ArgumentParser +from importlib.metadata import version + +from pecos.engines.hybrid_engine import HybridEngine # type:ignore [import-not-found] + +from phir.model import PHIRModel +from pytket.qasm.qasm import ( + circuit_from_qasm, + circuit_from_qasm_str, + circuit_to_qasm_str, +) + +from .api import pytket_to_phir +from .qtm_machine import QtmMachine +from .rebasing.rebaser import rebase_to_qtm_machine + + +def main() -> None: + """pytket-phir compiler CLI.""" + parser = ArgumentParser( + prog="phirc", + description="Simulates QASM file via PECOS", + ) + parser.add_argument( + "qasm_files", nargs="+", default=None, help="One or more QASM files to simulate" + ) + parser.add_argument( + "-m", + "--machine", + choices=["H1-1", "H1-2"], + default="H1-1", + help="machine name, H1-1 by default", + ) + parser.add_argument( + "-v", + "--version", + action="version", + version=f'{version("pytket-phir")}', + ) + args = parser.parse_args() + + for file in args.qasm_files: + print(f"Processing {file}") # noqa: T201 + c = circuit_from_qasm(file) + rc = rebase_to_qtm_machine(c, args.machine) + qasm = circuit_to_qasm_str(rc, header="hqslib1") + circ = circuit_from_qasm_str(qasm) + + match args.machine: + case "H1-1": + machine = QtmMachine.H1_1 + case "H1-2": + machine = QtmMachine.H1_2 + phir = pytket_to_phir(circ, machine) + PHIRModel.model_validate_json(phir) + + HybridEngine(qsim="state-vector").run(program=phir, shots=10)