Skip to content

Lazily create TrialResult.final_simulator_state #4317

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

Merged
merged 8 commits into from
Jul 30, 2021

Conversation

daxfohl
Copy link
Collaborator

@daxfohl daxfohl commented Jul 13, 2021

Final step of #4100, creates the TrialResult.final_simulator_state lazily so that it's possible to run an entire simulation on a sparsely entangled circuit and sample measurements without running out of memory. Unit test added with 59 qubits on density matrix simulator running 1000 repetitions (9 cirq.X's, 1 cirq.measure, on all qubits) in under 100 ms.

def test_large_untangled_okay():
    circuit = cirq.Circuit()
    for i in range(59):
        for _ in range(9):
            circuit.append(cirq.X(cirq.LineQubit(i)))
        circuit.append(cirq.measure(cirq.LineQubit(i)))
    with pytest.raises(MemoryError, match='Unable to allocate'):
        _ = cirq.DensityMatrixSimulator(split_untangled_states=False).simulate(circuit)
    result = cirq.DensityMatrixSimulator(split_untangled_states=True).simulate(circuit)
    assert set(result._step_result_or_state._qubits) == set(cirq.LineQubit.range(59))
    # _ = result.final_density_matrix hangs (as expected)
    result = cirq.DensityMatrixSimulator(split_untangled_states=True).run(circuit, repetitions=1000)
    assert len(result.measurements) == 59
    assert len(result.measurements['0']) == 1000
    assert (result.measurements['0'] == np.full(1000, 1)).all()

Supercedes #4198, which had diverged from master too much to merge.

@daxfohl daxfohl requested review from cduck, vtomole and a team as code owners July 13, 2021 14:20
@daxfohl daxfohl requested a review from viathor July 13, 2021 14:20
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Jul 13, 2021
@95-martin-orion 95-martin-orion self-requested a review July 13, 2021 14:32
@95-martin-orion 95-martin-orion self-assigned this Jul 13, 2021
@daxfohl
Copy link
Collaborator Author

daxfohl commented Jul 16, 2021

@95-martin-orion Start this review at simulator.py; otherwise it can be confusing. Basically all I did was take the field _final_simulator_state and change it to a property that is lazily calculated from the step result, and updated the code to pass in a that step result instead of the calculated simulator state. (It also still allows the calculated state to be passed in there for backwards compatibility). Then for all subclasses that had some field based on the final state, I changed those to properties that reference the _final_simulator_state property so that they are lazily calculated too.

Copy link
Collaborator

@95-martin-orion 95-martin-orion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall.

@daxfohl
Copy link
Collaborator Author

daxfohl commented Jul 21, 2021

@95-martin-orion The requested changes are complete.

@95-martin-orion 95-martin-orion merged commit 9c23053 into quantumlib:master Jul 30, 2021
@daxfohl daxfohl deleted the trial4 branch July 30, 2021 17:02
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
* Lazily create TrialResult.final_simulator_state

* Change TrialResult only to accept StepResult in constructor

* Backwards compatibility for TrialResult initializer

* lint

* Platform independent test

* Update density_matrix_simulator_test.py

Co-authored-by: Orion Martin <[email protected]>
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
* Lazily create TrialResult.final_simulator_state

* Change TrialResult only to accept StepResult in constructor

* Backwards compatibility for TrialResult initializer

* lint

* Platform independent test

* Update density_matrix_simulator_test.py

Co-authored-by: Orion Martin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Makes googlebot stop complaining.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants