Skip to content

Commit

Permalink
Refactor to_resolvers to use to_sweeps (#3343)
Browse files Browse the repository at this point in the history
This seems logical. Note, however, that since `to_sweeps` currently contains deprecated code, this introduces deprecated behavior into `to_resolvers`. To avoid that we could wait for the next release before merging this.
  • Loading branch information
kevinsung committed Sep 21, 2020
1 parent 77eed43 commit 4298a97
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
6 changes: 6 additions & 0 deletions cirq/sim/simulator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def test_run_simulator_run():
circuit = mock.Mock(cirq.Circuit)
circuit.__iter__ = mock.Mock(return_value=iter([]))
param_resolver = mock.Mock(cirq.ParamResolver)
param_resolver.param_dict = {}
expected_result = cirq.TrialResult.from_single_parameter_set(
measurements=expected_measurements, params=param_resolver)
assert expected_result == simulator.run(program=circuit,
Expand All @@ -52,6 +53,8 @@ def test_run_simulator_sweeps():
circuit.__iter__ = mock.Mock(return_value=iter([]))
param_resolvers = [mock.Mock(cirq.ParamResolver),
mock.Mock(cirq.ParamResolver)]
for resolver in param_resolvers:
resolver.param_dict = {}
expected_results = [
cirq.TrialResult.from_single_parameter_set(
measurements=expected_measurements, params=param_resolvers[0]),
Expand Down Expand Up @@ -86,6 +89,7 @@ def steps(*args, **kwargs):
simulator._simulator_iterator.side_effect = steps
circuit = mock.Mock(cirq.Circuit)
param_resolver = mock.Mock(cirq.ParamResolver)
param_resolver.param_dict = {}
qubit_order = mock.Mock(cirq.QubitOrder)
result = simulator.simulate(program=circuit,
param_resolver=param_resolver,
Expand Down Expand Up @@ -116,6 +120,8 @@ def steps(*args, **kwargs):
circuit = mock.Mock(cirq.Circuit)
param_resolvers = [mock.Mock(cirq.ParamResolver),
mock.Mock(cirq.ParamResolver)]
for resolver in param_resolvers:
resolver.param_dict = {}
qubit_order = mock.Mock(cirq.QubitOrder)
results = simulator.simulate_sweep(program=circuit,
params=param_resolvers,
Expand Down
16 changes: 2 additions & 14 deletions cirq/study/sweepable.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,8 @@

def to_resolvers(sweepable: Sweepable) -> Iterator[ParamResolver]:
"""Convert a Sweepable to a list of ParamResolvers."""
if sweepable is None:
yield ParamResolver({})
elif isinstance(sweepable, ParamResolver):
yield sweepable
elif isinstance(sweepable, Sweep):
yield from sweepable
elif isinstance(sweepable, dict):
yield ParamResolver(cast(Dict, sweepable))
elif isinstance(sweepable, Iterable) and not isinstance(sweepable, str):
for item in cast(Iterable, sweepable):
yield from to_resolvers(item)
else:
raise TypeError(f'Unrecognized sweepable type: {type(sweepable)}.\n'
f'sweepable: {sweepable}')
for sweep in to_sweeps(sweepable):
yield from sweep


def to_sweeps(sweepable: Sweepable) -> List[Sweep]:
Expand Down

0 comments on commit 4298a97

Please sign in to comment.