-
Notifications
You must be signed in to change notification settings - Fork 23
Changes to the iterator interface #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mtfishman
merged 56 commits into
ITensor:network_solvers
from
jack-dunham:network_solvers
Oct 20, 2025
Merged
Changes from 25 commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
7a51966
Add `Problem` as type parameter to `SweepIterator`
245182b
Format test files and improve comparisons for readabilty on failure
7af4b25
Redesign iterator interface by introducing AbstractNetworkIterator ab…
c0ae5d0
Add `EachRegion` adapter that wraps `RegionIterator`, behaving the sa…
3b9d0af
Add unit tests for the `AbstractNetworkIterator` interface
4ef4e75
Rename `done` to `laststep` to better reflect the when it evalutes to…
e112eb4
Rename `previous_region` to `prev_region` to better align with julia …
da360e0
Rename `PauseAfterIncrement` -> `NoComputeStep` and improve some vari…
8bfc483
Make `extract` and `subspace_expand` mutating
1ef8498
Make `update` mutable
0a6e891
Make `insert` mutable
0653c47
First implementation of an `options` system.
d77321e
Simplify options interface to a single function `default_kwargs`.
aff14c7
Put calls to `extract!` etc in `compute!` function directly
4b21cc9
Refactor the region plan generating code.
e71512f
Have `dmrg` take a strict number of arguments
a4ce308
Purge non-mutating field setter functions.
a8b2c51
Use `current_kwargs` for getting kwargs from `RegionIterator`
18a8503
Introduce defaults using `default_kwargs` and be stricter about which…
0c9022c
Swap order of local_state and region_iter args
a9be11e
Add some unit tests for the defaults
4d52088
Rename file options.jl -> test_default_kwargs.jl
613d533
Fix `euler_sweep` returning kwargs not as `NamedTuple`
20bf783
The `sweep_solve` callbacks now get called without any keyword argume…
568c631
Some minor refactoring of the iterators.
fed9137
The `EachRegion` adapter now flattens the nested Sweep/Region iterato…
4ce453e
Add tests for `EachRegion` and `eachregion` wrapper functions
c59a9c5
Rename `laststep` -> `islaststep` in fitting with Julia conventions.
62195b6
Overhaul `default_kwargs` such that it mirrors the function signature…
917f2f1
Rename `NoComputeStep` to `IncrementOnly`
112d55e
Remove @info statement and fix bug with `astypes` not promoting corre…
0a9f127
Update `default_kwargs` tests.
e35f325
Remove stray `end` from `adapters.jl`.
6a8cdb1
Fix typo in docstring of `EachRegion` adapter.
jack-dunham 9760de1
Function `reverse_regions` is now more concise.
26ece7b
Use explicit imports in `default_kwargs.jl`
340d805
Fix test imports and broken tests in `test_iterators.jl`.
f89c379
Merge branch 'network_solvers' of https://github.com/jack-dunham/ITen…
6a33f29
Rename @default_kwargs -> @define_default_kwargs
b4bcb93
Remove `astypes` option from `@define_default_kwargs`.
624f964
Update `default_kwargs` tests.
bd35f09
Add `sweep_solve` method for `EachRegion` adapter.
0b5314d
Add `@with_kwargs` macro which automatically splats `default_kwargs` …
a58ec92
Make use of `@with_kwargs` macro make code more concise.
b72a08f
The fallback default callback functions now no longer accept `kwargs.…
c5de5c4
Test fix: tests founds in sub-directories are now actually ran when i…
2788057
Skip broken tests for now
33b9e28
Rename `sweep_solve` -> `sweep_solve!` to obey convention
dedd82e
The `EachRegion` adapter now returns itself from `iterate` instead of…
d39f09e
The `sweep_solve!` function now always returns the type of the input …
3f5c97c
Mutating functions now return the first argument before any additiona…
7ad3138
Remove depreciated `solvers` code and tests from old interface
60235bc
Method `subspace_expand!(::Backend"densitymatrix")` now defines kwarg…
da3ad27
Solvers code now no longer relies on `default_kwargs` system
8725370
Remove `default_kwargs` related to source files
8afce8a
Delete stale include
mtfishman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
|
|
||
| abstract type AbstractProblem end | ||
|
|
||
| set_truncation_info(P::AbstractProblem, args...; kws...) = P | ||
| set_truncation_info!(P::AbstractProblem, args...; kws...) = P |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,39 @@ | ||
| """ | ||
| struct PauseAfterIncrement{S<:AbstractNetworkIterator} | ||
|
|
||
| # | ||
| # TupleRegionIterator | ||
| # | ||
| # Adapts outputs to be (region, region_kwargs) tuples | ||
| # | ||
| # More generic design? maybe just assuming RegionIterator | ||
| # or its outputs implement some interface function that | ||
| # generates each tuple? | ||
| # | ||
|
|
||
| mutable struct TupleRegionIterator{RegionIter} | ||
| region_iterator::RegionIter | ||
| Iterator wrapper whos `compute!` function simply returns itself, doing nothing in the | ||
| process. This allows one to manually call a custom `compute!` or insert their own code it in | ||
| the loop body in place of `compute!`. | ||
| """ | ||
| struct NoComputeStep{S<:AbstractNetworkIterator} <: AbstractNetworkIterator | ||
| parent::S | ||
| end | ||
|
|
||
| region_iterator(T::TupleRegionIterator) = T.region_iterator | ||
| laststep(adapter::NoComputeStep) = laststep(adapter.parent) | ||
| state(adapter::NoComputeStep) = state(adapter.parent) | ||
| increment!(adapter::NoComputeStep) = increment!(adapter.parent) | ||
| compute!(adapter::NoComputeStep) = adapter | ||
|
|
||
| function Base.iterate(T::TupleRegionIterator, which=1) | ||
| state = iterate(region_iterator(T), which) | ||
| isnothing(state) && return nothing | ||
| (current_region, region_kwargs) = current_region_plan(region_iterator(T)) | ||
| return (current_region, region_kwargs), last(state) | ||
| end | ||
| NoComputeStep(adapter::NoComputeStep) = adapter | ||
|
|
||
| """ | ||
| region_tuples(R::RegionIterator) | ||
| struct EachRegion{RegionIterator} <: AbstractNetworkIterator | ||
|
|
||
| The `region_tuples` adapter converts a RegionIterator into an | ||
| iterator which outputs a tuple of the form (current_region, current_region_kwargs) | ||
| at each step. | ||
| Wapper adapter that returns a tuple (region, kwargs) at each step rather than the iterator | ||
| itself. | ||
| """ | ||
| region_tuples(R::RegionIterator) = TupleRegionIterator(R) | ||
| struct EachRegion{R<:RegionIterator} <: AbstractNetworkIterator | ||
| parent::R | ||
| end | ||
|
|
||
| # Essential definitions | ||
| Base.length(adapter::EachRegion) = length(adapter.parent) | ||
| state(adapter::EachRegion) = state(adapter.parent) | ||
| increment!(adapter::EachRegion) = state(adapter.parent) | ||
jack-dunham marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| function compute!(adapter::EachRegion) | ||
| # Do the usual compute! for RegionIterator | ||
| compute!(adapter.parent) | ||
| # But now lets return something useful | ||
| return current_region_plan(adapter) | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| """ | ||
| default_kwargs(f, [obj = Any]) | ||
| Return the default keyword arguments for the function `f`. These defaults may be | ||
| derived from the contents or type of the second arugment `obj`. | ||
| ## Interface | ||
| Given a function `f`, one can optionally set the default keyword arguments for this | ||
| function by specializing either of the following two-argument methods: | ||
| ``` | ||
| ITensorNetworks.default_kwargs(::typeof(f), prob::AbstractProblem) | ||
| ITensorNetworks.default_kwargs(::typeof(f), ::Type{<:AbstractProblem}) | ||
| ``` | ||
| If one does not require the contents of `prob::Prob` to generate the defaults then it is | ||
| recommended to dispatch on `Type{<:Prob}` directly (second method) so the defaults | ||
| can be accessed without constructing an instance of a `Prob`. | ||
| The return value of `default_kwargs` should be a `NamedTuple`, and will overwrite any | ||
| default values set in the function signature. | ||
| """ | ||
| default_kwargs(f) = default_kwargs(f, Any) | ||
| default_kwargs(f, obj) = _default_kwargs_fallback(f, obj) | ||
jack-dunham marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # To avoid annoying potential method ambiguities. | ||
| function _default_kwargs_fallback(f, iter::RegionIterator) | ||
| return default_kwargs(f, problem(iter)) | ||
| end | ||
| function _default_kwargs_fallback(f, problem::AbstractProblem) | ||
| return default_kwargs(f, typeof(problem)) | ||
| end | ||
|
|
||
| # Eventually we reach this if nothing is specialized. | ||
| _default_kwargs_fallback(::Any, ::DataType) = (;) | ||
|
|
||
| """ | ||
| current_kwargs(f, iter::RegionIterator) | ||
| Return the keyword arguments to be passed to the function `f` for the current region | ||
| defined by the stateful iterator `iter`. | ||
| """ | ||
| function current_kwargs(f::Function, iter::RegionIterator) | ||
jack-dunham marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| region_kwargs = get(current_region_kwargs(iter), Symbol(f, :_kwargs), (;)) | ||
| rv = merge(default_kwargs(f, iter), region_kwargs) | ||
| return rv | ||
| end | ||
|
|
||
| # Generic | ||
|
|
||
| # I think these should be set independent of a function, but for now: | ||
| function default_kwargs(::typeof(factorize), ::Any) | ||
| return (; maxdim=typemax(Int), cutoff=0.0, mindim=1) | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.