Skip to content
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

Introduce gauge compilation #6526

Merged
merged 25 commits into from
Apr 3, 2024

Conversation

NoureldinYosri
Copy link
Collaborator

@NoureldinYosri NoureldinYosri commented Mar 26, 2024

This PR introduces the abstraction for Gauge compilation as well as implementation for Sycamore gate, CZ gate, SqrtCZ gate, ZZ (a.k.a spin inversion), ISWAP gate, and SQRT_ISWAP gate

@NoureldinYosri NoureldinYosri marked this pull request as ready for review March 26, 2024 18:49
Copy link

codecov bot commented Mar 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.79%. Comparing base (45c5fa3) to head (87ab1bb).

❗ Current head 87ab1bb differs from pull request most recent head db24b44. Consider uploading reports for the commit db24b44 to get more accurate results

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #6526    +/-   ##
========================================
  Coverage   97.78%   97.79%            
========================================
  Files        1107     1124    +17     
  Lines       95188    95460   +272     
========================================
+ Hits        93079    93351   +272     
  Misses       2109     2109            

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

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! (already tested and gave feedback on an earlier version via email)

Thanks for this great work, Nour!

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.

For the sqrt_iswap gauges, since the gauges that you found are special cases of the continuous ones, I'm not sure it makes sense to include both. Maybe just include the continuous ones?

@NoureldinYosri
Copy link
Collaborator Author

For the sqrt_iswap gauges, since the gauges that you found are special cases of the continuous ones, I'm not sure it makes sense to include both. Maybe just include the continuous ones?

done

Comment on lines 64 to 67
pre_q0: _SINGLE_QUBIT_GATES_T = None
pre_q1: _SINGLE_QUBIT_GATES_T = None
post_q0: _SINGLE_QUBIT_GATES_T = None
post_q1: _SINGLE_QUBIT_GATES_T = None
Copy link
Collaborator

Choose a reason for hiding this comment

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

For the sake of type simplicity - can this be just a tuple[ops.Gate, ...] with a () default instead?

Alternatively you could use Sequence[ops.Gate], but those arguments should be converted to tuples on initialization to make them immutable. This would let you also remove the _as_sequence conversion.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Union[ops.Gate, Tuple[ops.Gate, ...]] is more like what I want since I want users to be assign just a gate to these fields.

those arguments should be converted to tuples on initialization

The class is defined as dataclass(froze=True) and dataclasses library doesn't have field transformers like (unlike attrs), unless I use object.__setattr__ in __post_init, wdyt?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it would be better to require either the (a) tuple[ops.Gate, ...] or (b) Sequence[ops.Gate, ...] type for the pre_q / post_q fields. It is unlikely users would be defining these classes interactively; in such case it is OK to have them provide the sequence type that is stored in the instance.

For option (b) I am fine with using object.__setattr__ in __post_init to convert a sequence argument to tuple.
If that feels ugly, I am also fine with adding a dependency on attrs and using attrs.frozen with a field converter here.

Converting to a tuple on initialization would let you remove the _as_sequence helper and property caching which effectively store gate sequences twice.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

used attrs with a field(converter=)

q0, q1 = op.qubits
left.extend([g(q) for g in gs] for q, gs in zip(op.qubits, gauge.pre))
center.append(gauge.two_qubit_gate(q0, q1))
right.extend([g(q) for g in gs] for q, gs in zip(op.qubits, gauge.post))
Copy link
Collaborator

Choose a reason for hiding this comment

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

IIUC, the order of pre and post gates from different operations in the moment does not matter, correct?

Otherwise I would expect to add the post gates in a reversed order to pre-gates, e.g., [PreA, PreB, A, B, PostB, PostA].

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, the order doesn't matter since the pre and post operations are single qubit ops. there is only one two-qubit gate here and that this the gauge.two_qubit_gate. so it doesn't matter if we do postA, postB or postB, postA

The Gauge class in general represents a family of closely related gauges
(e.g. random z-rotations); Use `sample` method to get a specific gauge.
"""

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we can add annotation to make it clear derived classes should have that attribute.

Suggested change
two_qubit_gate: ops.Gate

Copy link
Collaborator Author

@NoureldinYosri NoureldinYosri Apr 1, 2024

Choose a reason for hiding this comment

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

not all derived classes have it. only ConstantGauge have it.

Copy link
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

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

Please see the inline comments. Otherwise LGTM.



def _select(choices: Sequence[Gauge], probabilites: np.ndarray, prng: np.random.Generator) -> Gauge:
return choices[prng.choice(len(choices), p=probabilites)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

The _select is only used here - consider moving that line to GaugeSelector.__call__.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it's needed here in order to force select specific gauges in tests


def weight(self) -> float:
"""Returns the relative frequency for selecting this gauge."""
return 1.0
Copy link
Collaborator

Choose a reason for hiding this comment

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

The weight seems to be only used in the GaugeSelector.
How about moving weights to a field of GaugeSelector?
This would allow reuse of Gauge classes in several selectors with a different weights.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

no the weight is a property of the gauge, the GaugeSelector just asks each gauge about its weights and normalizes them to get probabilities. but the weight is a property of a gauge.

gauge_transformer: GaugeTransformer

@pytest.mark.parametrize('generation_seed', [*range(5)])
@pytest.mark.parametrize('transformation_seed', [*range(5)])
Copy link
Collaborator

Choose a reason for hiding this comment

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

The 2 parametrize decorators do Cartesian product so there is 25 test cases - is that necessary?
Perhaps we can combine to a single parametrization of 2 seed values.
Also consider using np.random.randint(2**31, size=(5, 2)).tolist() for the seed values in parametrization to check with non-repetitive seeding.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done, but instead of using np.random.randint I used np.random.RandomState(0).randint inorder to make the test reproducible

Comment on lines 50 to 53
@pytest.mark.xfail(strict=True)
class TestInvalidTransformer(GaugeTester):
two_qubit_gate = _TEST_TARGET
gauge_transformer = _BAD_TRANSFORMER
Copy link
Collaborator

Choose a reason for hiding this comment

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

This adds quite a few xfailed outcomes to pytest. Here the xfail is required, but in general it marks tests that are for features not yet working. Please consider removing this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I replaced xfail with a flag

cirq-google/cirq_google/transformers/sycamore_gauge.py Outdated Show resolved Hide resolved
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! Thanks, Nour!

Copy link
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

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

LGTM with 2 comments.

def sample(self, gate: ops.Gate, prng: np.random.Generator) -> "ConstantGauge":
return self

def pre(self) -> Tuple[Tuple[ops.Gate, ...], Tuple[ops.Gate, ...]]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I meant using a regular @property decorator instead of the cached_property.
Sorry about constant nitpicking - just that you can have your initial interface here if you want. :-)

Comment on lines +70 to +74
if self.must_fail:
with pytest.raises(AssertionError):
_check_equivalent_with_error_message(c, nc, gauge)
else:
_check_equivalent_with_error_message(c, nc, gauge)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please consider removing the must_fail logic for the sake of simplicity and for whoever is going to read this in the future.

We don't check all assertions in tests to make sure they fail for bad tested functions. I feel it is sufficient that you verified your test class catches a bad transformer in an intermediate commit, but it is probably better to have a simpler rather than all covering final code here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I disagree, without this the test module morph into something that accepts faulty gauges. this is essential for the integrity of the tests.

so either using the flag or xfail is need. for now I will keep the flag and we can revisit this later.

@NoureldinYosri NoureldinYosri enabled auto-merge (squash) April 3, 2024 00:43
@NoureldinYosri NoureldinYosri merged commit f9f0d66 into quantumlib:main Apr 3, 2024
29 checks passed
jselig-rigetti pushed a commit to jselig-rigetti/Cirq that referenced this pull request May 28, 2024
This PR introduces the abstraction for Gauge compilation as well as implementation for Sycamore gate, CZ gate, SqrtCZ gate, ZZ (a.k.a spin inversion), ISWAP gate, and SQRT_ISWAP gate
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
This PR introduces the abstraction for Gauge compilation as well as implementation for Sycamore gate, CZ gate, SqrtCZ gate, ZZ (a.k.a spin inversion), ISWAP gate, and SQRT_ISWAP gate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants