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

BloqInstance.i index should not be an arbitrary index and should preserve insertion ordering #1098

Open
tanujkhattar opened this issue Jun 30, 2024 · 5 comments
Milestone

Comments

@tanujkhattar
Copy link
Collaborator

tanujkhattar commented Jun 30, 2024

I think it would be nice if BloqInstance.i has some semantic meaning in the context of a CompositeBloq. Some possible options are:

  • i'th instance of that particular bloq in the CompositeBloq DAG (ordered based on (topological, insertion) order)
  • i'th node in some topological sorting of the DAG represented by CompositeBloq

It seems like these properties break down because of

# We take particular care during flattening to preserve the `binst.i` of bloq instances
# that are not flattened. We do this by initializing the bloq builder's `i` counter
# to one greater than the existing maximum value, so all calls to `add_from` will result
# in new, higher `binst.i` values.
# pylint: disable=protected-access
bb._i = max(binst.i for binst in self.bloq_instances) + 1

Is there a particular reason we chose to preserve this property where binst.i of bloqs which are not flattened is preserved? @mpharrigan

I'm particularly interested to query the following property:

  • Given two nodes x and y in the binst_graph of a composite bloq, I want to know which of these two bloqs (or their parents therefore, if we have flattened a composite bloq) was inserted earlier by the user when constructing the composite bloq? i.e. I want a way to preserve insertion ordering of bloq instances and query this property efficiently given access to bloq instances and a composite bloq / binst_graph

This is useful for a PR I'm working on to provide a greedy topological sorting with a heuristic that aims to help minimize qubit allocations / deallocations and would be useful for qubit count costs (xref #1097) and translation of a composite bloq to a cirq circuit (xref comment by Anurudh #963 (comment))

@tanujkhattar tanujkhattar changed the title BloqInstance.i index should not be an arbitrary index and have some meaning BloqInstance.i index should not be an arbitrary index and should preserve insertion ordering Jun 30, 2024
@mpharrigan
Copy link
Collaborator

As noted, that's to support drawing circuits where you can flatten only parts of it while keeping the identity of the unmodified parts the same

@tanujkhattar
Copy link
Collaborator Author

Do the drawings specifically depend upon the value of binst.i ? Can you point me to where that happens?

@mpharrigan
Copy link
Collaborator

It's used in the prototype interactive drawing musical_score.js which will tween between the top-level circuit and a partially decompose&flattened one. binst.i is the "key" that d3.js uses to know which things in the original circuit correspond to which in the decomposed one

@tanujkhattar
Copy link
Collaborator Author

One way to satisfy both the requirements would be to make binst.i a tuple instead of an index; then as we decompose we can keep appending to the tuple and the property that binst1.i < binst2.i preserves insertion ordering would also be satisfied. We can cache the hash if we think this would add a performance bottleneck. WDYT ?

@mpharrigan
Copy link
Collaborator

I think there are ways to preserve the insertion index. An easy one would be to have the CompositeBloq._bloq_instances attribute be an ordered list. In general, I'd be happier if the insertion order was kept within the context of the container rather than on the BloqInstance itself. Another option would be to have an additional field on BloqInstance that is the insertion order. Keeping the i as an arbitrary identifier that is preserved through flattening makes conceptual sense to me.

@mpharrigan mpharrigan added this to the After v1.0 milestone Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants