From 7f41edd5280ea14e0b00fe684e7ac94894453955 Mon Sep 17 00:00:00 2001 From: Neal Erickson Date: Tue, 26 Sep 2023 21:21:10 -0600 Subject: [PATCH 01/11] First pass of sharding data types --- Makefile | 4 ++ pytket/phir/main.py | 33 ++++++++++++--- pytket/phir/sharding/__init__.py | 0 pytket/phir/sharding/shard.py | 51 +++++++++++++++++++++++ pytket/phir/sharding/sharder.py | 70 ++++++++++++++++++++++++++++++++ requirements.txt | 1 + tests/data/qasm/baby.qasm | 11 +++++ tests/data/qasm/bv_n10.qasm | 47 +++++++++++++++++++++ tests/data/qasm/cond_1.qasm | 16 ++++++++ tests/data/qasm/simple.qasm | 11 +++++ tests/sample_data.py | 24 +++++++++++ tests/test_requirements.txt | 1 + tests/test_sharder.py | 25 ++++++++++++ 13 files changed, 288 insertions(+), 6 deletions(-) create mode 100644 Makefile create mode 100644 pytket/phir/sharding/__init__.py create mode 100644 pytket/phir/sharding/shard.py create mode 100644 pytket/phir/sharding/sharder.py create mode 100644 tests/data/qasm/baby.qasm create mode 100644 tests/data/qasm/bv_n10.qasm create mode 100644 tests/data/qasm/cond_1.qasm create mode 100644 tests/data/qasm/simple.qasm create mode 100644 tests/sample_data.py create mode 100644 tests/test_requirements.txt create mode 100644 tests/test_sharder.py diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..14274d8 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +.PHONY: tests + +tests: + pytest -s -x -vv tests/test*.py \ No newline at end of file diff --git a/pytket/phir/main.py b/pytket/phir/main.py index bca21dd..542798b 100644 --- a/pytket/phir/main.py +++ b/pytket/phir/main.py @@ -1,10 +1,31 @@ -"""This is the main module of the pytket-phir package.""" +############################################################################## +# +# (c) 2023 @ Quantinuum LLC. All Rights Reserved. +# This software and all information and expression are the property of +# Quantinuum LLC, are Quantinuum LLC Confidential & Proprietary, +# contain trade secrets and may not, in whole or in part, be licensed, +# used, duplicated, disclosed, or reproduced for any purpose without prior +# written permission of Quantinuum LLC. +# +############################################################################## +""" +NOTE: Just a placeholder to allow convenient testing of the flows +""" -def hello_world() -> str: - """Print 'Hello, world!' to the console.""" - hw = "Hello, World!" - return hw +from pytket.qasm import circuit_from_qasm +from pytket import Circuit +from sharding.sharder import Sharder -print(hello_world()) # noqa: T201 +# Load a qasm circuit and parse +circuit: Circuit = circuit_from_qasm("tests/data/qasm/simple.qasm") + +# https://cqcl.github.io/tket/pytket/api/circuit_class.html + +# Just a little debuggin fun +print('Input circuit:') +print(circuit) +print() + +sharding_output = Sharder(circuit).shard() \ No newline at end of file diff --git a/pytket/phir/sharding/__init__.py b/pytket/phir/sharding/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pytket/phir/sharding/shard.py b/pytket/phir/sharding/shard.py new file mode 100644 index 0000000..963f999 --- /dev/null +++ b/pytket/phir/sharding/shard.py @@ -0,0 +1,51 @@ +############################################################################## +# +# (c) 2023 @ Quantinuum LLC. All Rights Reserved. +# This software and all information and expression are the property of +# Quantinuum LLC, are Quantinuum LLC Confidential & Proprietary, +# contain trade secrets and may not, in whole or in part, be licensed, +# used, duplicated, disclosed, or reproduced for any purpose without prior +# written permission of Quantinuum LLC. +# +############################################################################## + +from __future__ import annotations +from dataclasses import dataclass + +from pytket.circuit import Command +from pytket.unit_id import UnitID + +@dataclass +class Shard: + """ + A shard is a logical grouping of operations that represents the unit by which + we actually do placement of qubits + """ + + # The schedulable command of the shard + primary_command: Command + + # The other commands related to the primary schedulable command, stored + # as a map of bit-handle (unitID) -> list[Command] + sub_commands: dict[UnitID, list[Command]] + + # A set of the other shards this particular shard depends upon, and thus + # must be scheduled after + depends_upon: set[Shard] + + # def __init__(self, + # primary_command: Command, + # sub_commands: dict[UnitID, list[Command]], + # depends_upon: set[Shard]) -> None: + + # # The schedulable command of the shard + # self.primary_command = primary_command + + # # The other commands related to the primary schedulable command, stored + # # as a map of bit handle (unitID) -> list[Command] + # self.sub_commands = sub_commands + + # # A set of the other shards this particular shard depends upon, and thus + # # must be scheduled after + # self.depends_upon = depends_upon + diff --git a/pytket/phir/sharding/sharder.py b/pytket/phir/sharding/sharder.py new file mode 100644 index 0000000..d193d48 --- /dev/null +++ b/pytket/phir/sharding/sharder.py @@ -0,0 +1,70 @@ +############################################################################## +# +# (c) 2023 @ Quantinuum LLC. All Rights Reserved. +# This software and all information and expression are the property of +# Quantinuum LLC, are Quantinuum LLC Confidential & Proprietary, +# contain trade secrets and may not, in whole or in part, be licensed, +# used, duplicated, disclosed, or reproduced for any purpose without prior +# written permission of Quantinuum LLC. +# +############################################################################## + +from pytket import Circuit +from pytket.circuit import Command, Op, OpType +from pytket._tket.unit_id import UnitID + +from .shard import Shard + +NOT_IMPLEMENTED_OP_TYPES = [OpType.CircBox, OpType.WASM] + +class Sharder: + """ + The sharder class is responsible for taking in a circuit in TKET representation + and converting it into shards that can be subsequently handled in the + compilation pipeline. + """ + + def __init__(self, circuit: Circuit) -> None: + self._circuit = circuit + print(f'Sharder created for circuit {self._circuit}') + self._pending_commands: dict[UnitID, list[Command]] = {} + self._shards: list[Shard] = [] + + def shard(self) -> list[Shard]: + print('Sharding begins....') + # https://cqcl.github.io/tket/pytket/api/circuit.html#pytket.circuit.Command + + for command in self._circuit.get_commands(): + self._process_command(command) + return self._shards + + def _process_command(self, command: Command) -> None: + print('Processing command: ', command.op, command.op.type, command.args) + if command.op.type in NOT_IMPLEMENTED_OP_TYPES: + raise NotImplementedError(f'OpType {command.op.type} not supported!') + + if self._is_op_schedulable(command.op): + print(f'Scheduling command: {command}') + self._build_shard(command) + else: + self._add_pending_command(command) + + + def _build_shard(self, command: Command) -> None: + shard = Shard(command, + {}, + []) + # TODO: Add sub operations + self._shards.append(shard) + print('Appended shard: ', shard) + + def _add_pending_command(self, command: Command) -> None: + #if command. + pass + + def _is_op_schedulable(self, op: Op) -> bool: + return ( + op.type == OpType.Measure + or op.type == OpType.Reset + or (op.is_gate() and op.n_qubits > 1) + ) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 214e0e8..76bf51a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ pytest==7.4.2 ruff==0.0.291 sphinx==7.2.6 wheel==0.41.2 +pytket==1.20.1 \ No newline at end of file diff --git a/tests/data/qasm/baby.qasm b/tests/data/qasm/baby.qasm new file mode 100644 index 0000000..65ec93f --- /dev/null +++ b/tests/data/qasm/baby.qasm @@ -0,0 +1,11 @@ +OPENQASM 2.0; +include "hqslib1.inc"; + +qreg q[2]; +creg c[2]; + +h q[0]; +h q[1]; +CX q[0], q[1]; + +measure q->c; \ No newline at end of file diff --git a/tests/data/qasm/bv_n10.qasm b/tests/data/qasm/bv_n10.qasm new file mode 100644 index 0000000..63775cc --- /dev/null +++ b/tests/data/qasm/bv_n10.qasm @@ -0,0 +1,47 @@ +//@author Raymond Harry Rudy rudyhar@jp.ibm.com +//Bernstein-Vazirani with 10 qubits. +//Hidden string is 111111111 +OPENQASM 2.0; +include "qelib1.inc"; +qreg qr[10]; +creg cr[9]; +h qr[0]; +h qr[1]; +h qr[2]; +h qr[3]; +h qr[4]; +h qr[5]; +h qr[6]; +h qr[7]; +h qr[8]; +x qr[9]; +h qr[9]; +barrier qr[0],qr[1],qr[2],qr[3],qr[4],qr[5],qr[6],qr[7],qr[8],qr[9]; +cx qr[0],qr[9]; +cx qr[1],qr[9]; +cx qr[2],qr[9]; +cx qr[3],qr[9]; +cx qr[4],qr[9]; +cx qr[5],qr[9]; +cx qr[6],qr[9]; +cx qr[7],qr[9]; +cx qr[8],qr[9]; +barrier qr[0],qr[1],qr[2],qr[3],qr[4],qr[5],qr[6],qr[7],qr[8],qr[9]; +h qr[0]; +h qr[1]; +h qr[2]; +h qr[3]; +h qr[4]; +h qr[5]; +h qr[6]; +h qr[7]; +h qr[8]; +measure qr[0] -> cr[0]; +measure qr[1] -> cr[1]; +measure qr[2] -> cr[2]; +measure qr[3] -> cr[3]; +measure qr[4] -> cr[4]; +measure qr[5] -> cr[5]; +measure qr[6] -> cr[6]; +measure qr[7] -> cr[7]; +measure qr[8] -> cr[8]; diff --git a/tests/data/qasm/cond_1.qasm b/tests/data/qasm/cond_1.qasm new file mode 100644 index 0000000..6dfe4a5 --- /dev/null +++ b/tests/data/qasm/cond_1.qasm @@ -0,0 +1,16 @@ +OPENQASM 2.0; +include "hqslib1.inc"; + +qreg q[1]; +creg c[2]; + +h q; +measure q[0]->c[0]; +reset q; +if (c==1) h q; +if (c<1) h q; +if (c>1) h q; +if (c<=1) h q; +if (c>=1) h q; +if (c!=1) h q; +measure q[0]->c[0]; diff --git a/tests/data/qasm/simple.qasm b/tests/data/qasm/simple.qasm new file mode 100644 index 0000000..9ff7b72 --- /dev/null +++ b/tests/data/qasm/simple.qasm @@ -0,0 +1,11 @@ +OPENQASM 2.0; +include "qelib1.inc"; +qreg q[1]; +creg c[1]; + +h q[0]; +h q[0]; +h q[0]; +h q[0]; +h q[0]; +measure q->c; diff --git a/tests/sample_data.py b/tests/sample_data.py new file mode 100644 index 0000000..8951121 --- /dev/null +++ b/tests/sample_data.py @@ -0,0 +1,24 @@ +############################################################################## +# +# (c) 2023 @ Quantinuum LLC. All Rights Reserved. +# This software and all information and expression are the property of +# Quantinuum LLC, are Quantinuum LLC Confidential & Proprietary, +# contain trade secrets and may not, in whole or in part, be licensed, +# used, duplicated, disclosed, or reproduced for any purpose without prior +# written permission of Quantinuum LLC. +# +############################################################################## + +from enum import Enum + +from pytket import Circuit +from pytket.qasm import circuit_from_qasm + +class QasmFiles(Enum): + simple = 1 + cond_1 = 2 + bv_n10 = 3 + baby = 4 + +def get_qasm_as_circuit(qasm_file: QasmFiles) -> Circuit: + return circuit_from_qasm(f'tests/data/qasm/{qasm_file.name}.qasm') \ No newline at end of file diff --git a/tests/test_requirements.txt b/tests/test_requirements.txt new file mode 100644 index 0000000..de1887b --- /dev/null +++ b/tests/test_requirements.txt @@ -0,0 +1 @@ +pytest==7.4.2 \ No newline at end of file diff --git a/tests/test_sharder.py b/tests/test_sharder.py new file mode 100644 index 0000000..edda096 --- /dev/null +++ b/tests/test_sharder.py @@ -0,0 +1,25 @@ +############################################################################## +# +# (c) 2023 @ Quantinuum LLC. All Rights Reserved. +# This software and all information and expression are the property of +# Quantinuum LLC, are Quantinuum LLC Confidential & Proprietary, +# contain trade secrets and may not, in whole or in part, be licensed, +# used, duplicated, disclosed, or reproduced for any purpose without prior +# written permission of Quantinuum LLC. +# +############################################################################## + +from .sample_data import get_qasm_as_circuit, QasmFiles +from pytket.phir import Sharder + +class TestSharder: + + def test_ctor(self) -> None: + sharder = Sharder(get_qasm_as_circuit(QasmFiles.baby)) + assert sharder is not None + + output = sharder.shard() + + assert len(output) > 0 + + print(output) \ No newline at end of file From ea8db4f324c8533ef85a3d8a23b1a24ced2b1488 Mon Sep 17 00:00:00 2001 From: Neal Erickson Date: Wed, 27 Sep 2023 09:53:56 -0600 Subject: [PATCH 02/11] first pass --- pytket/phir/main.py | 8 +++++++- pytket/phir/sharding/sharder.py | 30 ++++++++++++++++++++++++------ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/pytket/phir/main.py b/pytket/phir/main.py index 542798b..3112fd1 100644 --- a/pytket/phir/main.py +++ b/pytket/phir/main.py @@ -19,7 +19,13 @@ from sharding.sharder import Sharder # Load a qasm circuit and parse -circuit: Circuit = circuit_from_qasm("tests/data/qasm/simple.qasm") +# ,=""=, +# c , _,{ +# /\ @ ) __ +# / ^~~^\ <=.,__/ '}= +# (_/ ,, ,,) \_ _>_/~ +# ~\_(/-\)'-,_,_,_,-'(_)-(_) +circuit: Circuit = circuit_from_qasm("tests/data/qasm/baby.qasm") # https://cqcl.github.io/tket/pytket/api/circuit_class.html diff --git a/pytket/phir/sharding/sharder.py b/pytket/phir/sharding/sharder.py index d193d48..a742417 100644 --- a/pytket/phir/sharding/sharder.py +++ b/pytket/phir/sharding/sharder.py @@ -31,14 +31,21 @@ def __init__(self, circuit: Circuit) -> None: self._shards: list[Shard] = [] def shard(self) -> list[Shard]: + """ + Performs the sharding algorithm on the circuit the Sharder was initialized + with, returning the list of Shards needed to schedule + """ print('Sharding begins....') # https://cqcl.github.io/tket/pytket/api/circuit.html#pytket.circuit.Command - for command in self._circuit.get_commands(): self._process_command(command) return self._shards def _process_command(self, command: Command) -> None: + """ + Handles a given TKET command (operation, bits, etc) according to the type + and the extant context within the Sharder + """ print('Processing command: ', command.op, command.op.type, command.args) if command.op.type in NOT_IMPLEMENTED_OP_TYPES: raise NotImplementedError(f'OpType {command.op.type} not supported!') @@ -51,16 +58,27 @@ def _process_command(self, command: Command) -> None: def _build_shard(self, command: Command) -> None: + """ + Creates a Shard object given the extant sharding context and the schedulable + Command object passed in, and appends it to the Shard list + """ shard = Shard(command, - {}, + self._pending_commands, []) - # TODO: Add sub operations + # TODO: Dependencies! + self._pending_commands = {} self._shards.append(shard) - print('Appended shard: ', shard) + print('Appended shard:', shard) def _add_pending_command(self, command: Command) -> None: - #if command. - pass + """ + Adds a pending sub command to the buffer to be flushed when a schedulable + operation creates a Shard. + """ + # TODO NJE: Need to make sure 'args[0]' is the right key to use. + if command.args[0] not in self._pending_commands: + self._pending_commands[command.args[0]] = [] + self._pending_commands[command.args[0]].append(command) def _is_op_schedulable(self, op: Op) -> bool: return ( From 60f21be881475e848822d5cd966059981091d3d3 Mon Sep 17 00:00:00 2001 From: Neal Erickson Date: Wed, 27 Sep 2023 12:21:58 -0600 Subject: [PATCH 03/11] cleanup --- .pre-commit-config.yaml | 2 +- Makefile | 7 ++++-- pytket/phir/main.py | 17 ++++++++------ pytket/phir/sharding/shard.py | 17 +++++++------- pytket/phir/sharding/sharder.py | 41 ++++++++++++++++++--------------- pytket/phir/utils.py | 6 ----- requirements.txt | 2 +- ruff.toml | 7 +++++- tests/data/qasm/baby.qasm | 2 +- tests/sample_data.py | 8 ++++--- tests/test_main.py | 8 ------- tests/test_requirements.txt | 2 +- tests/test_sharder.py | 11 ++++----- tests/test_utils.py | 10 -------- 14 files changed, 67 insertions(+), 73 deletions(-) delete mode 100644 pytket/phir/utils.py delete mode 100644 tests/test_main.py delete mode 100644 tests/test_utils.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 99f7ad3..b343688 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -37,5 +37,5 @@ repos: additional_dependencies: [ pytest, types-setuptools, - pytket, + pytket==1.20.1, ] diff --git a/Makefile b/Makefile index 14274d8..98b123e 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,7 @@ -.PHONY: tests +.PHONY: tests, lint tests: - pytest -s -x -vv tests/test*.py \ No newline at end of file + pytest -s -x -vv tests/test*.py + +lint: + pre-commit run --all-files diff --git a/pytket/phir/main.py b/pytket/phir/main.py index 3112fd1..7e33568 100644 --- a/pytket/phir/main.py +++ b/pytket/phir/main.py @@ -13,10 +13,10 @@ NOTE: Just a placeholder to allow convenient testing of the flows """ -from pytket.qasm import circuit_from_qasm -from pytket import Circuit +from pytket.circuit import Circuit +from pytket.qasm.qasm import circuit_from_qasm -from sharding.sharder import Sharder +from .sharding.sharder import Sharder # Load a qasm circuit and parse # ,=""=, @@ -24,14 +24,17 @@ # /\ @ ) __ # / ^~~^\ <=.,__/ '}= # (_/ ,, ,,) \_ _>_/~ -# ~\_(/-\)'-,_,_,_,-'(_)-(_) +# ~\_(/-\)'-,_,_,_,-'(_)-(_) circuit: Circuit = circuit_from_qasm("tests/data/qasm/baby.qasm") # https://cqcl.github.io/tket/pytket/api/circuit_class.html -# Just a little debuggin fun -print('Input circuit:') +# Just a little debugging fun +print("Input circuit:") print(circuit) print() -sharding_output = Sharder(circuit).shard() \ No newline at end of file +sharding_output = Sharder(circuit).shard() + +print("Sharding output:") +print(sharding_output) diff --git a/pytket/phir/sharding/shard.py b/pytket/phir/sharding/shard.py index 963f999..5fb6564 100644 --- a/pytket/phir/sharding/shard.py +++ b/pytket/phir/sharding/shard.py @@ -10,16 +10,18 @@ ############################################################################## from __future__ import annotations + from dataclasses import dataclass from pytket.circuit import Command from pytket.unit_id import UnitID + @dataclass class Shard: """ A shard is a logical grouping of operations that represents the unit by which - we actually do placement of qubits + we actually do placement of qubits """ # The schedulable command of the shard @@ -28,24 +30,23 @@ class Shard: # The other commands related to the primary schedulable command, stored # as a map of bit-handle (unitID) -> list[Command] sub_commands: dict[UnitID, list[Command]] - + # A set of the other shards this particular shard depends upon, and thus - # must be scheduled after + # must be scheduled after depends_upon: set[Shard] - # def __init__(self, + # def __init__(self, # primary_command: Command, # sub_commands: dict[UnitID, list[Command]], # depends_upon: set[Shard]) -> None: - + # # The schedulable command of the shard # self.primary_command = primary_command - + # # The other commands related to the primary schedulable command, stored # # as a map of bit handle (unitID) -> list[Command] # self.sub_commands = sub_commands - + # # A set of the other shards this particular shard depends upon, and thus # # must be scheduled after # self.depends_upon = depends_upon - diff --git a/pytket/phir/sharding/sharder.py b/pytket/phir/sharding/sharder.py index a742417..ddc4abf 100644 --- a/pytket/phir/sharding/sharder.py +++ b/pytket/phir/sharding/sharder.py @@ -9,24 +9,24 @@ # ############################################################################## -from pytket import Circuit -from pytket.circuit import Command, Op, OpType from pytket._tket.unit_id import UnitID +from pytket.circuit import Circuit, Command, Op, OpType from .shard import Shard NOT_IMPLEMENTED_OP_TYPES = [OpType.CircBox, OpType.WASM] + class Sharder: """ The sharder class is responsible for taking in a circuit in TKET representation - and converting it into shards that can be subsequently handled in the + and converting it into shards that can be subsequently handled in the compilation pipeline. """ def __init__(self, circuit: Circuit) -> None: self._circuit = circuit - print(f'Sharder created for circuit {self._circuit}') + print(f"Sharder created for circuit {self._circuit}") self._pending_commands: dict[UnitID, list[Command]] = {} self._shards: list[Shard] = [] @@ -35,54 +35,59 @@ def shard(self) -> list[Shard]: Performs the sharding algorithm on the circuit the Sharder was initialized with, returning the list of Shards needed to schedule """ - print('Sharding begins....') + print("Sharding begins....") # https://cqcl.github.io/tket/pytket/api/circuit.html#pytket.circuit.Command for command in self._circuit.get_commands(): self._process_command(command) return self._shards - + def _process_command(self, command: Command) -> None: """ Handles a given TKET command (operation, bits, etc) according to the type and the extant context within the Sharder """ - print('Processing command: ', command.op, command.op.type, command.args) + print("Processing command: ", command.op, command.op.type, command.args) if command.op.type in NOT_IMPLEMENTED_OP_TYPES: - raise NotImplementedError(f'OpType {command.op.type} not supported!') - + msg = f"OpType {command.op.type} not supported!" + raise NotImplementedError(msg) + if self._is_op_schedulable(command.op): - print(f'Scheduling command: {command}') + print(f"Scheduling command: {command}") self._build_shard(command) else: self._add_pending_command(command) - def _build_shard(self, command: Command) -> None: """ Creates a Shard object given the extant sharding context and the schedulable Command object passed in, and appends it to the Shard list """ - shard = Shard(command, - self._pending_commands, - []) + shard = Shard(command, self._pending_commands, set()) # TODO: Dependencies! self._pending_commands = {} self._shards.append(shard) - print('Appended shard:', shard) + print("Appended shard:", shard) def _add_pending_command(self, command: Command) -> None: """ Adds a pending sub command to the buffer to be flushed when a schedulable operation creates a Shard. """ - # TODO NJE: Need to make sure 'args[0]' is the right key to use. + # TODO: Need to make sure 'args[0]' is the right key to use. if command.args[0] not in self._pending_commands: self._pending_commands[command.args[0]] = [] self._pending_commands[command.args[0]].append(command) - def _is_op_schedulable(self, op: Op) -> bool: + @staticmethod + def _is_op_schedulable(op: Op) -> bool: + """ + Returns `True` if the operation is one that should be scheduled, that is, + that will have a shard created for it. This includes non-gate operations + like measure/reset as well as 2-qubit gates. + """ + # TODO: This is almost certainly inadequate right now return ( op.type == OpType.Measure or op.type == OpType.Reset or (op.is_gate() and op.n_qubits > 1) - ) \ No newline at end of file + ) diff --git a/pytket/phir/utils.py b/pytket/phir/utils.py deleted file mode 100644 index 4114ecb..0000000 --- a/pytket/phir/utils.py +++ /dev/null @@ -1,6 +0,0 @@ -"""Utility functions for the pytket-phir package.""" - - -def add_numbers(a: int, b: int) -> int: - """Add two numbers and returns the result.""" - return a + b diff --git a/requirements.txt b/requirements.txt index 76bf51a..c602e38 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,4 +7,4 @@ pytest==7.4.2 ruff==0.0.291 sphinx==7.2.6 wheel==0.41.2 -pytket==1.20.1 \ No newline at end of file +pytket==1.20.1 diff --git a/ruff.toml b/ruff.toml index 7dc820d..8e3ca5f 100644 --- a/ruff.toml +++ b/ruff.toml @@ -42,7 +42,12 @@ select = [ "YTT", # flake8-2020 ] -ignore = [] +ignore = [ + "T201", # no prints flake-8 + "FIX002", # Allow todos + "TD002", # Allow no author todos + "TD003", # allow todos with no issues +] [per-file-ignores] "__init__.py" = ["F401"] # module imported but unused diff --git a/tests/data/qasm/baby.qasm b/tests/data/qasm/baby.qasm index 65ec93f..72a521b 100644 --- a/tests/data/qasm/baby.qasm +++ b/tests/data/qasm/baby.qasm @@ -8,4 +8,4 @@ h q[0]; h q[1]; CX q[0], q[1]; -measure q->c; \ No newline at end of file +measure q->c; diff --git a/tests/sample_data.py b/tests/sample_data.py index 8951121..e64afb2 100644 --- a/tests/sample_data.py +++ b/tests/sample_data.py @@ -11,8 +11,9 @@ from enum import Enum -from pytket import Circuit -from pytket.qasm import circuit_from_qasm +from pytket.circuit import Circuit +from pytket.qasm.qasm import circuit_from_qasm + class QasmFiles(Enum): simple = 1 @@ -20,5 +21,6 @@ class QasmFiles(Enum): bv_n10 = 3 baby = 4 + def get_qasm_as_circuit(qasm_file: QasmFiles) -> Circuit: - return circuit_from_qasm(f'tests/data/qasm/{qasm_file.name}.qasm') \ No newline at end of file + return circuit_from_qasm(f"tests/data/qasm/{qasm_file.name}.qasm") diff --git a/tests/test_main.py b/tests/test_main.py deleted file mode 100644 index 9d673cb..0000000 --- a/tests/test_main.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Tests for pytket-phir.main module.""" - -from pytket.phir.main import hello_world - - -def test_hello_world(): - """Test the hello_world function.""" - assert hello_world() == "Hello, World!" diff --git a/tests/test_requirements.txt b/tests/test_requirements.txt index de1887b..2a929ed 100644 --- a/tests/test_requirements.txt +++ b/tests/test_requirements.txt @@ -1 +1 @@ -pytest==7.4.2 \ No newline at end of file +pytest==7.4.2 diff --git a/tests/test_sharder.py b/tests/test_sharder.py index edda096..ee8815a 100644 --- a/tests/test_sharder.py +++ b/tests/test_sharder.py @@ -9,17 +9,16 @@ # ############################################################################## -from .sample_data import get_qasm_as_circuit, QasmFiles -from pytket.phir import Sharder +from pytket.phir.sharding.sharder import Sharder + +from .sample_data import QasmFiles, get_qasm_as_circuit -class TestSharder: +class TestSharder: def test_ctor(self) -> None: sharder = Sharder(get_qasm_as_circuit(QasmFiles.baby)) assert sharder is not None output = sharder.shard() - assert len(output) > 0 - - print(output) \ No newline at end of file + assert len(output) == 2 # noqa: PLR2004 diff --git a/tests/test_utils.py b/tests/test_utils.py deleted file mode 100644 index d181a80..0000000 --- a/tests/test_utils.py +++ /dev/null @@ -1,10 +0,0 @@ -"""Tests for pytket-phir.utils.""" - -from pytket.phir.utils import add_numbers - - -def test_add(): - """Test the add function.""" - assert add_numbers(2, 3) == 5 # noqa: PLR2004 - assert add_numbers(0, 0) == 0 - assert add_numbers(-1, 1) == 0 From c5bdcb00ef36e95e6d3c4674d95e4ad2fa25e72f Mon Sep 17 00:00:00 2001 From: Neal Erickson Date: Wed, 27 Sep 2023 12:31:58 -0600 Subject: [PATCH 04/11] trying a fix --- pytket/phir/sharding/sharder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytket/phir/sharding/sharder.py b/pytket/phir/sharding/sharder.py index ddc4abf..482df68 100644 --- a/pytket/phir/sharding/sharder.py +++ b/pytket/phir/sharding/sharder.py @@ -9,8 +9,8 @@ # ############################################################################## -from pytket._tket.unit_id import UnitID from pytket.circuit import Circuit, Command, Op, OpType +from pytket.unit_id import UnitID from .shard import Shard From b13d489d0604765d40a8284cc20ccaf21b9cfefb Mon Sep 17 00:00:00 2001 From: Kartik Singhal Date: Wed, 27 Sep 2023 15:00:03 -0500 Subject: [PATCH 05/11] Fix namespace collision introduced by unneeded __init__.py --- .pre-commit-config.yaml | 4 ++-- pyproject.toml | 1 + pytket/__init__.py | 0 3 files changed, 3 insertions(+), 2 deletions(-) delete mode 100644 pytket/__init__.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b343688..4210b23 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -33,9 +33,9 @@ repos: hooks: - id: mypy pass_filenames: false - args: [.] + args: [--package=pytket.phir, --package=tests] additional_dependencies: [ pytest, - types-setuptools, pytket==1.20.1, + types-setuptools, ] diff --git a/pyproject.toml b/pyproject.toml index aafb143..c60e2c1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,3 +16,4 @@ where = ["."] pythonpath = [ "." ] +filterwarnings = ["ignore:::lark.s*"] diff --git a/pytket/__init__.py b/pytket/__init__.py deleted file mode 100644 index e69de29..0000000 From 3f529768a46f7c1b550b66c813757a61eded1aeb Mon Sep 17 00:00:00 2001 From: Neal Erickson Date: Wed, 27 Sep 2023 14:33:12 -0600 Subject: [PATCH 06/11] small changes --- pytket/phir/sharding/sharder.py | 4 ++-- tests/test_sharder.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pytket/phir/sharding/sharder.py b/pytket/phir/sharding/sharder.py index 482df68..7f5ab54 100644 --- a/pytket/phir/sharding/sharder.py +++ b/pytket/phir/sharding/sharder.py @@ -51,7 +51,7 @@ def _process_command(self, command: Command) -> None: msg = f"OpType {command.op.type} not supported!" raise NotImplementedError(msg) - if self._is_op_schedulable(command.op): + if self.is_op_schedulable(command.op): print(f"Scheduling command: {command}") self._build_shard(command) else: @@ -79,7 +79,7 @@ def _add_pending_command(self, command: Command) -> None: self._pending_commands[command.args[0]].append(command) @staticmethod - def _is_op_schedulable(op: Op) -> bool: + def is_op_schedulable(op: Op) -> bool: """ Returns `True` if the operation is one that should be scheduled, that is, that will have a shard created for it. This includes non-gate operations diff --git a/tests/test_sharder.py b/tests/test_sharder.py index ee8815a..dbfe57b 100644 --- a/tests/test_sharder.py +++ b/tests/test_sharder.py @@ -21,4 +21,4 @@ def test_ctor(self) -> None: output = sharder.shard() - assert len(output) == 2 # noqa: PLR2004 + assert len(output) == 3 # noqa: PLR2004 From a7c1c68737d8259adfca260af2270332a9c0b3f0 Mon Sep 17 00:00:00 2001 From: Neal Erickson Date: Wed, 27 Sep 2023 14:42:50 -0600 Subject: [PATCH 07/11] removing --- tests/test_requirements.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 tests/test_requirements.txt diff --git a/tests/test_requirements.txt b/tests/test_requirements.txt deleted file mode 100644 index 2a929ed..0000000 --- a/tests/test_requirements.txt +++ /dev/null @@ -1 +0,0 @@ -pytest==7.4.2 From f759167d9f10a04a2b1e2b1429c67e0a918b0798 Mon Sep 17 00:00:00 2001 From: Neal Erickson Date: Wed, 27 Sep 2023 15:20:40 -0600 Subject: [PATCH 08/11] feedback 1 --- pytket/phir/sharding/shard.py | 20 +------------------- ruff.toml | 5 ++++- tests/test_sharder.py | 2 +- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/pytket/phir/sharding/shard.py b/pytket/phir/sharding/shard.py index 5fb6564..7ae8beb 100644 --- a/pytket/phir/sharding/shard.py +++ b/pytket/phir/sharding/shard.py @@ -9,8 +9,6 @@ # ############################################################################## -from __future__ import annotations - from dataclasses import dataclass from pytket.circuit import Command @@ -33,20 +31,4 @@ class Shard: # A set of the other shards this particular shard depends upon, and thus # must be scheduled after - depends_upon: set[Shard] - - # def __init__(self, - # primary_command: Command, - # sub_commands: dict[UnitID, list[Command]], - # depends_upon: set[Shard]) -> None: - - # # The schedulable command of the shard - # self.primary_command = primary_command - - # # The other commands related to the primary schedulable command, stored - # # as a map of bit handle (unitID) -> list[Command] - # self.sub_commands = sub_commands - - # # A set of the other shards this particular shard depends upon, and thus - # # must be scheduled after - # self.depends_upon = depends_upon + depends_upon: set["Shard"] diff --git a/ruff.toml b/ruff.toml index 8e3ca5f..c66ae97 100644 --- a/ruff.toml +++ b/ruff.toml @@ -51,7 +51,10 @@ ignore = [ [per-file-ignores] "__init__.py" = ["F401"] # module imported but unused -"tests/*" = ["S101"] # Use of `assert` detected +"tests/*" = [ + "S101", # Use of `assert` detected + "PLR2004" # Magic constants + ] [pydocstyle] convention = "google" diff --git a/tests/test_sharder.py b/tests/test_sharder.py index dbfe57b..1249284 100644 --- a/tests/test_sharder.py +++ b/tests/test_sharder.py @@ -21,4 +21,4 @@ def test_ctor(self) -> None: output = sharder.shard() - assert len(output) == 3 # noqa: PLR2004 + assert len(output) == 3 From 34f045c9e8422e0ec72d4933e1640a976a722301 Mon Sep 17 00:00:00 2001 From: Neal Erickson Date: Wed, 27 Sep 2023 17:14:39 -0600 Subject: [PATCH 09/11] rename of method in sharder --- pytket/phir/sharding/sharder.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pytket/phir/sharding/sharder.py b/pytket/phir/sharding/sharder.py index 7f5ab54..4371093 100644 --- a/pytket/phir/sharding/sharder.py +++ b/pytket/phir/sharding/sharder.py @@ -51,8 +51,8 @@ def _process_command(self, command: Command) -> None: msg = f"OpType {command.op.type} not supported!" raise NotImplementedError(msg) - if self.is_op_schedulable(command.op): - print(f"Scheduling command: {command}") + if self.should_op_create_shard(command.op): + print(f"Building shard for command: {command}") self._build_shard(command) else: self._add_pending_command(command) @@ -79,11 +79,10 @@ def _add_pending_command(self, command: Command) -> None: self._pending_commands[command.args[0]].append(command) @staticmethod - def is_op_schedulable(op: Op) -> bool: + def should_op_create_shard(op: Op) -> bool: """ - Returns `True` if the operation is one that should be scheduled, that is, - that will have a shard created for it. This includes non-gate operations - like measure/reset as well as 2-qubit gates. + Returns `True` if the operation is one that should result in shard creation. + This includes non-gate operations like measure/reset as well as 2-qubit gates. """ # TODO: This is almost certainly inadequate right now return ( From c2eb07831abbc9432d9d1da75e12205349bde3a7 Mon Sep 17 00:00:00 2001 From: Neal Erickson Date: Thu, 28 Sep 2023 08:29:31 -0600 Subject: [PATCH 10/11] removing headers --- pytket/phir/main.py | 11 ----------- pytket/phir/sharding/shard.py | 11 ----------- pytket/phir/sharding/sharder.py | 11 ----------- tests/sample_data.py | 11 ----------- tests/test_sharder.py | 8 -------- 5 files changed, 52 deletions(-) diff --git a/pytket/phir/main.py b/pytket/phir/main.py index 7e33568..a8d37c1 100644 --- a/pytket/phir/main.py +++ b/pytket/phir/main.py @@ -1,14 +1,3 @@ -############################################################################## -# -# (c) 2023 @ Quantinuum LLC. All Rights Reserved. -# This software and all information and expression are the property of -# Quantinuum LLC, are Quantinuum LLC Confidential & Proprietary, -# contain trade secrets and may not, in whole or in part, be licensed, -# used, duplicated, disclosed, or reproduced for any purpose without prior -# written permission of Quantinuum LLC. -# -############################################################################## - """ NOTE: Just a placeholder to allow convenient testing of the flows """ diff --git a/pytket/phir/sharding/shard.py b/pytket/phir/sharding/shard.py index 7ae8beb..0ff4cb8 100644 --- a/pytket/phir/sharding/shard.py +++ b/pytket/phir/sharding/shard.py @@ -1,14 +1,3 @@ -############################################################################## -# -# (c) 2023 @ Quantinuum LLC. All Rights Reserved. -# This software and all information and expression are the property of -# Quantinuum LLC, are Quantinuum LLC Confidential & Proprietary, -# contain trade secrets and may not, in whole or in part, be licensed, -# used, duplicated, disclosed, or reproduced for any purpose without prior -# written permission of Quantinuum LLC. -# -############################################################################## - from dataclasses import dataclass from pytket.circuit import Command diff --git a/pytket/phir/sharding/sharder.py b/pytket/phir/sharding/sharder.py index 4371093..b3e9cf1 100644 --- a/pytket/phir/sharding/sharder.py +++ b/pytket/phir/sharding/sharder.py @@ -1,14 +1,3 @@ -############################################################################## -# -# (c) 2023 @ Quantinuum LLC. All Rights Reserved. -# This software and all information and expression are the property of -# Quantinuum LLC, are Quantinuum LLC Confidential & Proprietary, -# contain trade secrets and may not, in whole or in part, be licensed, -# used, duplicated, disclosed, or reproduced for any purpose without prior -# written permission of Quantinuum LLC. -# -############################################################################## - from pytket.circuit import Circuit, Command, Op, OpType from pytket.unit_id import UnitID diff --git a/tests/sample_data.py b/tests/sample_data.py index e64afb2..73487d1 100644 --- a/tests/sample_data.py +++ b/tests/sample_data.py @@ -1,14 +1,3 @@ -############################################################################## -# -# (c) 2023 @ Quantinuum LLC. All Rights Reserved. -# This software and all information and expression are the property of -# Quantinuum LLC, are Quantinuum LLC Confidential & Proprietary, -# contain trade secrets and may not, in whole or in part, be licensed, -# used, duplicated, disclosed, or reproduced for any purpose without prior -# written permission of Quantinuum LLC. -# -############################################################################## - from enum import Enum from pytket.circuit import Circuit diff --git a/tests/test_sharder.py b/tests/test_sharder.py index 1249284..68e3c37 100644 --- a/tests/test_sharder.py +++ b/tests/test_sharder.py @@ -1,14 +1,6 @@ ############################################################################## # # (c) 2023 @ Quantinuum LLC. All Rights Reserved. -# This software and all information and expression are the property of -# Quantinuum LLC, are Quantinuum LLC Confidential & Proprietary, -# contain trade secrets and may not, in whole or in part, be licensed, -# used, duplicated, disclosed, or reproduced for any purpose without prior -# written permission of Quantinuum LLC. -# -############################################################################## - from pytket.phir.sharding.sharder import Sharder from .sample_data import QasmFiles, get_qasm_as_circuit From 1232f14ee59b0847af4d9cec29aad1e7dfad31e1 Mon Sep 17 00:00:00 2001 From: Neal Erickson Date: Thu, 28 Sep 2023 10:10:46 -0600 Subject: [PATCH 11/11] mistake in deletion --- tests/test_sharder.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/test_sharder.py b/tests/test_sharder.py index 68e3c37..7a6c533 100644 --- a/tests/test_sharder.py +++ b/tests/test_sharder.py @@ -1,6 +1,3 @@ -############################################################################## -# -# (c) 2023 @ Quantinuum LLC. All Rights Reserved. from pytket.phir.sharding.sharder import Sharder from .sample_data import QasmFiles, get_qasm_as_circuit