Skip to content

Support multi moment gauge compiling #7501

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 1 commit into
base: main
Choose a base branch
from

Conversation

babacry
Copy link
Collaborator

@babacry babacry commented Jul 17, 2025

Support multi moment gauge transformation, where target moments will be gauged with new moments, e.g.,

        q₀: ... ───LG0───╭───────────╮────RG0───...
                         │           │
        q₁: ... ───LG1───┤  moments  ├────RG1───...
                         │   to be   │
        q₂: ... ───LG2───┤ gauged on ├────RG2───...
                         │           │
        q₃: ... ───LG3───╰───────────╯────RG3───...
  • A common MultiMomentGaugeTransformer class is defined.
  • Implemented the CPhase multi moment gauge CPhaseGaugeTransformerMM.
  • A moment is a target moment if it contains at least one target op (CZPowGate) and all its operations are supported by this transformer.
  • A couple of single qubit gates are supported for CPhaseGaugeTransformerMM, e.g., Pauli Gates, ZPowGate etc. If these gates are detected, gauge will be calculated accordingly. Users can tweak their desired gates by instantiate their own transformer by transformer = CPhaseGaugeTransformerMM(supported_gates=ops.Gateset(g1,g2,g3)). By default _SUPPORTED_GATESET are used.

Example:

  Input:
                  ┌──┐
    0: ───@────────@─────H───Rz(-0.255π)───────────@───────@───────
          │        │                               │       │
    1: ───@^0.2────┼@──────────────────────────────@^0.1───@───────
                   ││
    2: ───@────────@┼────────@─────────────@───────@───────@───────
          │         │        │             │       │       │
    3: ───@─────────@────────@^0.2─────────@^0.2───@───────@^0.2───
                  └──┘
    Output:
                       ┌──┐
    0: ───Y───@─────────@─────PhXZ(a=0,x=1,z=0)───H───X───Rz(0.255π)────────────@───────@────────PhXZ(a=0,x=1,z=1.1)───
              │         │                                                       │       │
    1: ───I───@^-0.2────┼@────Z^0.2───────────────────Y─────────────────────────@^0.1───@────────PhXZ(a=0,x=1,z=0.1)───
                        ││
    2: ───X───@─────────@┼────PhXZ(a=0,x=1,z=1)───────X───@────────────@────────@───────@────────PhXZ(a=0,x=1,z=0)─────
              │          │                                │            │        │       │
    3: ───Z───@──────────@────I───────────────────────I───@^-0.2───────@^-0.2───@───────@^-0.2───Z^-0.4────────────────
                       └──┘

Usage

case 1: default setup, any moment with CZPowGate and optionally with _SUPPORTED_GATES will be gauged (see example above),

cphase_transformer = CPhaseGaugeTransformerMM()
output_circuit = cphase_transformer(input_circuit)

case 2: Only CZPowGate-only moments will be gauged (see test_gauge_on_czpow_only_moments test case),

cphase_transformer = CPhaseGaugeTransformerMM(supported_gates=cirq.Gateset())
output_circuit = cphase_transformer(input_circuit)

@github-actions github-actions bot added the size: L 250< lines changed <1000 label Jul 17, 2025
@babacry babacry force-pushed the gc_mm branch 7 times, most recently from c182892 to cc2b38a Compare July 17, 2025 04:21
Copy link

codecov bot commented Jul 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.68%. Comparing base (3d4f6f1) to head (80412d6).
Report is 13 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #7501    +/-   ##
========================================
  Coverage   98.68%   98.68%            
========================================
  Files        1091     1094     +3     
  Lines       96851    97089   +238     
========================================
+ Hits        95576    95815   +239     
+ Misses       1275     1274     -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.

@babacry babacry force-pushed the gc_mm branch 3 times, most recently from 32c5bb3 to 05cc48b Compare July 17, 2025 05:12
@babacry babacry marked this pull request as ready for review July 17, 2025 05:12
@babacry babacry requested review from vtomole and a team as code owners July 17, 2025 05:12
@babacry babacry requested a review from MichaelBroughton July 17, 2025 05:12
@babacry
Copy link
Collaborator Author

babacry commented Jul 17, 2025

Hey @eliottrosenberg @NoureldinYosri , here is multi moment gauge compiling!! After evaluating multiple strategies, this approach proves to be optimal regarding both execution speed and future extensibility.

Note gauges could be automatically added to circuits with default setup, wonder if the default setup is suitable for most use cases? (see Example section and Usage section in the description above)

Copy link
Collaborator

@eliottrosenberg eliottrosenberg left a comment

Choose a reason for hiding this comment

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

LGTM! Great work, @babacry!

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.

@babacry is there a way to reuse code you wrote before in _PauliAndZPow ?


"""Creates the abstraction for multi moment gauge compiling as a cirq transformer."""

from __future__ import annotations
Copy link
Collaborator

Choose a reason for hiding this comment

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

why is this needed?

self,
target: ops.Gate | ops.Gateset | ops.GateFamily,
supported_gates: ops.Gateset = ops.Gateset(),
rng: np.random.Generator = np.random.default_rng(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

there are two issues here, default values get initialized once so all instances of the class the don't explicitly pass a rng object will share the same rng. and an rng as a class property means that the class has an internal mutable state which is undesirable.

please prefer to pass the an rng_or_seed to the functions that need it instead of having an internal state

else:
self.zpow = ops.ZPowGate(exponent=-left.exponent + self.zpow.exponent)

def _merge_right_zpow(self, right: ops.ZPowGate):
Copy link
Collaborator

Choose a reason for hiding this comment

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

didn't you implement the logic for pulling gates somewhere else? or is this specific to these gates?



class CPhaseGaugeTransformerMM(MultiMomentGaugeTransformer):

Copy link
Collaborator

Choose a reason for hiding this comment

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

lets make both MultiMomentGaugeTransformer and CPhaseGaugeTransformerMM be @attrs.frozen

then here we can do

    supported_gates = attrs.field(default=_SUPPORTED_GATESET)
    target = attrs.field(default=_TARGET_GATESET, init=False, repr=False, eq=False, hash=False)

@pavoljuhas pavoljuhas added the priority/after-1.6 Leave for after the Cirq 1.6 release label Jul 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority/after-1.6 Leave for after the Cirq 1.6 release size: L 250< lines changed <1000
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants