TensorNetwork icon indicating copy to clipboard operation
TensorNetwork copied to clipboard

Merge axes names after contraction

Open chaserileyroberts opened this issue 6 years ago • 15 comments

As discussed in https://github.com/google/TensorNetwork/issues/179, we should merge axes names after contraction and throw an error if the user tries to access an ambiguous name.

chaserileyroberts avatar Aug 21 '19 20:08 chaserileyroberts

I can sure give this a try! One thing I'm immediately wondering is whether we should exploit a node's name to introduce uniqueness. I tend to reuse a name like "physical_index" for multiple MPS nodes, but I would give the nodes different names.

For instance, a Node Awith axis names [a, b, c]and another node B with the same axis names. After contracting the a axis we can return a Node with axis names [A_b, A_c, B_b, B_c] ... What do you think?

MichaelMarien avatar Aug 21 '19 20:08 MichaelMarien

That might get messy once you start contracting multiple nodes together. Also, unless you specify a node name, the new node created after contraction will have a default name "__Node_X".

chaserileyroberts avatar Aug 21 '19 20:08 chaserileyroberts

Yes, you are right. I always specify node names, but unless we make this a required property (which we should not I think), we can't assume the user will do this and than it can become a complete jungle very quickly.

MichaelMarien avatar Aug 22 '19 06:08 MichaelMarien

Should we throw an error immediately when a contraction is done that leaves unambiguous axis, or delay this as long as possible (only for instance when user makes explicit reference to such axis name?)

MichaelMarien avatar Aug 22 '19 07:08 MichaelMarien

I think when the user does it explicitly.

chaserileyroberts avatar Aug 22 '19 08:08 chaserileyroberts

Currently we are throwing that error at Node creation time (see add_axis_names method)? Should we change this also and only throw errors when axis is requested not when names are added? Or treat nodes added and nodes created from contraction differently. The latter feels awkward to me.

MichaelMarien avatar Aug 22 '19 08:08 MichaelMarien

@Thenerdstation You want it when user does it explicitly? See my remake above :)

MichaelMarien avatar Aug 29 '19 17:08 MichaelMarien

Sorry for the late response.

I see what you mean by the latter feeling awkward, but right now I think it's the best compromise we can do. It makes sense to throw an error when a user writes it, as it can be easily fixed, but it's the best compromise we could come up with when the axis names collide after contraction.

chaserileyroberts avatar Aug 29 '19 18:08 chaserileyroberts

Hey been looking at this project for a while, wanted to contribute. Seems like if you wanted to only throw an exception on lookup of a ambiguous axis name, instead of on creation of a node with ambiguous axis name, one could add an 'set_unsafe_axes_name' method to the base node with which to add axes_names to newly created nodes in contract_between and then throw an exception in the get_axis_number lookup function when a lookup is performed on the ambiguous name. Does that seem like a reasonable approach?

jacksonwb avatar Jan 16 '20 19:01 jacksonwb

Also, would you want to try to retain axes names on an outer product call?

jacksonwb avatar Jan 16 '20 19:01 jacksonwb

Yeah I think that seems ok by me! And yes please include this with outer_product

chaserileyroberts avatar Jan 16 '20 20:01 chaserileyroberts

Great, I'm adding this behavior to contract_between, outer_product, and contract. Do we also want to preserve remaining axis names after a call to _contract_trace?

jacksonwb avatar Jan 17 '20 22:01 jacksonwb

It also appears that _flatten_trace_edges reshapes the node tensor, but does not regenerate the axis_names list leaving a mismatching number of names. This differs from flatten_edges. I assume this is not intentional?

jacksonwb avatar Jan 18 '20 01:01 jacksonwb

Yes, please include support for all of the functions you listed including _contract_trace.

And yes that is a bug. A PR for that would also be awesome! (they can be separate PRs).

chaserileyroberts avatar Jan 18 '20 01:01 chaserileyroberts

was it fixed in the main package? how do I use the fixed version?

ronf6co avatar May 02 '22 07:05 ronf6co