Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Empty file modified check/pytest-minimal-install
100755 → 100644
Empty file.
Empty file modified check/ruff
100755 → 100644
Empty file.
Empty file modified dev_tools/requirements/export-from-uv-lock.sh
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion qualtran/Adjoint.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
" def signature(self) -> 'Signature':\n",
" return Signature.build(x=1)\n",
"\n",
" def build_composite_bloq(self, bb: 'BloqBuilder', x: 'SoquetT') -> Dict[str, 'SoquetT']:\n",
" def build_composite_bloq(self, bb: 'BloqBuilder', x: 'SoquetT') -> dict[str, 'SoquetT']:\n",
" x = bb.add(Hadamard(), q=x)\n",
" x = bb.add(TGate(), q=x)\n",
" return {'x': x}\n",
Expand Down
8 changes: 4 additions & 4 deletions qualtran/_infra/Bloqs-Tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@
"\n",
" def build_composite_bloq(\n",
" self, bb: BloqBuilder, *, x: SoquetT, y: SoquetT\n",
" ) -> Dict[str, SoquetT]:\n",
" ) -> dict[str, SoquetT]:\n",
" # THIS WON'T ACTUALLY WORK! Read on.\n",
" for i in range(self.n):\n",
" x[i], y[i] = bb.add(SwapTwoBits(), x=x[i], y=y[i])\n",
Expand Down Expand Up @@ -682,7 +682,7 @@
"\n",
" def build_composite_bloq(\n",
" self, bb: BloqBuilder, *, x: SoquetT, y: SoquetT\n",
" ) -> Dict[str, SoquetT]:\n",
" ) -> dict[str, SoquetT]:\n",
" for i in range(self.n):\n",
" x[i], y[i] = bb.add(SwapTwoBits(), x=x[i], y=y[i])\n",
" return {'x': x, 'y': y}"
Expand Down Expand Up @@ -758,7 +758,7 @@
"\n",
" def build_composite_bloq(\n",
" self, bb: BloqBuilder, *, x: SoquetT, y: SoquetT\n",
" ) -> Dict[str, SoquetT]:\n",
" ) -> dict[str, SoquetT]:\n",
" xs = bb.split(x)\n",
" ys = bb.split(y)\n",
"\n",
Expand Down Expand Up @@ -935,7 +935,7 @@
"metadata": {},
"outputs": [],
"source": [
"def build_composite_bloq(self, bb: 'BloqBuilder', exponent: 'SoquetT') -> Dict[str, 'SoquetT']:\n",
"def build_composite_bloq(self, bb: 'BloqBuilder', exponent: 'SoquetT') -> dict[str, 'SoquetT']:\n",
" x = bb.add(IntState(val=1, bitsize=self.x_bitsize))\n",
" exponent = bb.split(exponent)\n",
"\n",
Expand Down
2 changes: 1 addition & 1 deletion qualtran/_infra/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ once) and are instantiations of registers. There is generally a 1-to-many relati

`SoquetT` is a union type between `Soquet` and ndarray of `Soquet`. It means all the soquets
for a given register. When you see variable names like `soqdict`, `in_soqs`, or `final_soqs`
with type `Dict[str, SoquetT]`: this is a 1-to-1 mapping between register (by name) and
with type `dict[str, SoquetT]`: this is a 1-to-1 mapping between register (by name) and
all the `Soquet`s for that potentially-multidimensional register. When you see names
like `idxed_soq`, it means an individual `Soquet` has been plucked
from a potentially-multidimensional array of them.
Expand Down
8 changes: 4 additions & 4 deletions qualtran/_infra/adjoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from collections import Counter
from functools import cached_property
from typing import Dict, List, Optional, Tuple, TYPE_CHECKING
from typing import Optional, TYPE_CHECKING

from attrs import frozen

Expand All @@ -39,7 +39,7 @@
from qualtran.resource_counting import BloqCountDictT, SympySymbolAllocator


def _adjoint_final_soqs(cbloq: 'CompositeBloq', new_signature: Signature) -> Dict[str, '_SoquetT']:
def _adjoint_final_soqs(cbloq: 'CompositeBloq', new_signature: Signature) -> dict[str, '_SoquetT']:
"""`CompositeBloq.final_soqs()` but backwards."""
if LeftDangle not in cbloq._binst_graph:
return {}
Expand Down Expand Up @@ -73,7 +73,7 @@ def _adjoint_cbloq(cbloq: 'CompositeBloq') -> 'CompositeBloq':
bb, _ = BloqBuilder.from_signature(new_signature)
old_i_soqs = [_reg_to_soq(RightDangle, reg) for reg in old_signature.rights()]
new_i_soqs = [bb._reg_to_qvar(LeftDangle, reg) for reg in new_signature.lefts()]
soq_map: List[Tuple[_SoquetT, QVarT]] = list(zip(old_i_soqs, new_i_soqs))
soq_map: list[tuple[_SoquetT, QVarT]] = list(zip(old_i_soqs, new_i_soqs))
for binst, preds, succs in bloqnections:
# Instead of get_me returning the right element of a predecessor connection,
# it's the left element of a successor connection.
Expand Down Expand Up @@ -187,7 +187,7 @@ def _pkg_(cls) -> str:
return 'qualtran'

def wire_symbol(
self, reg: Optional['Register'], idx: Tuple[int, ...] = tuple()
self, reg: Optional['Register'], idx: tuple[int, ...] = tuple()
) -> 'WireSymbol':
# Note: since we pass are passed a soquet which has the 'new' side, we flip it before
# delegating and then flip back. Subbloqs only have to answer this protocol
Expand Down
3 changes: 2 additions & 1 deletion qualtran/_infra/binst_graph_iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Iterator, TYPE_CHECKING
from collections.abc import Iterator
from typing import TYPE_CHECKING

import networkx as nx

Expand Down
40 changes: 15 additions & 25 deletions qualtran/_infra/bloq.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,8 @@
"""Contains the main interface for defining `Bloq`s."""

import abc
from typing import (
Callable,
Dict,
List,
Mapping,
Optional,
Sequence,
Set,
Tuple,
TYPE_CHECKING,
Union,
)
from collections.abc import Callable, Mapping, Sequence
from typing import Optional, TYPE_CHECKING, Union

if TYPE_CHECKING:
import cirq
Expand Down Expand Up @@ -164,7 +154,7 @@ def signature(self) -> 'Signature':
`qualtran.Signature` for details on how to construct a signature.
"""

def build_composite_bloq(self, bb: 'BloqBuilder', **soqs: 'SoquetT') -> Dict[str, 'SoquetT']:
def build_composite_bloq(self, bb: 'BloqBuilder', **soqs: 'SoquetT') -> dict[str, 'SoquetT']:
"""Override this method to define a bloq as a composition of sub-bloqs.

Bloq authors should override this method. If you already have an instantiated bloq object,
Expand Down Expand Up @@ -279,7 +269,7 @@ def on_classical_vals(

def basis_state_phase(
self, **vals: 'ClassicalValT'
) -> Union[complex, 'MeasurementPhase', None]:
) -> Optional[Union[complex, 'MeasurementPhase']]:
"""How this bloq phases classical basis states.

Override this method if your bloq represents classical logic with basis-state
Expand All @@ -301,7 +291,7 @@ def basis_state_phase(

def call_classically(
self, **vals: Union['sympy.Symbol', 'ClassicalValT']
) -> Tuple['ClassicalValT', ...]:
) -> tuple['ClassicalValT', ...]:
"""Call this bloq on classical data.

Bloq users can call this function to apply bloqs to classical data. If you're
Expand Down Expand Up @@ -360,8 +350,8 @@ def tensor_contract(self, superoperator: bool = False) -> 'NDArray':
return bloq_to_dense(self, superoperator=superoperator)

def my_tensors(
self, incoming: Dict[str, 'ConnectionT'], outgoing: Dict[str, 'ConnectionT']
) -> List[Union['qtn.Tensor', 'DiscardInd']]:
self, incoming: dict[str, 'ConnectionT'], outgoing: dict[str, 'ConnectionT']
) -> list[Union['qtn.Tensor', 'DiscardInd']]:
"""Override this method to support native quimb simulation of this Bloq.

This method is responsible for returning tensors corresponding to the unitary, state, or
Expand Down Expand Up @@ -395,7 +385,7 @@ def my_tensors(

def build_call_graph(
self, ssa: 'SympySymbolAllocator'
) -> Union['BloqCountDictT', Set['BloqCountT']]:
) -> Union['BloqCountDictT', set['BloqCountT']]:
"""Override this method to build the bloq call graph.

This method must return a set of `(bloq, n)` tuples where `bloq` is called `n` times in
Expand Down Expand Up @@ -431,7 +421,7 @@ def call_graph(
generalizer: Optional[Union['GeneralizerT', Sequence['GeneralizerT']]] = None,
keep: Optional[Callable[['Bloq'], bool]] = None,
max_depth: Optional[int] = None,
) -> Tuple['nx.DiGraph', Dict['Bloq', Union[int, 'sympy.Expr']]]:
) -> tuple['nx.DiGraph', dict['Bloq', Union[int, 'sympy.Expr']]]:
"""Get the bloq call graph and call totals.

The call graph has edges from a parent bloq to each of the bloqs that it calls in
Expand Down Expand Up @@ -460,7 +450,7 @@ def call_graph(

def bloq_counts(
self, generalizer: Optional[Union['GeneralizerT', Sequence['GeneralizerT']]] = None
) -> Dict['Bloq', Union[int, 'sympy.Expr']]:
) -> dict['Bloq', Union[int, 'sympy.Expr']]:
"""The number of subbloqs directly called by this bloq.

This corresponds to one level of the call graph, see `Bloq.call_graph()`.
Expand All @@ -480,7 +470,7 @@ def bloq_counts(

return dict(get_bloq_callee_counts(self, generalizer=generalizer))

def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> tuple['Bloq', 'AddControlledT']:
"""Get a controlled version of this bloq and a function to wire it up correctly.

Users should likely call `Bloq.controlled(...)` which uses this method behind-the-scenes.
Expand All @@ -501,8 +491,8 @@ def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlled
It must have the following signature:

def _my_add_controlled(
bb: 'BloqBuilder', ctrl_soqs: Sequence['SoquetT'], in_soqs: Dict[str, 'SoquetT']
) -> Tuple[Iterable['SoquetT'], Iterable['SoquetT']]:
bb: 'BloqBuilder', ctrl_soqs: Sequence['SoquetT'], in_soqs: dict[str, 'SoquetT']
) -> tuple[Iterable['SoquetT'], Iterable['SoquetT']]:

Which takes a bloq builder (for adding the controlled bloq), the new control soquets,
input soquets for the existing registers; and returns a sequence of the output control
Expand Down Expand Up @@ -554,7 +544,7 @@ def t_complexity(self) -> 'TComplexity':

def as_cirq_op(
self, qubit_manager: 'cirq.QubitManager', **cirq_quregs: 'CirqQuregT'
) -> Tuple[Union['cirq.Operation', None], Dict[str, 'CirqQuregT']]:
) -> tuple[Optional['cirq.Operation'], dict[str, 'CirqQuregT']]:
"""Override this method to support conversion to a Cirq operation.

If this method is not overriden, the default implementation will wrap this bloq
Expand Down Expand Up @@ -658,7 +648,7 @@ def on_registers(
return self.on(*merge_qubits(self.signature, **qubit_regs))

def wire_symbol(
self, reg: Optional['Register'], idx: Tuple[int, ...] = tuple()
self, reg: Optional['Register'], idx: tuple[int, ...] = tuple()
) -> 'WireSymbol':
"""On a musical score visualization, use this `WireSymbol` to represent the register.

Expand Down
19 changes: 10 additions & 9 deletions qualtran/_infra/bloq_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import inspect
import typing
from typing import Any, Callable, Generic, Iterable, Optional, Sequence, Type, TypeVar, Union
from collections.abc import Callable, Iterable, Sequence
from typing import Any, Generic, Optional, overload, TypeVar, Union

from attrs import field, frozen

Expand Down Expand Up @@ -45,7 +46,7 @@

_func: Callable[[], _BloqType] = field(repr=False, hash=False)
name: str
bloq_cls: Type[Bloq]
bloq_cls: type[Bloq]
generalizer: _GeneralizerType = field(
converter=lambda x: tuple(x) if isinstance(x, Sequence) else x, default=lambda x: x
)
Expand All @@ -69,7 +70,7 @@
return func.__name__.lstrip('_')


def _bloq_cls_from_func_annotation(func: Callable[[], _BloqType]) -> Type[_BloqType]:
def _bloq_cls_from_func_annotation(func: Callable[[], _BloqType]) -> type[_BloqType]:
"""Use the function return type annotation as the `BloqExample.bloq_cls` with the decorator."""
anno = inspect.get_annotations(func, eval_str=True)
if 'return' not in anno:
Expand All @@ -80,11 +81,11 @@
return cls


@typing.overload
@overload
def bloq_example(_func: Callable[[], _BloqType], **kwargs: Any) -> BloqExample[_BloqType]: ...


@typing.overload
@overload
def bloq_example(
_func: None = None, *, generalizer: _GeneralizerType = lambda x: x
) -> Callable[[Callable[[], _BloqType]], BloqExample[_BloqType]]: ...
Expand Down Expand Up @@ -148,10 +149,10 @@
graph. Note that this example must be included in `examples`.
"""

bloq_cls: Type
bloq_cls: type
examples: Sequence[BloqExample] = field(converter=_to_tuple, factory=tuple)
import_line: str = field()
call_graph_example: Union[BloqExample, None] = field()
call_graph_example: BloqExample | None = field()

Check failure on line 155 in qualtran/_infra/bloq_example.py

View workflow job for this annotation

GitHub Actions / pylint

E1131: unsupported operand type(s) for | (unsupported-binary-operation)

@import_line.default
def _import_line_default(self) -> str:
Expand All @@ -160,7 +161,7 @@
return line

@call_graph_example.default
def _call_graph_example_default(self) -> Union[BloqExample, None]:
def _call_graph_example_default(self) -> BloqExample | None:

Check failure on line 164 in qualtran/_infra/bloq_example.py

View workflow job for this annotation

GitHub Actions / pylint

E1131: unsupported operand type(s) for | (unsupported-binary-operation)
if not self.examples:
return None
return self.examples[0]
6 changes: 3 additions & 3 deletions qualtran/_infra/composite_bloq.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"\n",
" def build_composite_bloq(\n",
" self, bb: 'BloqBuilder', q1: 'Soquet', q2: 'Soquet'\n",
" ) -> Dict[str, 'Soquet']:\n",
" ) -> dict[str, 'Soquet']:\n",
" q1, q2 = bb.add(CNOT(), ctrl=q1, target=q2)\n",
" q1, q2 = bb.add(CNOT(), ctrl=q2, target=q1)\n",
" return {'q1': q1, 'q2': q2}"
Expand Down Expand Up @@ -469,7 +469,7 @@
"\n",
" def build_composite_bloq(\n",
" self, bb: 'BloqBuilder', stuff: 'SoquetT'\n",
" ) -> Dict[str, 'SoquetT']:\n",
" ) -> dict[str, 'SoquetT']:\n",
" stuff = bb.add(TestParallelCombo(), reg=stuff)\n",
" stuff = bb.add(TestParallelCombo(), reg=stuff)\n",
" stuff = bb.add(TestParallelCombo(), reg=stuff)\n",
Expand Down Expand Up @@ -512,7 +512,7 @@
"# Go through and decompose each subbloq\n",
"# We'll manually code this up in this notebook since this isn't a useful operation.\n",
"bb, _ = BloqBuilder.from_signature(flat_three_p.signature)\n",
"soq_map: List[Tuple[SoquetT, SoquetT]] = bb.initial_soq_map(flat_three_p.signature.lefts())\n",
"soq_map: list[tuple[SoquetT, SoquetT]] = bb.initial_soq_map(flat_three_p.signature.lefts())\n",
"\n",
"for binst, in_soqs, old_out_soqs in flat_three_p.iter_bloqsoqs():\n",
" in_soqs = bb.map_soqs(in_soqs, soq_map)\n",
Expand Down
Loading
Loading