Skip to content

Commit

Permalink
Merge pull request #82 from CQCL/rxrx-order-swapping
Browse files Browse the repository at this point in the history
RZ/R1XY order swapping bugfix
  • Loading branch information
Asa-Kosto-QTM authored Jan 10, 2024
2 parents ea7eae8 + 0bc1154 commit 5ed2c4f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pytket/phir/phirgen_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
# 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)
)
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 = {}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions tests/data/qasm/rxrz.qasm
Original file line number Diff line number Diff line change
@@ -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;
19 changes: 19 additions & 0 deletions tests/test_parallelization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
1 change: 1 addition & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down

0 comments on commit 5ed2c4f

Please sign in to comment.