Skip to content

Commit

Permalink
style: use enumerate over range(len...), better types
Browse files Browse the repository at this point in the history
  • Loading branch information
qartik committed Dec 14, 2023
1 parent bd63bdc commit 7eccd8c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
6 changes: 2 additions & 4 deletions pytket/phir/phirgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,13 @@ def convert_subcmd(op: tk.Op, cmd: tk.Command) -> dict[str, Any]:
if len(cmd.bits) != len(op.values):
logger.error("LHS and RHS lengths mismatch for classical assignment")
raise ValueError
return assign_cop(
[arg_to_bit(cmd.bits[i]) for i in range(len(cmd.bits))], op.values
)
return assign_cop([arg_to_bit(bit) for bit in cmd.bits], op.values)

case tk.CopyBitsOp():
if len(cmd.bits) != len(cmd.args) // 2:
logger.warning("LHS and RHS lengths mismatch for CopyBits")
return assign_cop(
[arg_to_bit(cmd.bits[i]) for i in range(len(cmd.bits))],
[arg_to_bit(bit) for bit in cmd.bits],
[arg_to_bit(cmd.args[i]) for i in range(len(cmd.args) // 2)],
)

Expand Down
11 changes: 5 additions & 6 deletions tests/test_parallel_tk2.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_pll_tk2() -> None:
# it is the correct output for the tk2.qasm file
# if you change the tk2.qasm file, you just re-generate the correct
# phir json and replace the expected or the test will fail
expected = {
expected: dict[str, Any] = {
"ops": [
{"data": "qvar_define", "data_type": "qubits", "variable": "q", "size": 4},
{"data": "cvar_define", "data_type": "u32", "variable": "c", "size": 4},
Expand Down Expand Up @@ -101,11 +101,10 @@ def test_pll_tk2() -> None:
}

assert actual["ops"][6]["block"] == "qparallel"
exp_qpar_ops = expected["ops"][6]["ops"] # type: ignore[index]
for i in range(len(exp_qpar_ops)):
assert exp_qpar_ops[i] in actual["ops"][6]["ops"]
for op in expected["ops"][6]["ops"]:
assert op in actual["ops"][6]["ops"]

act_meas_op = actual["ops"][8]
assert act_meas_op["qop"] == "Measure"
assert sorted(act_meas_op["args"]) == expected["ops"][8]["args"] # type: ignore[index]
assert sorted(act_meas_op["returns"]) == expected["ops"][8]["returns"] # type: ignore[index]
assert sorted(act_meas_op["args"]) == expected["ops"][8]["args"]
assert sorted(act_meas_op["returns"]) == expected["ops"][8]["returns"]
11 changes: 5 additions & 6 deletions tests/test_parallelization.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_phir_json(qasmfile: QasmFile) -> dict[str, Any]:
def test_bv_n10() -> None:
"""Make sure the parallelization is happening properly for the test circuit."""
actual = get_phir_json(QasmFile.parallelization_test)
expected = {
expected: dict[str, Any] = {
"ops": [
{"data": "qvar_define", "data_type": "qubits", "variable": "q", "size": 4},
{"data": "cvar_define", "data_type": "u32", "variable": "c", "size": 4},
Expand Down Expand Up @@ -82,11 +82,10 @@ def test_bv_n10() -> None:
}

assert actual["ops"][7]["block"] == "qparallel"
exp_qpar_ops = expected["ops"][7]["ops"] # type: ignore[index]
for i in range(len(exp_qpar_ops)):
assert exp_qpar_ops[i] in actual["ops"][7]["ops"]
for op in expected["ops"][7]["ops"]:
assert op in actual["ops"][7]["ops"]

act_meas_op = actual["ops"][9]
assert act_meas_op["qop"] == "Measure"
assert sorted(act_meas_op["args"]) == expected["ops"][9]["args"] # type: ignore[index]
assert sorted(act_meas_op["returns"]) == expected["ops"][9]["returns"] # type: ignore[index]
assert sorted(act_meas_op["args"]) == expected["ops"][9]["args"]
assert sorted(act_meas_op["returns"]) == expected["ops"][9]["returns"]

0 comments on commit 7eccd8c

Please sign in to comment.