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

Decompose protocol must create a context when given None and SimpleQubitManager must add a prefix to its qubits #6172

Merged
merged 4 commits into from
Jun 30, 2023

Conversation

NoureldinYosri
Copy link
Collaborator

@NoureldinYosri NoureldinYosri commented Jun 28, 2023

Problem:

Before this change successive calls to cirq.decompose_once can break since they might endup making a gate act on the same qubits.

The successive calls to cirq.decompose_once can happen implicitly by calling other protocols. for example

decomposition = cirq.decompose_once(op)
for d in decomposition:
      print(cirq.has_unitary(d)

since has_unitary falls to decompose_once if the gate doesn't implement _has_unitary_. another case is when running a simulator on a decomposed operation. the simulator will call has_unitary in its checks.


Solution: For cirq.decompose_once add a unique prefix to the qubits allocated during decompose if decompose_once allocates its own qubit manager. for cirq.decompose I think it is okay to have a constant prefix just to distinguish it from qubits allocated by other SimpleQubitManagers.

@NoureldinYosri NoureldinYosri requested review from a team, vtomole and cduck as code owners June 28, 2023 17:27
@CirqBot CirqBot added the size: M 50< lines changed <250 label Jun 28, 2023
@@ -364,6 +367,11 @@ def decompose_once(
TypeError: `val` didn't have a `_decompose_` method (or that method returned
`NotImplemented` or `None`) and `default` wasn't set.
"""
if context is None:
context = DecompositionContext(
ops.SimpleQubitManager(prefix=f'_decompose_protocol_{time.time_ns()}')
Copy link
Collaborator

@pavoljuhas pavoljuhas Jun 28, 2023

Choose a reason for hiding this comment

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

Can we use some global counter instead of time_ns here?
It would be nice to have naming reproducibility in repeated executions.
Debugging and making sense of allocated qubit names should also get easier.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added using itertools.count()

@NoureldinYosri
Copy link
Collaborator Author

@pavoljuhas PTAL

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 modulo small suggestion.

@NoureldinYosri NoureldinYosri merged commit 9849695 into master Jun 30, 2023
@tanujkhattar tanujkhattar added the area/cirq-ft Issues related to the Cirq-FT sub-package label Jun 30, 2023
@NoureldinYosri NoureldinYosri deleted the qubitManager branch October 26, 2023 20:06
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/cirq-ft Issues related to the Cirq-FT sub-package size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants