Skip to content

Commit c2e90b3

Browse files
authored
Fixes bug where simulator would barf on empty circuit.
1 parent a6aea62 commit c2e90b3

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

cirq/google/sim/xmon_simulator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,12 @@ def run_sweep(
200200
qubits,
201201
initial_state,
202202
param_resolver)
203+
step_result = None
203204
for step_result in all_step_results:
204205
for k, v in step_result.measurements.items():
205206
measurements[k].append(np.array(v, dtype=bool))
206-
final_states.append(step_result.state())
207+
if step_result:
208+
final_states.append(step_result.state())
207209
trial_results.append(SimulatorTrialResult(
208210
param_resolver,
209211
repetitions,

cirq/google/sim/xmon_simulator_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ def test_run(scheduler):
118118
assert result.measurements == {'a': [False], 'b': [False]}
119119

120120

121+
@pytest.mark.parametrize('scheduler', SCHEDULERS)
122+
def test_run_empty_circuit(scheduler):
123+
simulator = xmon_simulator.Simulator()
124+
result = run(simulator, Circuit(), scheduler)
125+
assert len(result.measurements) == 0
126+
127+
121128
@pytest.mark.parametrize('scheduler', SCHEDULERS)
122129
def test_run_state(scheduler):
123130
simulator = xmon_simulator.Simulator()

0 commit comments

Comments
 (0)