From ca9da5609df3f16917766bb450a2e284c0e76644 Mon Sep 17 00:00:00 2001 From: Asa Kosto Date: Tue, 9 Jan 2024 12:10:06 -0700 Subject: [PATCH 1/8] remove ordering of subcommand groups dict to preserve relative subcommand ordering and added unit test with offending circuit --- pytket/phir/phirgen_parallel.py | 10 +++--- tests/data/qasm/rxrz.qasm | 13 +++++++ ...t_parallel_subcommand_relative_ordering.py | 34 +++++++++++++++++++ tests/test_utils.py | 1 + 4 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 tests/data/qasm/rxrz.qasm create mode 100644 tests/test_parallel_subcommand_relative_ordering.py diff --git a/pytket/phir/phirgen_parallel.py b/pytket/phir/phirgen_parallel.py index 229aff0..c8b2e0e 100644 --- a/pytket/phir/phirgen_parallel.py +++ b/pytket/phir/phirgen_parallel.py @@ -36,16 +36,16 @@ def process_sub_commands( int, list[tk.Command] ] = {} # be sure to order by group number into a list when returning qubits2groups = {} # track the most recent group in which a qubit was used - # group numbers for each gate are incremented by 2 so they don't overlap + # group numbers for each gate are incremented by 3 so they don't overlap # and different gate types don't go in the same group - # RZ gates go in %3=1 groups, R1XY gates go in %3=1 groups, + # RZ gates go in %3=0 groups, R1XY gates go in %3=1 groups, # and all other gates will go in %3=2 groups rz_group_number = -3 # will be set to 0 when first RZ gate is assigned (-3 + 3 = 0) r1xy_group_number = ( -2 # will be set to 1 when first R1XY gate is assigned (-2 + 3 = 1) ) other_group_number = ( - -1 # will be set to 2 when first R1XY gate is assigned (-1 + 3 = 2) + -1 # will be set to 2 when first other gate is assigned (-1 + 3 = 2) ) num_scs_per_qubit = {} @@ -105,7 +105,7 @@ def process_sub_commands( groups[group_number] = [sc] qubits2groups[qubit] = group_number - return dict(sorted(groups.items())) + return dict(groups.items()) def groups2qops(groups: dict[int, list[tk.Command]], ops: list[dict[str, Any]]) -> None: # noqa: PLR0912 @@ -273,7 +273,7 @@ def genphir_parallel( max_parallel_sq_gates = len(machine.sq_options) phir = PHIR_HEADER - phir["metadata"]["strict_parallelism"] = "true" + phir["metadata"]["strict_parallelism"] = "true" # err ciaran mentioned? ops: list[dict[str, Any]] = [] qbits = set() diff --git a/tests/data/qasm/rxrz.qasm b/tests/data/qasm/rxrz.qasm new file mode 100644 index 0000000..e245799 --- /dev/null +++ b/tests/data/qasm/rxrz.qasm @@ -0,0 +1,13 @@ +OPENQASM 2.0; +include "qelib1.inc"; + +qreg q[1]; +creg m[1]; + +rz(pi/2) q; +rx(-pi/2) q; + +rx(pi/2) q; +rz(-pi/2) q; + +measure q -> m; diff --git a/tests/test_parallel_subcommand_relative_ordering.py b/tests/test_parallel_subcommand_relative_ordering.py new file mode 100644 index 0000000..19f0006 --- /dev/null +++ b/tests/test_parallel_subcommand_relative_ordering.py @@ -0,0 +1,34 @@ +############################################################################## +# +# Copyright (c) 2023 Quantinuum LLC All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. +# +############################################################################## + +# mypy: disable-error-code="misc" + +import logging + +from .test_utils import QasmFile, get_phir_json + +logger = logging.getLogger(__name__) + + +def test_parallelization() -> None: + """Make sure the proper relative ordering of sub-commands is preserved.""" + phir = get_phir_json(QasmFile.rxrz, rebase=True) + # make sure it is ordered like the qasm file + ops = phir["ops"] + frst_sc = ops[3] + scnd_sc = ops[5] + thrd_sc = ops[7] + frth_sc = ops[9] + assert frst_sc["qop"] == "RZ" + assert frst_sc["angles"] == [[0.5], "pi"] + assert scnd_sc["qop"] == "R1XY" + assert scnd_sc["angles"] == [[3.5, 0.0], "pi"] + assert thrd_sc["qop"] == "R1XY" + assert thrd_sc["angles"] == [[0.5, 0.0], "pi"] + assert frth_sc["qop"] == "RZ" + assert frth_sc["angles"] == [[3.5], "pi"] diff --git a/tests/test_utils.py b/tests/test_utils.py index 4120b91..9a9314f 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -40,6 +40,7 @@ class QasmFile(Enum): parallelization_test = auto() tk2_same_angle = auto() tk2_diff_angles = auto() + rxrz = auto() def get_qasm_as_circuit(qasm_file: QasmFile) -> "Circuit": From ffd75f8c564dcdc29a778e406d4282f311e3c058 Mon Sep 17 00:00:00 2001 From: Asa Kosto Date: Tue, 9 Jan 2024 12:22:38 -0700 Subject: [PATCH 2/8] deleted unnecessary comment --- pytket/phir/phirgen_parallel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytket/phir/phirgen_parallel.py b/pytket/phir/phirgen_parallel.py index c8b2e0e..eeb24ec 100644 --- a/pytket/phir/phirgen_parallel.py +++ b/pytket/phir/phirgen_parallel.py @@ -273,7 +273,7 @@ def genphir_parallel( max_parallel_sq_gates = len(machine.sq_options) phir = PHIR_HEADER - phir["metadata"]["strict_parallelism"] = "true" # err ciaran mentioned? + phir["metadata"]["strict_parallelism"] = "true" ops: list[dict[str, Any]] = [] qbits = set() From ef6b7f603c377c19d8fc0169edee042d687609e2 Mon Sep 17 00:00:00 2001 From: Asa Kosto Date: Tue, 9 Jan 2024 13:05:33 -0700 Subject: [PATCH 3/8] changed string true in metadata to bool True --- pytket/phir/phirgen_parallel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytket/phir/phirgen_parallel.py b/pytket/phir/phirgen_parallel.py index eeb24ec..f264b4d 100644 --- a/pytket/phir/phirgen_parallel.py +++ b/pytket/phir/phirgen_parallel.py @@ -273,7 +273,7 @@ def genphir_parallel( max_parallel_sq_gates = len(machine.sq_options) phir = PHIR_HEADER - phir["metadata"]["strict_parallelism"] = "true" + phir["metadata"]["strict_parallelism"] = True ops: list[dict[str, Any]] = [] qbits = set() From e677a4a22a4fbc44e9a6a9b53afce327548439c6 Mon Sep 17 00:00:00 2001 From: Asa-Kosto-QTM <108833721+Asa-Kosto-QTM@users.noreply.github.com> Date: Tue, 9 Jan 2024 16:52:01 -0500 Subject: [PATCH 4/8] Update pytket/phir/phirgen_parallel.py Co-authored-by: Kartik Singhal --- pytket/phir/phirgen_parallel.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pytket/phir/phirgen_parallel.py b/pytket/phir/phirgen_parallel.py index f264b4d..015ab9b 100644 --- a/pytket/phir/phirgen_parallel.py +++ b/pytket/phir/phirgen_parallel.py @@ -38,8 +38,8 @@ def process_sub_commands( qubits2groups = {} # track the most recent group in which a qubit was used # group numbers for each gate are incremented by 3 so they don't overlap # and different gate types don't go in the same group - # RZ gates go in %3=0 groups, R1XY gates go in %3=1 groups, - # and all other gates will go in %3=2 groups + # RZ gates go in (mod 3)=0 groups, R1XY gates go in (mod 3)=1 groups, + # and all other gates will go in (mod 3)=2 groups rz_group_number = -3 # will be set to 0 when first RZ gate is assigned (-3 + 3 = 0) r1xy_group_number = ( -2 # will be set to 1 when first R1XY gate is assigned (-2 + 3 = 1) From eafa660a74cfed69e59f40ab7c93eee4eef52487 Mon Sep 17 00:00:00 2001 From: Asa-Kosto-QTM <108833721+Asa-Kosto-QTM@users.noreply.github.com> Date: Tue, 9 Jan 2024 16:52:11 -0500 Subject: [PATCH 5/8] Update tests/test_parallel_subcommand_relative_ordering.py Co-authored-by: Kartik Singhal --- tests/test_parallel_subcommand_relative_ordering.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_parallel_subcommand_relative_ordering.py b/tests/test_parallel_subcommand_relative_ordering.py index 19f0006..5ae7c8b 100644 --- a/tests/test_parallel_subcommand_relative_ordering.py +++ b/tests/test_parallel_subcommand_relative_ordering.py @@ -1,6 +1,6 @@ ############################################################################## # -# Copyright (c) 2023 Quantinuum LLC All rights reserved. +# Copyright (c) 2024 Quantinuum LLC All rights reserved. # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. # From 2624dcee3e6b40f5b067d3ee96ceefe63a0fa067 Mon Sep 17 00:00:00 2001 From: Asa Kosto Date: Wed, 10 Jan 2024 06:59:43 -0700 Subject: [PATCH 6/8] pre-commit --- pytket/phir/cli.py | 2 +- ...t_parallel_subcommand_relative_ordering.py | 34 ------------------- tests/test_parallelization.py | 19 +++++++++++ 3 files changed, 20 insertions(+), 35 deletions(-) delete mode 100644 tests/test_parallel_subcommand_relative_ordering.py diff --git a/pytket/phir/cli.py b/pytket/phir/cli.py index b7704d2..08265fd 100644 --- a/pytket/phir/cli.py +++ b/pytket/phir/cli.py @@ -6,8 +6,8 @@ # ############################################################################## -# mypy: disable-error-code="misc" +# mypy: disable-error-code="misc" from argparse import ArgumentParser from importlib.metadata import version diff --git a/tests/test_parallel_subcommand_relative_ordering.py b/tests/test_parallel_subcommand_relative_ordering.py deleted file mode 100644 index 19f0006..0000000 --- a/tests/test_parallel_subcommand_relative_ordering.py +++ /dev/null @@ -1,34 +0,0 @@ -############################################################################## -# -# Copyright (c) 2023 Quantinuum LLC All rights reserved. -# Use of this source code is governed by a BSD-style -# license that can be found in the LICENSE file. -# -############################################################################## - -# mypy: disable-error-code="misc" - -import logging - -from .test_utils import QasmFile, get_phir_json - -logger = logging.getLogger(__name__) - - -def test_parallelization() -> None: - """Make sure the proper relative ordering of sub-commands is preserved.""" - phir = get_phir_json(QasmFile.rxrz, rebase=True) - # make sure it is ordered like the qasm file - ops = phir["ops"] - frst_sc = ops[3] - scnd_sc = ops[5] - thrd_sc = ops[7] - frth_sc = ops[9] - assert frst_sc["qop"] == "RZ" - assert frst_sc["angles"] == [[0.5], "pi"] - assert scnd_sc["qop"] == "R1XY" - assert scnd_sc["angles"] == [[3.5, 0.0], "pi"] - assert thrd_sc["qop"] == "R1XY" - assert thrd_sc["angles"] == [[0.5, 0.0], "pi"] - assert frth_sc["qop"] == "RZ" - assert frth_sc["angles"] == [[3.5], "pi"] diff --git a/tests/test_parallelization.py b/tests/test_parallelization.py index dc86508..edb08fc 100644 --- a/tests/test_parallelization.py +++ b/tests/test_parallelization.py @@ -55,3 +55,22 @@ def test_parallelization() -> None: assert measure_args.index(["q", 1]) == measure_returns.index(["c", 1]) assert measure_args.index(["q", 2]) == measure_returns.index(["c", 2]) assert measure_args.index(["q", 3]) == measure_returns.index(["c", 3]) + + +def test_parallel_subcommand_relative_ordering() -> None: + """Make sure the proper relative ordering of sub-commands is preserved.""" + phir = get_phir_json(QasmFile.rxrz, rebase=True) + # make sure it is ordered like the qasm file + ops = phir["ops"] + frst_sc = ops[3] + scnd_sc = ops[5] + thrd_sc = ops[7] + frth_sc = ops[9] + assert frst_sc["qop"] == "RZ" + assert frst_sc["angles"] == [[0.5], "pi"] + assert scnd_sc["qop"] == "R1XY" + assert scnd_sc["angles"] == [[3.5, 0.0], "pi"] + assert thrd_sc["qop"] == "R1XY" + assert thrd_sc["angles"] == [[0.5, 0.0], "pi"] + assert frth_sc["qop"] == "RZ" + assert frth_sc["angles"] == [[3.5], "pi"] From 8346f7b9cc60edc82f94cb567c46559f28ad3c98 Mon Sep 17 00:00:00 2001 From: Asa Kosto Date: Wed, 10 Jan 2024 07:01:31 -0700 Subject: [PATCH 7/8] removed extra test file --- ...t_parallel_subcommand_relative_ordering.py | 34 ------------------- 1 file changed, 34 deletions(-) delete mode 100644 tests/test_parallel_subcommand_relative_ordering.py diff --git a/tests/test_parallel_subcommand_relative_ordering.py b/tests/test_parallel_subcommand_relative_ordering.py deleted file mode 100644 index 5ae7c8b..0000000 --- a/tests/test_parallel_subcommand_relative_ordering.py +++ /dev/null @@ -1,34 +0,0 @@ -############################################################################## -# -# Copyright (c) 2024 Quantinuum LLC All rights reserved. -# Use of this source code is governed by a BSD-style -# license that can be found in the LICENSE file. -# -############################################################################## - -# mypy: disable-error-code="misc" - -import logging - -from .test_utils import QasmFile, get_phir_json - -logger = logging.getLogger(__name__) - - -def test_parallelization() -> None: - """Make sure the proper relative ordering of sub-commands is preserved.""" - phir = get_phir_json(QasmFile.rxrz, rebase=True) - # make sure it is ordered like the qasm file - ops = phir["ops"] - frst_sc = ops[3] - scnd_sc = ops[5] - thrd_sc = ops[7] - frth_sc = ops[9] - assert frst_sc["qop"] == "RZ" - assert frst_sc["angles"] == [[0.5], "pi"] - assert scnd_sc["qop"] == "R1XY" - assert scnd_sc["angles"] == [[3.5, 0.0], "pi"] - assert thrd_sc["qop"] == "R1XY" - assert thrd_sc["angles"] == [[0.5, 0.0], "pi"] - assert frth_sc["qop"] == "RZ" - assert frth_sc["angles"] == [[3.5], "pi"] From 0bc11547a68269184cc355faea12a6245e847dd1 Mon Sep 17 00:00:00 2001 From: Asa Kosto Date: Wed, 10 Jan 2024 08:51:16 -0700 Subject: [PATCH 8/8] removed unnecessary change --- pytket/phir/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytket/phir/cli.py b/pytket/phir/cli.py index 08265fd..b7704d2 100644 --- a/pytket/phir/cli.py +++ b/pytket/phir/cli.py @@ -6,8 +6,8 @@ # ############################################################################## - # mypy: disable-error-code="misc" + from argparse import ArgumentParser from importlib.metadata import version