quimb icon indicating copy to clipboard operation
quimb copied to clipboard

Cannot compress disconnected `MatrixProductOperator`

Open garrison opened this issue 8 months ago • 3 comments

What happened?

It's possible to construct, from a circuit, a MatrixProductOperator that is disconnected, but after that it cannot be compressed (it raises an error instead).

What did you expect to happen?

I expected that after conversion to MatrixProductOperator the object would have a bond of dimension 1 in place of the disconnected bond, and therefore a compression operation should succeed.

Minimal Complete Verifiable Example

# This is a tweaked version of the example at https://quimb.readthedocs.io/en/main/examples/ex_circuit_to_mpo.html

import quimb.tensor as qtn

gates = [
    qtn.Gate("CX", params=[], qubits=(0, 1)),
    #qtn.Gate("CX", params=[], qubits=(1, 2)),  # exception goes away if this line is uncommented
    qtn.Gate("CX", params=[], qubits=(2, 3)),
]

circ = qtn.Circuit.from_gates(
    gates,
    N=4,
    # this ensure each tensor belongs to one site only
    gate_contract="split-gate",
    # just for cleaner tags
    tag_gate_numbers=False,
)

tn_uni = circ.get_uni()

for site in tn_uni.site_tags:
    tn_uni ^= site

tn_uni.fuse_multibonds_()

tn_uni.view_as_(
    qtn.MatrixProductOperator,
    cyclic=False,
    L=circ.N,
)

tn_uni.compress(cutoff=1e-6, cutoff_mode="rel")  # Exception raised here

Relevant log output

ValueError: The tensors specified don't share an bond.

Anything else we need to know?

No response

Environment

yes, 1.10.0

garrison avatar May 07 '25 19:05 garrison

Thanks for the issue @garrison, yes this would definitely be nice to handle. Does creating a size 1 bond make more sense that leaving it disconnected? I think probably yes as well, so that it reliably always has a MPS/MPO structure afterwards.

jcmgray avatar May 08 '25 00:05 jcmgray

I've added a create_bond kwarg in https://github.com/jcmgray/quimb/commit/463b825b1d76675910a133d039951cffadbdb81c to all the canonize/compress sweeping methods. By default it is off, for explicitness.

jcmgray avatar May 16 '25 22:05 jcmgray

I've added a create_bond kwarg in 463b825 to all the canonize/compress sweeping methods.

Thank you, that works wonderfully.

By the way, this feature is related to the discussion I opened at https://github.com/jcmgray/quimb/discussions/295. I am curious if you have any thoughts on the idea I presented of layer-wise converting a circuit to MPO, and in particular whether you think it is functionality that belongs in quimb.

garrison avatar May 19 '25 20:05 garrison