Expand product sweep parameters iteratively rather than recursively #7523
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.
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 anIterator
as its output, meaning the output object can only be iterated through once before it is exhausted. However,Params
is a type alias forIterable[tuple['cirq.TParamKey', 'cirq.TParamVal']]
, meaning it is expected for a givenParams
to be iterated through repeatedly. This is also necessary in order for theParams
produced by aProduct
sweep to function correctly, as theParams
yielded by the individual factors'param_tuples()
can appear multiple times in the compoundParams
of the product'sparam_tuples()
. To properly allow the yieldedParams
to be iterated through repeatedly, we now uselambda values: tuple(itertools.chain.from_iterable(values)
on line 234 of sweeps.py whereas the previous implementation effectively hadlambda values: itertools.chain.from_iterables(values)
. Collecting into atuple
guarantees that we yield a repeatedly-iterableParams
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.