Skip to content

Use an efficient representation for merged components of operations #7484

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

codrut3
Copy link
Contributor

@codrut3 codrut3 commented Jul 13, 2025

Currently merge_operations represents merged components as a CircuitOperation. This means in a merge of n operations, n-1 CircuitOperations are created, with a complexity of O(n^2).
I use a disjont-set data structure to reduce the complexity to O(n) for merge_k_qubit_unitaries_to_circuit_op. merge_operations itself can't be improved because it uses a merge_func that requires creating a CircuitOperation at every step.

…s in the merge methods.

Currently merge_operations represents merged components as a CircuitOperation.
This means in a merge of n operations, n-1 CircuitOperations are created,
with a complexity of O(n^2).
We use a disjont-set data structure to reduce the complexity to O(n) for
merge_k_qubit_unitaries_to_circuit_op. merge_operations itself can't be
improved because it uses a merge_func that requires creation of
CircuitOperation at every step.
@codrut3 codrut3 requested review from vtomole and a team as code owners July 13, 2025 17:29
@codrut3 codrut3 requested a review from dabacon July 13, 2025 17:29
@github-actions github-actions bot added the size: XL lines changed >1000 label Jul 13, 2025
@codrut3
Copy link
Contributor Author

codrut3 commented Jul 13, 2025

I didn't change the algorithm in merge operations, but switched from CircuitOperation to Component for intermediate merges.
I wrote an explanation for the algorithm here.

I found this inefficiency while investigating why merge_single_qubit_gates_to_phased_x_and_z in the Shannon decomposition is slow. Right now this method is not called anymore by the Shannon decomposition, I replaced it with a custom version that is faster.

Still this change improves transpilation. On my laptop, I measured an average improvement of 5,09% from 86.89 seconds to 82,46 seconds for 7 qubits.

I also measured the improvement, if merge_single_qubit_gates_to_phased_x_and_z would still be used in the Shannon decomposition. In this case it is 20.7% from 40.88 seconds to 32.40 seconds for 8 qubits.

This is the script I used to measure the transpiler improvement:

"""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 for transpiling: {sum(measurements) / len(measurements):.2f} seconds')


if __name__ == '__main__':
    main()

@NoureldinYosri NoureldinYosri self-assigned this Jul 23, 2025
@babacry babacry self-assigned this Jul 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: XL lines changed >1000
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants