quantumlib/Qualtran

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

Opened this issue · 3 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)

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

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.

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