Skip to content

Conversation

@daxfohl
Copy link
Collaborator

@daxfohl daxfohl commented Aug 13, 2021

Fixes #4337

Changes single-qubit layer generation into two steps. First step creates the {qid: gate_index} lookup so that we can compare against gate_index equality instead of gate equality. Then the second step is generating the moment from that lookup.

Note the change in algo will cause the random circuits generated by a specific seed to change (hence the change in one test case). If this is a blocker, it should be possible to revert to the old retry algo but just compare indexes instead of gates. Reverted to the retry algo since some of the xeb tests were tied to the generated circuit.

Also note any change like this assumes that each gate only appears once in the allowed-gates list. I assume that's a safe assumption.

@mpharrigan

@daxfohl daxfohl requested review from a team, cduck, mrwojtek and vtomole as code owners August 13, 2021 20:03
@daxfohl daxfohl requested a review from balopat August 13, 2021 20:03
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Aug 13, 2021
@CirqBot CirqBot added the size: S 10< lines changed <50 label Aug 13, 2021
Copy link
Collaborator

@mpharrigan mpharrigan left a comment

Choose a reason for hiding this comment

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

I don't understand the logic of how this works

@mpharrigan
Copy link
Collaborator

Technically a breaking change because of the change of API for the gate factories and the seed issue you noted. I think it should be fine

@mpharrigan mpharrigan added the BREAKING CHANGE For pull requests that are important to mention in release notes. label Aug 13, 2021
@mpharrigan mpharrigan self-assigned this Aug 13, 2021
@mpharrigan
Copy link
Collaborator

Do you have profiling data? Does this indeed speed things up?

@daxfohl
Copy link
Collaborator Author

daxfohl commented Aug 13, 2021

Improves generation of 5 circuits of depth 13000 from around 7.3s to around 5.1s on my machine. The time lost due to the "retry" strategy compared to the deterministic strategy was insignificant.

@mpharrigan
Copy link
Collaborator

Sweet. I'm a little surprised the retry logic doesn't cause a slowdown; but I guess it's exceedingly unlikely for you to have to retry more than a couple times.

Thanks for profiling it.

@mpharrigan mpharrigan added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Aug 13, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Aug 13, 2021
@CirqBot CirqBot merged commit 75eccd3 into quantumlib:master Aug 13, 2021
@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 Aug 13, 2021
@daxfohl
Copy link
Collaborator Author

daxfohl commented Aug 14, 2021

FWIW I also tried an alternate approach, just caching the canonicalization of the XZPowGate, since that is called by the equality checker, and the same gates were reused over and over. That reduced the time from 7.3s to 6.1s. So that's about half the overhead in the equality tester for XZPowGates. Don't think we should do anything since we've solved the problem better already. Just a data point.

@daxfohl
Copy link
Collaborator Author

daxfohl commented Aug 14, 2021

Wait, this was really dumb. After reading the above again, I realized all we had to do for this is change == to is in the while loop. This actually seems slightly faster than the implemented solution, is no longer breaking, and it handles the case of multiple identical gates in the allow list too. I'll revert this and change to is instead.

daxfohl added a commit to daxfohl/Cirq that referenced this pull request Aug 14, 2021
CirqBot pushed a commit that referenced this pull request Aug 14, 2021
@mpharrigan
Copy link
Collaborator

I'll remove the breaking change label since this was reverted

@mpharrigan mpharrigan removed the BREAKING CHANGE For pull requests that are important to mention in release notes. label Aug 16, 2021
CirqBot pushed a commit that referenced this pull request Aug 16, 2021
Fixes #4337 (again)

Changes equality check to identity check to give the perf improvement. Since the gate instances are reused, and since equality checking if XZPowGate is very expensive, we change equality check to identity check and get a big perf boost. This issue was originally fixed in a different way in #4428, but that has been reverted in llieu of this fix which is simpler, faster, and not a breaking change.

ref #4428 (comment)

@mpharrigan
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
Fixes quantumlib#4337

Changes single-qubit layer generation into two steps. First step creates the `{qid: gate_index}` lookup so that we can compare against gate_index equality instead of gate equality. Then the second step is generating the moment from that lookup.

~~Note the change in algo will cause the random circuits generated by a specific seed to change (hence the change in one test case). If this is a blocker, it should be possible to revert to the old retry algo but just compare indexes instead of gates.~~ Reverted to the retry algo since some of the xeb tests were tied to the generated circuit.

Also note any change like this assumes that each gate only appears once in the allowed-gates list. I assume that's a safe assumption.

@mpharrigan
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
Fixes quantumlib#4337 (again)

Changes equality check to identity check to give the perf improvement. Since the gate instances are reused, and since equality checking if XZPowGate is very expensive, we change equality check to identity check and get a big perf boost. This issue was originally fixed in a different way in quantumlib#4428, but that has been reverted in llieu of this fix which is simpler, faster, and not a breaking change.

ref quantumlib#4428 (comment)

@mpharrigan
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
Fixes quantumlib#4337

Changes single-qubit layer generation into two steps. First step creates the `{qid: gate_index}` lookup so that we can compare against gate_index equality instead of gate equality. Then the second step is generating the moment from that lookup.

~~Note the change in algo will cause the random circuits generated by a specific seed to change (hence the change in one test case). If this is a blocker, it should be possible to revert to the old retry algo but just compare indexes instead of gates.~~ Reverted to the retry algo since some of the xeb tests were tied to the generated circuit.

Also note any change like this assumes that each gate only appears once in the allowed-gates list. I assume that's a safe assumption.

@mpharrigan
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
Fixes quantumlib#4337 (again)

Changes equality check to identity check to give the perf improvement. Since the gate instances are reused, and since equality checking if XZPowGate is very expensive, we change equality check to identity check and get a big perf boost. This issue was originally fixed in a different way in quantumlib#4428, but that has been reverted in llieu of this fix which is simpler, faster, and not a breaking change.

ref quantumlib#4428 (comment)

@mpharrigan
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. size: S 10< lines changed <50

Projects

None yet

Development

Successfully merging this pull request may close these issues.

random_quantum_circuit_generation potential speed improvement

3 participants