quantumlib / Cirq

A Python framework for creating, editing, and invoking Noisy Intermediate Scale Quantum (NISQ) circuits.
Apache License 2.0
4.29k stars 1.02k forks source link

Switch default circuit unitary/sim precision from `np.complex128` to `np.complex64` #1030

Closed Strilanc closed 2 years ago

Strilanc commented 6 years ago

For NISQ circuits, the reduced precision isn't particularly relevant but the speed gains are.

dabacon commented 4 years ago

The simulators currently use np.complex64 but there are other places where the default is still np.complex128.

dabacon commented 4 years ago

The places where it needs to be changed are circuit.unitary and circuit.final_state_vector.

diabhiue commented 3 years ago

As discussed in Cirq-cync, can someone please assign me here as I don't have permissions?

vtomole commented 3 years ago

@diabhiue I've assigned you.

vtomole commented 3 years ago

@diabhiue Do your new changes actually improve the performance?

diabhiue commented 3 years ago

@vtomole, I tried running ./check/asv_run for performance tests(post changes). Initially, it asked for system info (for initial set up) which was chosen by default, but it failed while running. Below is the error message. How can I resolve this?

Error message

```zsh (cirq-py3) ~/projects/Cirq(1030 ✗) ./check/asv_run · No executable found for python 3.6 · No executable found for python 3.7 · Creating environments · Discovering benchmarks ·· Uninstalling from virtualenv-py3.8 ·· Building 31daa941 for virtualenv-py3.8. ·· Installing 31daa941 into virtualenv-py3.8. ·· Error running /home/diabhiue/projects/Cirq/.asv/env/76391772e92136ec87b9940d70226329/bin/python -mpip install /home/diabhiue/projects/Cirq/.asv/env/76391772e92136ec87b9940d70226329/asv-build-cache/31daa9410a0e1e1ac3da38109aa8ce3a15aed17b/cirq-0.13.0.dev0-py3-none-any.whl (exit status 1) STDOUT --------> Processing ./asv-build-cache/31daa9410a0e1e1ac3da38109aa8ce3a15aed17b/cirq-0.13.0.dev0-py3-none-any.whl STDERR --------> ERROR: Could not find a version that satisfies the requirement cirq-aqt==0.13.0.dev (from cirq) (from versions: 0.12.0.dev20210612195918, 0.12.0.dev20210612203412, 0.12.0.dev20210614144502, 0.12.0.dev20210614155514, 0.12.0.dev20210614212301, 0.12.0.dev20210615004208, 0.12.0.dev20210615021241, 0.12.0.dev20210615161805, 0.12.0.dev20210615184533, 0.12.0.dev20210616203540, 0.12.0.dev20210617161841, 0.12.0.dev20210617163522, 0.12.0.dev20210621203848, 0.12.0.dev20210622155818, 0.12.0.dev20210622164449, 0.12.0.dev20210622165747, 0.12.0.dev20210622211622, 0.12.0.dev20210623024554, 0.12.0.dev20210623155331, 0.12.0.dev20210623201424, 0.12.0.dev20210624115740, 0.12.0.dev20210624162903, 0.12.0.dev20210624192532, 0.12.0.dev20210625201025, 0.12.0.dev20210625210534, 0.12.0.dev20210628152018, 0.12.0.dev20210628225324, 0.12.0.dev20210628232440, 0.12.0.dev20210629225807, 0.12.0.dev20210630005811, 0.12.0.dev20210630121854, 0.12.0.dev20210630205305, 0.12.0.dev20210702133320, 0.12.0.dev20210702154855, 0.12.0.dev20210702231711, 0.12.0.dev20210703225533, 0.12.0.dev20210706022704, 0.12.0.dev20210707192225, 0.12.0.dev20210707203903, 0.12.0.dev20210707222957, 0.12.0.dev20210708190715, 0.12.0.dev20210709011140, 0.12.0.dev20210709023918, 0.12.0.dev20210709040206, 0.12.0.dev20210709042051, 0.12.0.dev20210709193054, 0.12.0.dev20210709205112, 0.12.0.dev20210709212241, 0.12.0.dev20210709231148, 0.12.0.dev20210709235105, 0.12.0.dev20210712211116, 0.12.0.dev20210713144931, 0.12.0.dev20210714160402, 0.12.0.dev20210714194520, 0.12.0.dev20210714201237, 0.12.0.dev20210714210844, 0.12.0.dev20210714232449, 0.12.0.dev20210715174317, 0.12.0.dev20210715191538, 0.12.0.dev20210715205013, 0.12.0.dev20210719133031, 0.12.0.dev20210720163652, 0.12.0.dev20210720172314, 0.12.0.dev20210720193909, 0.12.0.dev20210720201718, 0.12.0.dev20210721174133, 0.12.0.dev20210721180150, 0.12.0.dev20210723065135, 0.12.0.dev20210725033218, 0.12.0.dev20210726163855, 0.12.0.dev20210726180724, 0.12.0.dev20210726183417, 0.12.0.dev20210726185245, 0.12.0.dev20210726203014, 0.12.0.dev20210728203212, 0.12.0.dev20210728215634, 0.12.0.dev20210728232936, 0.12.0.dev20210729211930, 0.12.0.dev20210730164821, 0.12.0.dev20210802154505, 0.12.0.dev20210804161122, 0.12.0.dev20210804163622, 0.12.0.dev20210804165457, 0.12.0.dev20210804212731, 0.12.0.dev20210805064526, 0.12.0.dev20210806000123, 0.12.0.dev20210806032707, 0.12.0.dev20210806161017, 0.12.0.dev20210808182659, 0.12.0.dev20210810005821, 0.12.0.dev20210810045225, 0.12.0.dev20210810051536, 0.12.0.dev20210810223624, 0.12.0.dev20210811122134, 0.12.0.dev20210811170011, 0.12.0.dev20210811172937, 0.12.0.dev20210811175635, 0.12.0.dev20210811183507, 0.12.0.dev20210811191033, 0.12.0.dev20210811195003, 0.12.0.dev20210811212103, 0.12.0, 0.13.0.dev20210811232553, 0.13.0.dev20210811234906, 0.13.0.dev20210812001719, 0.13.0.dev20210812031536, 0.13.0.dev20210812040004, 0.13.0.dev20210812042106, 0.13.0.dev20210812050310, 0.13.0.dev20210812162815, 0.13.0.dev20210813221249, 0.13.0.dev20210814185508, 0.13.0.dev20210816193359, 0.13.0.dev20210816195849, 0.13.0.dev20210816203535, 0.13.0.dev20210816215342, 0.13.0.dev20210816233321, 0.13.0.dev20210817004448, 0.13.0.dev20210817193329, 0.13.0.dev20210818061230, 0.13.0.dev20210818160808, 0.13.0.dev20210818171827, 0.13.0.dev20210818183948, 0.13.0.dev20210819012929, 0.13.0.dev20210819020806, 0.13.0.dev20210819030321, 0.13.0.dev20210819063020, 0.13.0.dev20210819235148, 0.13.0.dev20210820160855, 0.13.0.dev20210820185342, 0.13.0.dev20210820222802, 0.13.0.dev20210820224938, 0.13.0.dev20210820231011, 0.13.0.dev20210820233111, 0.13.0.dev20210820235213, 0.13.0.dev20210821001415, 0.13.0.dev20210821003550, 0.13.0.dev20210821014747, 0.13.0.dev20210821025135, 0.13.0.dev20210821030827, 0.13.0.dev20210822190736, 0.13.0.dev20210823180755, 0.13.0.dev20210823231908, 0.13.0.dev20210824004854, 0.13.0.dev20210824170204, 0.13.0.dev20210825162618, 0.13.0.dev20210825205214, 0.13.0.dev20210825212610, 0.13.0.dev20210826004924, 0.13.0.dev20210826185640, 0.13.0.dev20210826225825, 0.13.0.dev20210826231852, 0.13.0.dev20210831164745, 0.13.0.dev20210901060306, 0.13.0.dev20210901195302, 0.13.0.dev20210907233535, 0.13.0.dev20210908171519, 0.13.0.dev20210910231739, 0.13.0.dev20210914155943, 0.13.0.dev20210914173402, 0.13.0.dev20210915180606, 0.13.0.dev20210916200646, 0.13.0.dev20210917162100, 0.13.0.dev20210917191651, 0.13.0.dev20210917210754, 0.13.0.dev20210917214256, 0.13.0.dev20210918213009, 0.13.0.dev20210918215545, 0.13.0.dev20210921185444, 0.13.0.dev20210922165312, 0.13.0.dev20210923190159, 0.13.0.dev20210923212346, 0.13.0.dev20210924035311, 0.13.0.dev20210924202240, 0.13.0.dev20210924204253, 0.13.0.dev20210924212510, 0.13.0.dev20210924233357, 0.13.0.dev20210927221740, 0.13.0.dev20210928043924, 0.13.0.dev20210928210043, 0.13.0.dev20210929041120, 0.13.0.dev20210929175218, 0.13.0.dev20210929200623, 0.13.0.dev20210929215257, 0.13.0.dev20210929225605, 0.13.0.dev20210930233358, 0.13.0.dev20211001180414, 0.13.0.dev20211001182844, 0.13.0.dev20211001184738, 0.13.0.dev20211004030023, 0.13.0.dev20211005015203, 0.13.0.dev20211005182832, 0.13.0.dev20211005202633, 0.13.0.dev20211005225442, 0.13.0.dev20211006192945, 0.13.0.dev20211006230807, 0.13.0.dev20211008171744, 0.13.0.dev20211008211012, 0.13.0.dev20211011220323, 0.13.0.dev20211012141432, 0.13.0.dev20211012175707, 0.13.0.dev20211012220646, 0.13.0.dev20211012234056, 0.13.0.dev20211013002532, 0.13.0.dev20211013173027, 0.13.0.dev20211013203102, 0.13.0.dev20211013213216, 0.13.0.dev20211014011808, 0.13.0.dev20211014162700, 0.13.0.dev20211014185350, 0.13.0.dev20211015205930, 0.13.0.dev20211015220941, 0.13.0.dev20211016034750, 0.13.0.dev20211018204936, 0.13.0.dev20211019085906, 0.13.0.dev20211021025555, 0.13.0.dev20211024223811, 0.13.0, 0.13.1, 0.14.0.dev20211024234303, 0.14.0.dev20211025014153, 0.14.0.dev20211025210608, 0.14.0.dev20211026024027, 0.14.0.dev20211026172444) ERROR: No matching distribution found for cirq-aqt==0.13.0.dev ·· Failed to build the project and import the benchmark suite. ```

Adding @tanujkhattar as per git blame

I also noticed that there were some test failures before making any changes. Below are those failed tests. Are these expected?

Failed tests(./check/all)

``` =========================== short test summary info ============================ FAILED cirq-rigetti/cirq_rigetti/sampler_bell_circuit_test.py::test_bell_circuit_through_sampler FAILED cirq-rigetti/cirq_rigetti/sampler_parametric_circuit_test.py::test_parametric_circuit_through_sampler FAILED cirq-rigetti/cirq_rigetti/sampler_parametric_circuit_test.py::test_parametric_circuit_through_sampler_with_parametric_compilation FAILED cirq-rigetti/cirq_rigetti/sampler_readout_reassigned_qubits_test.py::test_readout_on_reassigned_qubits FAILED cirq-rigetti/cirq_rigetti/sampler_readout_separate_memory_regions_test.py::test_circuit_with_separate_readout_keys_through_sampler FAILED cirq-rigetti/cirq_rigetti/sampler_test.py::test_get_rigetti_qcs_sampler FAILED cirq-rigetti/cirq_rigetti/service_bell_circuit_test.py::test_bell_circuit_through_service FAILED cirq-rigetti/cirq_rigetti/service_parametric_circuit_test.py::test_parametric_circuit_through_service FAILED cirq-rigetti/cirq_rigetti/service_test.py::test_get_rigetti_qcs_service FAILED dev_tools/bash_scripts_test.py::test_pytest_changed_files_file_selection FAILED dev_tools/bash_scripts_test.py::test_pytest_changed_files_branch_selection FAILED dev_tools/bash_scripts_test.py::test_pytest_and_incremental_coverage_branch_selection FAILED dev_tools/bash_scripts_test.py::test_incremental_format_branch_selection FAILED dev_tools/bash_scripts_test.py::test_pylint_changed_files_file_selection FAILED dev_tools/docker_test.py::test_docker_stable - AssertionError: assert ... FAILED dev_tools/docker_test.py::test_docker_pre - AssertionError: assert 1 == 0 16 failed, 14660 passed, 51 skipped, 77 xfailed, 571 warnings in 582.07s (0:09:42) ```

vtomole commented 3 years ago

Hey @diabhiue, I'm just seeing your ping. I'm also going to be out of office for the next 2 weeks.

Anyway, it seems like the second error is due to https://github.com/quantumlib/Cirq/issues/4660 and the first one is due to cirq's sub-packages are not installed before the benchmarks are executed. Since the benchmarks only need cirq-google, you will need to tell asv where it is. I was able to run the benchmarks after making this change: https://github.com/quantumlib/Cirq/commit/024f6fb80bbcb5f246ff0066b994732a43c4fa09 on my local asv.conf.json.

(venv3.7) vtomole@vtomole:~/Cirq$ ./check/asv_run 
· No executable found for python 3.6
· Creating environments
· Discovering benchmarks
·· Uninstalling from virtualenv-py3.7
·· Building a75b215f <master> for virtualenv-py3.7.
·· Installing a75b215f <master> into virtualenv-py3.7.
· Running 22 total benchmarks (1 commits * 2 environments * 11 benchmarks)
[  0.00%] · For Cirq commit a75b215f <master>:
[  0.00%] ·· Building for virtualenv-py3.7
[  0.00%] ·· Benchmarking virtualenv-py3.7
[  2.27%] ··· Running (bench_examples.ExamplesTest.time_example_runs_bcs_mean_field_perf--)...........
[ 27.27%] ··· bench_examples.ExamplesTest.time_example_runs_bcs_mean_field_perf                                                                                                                     183±1ms
[ 29.55%] ··· bench_examples.ExamplesTest.time_example_runs_bell_inequality_perf                                                                                                                4.46±0.02ms
[ 31.82%] ··· bench_examples.ExamplesTest.time_example_runs_bernstein_vazirani_perf                                                                                                             4.33±0.07ms
[ 34.09%] ··· bench_examples.ExamplesTest.time_example_runs_grover_perf                                                                                                                          6.45±0.1ms
[ 36.36%] ··· bench_examples.ExamplesTest.time_example_runs_hello_line_perf                                                                                                                         199±7ms
[ 38.64%] ··· bench_examples.ExamplesTest.time_example_runs_hello_qubit_perf                                                                                                                    1.27±0.01ms
[ 40.91%] ··· bench_examples.ExamplesTest.time_example_runs_phase_estimator_perf                                                                                                                   44.2±2ms
[ 43.18%] ··· bench_examples.ExamplesTest.time_example_runs_quantum_fourier_transform_perf                                                                                                       6.44±0.3ms
[ 45.45%] ··· bench_examples.ExamplesTest.time_example_runs_quantum_teleportation                                                                                                                5.89±0.3ms
[ 47.73%] ··· bench_examples.ExamplesTest.time_example_runs_superdense_coding                                                                                                                      36.6±2ms
[ 50.00%] ··· bench_linalg_decompositions.time_kak_decomposition 

Thanks!

dabacon commented 2 years ago

Doing this for the methods on circuit seems to break a ton of tests (600+) likely because a lot of our default precisions are 1e-7 or 1e-8.

dabacon commented 2 years ago

The method final_state_vector will need to be updated to default to np.complex64.

vtomole commented 2 years ago

@dabacon Given https://github.com/quantumlib/Cirq/pull/5636, did we decide that this is not worth doing due to the frequency of the flakes? If so, then I think the only thing left is to monitor the CI for the next few days to confirm that the roll back fixed everything and then close this.

dabacon commented 2 years ago

Yes we decided to keep the different precision on the Circuit class. Closing.