Skip to content

[Draft] Lazily create TrialResult.final_simulator_state #4198

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

Closed
wants to merge 197 commits into from

Conversation

daxfohl
Copy link
Collaborator

@daxfohl daxfohl commented Jun 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()

Depends on #4110. Diff here: https://github.com/daxfohl/Cirq/compare/sample...daxfohl:trial?expand=1

balopat and others added 6 commits June 29, 2021 17:57
Extracts cirq-pasqal as a separate module.

Merge after quantumlib#4239, a fix for json testing and deprecation warning deduping and quantumlib#4250, a fix for flnyt / minimal pytest dependencies (it contains them currently)
…ion of keys" (quantumlib#4277)

* Revert "Support qubits in `MeasurementKey` and update JSON serialization of keys (quantumlib#4123)"

This reverts commit 81436fe.

* Safeguard against repr-inequality.

* assert_equivalent_repr
Problem: the PauliSum add function checks that the type of the other operand is one of numbers.Complex, PauliString, PauliSum and raises NotImplemented otherwise.
Fix: if the other operand is not one of the types try to cast it to PauliString and raise error only if that fails

Fixes quantumlib#4087.
Add optimization that ensures independent qubit sets are simulated independently. This is done by adding join, extract, and reorder methods to ActOnArgs, and updating SimulatorBase with the logic to merge qubit sets when necessary and split them when possible.

This optimization is enabled or disabled via a new parameter in the simulator constructors: `split_entangled_qubits`. Currently the PR has this set to True by default, though perhaps it should be disabled by default lest it breaks anything? The MPS simulator does not yet have `extract` defined and thus there's no option to enable this feature in MPS simulator's constructor yet, though nothing prevents this from being added later.

The perf boost of this implementation is limited because each StepResult still requires the full product state. It's still a speedup because full product state calculations will only have to occur once per moment rather than once per operation, but not as nice as avoiding full product state calculations entirely. *That* optimization will be available in a subsequent PR that never creates the full product state if possible: StepResults will join the product state only on demand, and sampling will sample each substate independently and zip up the results, avoiding the full state join: The WIP is here https://github.com/daxfohl/Cirq/compare/split...daxfohl:sample?expand=1.

I ramped up the number of qubits in the benchmarks to 25 for sparse and 12 for DM:

From master:
```
(cirq-py3) dax@DESKTOP-Q5MLJ3J:~/cirq$ time pytest dev_tools/profiling/benchmark_simulators_test.py
platform linux -- Python 3.8.5, pytest-5.4.3, py-1.10.0, pluggy-0.13.1
benchmark: 3.2.3 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: /home/dax/cirq
plugins: cov-2.5.1, asyncio-0.12.0, benchmark-3.2.3
collected 5 items

dev_tools/profiling/benchmark_simulators_test.py .....                                                                                                                                                    [100%]

real    0m16.973s
user    0m15.754s
sys     0m3.862s
```

From split branch (the current PR):
```
(cirq-py3) dax@DESKTOP-Q5MLJ3J:~/cirq$ time pytest dev_tools/profiling/benchmark_simulators_test.py
platform linux -- Python 3.8.5, pytest-5.4.3, py-1.10.0, pluggy-0.13.1
benchmark: 3.2.3 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: /home/dax/cirq
plugins: cov-2.5.1, asyncio-0.12.0, benchmark-3.2.3
collected 5 items

dev_tools/profiling/benchmark_simulators_test.py .....                                                                                                                                                    [100%]

real    0m10.073s
user    0m9.082s
sys     0m3.805s
```

From sample branch (future iteration mentioned above):
```
(cirq-py3) dax@DESKTOP-Q5MLJ3J:~/cirq$ time pytest dev_tools/profiling/benchmark_simulators_test.py
platform linux -- Python 3.8.5, pytest-5.4.3, py-1.10.0, pluggy-0.13.1
benchmark: 3.2.3 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: /home/dax/cirq
plugins: cov-2.5.1, asyncio-0.12.0, benchmark-3.2.3
collected 5 items

dev_tools/profiling/benchmark_simulators_test.py .....                                                                                                                                                    [100%]

real    0m2.885s
user    0m3.523s
sys     0m2.597s
```

Initial PR for quantumlib#3240
Closes quantumlib#882
…ion (quantumlib#4171)

Typescript development in Cirq and prototype Bloch sphere implementation.

### Typescript Development in Cirq

This PR implements the features discussed in the [Typescript Development in Cirq RFC](https://docs.google.com/document/d/16LybO9bNqL3LaIy1pZCJQXSPvKs5g3IqKkkE3efqM8U/edit). 

#### Listed below are the features included in the PR:
 - Infrastructure and directory structure to allow Typescript development in Cirq, including:
   - Bundling Typescript into Javascript, and examples of how to use these bundles
   - Hot reloading development environment for both browser and notebook settings
   - Typescript testing framework using Mocha and Chai, including a test for the bloch sphere.
   - Coverage checks for Typescript unit tests. (100% coverage needed in CI)
   - End-to-end integration between the Python and Javascript (the bloch sphere)
   - Example of full coverage testing of the Python side
   - CI for the Typescript environment for testing and linting. 
   - Check scripts for testing and linting
 - An example 3d bloch sphere implementation. This bloch sphere can be viewed:
   - In the browser (development)
   - In a Jupyter notebook (development and production)
   - Via a generated HTML file. This file can be sent anywhere and be viewed in the browser, regardless of if the recipient has Cirq installed or not. 
 - A [up-to-date package on Test PyPI](https://test.pypi.org/project/cirq-web/0.12.0.dev20210616161134/) representing the state of the cirq-web package as of 6/16/21.
 - Documentation for using cirq-web in the README files.

![colab_web_pr_trial](https://user-images.githubusercontent.com/17647506/122309538-1673ab80-ced4-11eb-96dd-112ea46b6df9.gif)
cduck and others added 18 commits July 3, 2021 22:55
…lib#4213)

- Add the method cirq.two_qubit_matrix_to_sqrt_iswap_operations() to optimally decompose with SQRT_ISWAP or SQRT_ISWAP_INV gates.
- Add cirq.SQRT_ISWAP and cirq.SQRT_ISWAP_INV (aliases for cirq.ISWAP**0.5 and cirq.ISWAP**-0.5).
…mlib#4288)

* Add test for the failing case with a tagged operation

* Fix merge interactions for tagged operation
Follow up to quantumlib#4213.  Fixes quantumlib#4083.  (Also contains a one-line change to fix quantumlib#4225.)

I'm open to name suggestions for `MergeInteractionsToSqrtIswap`.
Adds py39 to windows, mac and linux pytest matrices.
Fixes quantumlib#3772 
Fixes quantumlib#3769
 - Make the argument optional in benchmark_
 - If provided, you can do a subset of those in sampled_df
 - More robust checking that it matches sampled_df
* Use `partial_trace` to factor density matrix

* format

Co-authored-by: Cirq Bot <[email protected]>
Adds on_each support to the SupportsOnEachGate mixin for multi-qubit gates.

The handling here is not as flexible as for single-qubit gates, which allows any tree of gates and applies them depth-first. This allows the following two options for multi-qubit gates:

```
A: varargs form gate.on_each([q1, q2], [q3, q4])
B: explicit form gate.on_each([[q1, q2], [q3, q4]])
```


Discussion here, quantumlib#4034 (comment). Part of quantumlib#4236.
quantumlib#4267)

This PR is related to: quantumlib#3388

We'd like to add some documentation rules to pylint but doing so would require us to add a significant amount of documentation to the current code.  In order to start enforcing this rule sooner rather than later. I added an incremental lint task to the ci.  The incremental lint task will check more rules but only check them on changed files.
…antumlib#4282)

As part of the the review quantumlib#3989 we could use the ability to build a PauliSum from a Boolean expression. This PR makes available that function (smaller change).
increase per_page for checks query in automerge
Part of quantumlib#4034. (See quantumlib#4034 (comment)), moves the on_each functionality to cirq.Gate, and deprecates SupportsOnEachGate.

Note the tests that are deleted here had already been duplicated in raw_types_test.py by https://github.com/quantumlib/Cirq/pull/4281/files.
…#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 <[email protected]>
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@daxfohl
Copy link
Collaborator Author

daxfohl commented Jul 13, 2021

Closing in favor of #4317

@daxfohl daxfohl closed this Jul 13, 2021
@daxfohl daxfohl deleted the trial branch July 13, 2021 14:28
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.