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.unitary fails for bloq with And followed by And^dagger #1488

Open
anurudhp opened this issue Nov 6, 2024 · 3 comments
Open

cirq.unitary fails for bloq with And followed by And^dagger #1488

anurudhp opened this issue Nov 6, 2024 · 3 comments
Labels
cirq_interop Issues related to Cirq interop

Comments

@anurudhp
Copy link
Contributor

anurudhp commented Nov 6, 2024

import attrs
import cirq
from qualtran import Bloq, Signature
from qualtran.bloqs.mcmt import And
from qualtran.cirq_interop import BloqAsCirqGate

@attrs.frozen
class MyBloq(Bloq):
    @property
    def signature(self):
        return Signature.build(a=1, b=1)

    def build_composite_bloq(self, bb, a, b):
        [a, b], c = bb.add(And(), ctrl=[a, b])
        [a, b] = bb.add(And().adjoint(), ctrl=[a, b], target=c)
        return dict(a=a, b=b)

MyBloq().tensor_contract() # works
cirq.unitary(BloqAsCirqGate(MyBloq())) # fails
TypeError: cirq.unitary failed. Value doesn't have a (non-parameterized) unitary effect.

type: <class 'qualtran.cirq_interop._bloq_to_cirq.BloqAsCirqGate'>
value: BloqAsCirqGate(MyBloq)
@mpharrigan
Copy link
Collaborator

What's the expected behavior? And^dag doesn't have a unitary effect. The tensor contraction projects the target into the zero state, which isn't physical -- but keeps everything in state-vector-land

@anurudhp
Copy link
Contributor Author

There are bloqs which use And in compute-uncompute pairs, which then fails with the cirq simulator (even state vector). I think some old GateWithRegisters code return a cirq.ControlledGate in some cases, but newer bloq code using BloqAsCirqGate or ControlledViaAnd does not always do this.

Ideally it would be good if such cases (compute-uncompute pairs) can work with the cirq simulators as expected.

@mpharrigan mpharrigan added the cirq_interop Issues related to Cirq interop label Dec 19, 2024
@mpharrigan
Copy link
Collaborator

It's tricky because And^dag is not a unitary operation. If we want to treat pairs of compute-uncompute, we'd need to have a mechanism for grouping such pairs of bloqs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cirq_interop Issues related to Cirq interop
Projects
None yet
Development

No branches or pull requests

2 participants