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

Should cirq.Moment store operations sorted by the underlying qubits? #4742

Closed
tanujkhattar opened this issue Dec 9, 2021 · 2 comments
Closed
Labels
area/circuits area/moments kind/design-issue A conversation around design triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on

Comments

@tanujkhattar
Copy link
Collaborator

The iteration order of operations in a cirq.Moment currently depends upon the order in which the operations were inserted in the moment. This can lead to cases where two moments m1 == m2 is True but their reprs and iteration order of underlying operations are not the same.

> q = cirq.LineQubit.range(2)
> ops = [cirq.X(q[0]), cirq.Y(q[1])]
> m1, m2 = cirq.Moment(ops), cirq.Moment(ops.reverse())
> assert m1 == m2
> for op1, op2 in zip(m1, m2):
    ...:     print(op1 == op2) # Prints False

The proposal is to directly store and use sorted(self.operations, key=lambda op: op.qubits) as the list of underlying operations in a moment to avoid such inconsistencies and uniquely define the traversal order of operations in equal moments.

@tanujkhattar tanujkhattar added triage/discuss Needs decision / discussion, bring these up during Cirq Cynque kind/design-issue A conversation around design labels Dec 9, 2021
@95-martin-orion
Copy link
Collaborator

Potentially a breaking change, since there's an implicit rule that insertion order is preserved, but otherwise accepted.

@95-martin-orion 95-martin-orion added triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on and removed triage/discuss Needs decision / discussion, bring these up during Cirq Cynque labels Dec 15, 2021
@tanujkhattar
Copy link
Collaborator Author

I tried this change locally and it's indeed a pretty large breaking change. Specifically, it breaks:

  • Existing PointOptimizer based transformers because iteration order (and hence second order effects like inserting new operations in new moments etc.) currently depends upon the insertion order of operations in the moment.
  • Serialization of circuits / moments breaks which leads to a domino effect further breaking serialization of objects like QuantumExecutableGroups etc.

I was mainly interested in this change because I wanted to make the iteration order deterministic for new transformer primitives (i.e.merge_operations). I'm now doing that independently in #4764 and would propose to close this issue as this would be a huge breaking change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/circuits area/moments kind/design-issue A conversation around design triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on
Projects
None yet
Development

No branches or pull requests

3 participants