Skip to content
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

Rebasing initial implementation #7

Merged
merged 9 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ repos:
additional_dependencies: [
pytest,
pytket==1.20.1,
pytket-quantinuum==0.23.0,
types-setuptools,
]
32 changes: 32 additions & 0 deletions pytket/phir/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from pytket.circuit import Circuit

from .qtm_machine import QtmMachine

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than doing this form of relative imports, we should be importing from the actual package

from .rebasing.rebaser import rebase_to_qtm_machine
from .sharding.sharder import Sharder


def pytket_to_phir(
Jonhas-qtm marked this conversation as resolved.
Show resolved Hide resolved
circuit: Circuit,
qtm_machine: QtmMachine | None = None,
) -> str:
"""Converts a pytket circuit into its PHIR representation.

This can optionally include rebasing against a Quantinuum machine architecture.

:param circuit: Circuit object to be converted
:param qtm_machine: (Optional) Quantinuum machine architecture to rebase against

Returns:
-------
PHIR JSON as a str
"""
if qtm_machine:
circuit = rebase_to_qtm_machine(circuit, qtm_machine.value)

sharder = Sharder(circuit)
shards = sharder.shard()

# TODO: Pass shards[] into placement, routing, etc
# TODO: Convert to PHIR JSON spec and return

return str(shards) # Just returning fake string for now
27 changes: 0 additions & 27 deletions pytket/phir/main.py

This file was deleted.

9 changes: 9 additions & 0 deletions pytket/phir/qtm_machine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from enum import Enum


class QtmMachine(Enum):
"""Available machine architectures."""

H1_1 = "H1-1"
H1_2 = "H1-2"
H2_1 = "H2-1"
Empty file.
18 changes: 18 additions & 0 deletions pytket/phir/rebasing/rebaser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pytket.circuit import Circuit
from pytket.extensions.quantinuum.backends.api_wrappers import QuantinuumAPIOffline
from pytket.extensions.quantinuum.backends.quantinuum import (
QuantinuumBackend,
)


def rebase_to_qtm_machine(circuit: Circuit, qtm_machine: str) -> Circuit:
"""Rebases a circuit's gate to the gate set appropriate for the given machine."""
qapi_offline = QuantinuumAPIOffline()
backend = QuantinuumBackend(
device_name=qtm_machine,
machine_debug=False,
api_handler=qapi_offline, # type: ignore [arg-type]
)
# 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)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ pre-commit==3.4.0
pytest==7.4.2
ruff==0.0.292
pytket==1.20.1
pytket-quantinuum==0.23.0
wheel==0.41.2
24 changes: 24 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from pytket.phir.api import pytket_to_phir
from pytket.phir.qtm_machine import QtmMachine

from .sample_data import QasmFiles, get_qasm_as_circuit


class TestApi:
def test_pytket_to_phir_no_machine(self) -> None:
circuit = get_qasm_as_circuit(QasmFiles.baby)

phir = pytket_to_phir(circuit)

# TODO: Make this test more valuable once PHIR is actually returned
print(phir)
Jonhas-qtm marked this conversation as resolved.
Show resolved Hide resolved
assert len(phir) > 0

def test_pytket_to_phir_h1_1(self) -> None:
circuit = get_qasm_as_circuit(QasmFiles.baby)

phir = pytket_to_phir(circuit, QtmMachine.H1_1)

# TODO: Make this test more valuable once PHIR is actually returned
print(phir)
assert len(phir) > 0
23 changes: 23 additions & 0 deletions tests/test_rebaser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pytket.circuit import Circuit, OpType
from pytket.phir.rebasing.rebaser import rebase_to_qtm_machine

from .sample_data import QasmFiles, get_qasm_as_circuit

EXPECTED_GATES = [
OpType.Measure,
OpType.Rz,
OpType.PhasedX,
OpType.ZZPhase,
]


class TestRebaser:
def test_rebaser_happy_path_arc1a(self) -> None:
circ = get_qasm_as_circuit(QasmFiles.baby)
rebased: Circuit = rebase_to_qtm_machine(circ, "H1-1")

print(rebased)
for command in rebased.get_commands():
print(command)
if command.op.is_gate():
assert command.op.type in EXPECTED_GATES