Skip to content

Commit

Permalink
Issue 49 qasm string input (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
neal-erickson authored Dec 15, 2023
1 parent 5cd46aa commit d1b7f65
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 9 deletions.
35 changes: 32 additions & 3 deletions pytket/phir/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from rich import print

from phir.model import PHIRModel
from pytket.qasm.qasm import circuit_from_qasm_str

from .phirgen import genphir
from .phirgen_parallel import genphir_parallel
Expand All @@ -27,14 +28,22 @@

logger = logging.getLogger(__name__)

DEFAULT_TKET_OPT_LEVEL = 0

def pytket_to_phir(circuit: "Circuit", qtm_machine: QtmMachine | None = None) -> str:

def pytket_to_phir(
circuit: "Circuit",
qtm_machine: QtmMachine | None = None,
tket_optimization_level: int = DEFAULT_TKET_OPT_LEVEL,
) -> str:
"""Converts a pytket circuit into its PHIR representation.
This can optionally include rebasing against a Quantinuum machine architecture.
This can optionally include rebasing against a Quantinuum machine architecture,
and control of the TKET optimization level.
:param circuit: Circuit object to be converted
:param qtm_machine: (Optional) Quantinuum machine architecture to rebase against
:param tket_optimization_level: (Default=0) TKET circuit optimization level
Returns:
PHIR JSON as a str
Expand All @@ -43,7 +52,9 @@ def pytket_to_phir(circuit: "Circuit", qtm_machine: QtmMachine | None = None) ->
machine: Machine | None = None
if qtm_machine:
logger.info("Rebasing to machine %s", qtm_machine)
circuit = rebase_to_qtm_machine(circuit, qtm_machine.value)
circuit = rebase_to_qtm_machine(
circuit, qtm_machine.value, tket_optimization_level
)
machine = QTM_MACHINES_MAP.get(qtm_machine)
else:
machine = None
Expand All @@ -65,3 +76,21 @@ def pytket_to_phir(circuit: "Circuit", qtm_machine: QtmMachine | None = None) ->
if logger.getEffectiveLevel() <= logging.INFO:
print(PHIRModel.model_validate_json(phir_json)) # type: ignore[misc]
return phir_json


def qasm_to_phir(
qasm: str,
qtm_machine: QtmMachine | None = None,
tket_optimization_level: int = DEFAULT_TKET_OPT_LEVEL,
) -> str:
"""Converts a QASM circuit string into its PHIR representation.
This can optionally include rebasing against a Quantinuum machine architecture,
and control of the TKET optimization level.
:param circuit: Circuit object to be converted
:param qtm_machine: (Optional) Quantinuum machine architecture to rebase against
:param tket_optimization_level: (Default=0) TKET circuit optimization level
"""
circuit = circuit_from_qasm_str(qasm)
return pytket_to_phir(circuit, qtm_machine, tket_optimization_level)
10 changes: 9 additions & 1 deletion pytket/phir/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ def main() -> None:
default="H1-1",
help="machine name, H1-1 by default",
)
parser.add_argument(
"-o",
"--tket-opt-level",
choices=["0", "1", "2"],
default="0",
help="TKET optimization level, 0 by default",
)
parser.add_argument(
"-v",
"--version",
Expand All @@ -52,7 +59,8 @@ def main() -> None:
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)
tket_opt_level = int(args.tk)
rc = rebase_to_qtm_machine(c, args.machine, tket_opt_level)
qasm = circuit_to_qasm_str(rc, header="hqslib1")
circ = circuit_from_qasm_str(qasm)

Expand Down
6 changes: 4 additions & 2 deletions pytket/phir/rebasing/rebaser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
from pytket.circuit import Circuit


def rebase_to_qtm_machine(circuit: "Circuit", qtm_machine: str) -> "Circuit":
def rebase_to_qtm_machine(
circuit: "Circuit", qtm_machine: str, tket_optimization_level: int
) -> "Circuit":
"""Rebases a circuit's gate to the gate set appropriate for the given machine."""
qapi_offline = QuantinuumAPIOffline()
backend = QuantinuumBackend(
Expand All @@ -32,4 +34,4 @@ def rebase_to_qtm_machine(circuit: "Circuit", qtm_machine: str) -> "Circuit":

# Optimization level 0 includes rebasing and little else
# see: https://cqcl.github.io/pytket-quantinuum/api/#default-compilation
return backend.get_compiled_circuit(circuit, 0)
return backend.get_compiled_circuit(circuit, tket_optimization_level)
18 changes: 17 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import pytest

from pytket.circuit import Bit, Circuit
from pytket.phir.api import pytket_to_phir
from pytket.phir.api import pytket_to_phir, qasm_to_phir
from pytket.phir.qtm_machine import QtmMachine

from .sample_data import QasmFile, get_qasm_as_circuit
Expand Down Expand Up @@ -69,3 +69,19 @@ def test_pytket_classical_only(self) -> None:
"returns": [["a", 0], ["b", 0]],
"args": [["b", 2], ["a", 1]],
}

def test_qasm_to_phir(self) -> None:
"""Test the qasm string entrypoint works."""
qasm = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
h q;
ZZ q[1],q[0];
creg cr[3];
measure q[0]->cr[0];
measure q[1]->cr[0];
"""

assert qasm_to_phir(qasm, QtmMachine.H1_1)
2 changes: 1 addition & 1 deletion tests/test_parallelization.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_phir_json(qasmfile: QasmFile) -> dict[str, Any]:
"""Get the QASM file for the specified circuit."""
qtm_machine = QtmMachine.H1_1
circuit = get_qasm_as_circuit(qasmfile)
circuit = rebase_to_qtm_machine(circuit, qtm_machine.value)
circuit = rebase_to_qtm_machine(circuit, qtm_machine.value, 0)
machine = QTM_MACHINES_MAP.get(qtm_machine)
assert machine
sharder = Sharder(circuit)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_rebaser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class TestRebaser:
def test_rebaser_happy_path_arc1a(self) -> None:
circ = get_qasm_as_circuit(QasmFile.baby)
rebased: Circuit = rebase_to_qtm_machine(circ, "H1-1")
rebased: Circuit = rebase_to_qtm_machine(circ, "H1-1", 0)

logger.info(rebased)
for command in rebased.get_commands():
Expand Down

0 comments on commit d1b7f65

Please sign in to comment.