Skip to content

Conversation

codrut3
Copy link
Contributor

@codrut3 codrut3 commented Jul 13, 2025

Otherwise every time CircuitOperation.has_unitary is called, has_unitary_protocol decomposes the circuit into operations and calls has_unitary on each. This is inefficient, especially if repetitions > 1, because has_unitary_protocol looks at the circuit with repetitions.

To ensure this change is correct, I wrote the tests first, and then made the change.

@codrut3 codrut3 requested review from vtomole and a team as code owners July 13, 2025 10:45
@codrut3 codrut3 requested a review from dabacon July 13, 2025 10:45
@github-actions github-actions bot added the size: S 10< lines changed <50 label Jul 13, 2025
@codrut3
Copy link
Contributor Author

codrut3 commented Jul 13, 2025

When I worked on the Shannon decomposition I found that merge_single_qubit_gates_to_phased_x_and_z is slow. I investigated it and this is one of the inefficiencies I found.

Although the Shannon decomposition now uses a more efficient merge method in cleanup_operations, this change still improves a bit the transpiler.

For 7 qubits, I measured an average 1.6% improvement in the transpiler time on my laptop, from 86.89 seconds to 85.48 seconds.

Here is the code I used:

"""Tests performance of quantum Shannon decomposition.
"""

import sys
import time

from scipy.stats import unitary_group

import cirq


if not sys.warnoptions:
    import warnings
    warnings.simplefilter("ignore")


def main():
    measurements = []
    n_qubits = 7
    for i in range(10):
        print(f'Iteration {i}')
        U = unitary_group.rvs(2**n_qubits)
        qubits = [cirq.NamedQubit(f'q{i}') for i in range(n_qubits)]
        start_time = time.time()
        circuit = cirq.Circuit(cirq.quantum_shannon_decomposition(qubits, U))
        elapsed_time = time.time() - start_time
        print(f'Time taken: {elapsed_time:.2f} seconds')

        # Run cirq transpiler
        start_time = time.time()
        circuit = cirq.optimize_for_target_gateset(circuit, gateset=cirq.CZTargetGateset(preserve_moment_structure=False), max_num_passes=None)
        elapsed_time = time.time() - start_time
        measurements.append(elapsed_time)
        print(f'Cirq depth for {n_qubits} qubits (with transpiler): {len(circuit)} ({elapsed_time:.2f} sec)')

    print(f'Average time taken: {sum(measurements) / len(measurements):.2f} seconds')


if __name__ == '__main__':
    main()

Copy link

codecov bot commented Jul 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.68%. Comparing base (460dda0) to head (ed5eefa).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7483      +/-   ##
==========================================
- Coverage   98.68%   98.68%   -0.01%     
==========================================
  Files        1092     1092              
  Lines       96834    96855      +21     
==========================================
+ Hits        95562    95582      +20     
- Misses       1272     1273       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Collaborator

@NoureldinYosri NoureldinYosri left a comment

Choose a reason for hiding this comment

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

thanks @codrut3

@NoureldinYosri NoureldinYosri added this pull request to the merge queue Jul 14, 2025
Merged via the queue into quantumlib:main with commit 89ebad8 Jul 14, 2025
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: S 10< lines changed <50
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants