You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Expand product sweep parameters iteratively rather than recursively (#7523)
The new unit test assertion involving a product of 1025 sweeps crashes
the current implementation with a "max recursion depth exceeded" error,
but passes with the new implementation. This is particularly relevant
when dict_to_product_sweep is called with a large input dictionary.
This re-introduces the change reverted in
#7522 and addresses the issue
which caused the failure:
`itertools.chain.from_iterable` produces an `Iterator` as its output,
meaning the output object can only be iterated through once before it is
exhausted. However, `Params` is a type alias for
`Iterable[tuple['cirq.TParamKey', 'cirq.TParamVal']]`, meaning it is
expected for a given `Params` to be iterated through repeatedly. This is
also necessary in order for the `Params` produced by a `Product` sweep
to function correctly, as the `Params` yielded by the individual
factors' `param_tuples()` can appear multiple times in the compound
`Params` of the product's `param_tuples()`. To properly allow the
yielded `Params` to be iterated through repeatedly, we now use `lambda
values: tuple(itertools.chain.from_iterable(values)` on line 234 of
sweeps.py whereas the previous implementation effectively had `lambda
values: itertools.chain.from_iterables(values)`. Collecting into a
`tuple` guarantees that we yield a repeatedly-iterable `Params` object.
In addition to the new test `test_nested_product_zip()` which reproduced
the error in the previous implementation, this change has also been
tested against the internal library tests which were broken by the
previous implementation.
0 commit comments