Skip to content
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

Pad inhomogenous result arrays #5319

Merged
merged 3 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Pad inhomogenous result arrays
  • Loading branch information
daxfohl committed May 2, 2022
commit e8ab78590cf026d92e7faae6876f635237a6e8dc
13 changes: 10 additions & 3 deletions cirq-core/cirq/sim/simulator_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,19 @@ def _run(
for k, r in step_result._classical_data.records.items():
if k not in records:
records[k] = []
records[k].append(r)
records[k].append(np.array(r, dtype=np.uint8))
for k, cr in step_result._classical_data.channel_records.items():
if k not in records:
records[k] = []
records[k].append([cr])
return {str(k): np.array(v, dtype=np.uint8) for k, v in records.items()}
records[k].append(np.array([cr], dtype=np.uint8))
daxfohl marked this conversation as resolved.
Show resolved Hide resolved

def pad_evenly(results: Sequence[np.ndarray]):
largest = max([result.shape[0] for result in results])
return np.array(
[np.pad(result, ((0, largest - result.shape[0]), (0, 0))) for result in results]
)

return {str(k): pad_evenly(v) for k, v in records.items()}
daxfohl marked this conversation as resolved.
Show resolved Hide resolved

def simulate_sweep_iter(
self,
Expand Down
16 changes: 16 additions & 0 deletions cirq-core/cirq/sim/simulator_base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,22 @@ def _has_unitary_(self):
assert op2.count == 2


def test_inhomogeneous_measurement_count_padding():
q = cirq.LineQubit(0)
key = cirq.MeasurementKey('m')
sim = cirq.Simulator()
c = cirq.Circuit(
cirq.CircuitOperation(
cirq.FrozenCircuit(cirq.X(q) ** 0.2, cirq.measure(q, key=key)),
use_repetition_ids=False,
repeat_until=cirq.KeyCondition(key),
)
)
results = sim.run(c, repetitions=10)
for i in range(10):
assert np.sum(results.records['m'][i, :, :]) == 1


def test_deprecated_final_step_result():
class OldCountingSimulator(CountingSimulator):
def _create_simulator_trial_result( # type: ignore
Expand Down