Add a barrier operation for controlling transformations and optimizations of circuits

Open
#7,534 21 comments 0 reactions 4 assignees View on GitHub

Maintainers usually reply within 1 day

@vtomole is already working on this.

Since Aug 4, 2025.

Assessment

This issue has not been assessed yet.

Description

kind/feature-request priority/before-1.8 priority/p1 status/needs-agreed-design status/rfc-proposed triage/accepted

Is your feature request related to a use case or problem? Please explain

A barrier is an identity operation, equivalent to barrier_op = cirq.identity_each(*all_qubits), that serves as a directive for circuit compilation. Its main use is to separate pieces of a circuit, ensuring that any transformers, compiling processing and optimizers are restricted to only operate between barriers and not across them.

We've encountered a problem where applying transformations like cirq.merge_single_qubit_gates_to_phxz and cirq.stratified_circuit to an entire circuit can cause unwanted shifting of larger blocks (e.g., triangular blocks) within the circuit, even when the intention is to only affect specific initial moments. A barrier would prevent such unintended transformations from crossing a specified point in the circuit, allowing for more precise control over optimizations.

It is an extremely generally applicable tool for shaping experimental circuits into desired structure. This comes up in Trotter and many other contexts.


Describe the solution you would prefer

We would prefer a mechanism, similar to a "barrier" operation, that can be inserted into a cirq.Circuit. This barrier operation would act as a compile-time directive, preventing circuit transformations and optimizations from operating across it. Essentially, it would delineate sections of the circuit, ensuring that any re-writes or optimizations are confined to the gates within a given section, without affecting gates on the other side of the barrier.
There have been a discussions over time on this issue here.


How urgent is this for you? Is it blocking important work?

P1 – I need this no later than the next release


Describe alternatives/workarounds you've considered

One workaround we've considered is manually splitting the circuit into segments, applying transformations to the desired prefix, and then re-combining the circuits. For example:

qc = original_circuit[:k]  # prefix circuit
qc = cirq.merge_single_qubit_gates_to_phxz(qc)
qc = cirq.stratified_circuit(
    qc, categories=[lambda op: cirq.num_qubits(op) == k for k in (1, 2)]
)
final_circuit = qc + original_circuit[k:]
Image

This only works based on many assumptions and will require assertion tests, etc. and is not generalizable.

Dominant language
Python
Stars
5.1k
Forks
1.3k
Avg merge
1d 12h
Merged PRs (30d)
48

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from quantumlib/Cirq

All issues in quantumlib/Cirq

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.