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

Sample independent qubit sets without merging state space #4110

Merged
merged 161 commits into from Jul 12, 2021

Conversation

daxfohl
Copy link
Contributor

@daxfohl daxfohl commented May 15, 2021

Next step of #3409 (first step was #4100)

#4100 was a big change in that it allows independent state spaces to evolve independently, exponentially reducing the runtime of simulation when states are not entangled. However we still generate the fully-merged state space each time we create a StepResult, so the effect of those improvements is limited. This PR fixes that.

This PR avoids merging the whole state space after each moment to create the StepResult. Instead, the simulators pass the raw OperationTarget (the lookup of unentangled states) into the StepResult (StepResultBase created to handle common functionality). That only merges the full state when the _simulator_state() function is explicitly called. In particular, sampling from the StepResults is done by independently sampling the substates and zipping the results together, avoiding creating the fully merged state tensor.

The final PR will be #4198, which will also handle this for TrialResult, and thus allow end to end simulation of very large unentangled circuits without running out of memory.

BREAKING CHANGES:

  • The behavior in test_state_vector_copy and test_density_matrix_copy, where if you do a step_result.get_state(copy=False) and manually fiddle with that vector, and the changes to that vector are preserved, no longer works by default. However this behavior is not documented, and seems unusual to expect. If someone requires the behavior, it will continue to work if split_untangled_qubits=False, but this PR sets split_untangled_qubits=True by default.
  • Sampling is not deterministic even with same seed. ActOnArgsContainer.sample iterates a set, which appears to be non-deterministic ordering in Python. I could probably figure out a way around this.
  • Rounding errors may be affected. From observation, setting split_untangled_qubits=True seems to have a slightly worse effect on rounding errors and ATOLs may be affected. (Or actually it's likely the rounding error is equal magnitude but just different--I only notice the tests where tolerances need adjusted downward, not vice-versa).

@95-martin-orion 95-martin-orion self-requested a review July 8, 2021 13:14
@95-martin-orion 95-martin-orion self-assigned this Jul 8, 2021
@95-martin-orion
Copy link
Collaborator

@daxfohl, is this PR now up-to-date with master such that changes from #4100 do not also appear in the "changed files" here?

@daxfohl
Copy link
Contributor Author

daxfohl commented Jul 8, 2021

is this PR now up-to-date with master such that changes from #4100 do not also appear in the "changed files" here?

Yes.

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.

Full review pass. The issue I'm most concerned about here (and that is most likely to override other issues listed below) is the step_result._sim_state mutation concern.

cirq-core/cirq/sim/simulator_base.py Show resolved Hide resolved
cirq-core/cirq/sim/simulator_base_test.py Show resolved Hide resolved
examples/bb84.py Outdated Show resolved Hide resolved
cirq-core/cirq/sim/act_on_args_container.py Outdated Show resolved Hide resolved
Copy link
Contributor Author

@daxfohl daxfohl left a comment

Choose a reason for hiding this comment

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

I can't seem to fully cancel this review.

@95-martin-orion 95-martin-orion added the BREAKING CHANGE For pull requests that are important to mention in release notes. label Jul 12, 2021
@95-martin-orion 95-martin-orion added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Jul 12, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Jul 12, 2021
@CirqBot
Copy link
Collaborator

CirqBot commented Jul 12, 2021

Automerge cancelled: A required status check is not present.

Missing statuses: ['cla/google']

@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Jul 12, 2021
@95-martin-orion 95-martin-orion merged commit c28646f into quantumlib:master Jul 12, 2021
@daxfohl daxfohl deleted the sample branch July 13, 2021 13:34
@daxfohl daxfohl restored the sample branch July 13, 2021 13:37
@daxfohl daxfohl deleted the sample branch March 30, 2022 14:20
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
…#4110)

* split

* Allow config param split_entangled_states

* default split to off

* ensure consistent_act_on circuits have a qubit.

* lint

* lint

* mps

* lint

* lint

* run sparse by default

* fix tests

* fix tests

* fix tests

* most of sparse and dm

* clifford

* sim_base

* sim_base

* mps

* turn off on experiments with rounding error

* fix tests

* fix tests

* fix testsCreate base step result

* clifford

* mps

* mps

* mps

* tableau

* test simulator

* test simulator

* Update simulator_base.py

* Drop mps/join

* Fix clifford extract

* lint

* simplify index

* Add qubits to base class

* Fix clifford sampling

* Fix _sim_state_values

* fix tostring tests, format

* remove split/join from ch-form

* remove split/join from ch-form

* push merged state to base layer

* lint

* mypy

* mypy

* mypy

* Add default arg for zero qubit circuits

* Have last repetition reuse original state repr

* Remove cast

* Split all pure initial states by default

* Detangle on reset channels

* docstrings

* docstrings

* docstrings

* docstrings

* fix merge

* lint

* Add unit test for integer states

* format

* Add tests for splitting and joining

* remove unnecessary qubits param

* Clean up default args

* Fix failing test

* Add ActOnArgsContainer

* Add ActOnArgsContainer

* Clean up tests

* Clean up tests

* Clean up tests

* format

* Fix tests and coverage

* Add OperationTarget interface

* Fix unit tests

* mypy, lint, mocks, coverage

* coverage

* lint, tests

* lint, tests

* mypy

* mypy, tests

* remove test code

* test

* dead code

* mocks

* add log to container

* fix logs

* dead code

* unit test

* unit test

* dead code

* operationtarget samples

* StepResultBase

* Mock, format

* EmptyActOnArgs

* EmptyActOnArgs

* simplify dummyargs

* lint

* Add [] to actonargs

* rename _create_act_on_arg

* coverage

* coverage

* Default sparse sim to split=false

* format

* Default sparse sim to split=false

* Default density matrix sim to split=false

* lint

* lint

* lint

* lint

* address review comments

* lint

* Defaults back to split=false

* add error if setting state when split is enabled

* Unit tests

* coverage

* coverage

* coverage

* docs

* conflicts

* conflicts

* cover

* Add qubits to bb84

* mergedsimstate private

* q_set

* default to split=True

* Allow set_state

* Allow set_state

* format

* fix merge

* fix merge

* maintain order in sampling for determinicity.

* Pydoc fixes

* revert bb48 num_qubits change

* fix docstrings for set_state error

* Remove duplicate sample declaration from ActOnArgs

* Remove unnecessary split_untangled_states=True

* Reduce atol of dm/sv test

* Add test for sim_state propagation from step_result

* Add test for sim_state propagation from step_result

Co-authored-by: Cirq Bot <craiggidney+github+cirqbot@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BREAKING CHANGE For pull requests that are important to mention in release notes. cla: yes Makes googlebot stop complaining.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants