Skip to content

Add an improved stratifier #6067

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

Closed
wants to merge 33 commits into from
Closed

Add an improved stratifier #6067

wants to merge 33 commits into from

Conversation

perlinm
Copy link
Contributor

@perlinm perlinm commented Apr 15, 2023

Add a new and improved stratifying method, and (at least tentatively) an option as to which stratification method to use.

The basic idea is to:

  • iterate over operations in topological order,
  • create new moments on an as-needed basis, and
  • advance moments up/forward to absorb a new operation, whenever possible.

Improvements over the old stratifier (from #6013) include:

  • Shorter circuits! New circuit depth is upper bounded by the old circuit depth.
  • Linear runtime complexity with the number of categories (the old classifier is factorial, O((len(categories)+1)!)).
    • Corollary: ~5x reduced runtime on the example below, with 2 categories.

Test script (to run with a local version of cirq that contains this PR):

#!/usr/bin/env python3
import cirq
import time

circuit = cirq.testing.random_circuit(qubits=4, n_moments=100, op_density=1)
categories = [lambda op: len(op.qubits) == 1, cirq.CNOT]  # single-qubit ops, and CNOTs

for method in ["static", "dynamic"]:
    start = time.time()
    new_circuit = cirq.stratified_circuit(circuit, method=method, categories=categories)
    print("time:", time.time() - start)
    print("length:", len(new_circuit))
    assert cirq.approx_eq(circuit.final_state_vector(), new_circuit.final_state_vector())

NOTE: this PR is NOT COMPLETE at the moment. However, the PR is relatively long, so I thought to open it up for initial review. Apologies if that's not appropriate, in which case please let me know and I'll mark this PR as a draft for now.

The current TODO list is:

  • Properly deal with measurement/control keys (assumed to be absent at the moment).
  • Decide whether to remove the old stratifier. Since the new stratifier seems to be strictly better, I cautiously lean "yes".
  • Add/modify unit tests (which may depend on whether the old stratifier is removed).

Tagging @tanujkhattar, who reviewed my last statifier PR (#6013).

Feedback greatly appreciated 🙂, as well as suggestions for how to cleanly deal with measurement/control keys.

@perlinm perlinm requested review from a team, vtomole and cduck as code owners April 15, 2023 18:50
@perlinm perlinm requested a review from dstrain115 April 15, 2023 18:50
@perlinm perlinm marked this pull request as draft April 17, 2023 06:05
@perlinm perlinm marked this pull request as ready for review April 17, 2023 15:11
@CirqBot CirqBot added the size: L 250< lines changed <1000 label Apr 19, 2023
@tanujkhattar tanujkhattar self-assigned this May 30, 2023
@github-actions github-actions bot added the Stale label Feb 13, 2024
@github-actions github-actions bot closed this Mar 15, 2024
@mhucka mhucka added the status/stale This has been closed due to inactivity for an extended period of time. label Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: L 250< lines changed <1000 status/stale This has been closed due to inactivity for an extended period of time.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants