Skip to content

Commit d0e067f

Browse files
committed
Modified stuff for mypy, cleaner now
1 parent 4848dae commit d0e067f

1 file changed

Lines changed: 21 additions & 29 deletions

File tree

qualtran/bloqs/bookkeeping/partition.py

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import abc
1515
import warnings
1616
from functools import cached_property
17-
from typing import Dict, List, Optional, Sequence, Tuple, TYPE_CHECKING
17+
from typing import Dict, List, Optional, Sequence, Tuple, TYPE_CHECKING, cast
1818

1919
import numpy as np
2020
import sympy
@@ -221,42 +221,34 @@ class Partition(_PartitionBase):
221221
partition: bool = field(default=True)
222222

223223
def __attrs_post_init__(self):
224-
match (self.n, self.dtype_in):
225-
case (None, None):
226-
raise ValueError("Provide exactly n or dtype_in")
227-
case (n, None):
228-
warnings.warn(
229-
"Partition: By not setting dtype_in you could encounter errors when running "
230-
"assert_consistent_classical_action",
231-
category=LegacyPartitionWarning,
232-
)
233-
case (None, dt):
234-
assert dt is not None # for mypy
235-
object.__setattr__(self, "n", dt.num_qubits)
236-
case (n, dt):
237-
assert dt is not None
238-
assert n is not None # for mypy
239-
if n != dt.num_qubits:
240-
raise ValueError(f"{dt=} should have size {n=}, currently {dt.num_qubits=}")
241-
warnings.warn(
242-
"Specifying both n and dtype_in is redundant",
243-
category=UserWarning,
244-
stacklevel=1,
224+
if self.n is None and self.dtype_in is None:
225+
raise ValueError(f"Provide exactly n or dtype_in {self.n=}, {self.dtype_in=}")
226+
elif self.n is not None and self.dtype_in is None:
227+
warnings.warn(
228+
"Partition: By not setting dtype_in you could encounter errors when running "
229+
"assert_consistent_classical_action",
230+
category=LegacyPartitionWarning,
231+
)
232+
elif self.n is None and self.dtype_in is not None:
233+
object.__setattr__(self, "n", self.dtype_in.num_qubits)
234+
elif self.n is not None and self.dtype_in is not None:
235+
if self.n != self.dtype_in.num_qubits:
236+
raise ValueError(
237+
f"{self.dtype_in=} should have size {self.n=}, currently {self.dtype_in.num_qubits=}"
245238
)
239+
warnings.warn(
240+
"Specifying both n and dtype_in is redundant", category=UserWarning, stacklevel=1
241+
)
246242

247243
self._validate()
248244

249245
@property
250246
def lumped_dtype(self) -> QDType:
251-
if self.dtype_in is not None:
252-
return self.dtype_in
253-
254-
assert self.n is not None # for mypy
255-
return QUInt(bitsize=self.n)
247+
return QUInt(bitsize=cast(SymbolicInt, self.n)) if self.dtype_in is None else self.dtype_in
256248

257249
@property
258250
def _regs(self) -> Sequence[Register]:
259-
return self.regs
251+
return cast(Tuple[Register, ...], self.regs)
260252

261253
@cached_property
262254
def signature(self) -> 'Signature':
@@ -265,7 +257,7 @@ def signature(self) -> 'Signature':
265257

266258
return Signature(
267259
[Register('x', self.lumped_dtype, side=lumped)]
268-
+ [evolve(reg, side=partitioned) for reg in self.regs]
260+
+ [evolve(reg, side=partitioned) for reg in self._regs]
269261
)
270262

271263
def adjoint(self):

0 commit comments

Comments
 (0)