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

cirq.decompose_once fails when called on cirq.CZ #6667

Open
maffoo opened this issue Jul 16, 2024 · 4 comments
Open

cirq.decompose_once fails when called on cirq.CZ #6667

maffoo opened this issue Jul 16, 2024 · 4 comments
Assignees
Labels
good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. kind/bug-report Something doesn't seem to work. kind/docs Documentation related problems, ideas, requests triage/accepted there is consensus amongst maintainers that this is a real bug or a reasonable feature to add

Comments

@maffoo
Copy link
Contributor

maffoo commented Jul 16, 2024

Calling cirq.decomponse_once on cirq.CZ raises an error:

In [4]: cirq.decompose(cirq.CZ(cirq.q(0), cirq.q(1)))
Out[4]: [cirq.CZ(cirq.LineQubit(0), cirq.LineQubit(1))]

In [5]: cirq.decompose_once(cirq.CZ(cirq.q(0), cirq.q(1)))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[5], line 1
----> 1 cirq.decompose_once(cirq.CZ(cirq.q(0), cirq.q(1)))

File ~/.virtualenvs/pyle/lib/python3.11/site-packages/cirq/protocols/decompose_protocol.py:388, in decompose_once(val, default, flatten, context, *args, **kwargs)
    383 if method is None:
    384     raise TypeError(
    385         f"object of type '{type(val)}' has no _decompose_with_context_ or "
    386         f"_decompose_ method."
    387     )
--> 388 raise TypeError(
    389     f"object of type {type(val)} does have a _decompose_ method, "
    390     "but it returned NotImplemented or None."
    391 )

TypeError: object of type <class 'cirq.ops.gate_operation.GateOperation'> does have a _decompose_ method, but it returned NotImplemented or None.
@maffoo maffoo added the kind/bug-report Something doesn't seem to work. label Jul 16, 2024
@NoureldinYosri NoureldinYosri added the triage/discuss Needs decision / discussion, bring these up during Cirq Cynque label Jul 16, 2024
@NoureldinYosri
Copy link
Collaborator

NoureldinYosri commented Jul 16, 2024

what is the desired behaviour here? I think cirq.CZ is treated as a native gate so operations decompose into it but it doesn't have its own decomposition

@maffoo
Copy link
Contributor Author

maffoo commented Jul 16, 2024

I don't know what the expected behavior is; I guess maybe the idea here is that if an op can't be decomposed this is supposed to raise? The docs for decompose_once are not very clear on this, and obviously the behavior of decompose is different. Maybe decompose_once is intended to be something like "decompose exactly once" versus "decompose at most once".

@dstrain115
Copy link
Collaborator

dstrain115 commented Jul 17, 2024

cirq cync: The root cause which spawned this issue could be a problem with multiprocessing, since there is global state keeping track of this table. This global state is only in the calling code, not in cirq itself.

Conclusion is that this is intended behavior, since decompose_once should return exception denoting that you are at the base case.

The sole action item here is to document the behavior of decompose_once when you specify a "base gate".
Nice to have would be to have a specialized exception type (or a more helpful message).

@dstrain115 dstrain115 added kind/docs Documentation related problems, ideas, requests triage/accepted there is consensus amongst maintainers that this is a real bug or a reasonable feature to add good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. and removed triage/discuss Needs decision / discussion, bring these up during Cirq Cynque labels Jul 17, 2024
@pavoljuhas pavoljuhas self-assigned this Sep 4, 2024
@pavoljuhas
Copy link
Collaborator

TODO @pavoljuhas - verify if this still happens after #6674.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. kind/bug-report Something doesn't seem to work. kind/docs Documentation related problems, ideas, requests triage/accepted there is consensus amongst maintainers that this is a real bug or a reasonable feature to add
Projects
None yet
Development

No branches or pull requests

4 participants