Qualtran icon indicating copy to clipboard operation
Qualtran copied to clipboard

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

Open anurudhp opened this issue 1 year ago • 4 comments

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)

anurudhp avatar Nov 06 '24 23:11 anurudhp

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

mpharrigan avatar Dec 10 '24 00:12 mpharrigan

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.

anurudhp avatar Dec 10 '24 16:12 anurudhp

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

mpharrigan avatar Dec 19 '24 16:12 mpharrigan

This can be "fixed" by making BloqAsCirqGate._unitary_ and GateWithRegisters._unitary_ always immediately use bloq.tensor_contract() instead of letting cirq decompose the gate so it can simulate its parts with its own simulator. This can make cirq simulation very slow, however; since it will decompose everything in qualtran-land, do a tensor contraction into a potentially large unitary, cirq will apply the large unitary, and this will be repeated for each gate.

mpharrigan avatar Nov 11 '25 16:11 mpharrigan