Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloAndresCQ committed Nov 11, 2024
1 parent c2dd14a commit 67f5378
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
16 changes: 13 additions & 3 deletions pytket/extensions/cutensornet/structured_state/classical.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,17 @@ def apply_classical_command(

elif isinstance(op, ClExprOp):
# Convert bit_posn to dictionary of `ClBitVar` index to its value
bitvar_val = {var_id: int(bits_dict[args[bit_pos]]) for var_id, bit_pos in op.expr.bit_posn.items()}
bitvar_val = {
var_id: int(bits_dict[args[bit_pos]])
for var_id, bit_pos in op.expr.bit_posn.items()
}
# Convert reg_posn to dictionary of `ClRegVar` index to its value
regvar_val = {var_id: from_little_endian([bits_dict[args[bit_pos]] for bit_pos in reg_pos_list]) for var_id, reg_pos_list in op.expr.reg_posn.items()}
regvar_val = {
var_id: from_little_endian(
[bits_dict[args[bit_pos]] for bit_pos in reg_pos_list]
)
for var_id, reg_pos_list in op.expr.reg_posn.items()
}
result = evaluate_clexpr(op.expr.expr, bitvar_val, regvar_val)

# The result is an int in little-endian encoding. We update the
Expand Down Expand Up @@ -90,7 +98,9 @@ def apply_classical_command(
raise NotImplementedError(f"Commands of type {op.type} are not supported.")


def evaluate_clexpr(expr: ClExpr, bitvar_val: dict[int, int], regvar_val: dict[int, int]) -> int:
def evaluate_clexpr(
expr: ClExpr, bitvar_val: dict[int, int], regvar_val: dict[int, int]
) -> int:
"""Recursive evaluation of a ClExpr."""

# Evaluate arguments to operation
Expand Down
21 changes: 12 additions & 9 deletions tests/test_structured_state_conditionals.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
# (see https://github.com/CQCL/pytket-qir/blob/main/tests/conditional_test.py)
# Further down, there are tests to check that the simulation works correctly.


def test_circuit_with_clexpr_i() -> None:
# test conditional handling

Expand All @@ -35,11 +36,11 @@ def test_circuit_with_clexpr_i() -> None:
c = circ.add_c_register("c", 5)
d = circ.add_c_register("d", 5)
circ.H(0)
wexpr, args = wired_clexpr_from_logic_exp(a | b, c)
wexpr, args = wired_clexpr_from_logic_exp(a | b, c) # type: ignore
circ.add_clexpr(wexpr, args)
wexpr, args = wired_clexpr_from_logic_exp(c | b, d)
wexpr, args = wired_clexpr_from_logic_exp(c | b, d) # type: ignore
circ.add_clexpr(wexpr, args)
wexpr, args = wired_clexpr_from_logic_exp(c | b, d)
wexpr, args = wired_clexpr_from_logic_exp(c | b, d) # type: ignore
circ.add_clexpr(wexpr, args, condition=a[4])
circ.H(0)
circ.Measure(Qubit(0), d[4])
Expand Down Expand Up @@ -82,6 +83,7 @@ def test_circuit_with_classicalexpbox_i() -> None:
assert np.isclose(state.vdot(state), 1.0, atol=cfg._atol)
assert state.get_fidelity() == 1.0


def test_circuit_with_clexpr_ii() -> None:
# test conditional handling with else case

Expand All @@ -91,11 +93,11 @@ def test_circuit_with_clexpr_ii() -> None:
c = circ.add_c_register("c", 5)
d = circ.add_c_register("d", 5)
circ.H(0)
wexpr, args = wired_clexpr_from_logic_exp(a | b, c)
wexpr, args = wired_clexpr_from_logic_exp(a | b, c) # type: ignore
circ.add_clexpr(wexpr, args)
wexpr, args = wired_clexpr_from_logic_exp(c | b, d)
wexpr, args = wired_clexpr_from_logic_exp(c | b, d) # type: ignore
circ.add_clexpr(wexpr, args)
wexpr, args = wired_clexpr_from_logic_exp(c | b, d)
wexpr, args = wired_clexpr_from_logic_exp(c | b, d) # type: ignore
circ.add_clexpr(wexpr, args, condition=if_not_bit(a[4]))
circ.H(0)
circ.Measure(Qubit(0), d[4])
Expand Down Expand Up @@ -140,6 +142,7 @@ def test_circuit_with_classicalexpbox_ii() -> None:
assert np.isclose(state.vdot(state), 1.0, atol=cfg._atol)
assert state.get_fidelity() == 1.0


@pytest.mark.skip(reason="Currently not supporting arithmetic operations in ClExpr")
def test_circuit_with_clexpr_iii() -> None:
# test complicated conditions and recursive classical op
Expand All @@ -157,9 +160,9 @@ def test_circuit_with_clexpr_iii() -> None:
big_exp = bits[4] | bits[5] ^ bits[6] | bits[7] & bits[8]
circ.H(0, condition=big_exp)

wexpr, args = wired_clexpr_from_logic_exp(a + b - d, c)
wexpr, args = wired_clexpr_from_logic_exp(a + b - d, c) # type: ignore
circ.add_clexpr(wexpr, args)
wexpr, args = wired_clexpr_from_logic_exp(a * b * d * c, e)
wexpr, args = wired_clexpr_from_logic_exp(a * b * d * c, e) # type: ignore
circ.add_clexpr(wexpr, args)

with CuTensorNetHandle() as libhandle:
Expand Down Expand Up @@ -337,7 +340,7 @@ def test_pytket_qir_conditional_11() -> None:

box_circ.H(0)

wexpr, args = wired_clexpr_from_logic_exp(box_c | box_c, box_c)
wexpr, args = wired_clexpr_from_logic_exp(box_c | box_c, box_c) # type: ignore
box_circ.add_clexpr(wexpr, args)

cbox = CircBox(box_circ)
Expand Down

0 comments on commit 67f5378

Please sign in to comment.