-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Create an insertion_sort transformer and incorporate it into the transpiling protocol #6776
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6776 +/- ##
==========================================
- Coverage 97.84% 97.84% -0.01%
==========================================
Files 1077 1079 +2
Lines 92791 92825 +34
==========================================
+ Hits 90788 90821 +33
- Misses 2003 2004 +1 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to order by sorted qubits, otherwise LGTM with a few minor comments.
Thanks for adding this!
cirq-core/cirq/transformers/target_gatesets/compilation_target_gateset.py
Outdated
Show resolved
Hide resolved
sorted_operations: List['cirq.Operation'] = [] | ||
for op in circuit.all_operations(): | ||
sorted_operations.append(op) | ||
j = len(sorted_operations) - 1 | ||
while ( | ||
j | ||
and _id(sorted_operations[j]) < _id(sorted_operations[j - 1]) | ||
and protocols.commutes(sorted_operations[j], sorted_operations[j - 1], default=False) | ||
): | ||
sorted_operations[j], sorted_operations[j - 1] = ( | ||
sorted_operations[j - 1], | ||
sorted_operations[j], | ||
) | ||
j -= 1 | ||
return circuits.Circuit(sorted_operations) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please reconsider the implementation I suggested in #6776 (comment). Unlike here, the op.qubits are sorted just once and cached, and the output list is modified just once per iteration - after finding the correct insertion position.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please consider the suggested implementation.
I'm willing to bet that my implementation is faster. the most expensive operation is not the qubit comparison but the call to commutes, caching the sorted qubits in a map is not optimal since the getting the sorted tuple should do at most a single comparison (the operations are supposed to be either single or two qubit operations). |
05d6d8c
to
45f75ca
Compare
Challenge accepted :-) Here is a benchmark code to be saved as
and here is the outcome for the PR head and the optimized version: # current head of PR #6776
$ git checkout a8277a63bcdf69f20a0a81b182a2643220bb23ff
$ ipython bench_isort.ipy
14.1 s ± 93.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
# proposed change, https://github.com/pavoljuhas/Cirq/tree/amend-6776
$ git fetch https://github.com/pavoljuhas/Cirq.git amend-6776
$ git checkout 1ae465e118520e376de2820a1469a150459f7973
$ ipython bench_isort.ipy
5.59 s ± 72.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) Update - the benchmark time is down to 0.61 s after pavoljuhas@619eb56 |
Running the compilation workflow with the following command should produce circuits with a very short depth
cc: @ikd-sci
fixes #6771