-
Notifications
You must be signed in to change notification settings - Fork 104
Adding function to create a quirk url from a bloq #1821
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
Axel-pappalardo
wants to merge
5
commits into
quantumlib:main
Choose a base branch
from
Axel-pappalardo:bloq_to_quirk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a6552cc
feat: added function to give quirk url from a qualtran's bloq
Axel-pappalardo b1c6f0f
Merge branch 'quantumlib:main' into bloq_to_quirk
Axel-pappalardo 9d33db3
fix: linting fix
Axel-pappalardo 3726791
fix: implemented recommended correction + bug correction of SparseLin…
Axel-pappalardo 08e21cd
fix: implemented change recommended in review + added custom bloq for…
Axel-pappalardo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import subprocess | ||
|
|
||
| from qualtran import Bloq, DecomposeTypeError | ||
| from qualtran.bloqs.bookkeeping import Join, Split | ||
| from qualtran.drawing import ( | ||
| ModPlus, | ||
| Circle, | ||
| LarrowTextBox, | ||
| RarrowTextBox, | ||
| LineManager, | ||
| get_musical_score_data, | ||
| ) | ||
|
|
||
|
|
||
| class SparseLineManager(LineManager): | ||
| """ | ||
| LineManager which keeps partitioned line slots reserved for them until they need it again | ||
| """ | ||
|
|
||
| # DIDN'TDO: only handles partition patterns of the type (QAny(n)/QUInt(n)/... -> QBit((n,)) | ||
| # or QBit((n,)) -> QAny(n)) | ||
|
|
||
| def maybe_reserve(self, binst, reg, idx): | ||
| if binst.bloq_is(Join) and reg.shape: | ||
|
|
||
| def _keep_split_lines(binst_to_check, reg_to_check): | ||
| binst_cond = binst_to_check.bloq == binst.bloq.adjoint() | ||
| reg_cond = reg_to_check == reg.adjoint() | ||
| return binst_cond and reg_cond | ||
|
|
||
| self.reserve_n(1, _keep_split_lines) | ||
|
|
||
| if binst.bloq_is(Split) and not reg.shape: | ||
|
|
||
| def _keep_joined_line(binst_to_check, reg_to_check): | ||
| binst_cond = binst_to_check.bloq == binst.bloq.adjoint() | ||
| reg_cond = reg_to_check == reg.adjoint() | ||
| return binst_cond and reg_cond | ||
|
|
||
| self.reserve_n(1, _keep_joined_line) | ||
|
|
||
|
|
||
| handled_operations = { | ||
| ModPlus(): '"X"', | ||
| Circle(filled=True): '"•"', | ||
| Circle(filled=False): '"◦"', | ||
| LarrowTextBox(text='∧'): '"X"', | ||
| RarrowTextBox(text='∧'): '"X"', | ||
| } | ||
|
|
||
|
|
||
| def bloq_to_quirk( | ||
| bloq: Bloq, | ||
| line_manager: LineManager = SparseLineManager().__init__(), # type: ignore[misc] | ||
| open_quirk=False, | ||
| ) -> str: | ||
| """Convert a Bloq into a Quirk circuit URL. | ||
|
|
||
| The input bloq is decomposed and flattened before conversion. Only a limited set | ||
| of operations is currently supported: control, anti-control, and NOT. | ||
|
|
||
| Args: | ||
| bloq: The bloq to export to Quirk. | ||
| line_manager: Line manager used to assign and order circuit lines. | ||
| open_quirk: If True, opens the generated URL in Firefox. | ||
|
|
||
| Returns: | ||
| A URL encoding the corresponding Quirk circuit. | ||
| """ | ||
| try: | ||
| flat_bloq = bloq.decompose_bloq().flatten() | ||
| except DecomposeTypeError: # no need to flatten the bloq if it is atomic | ||
| flat_bloq = bloq.as_composite_bloq() | ||
Axel-pappalardo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| msd = get_musical_score_data(flat_bloq, manager=line_manager) | ||
|
|
||
| sparse_circuit = [(['1'] * (msd.max_y + 1)).copy() for _ in range(msd.max_x)] | ||
| for soq in msd.json_dict()['soqs']: | ||
Axel-pappalardo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try: | ||
| gate = handled_operations[soq.symb] | ||
| sparse_circuit[soq.rpos.seq_x][soq.rpos.y] = gate | ||
| except KeyError: | ||
| None | ||
|
|
||
| circuit = list(filter((['1'] * (msd.max_y + 1)).__ne__, sparse_circuit)) | ||
Axel-pappalardo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| nb_deleted_lines = 0 | ||
| for i in range( | ||
| msd.max_y + 1 | ||
| ): # deleting lines of the circuit which are not used (happens with partition) | ||
| ind = i - nb_deleted_lines | ||
| for col in circuit: | ||
| line_is_useless = col[ind] == '1' | ||
| if not line_is_useless: | ||
| break | ||
| if line_is_useless: | ||
| for col in circuit: | ||
| col.pop(ind) | ||
| nb_deleted_lines += 1 | ||
Axel-pappalardo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| quirk_url = "https://algassert.com/quirk" | ||
| start = '#circuit={"cols":[' | ||
| end = ']}' | ||
| url = quirk_url + start + ','.join('[' + ','.join(col) + ']' for col in circuit) + end | ||
|
|
||
| if open_quirk: | ||
| subprocess.run(["firefox", url], check=False) | ||
Axel-pappalardo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return url | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| from qualtran import QUInt | ||
| from qualtran.bloqs.basic_gates import Toffoli | ||
| from qualtran.bloqs.mcmt import MultiTargetCNOT | ||
| from qualtran.bloqs.arithmetic.addition import Add | ||
| from qualtran.quirk_interop.bloq_to_quirk import bloq_to_quirk | ||
|
|
||
|
|
||
| def test_bloq_to_quirk(): | ||
| bloq_to_quirk(Add(QUInt(5))) | ||
| bloq_to_quirk(MultiTargetCNOT(5)) | ||
|
|
||
|
|
||
| def test_bloq_to_quirk_on_atomic(): | ||
| bloq_to_quirk(Toffoli()) | ||
Axel-pappalardo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.