Skip to content

Commit

Permalink
style: eliminate all type casts
Browse files Browse the repository at this point in the history
  • Loading branch information
qartik committed Mar 22, 2024
1 parent 2942c77 commit c6bb114
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
11 changes: 3 additions & 8 deletions pytket/phir/sharding/sharder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
##############################################################################

import logging
from typing import cast

from pytket.circuit import Circuit, Command, Conditional, Op, OpType
from pytket.unit_id import Bit, Qubit, UnitID
Expand Down Expand Up @@ -273,11 +272,8 @@ def should_op_create_shard(op: Op) -> bool:
`True` if the operation is one that should result in shard creation
"""
return (
op.type in (SHARD_TRIGGER_OP_TYPES)
or (
op.type == OpType.Conditional
and cast(Conditional, op).op.type in (SHARD_TRIGGER_OP_TYPES)
)
op.type in SHARD_TRIGGER_OP_TYPES
or (isinstance(op, Conditional) and op.op.type in SHARD_TRIGGER_OP_TYPES)
or (op.is_gate() and op.n_qubits > 1)
)

Expand All @@ -289,6 +285,5 @@ def _is_command_global_phase(command: Command) -> bool:
command: Command to evaluate
"""
return command.op.type == OpType.Phase or (
command.op.type == OpType.Conditional
and cast(Conditional, command.op).op.type == OpType.Phase
isinstance(command.op, Conditional) and command.op.op.type == OpType.Phase
)
10 changes: 4 additions & 6 deletions tests/test_sharder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#
##############################################################################

from typing import cast

from pytket.circuit import Conditional, Op, OpType
from pytket.phir.sharding.sharder import Sharder

Expand Down Expand Up @@ -126,8 +124,8 @@ def test_simple_conditional(self) -> None:
assert not shards[1].bits_read

# shard 2: if (c==1) z=1;
assert shards[2].primary_command.op.type == OpType.Conditional
assert cast(Conditional, shards[2].primary_command.op).op.type == OpType.SetBits
assert isinstance(shards[2].primary_command.op, Conditional)
assert shards[2].primary_command.op.op.type == OpType.SetBits
assert not shards[2].sub_commands
assert not shards[2].qubits_used
assert shards[2].bits_written == {circuit.bits[1]}
Expand All @@ -143,8 +141,8 @@ def test_simple_conditional(self) -> None:
assert len(shards[3].sub_commands.items()) == 1
s2_qubit, s2_sub_cmds = next(iter(shards[3].sub_commands.items()))
assert s2_qubit == circuit.qubits[0]
assert s2_sub_cmds[0].op.type == OpType.Conditional
assert cast(Conditional, s2_sub_cmds[0].op).op.type == OpType.H
assert isinstance(s2_sub_cmds[0].op, Conditional)
assert s2_sub_cmds[0].op.op.type == OpType.H
assert s2_sub_cmds[0].qubits == [circuit.qubits[0]]

def test_complex_barriers(self) -> None: # noqa: PLR0915
Expand Down

0 comments on commit c6bb114

Please sign in to comment.