Skip to content

Adding function to create a quirk url from a bloq#1821

Open
Axel-pappalardo wants to merge 5 commits intoquantumlib:mainfrom
Axel-pappalardo:bloq_to_quirk
Open

Adding function to create a quirk url from a bloq#1821
Axel-pappalardo wants to merge 5 commits intoquantumlib:mainfrom
Axel-pappalardo:bloq_to_quirk

Conversation

@Axel-pappalardo
Copy link

As per the title, This adds a function which yields a quirk url from a qualtran bloq.
The function isn't perfect, and runs into many limitation:

  1. Quirk limitation (it will only work for small circuit <16 qubits)
  2. It only handles operation made with control, anti-control and NOT for now (but this should be easily expandable by adding new operations in the handled_operations dict), operations not handled are simply ignored.
  3. Does not handle CBit (but should be possible to implement later if needed)

It works by using the musical score of the bloq to look through the operations column per column and thus can use a LineManager.

I also implemented a default LineManager SparseLineManager to avoid an issue where if 2 different qubit lines share the same horizontal line in a musical score (which can happen with Partition) they would be put on the same line in Quirk (this is because to determine Quirk coordinates for an operation we only look at its coordinates in the musical score).
SparseLineManager avoid this issue by reserving the slots used by any line that ends after a Partition and freeing them only if the adjoint of that Partition appears again. (for example, if I join 3 QBit into a QAny(3), the slots used by the 3 QBit are reserved until I split the QAny(3) back into 3 QBit).

An example generated with that code:
Lookup table

It could still have some issue with certain Bloq, but making specific LineManager should help
Let me know what you think

@google-cla
Copy link

google-cla bot commented Feb 26, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@mpharrigan
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This PR adds a new feature to generate Quirk URLs from bloqs. The implementation is mostly correct, but there are several critical issues in SparseLineManager that could lead to runtime errors due to incorrect handling of DanglingT instances. I've provided suggestions to fix these. Additionally, there are opportunities to improve code quality and portability in composite_bloq_to_quirk by refactoring the line removal logic and using the webbrowser module instead of a hardcoded subprocess call.

Comment on lines +124 to +125
if open_quirk:
subprocess.run(["firefox", url], check=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

high

Using subprocess.run with a hardcoded "firefox" is not portable. The webbrowser module is the standard Python way to open a URL in the user's default browser, which is more portable and robust.

Suggested change
if open_quirk:
subprocess.run(["firefox", url], check=False)
if open_quirk:
import webbrowser
webbrowser.open(url)

Copy link
Author

@Axel-pappalardo Axel-pappalardo Mar 13, 2026

Choose a reason for hiding this comment

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

Tried the change but it links to a quirk error page with an empty circuit

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.

2 participants