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

Set default unitary precision np.complex64 #5426

Merged
merged 37 commits into from
Jun 15, 2022
Merged

Conversation

dabacon
Copy link
Collaborator

@dabacon dabacon commented May 31, 2022

This changes the unitary method on Circuit to return a numpy array that uses np.complex64 instead of np.complex128.

This is a breaking change.

Some ways we could deprecate this, but they all have some serious issues

  1. We could add a deprecation warning as was done with final_state_vector if no override is None. See Consistent final_state_vector #5267. However, unlike in that PR, if we do make this warn in tests, we will need to fix many 1000s of tests (which we will then want to revert), because final_state_vector is not widely used, but unitary is all over our tests.

  2. We could just start warning about this in our release notes, but not change it until a set version update (say 0.16). We could submit this PR, or use it for a future PR to update the tests as necessary.

Some comments on this change:

  1. Mostly the changes here are to update the precision in tests, since we only have a little over 7 digits of precision in the new switch most of the precision is set to 1e-6. However there are cases where we build larger circuits that have many rounding errors and so end up closer to 5e-6 or even on 1e-5.

  2. This changes the default precision on kak_decompositions, which was a large source of many of the test failures.

I check this for flakes at 200 repetitions. In the process I found some interesting failures. These are currently here, but I moved them out to #5431 for separate review.

Fixes: #1030

@CirqBot CirqBot added the size: M 50< lines changed <250 label May 31, 2022
@dabacon dabacon changed the title Make unitary precision np.complex64 Set default unitary precision np.complex64 Jun 1, 2022
@dabacon dabacon added BREAKING CHANGE For pull requests that are important to mention in release notes. area/circuits area/simulation labels Jun 1, 2022
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@95-martin-orion 95-martin-orion self-assigned this Jun 13, 2022
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.

I'm on board with the breaking change here, just some comments on your comments 😝

Comment on lines 598 to 599
kak decomposition is calculated from the `interaction`s. T
Thisd etermines how close $k_x$ must be to π/4 to guarantee
Copy link
Collaborator

Choose a reason for hiding this comment

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

T Thisd etermines -> This determines

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@@ -83,7 +83,7 @@ def test_decompose_two_qubit_interaction_into_two_b_gates(obj: Any):
desired_unitary = obj if isinstance(obj, np.ndarray) else cirq.unitary(obj)
for operation in circuit.all_operations():
assert len(operation.qubits) < 2 or operation.gate == _B
assert cirq.approx_eq(cirq.unitary(circuit), desired_unitary, atol=1e-6)
np.testing.assert_allclose(cirq.unitary(circuit), desired_unitary, atol=3e-5)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this also get the "lose a lot of precision" notice?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added

@@ -384,8 +384,10 @@ def _create_corrected_circuit(
implements `target_unitary`.
"""
# Get the local equivalents
# We use higher precision than normal to avoid nummerical instabilities.
Copy link
Collaborator

Choose a reason for hiding this comment

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

nummerical -> numerical

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@dabacon dabacon merged commit d7ee49d into quantumlib:master Jun 15, 2022
CirqBot pushed a commit that referenced this pull request Jun 29, 2022
…final_state_vector (#5636)

Rolls back change in #5426 that set the default precision on `Circuit#unitary` to `np.complex128`.

Also sets the default on `Circuit#final_state_vector` to `np.complex128`.  There was a deprecation warning that this was going to be downgraded to `np.complex64`,, but this will now remain `np.complex128`.  This is breaking in that is someone explicitly passed `dtype=None` to this method it would now break.  

Rolls back #5426 #5616 #5537 #5535
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
…final_state_vector (quantumlib#5636)

Rolls back change in quantumlib#5426 that set the default precision on `Circuit#unitary` to `np.complex128`.

Also sets the default on `Circuit#final_state_vector` to `np.complex128`.  There was a deprecation warning that this was going to be downgraded to `np.complex64`,, but this will now remain `np.complex128`.  This is breaking in that is someone explicitly passed `dtype=None` to this method it would now break.  

Rolls back quantumlib#5426 quantumlib#5616 quantumlib#5537 quantumlib#5535
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
…final_state_vector (quantumlib#5636)

Rolls back change in quantumlib#5426 that set the default precision on `Circuit#unitary` to `np.complex128`.

Also sets the default on `Circuit#final_state_vector` to `np.complex128`.  There was a deprecation warning that this was going to be downgraded to `np.complex64`,, but this will now remain `np.complex128`.  This is breaking in that is someone explicitly passed `dtype=None` to this method it would now break.  

Rolls back quantumlib#5426 quantumlib#5616 quantumlib#5537 quantumlib#5535
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/circuits area/simulation BREAKING CHANGE For pull requests that are important to mention in release notes. size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Switch default circuit unitary/sim precision from np.complex128 to np.complex64
3 participants